Ignore:
Timestamp:
May 11, 2009, 12:49:04 PM (16 years ago)
Author:
[email protected]
Message:

JavaScriptCore:

2009-05-11 Dmitry Titov <[email protected]>

Reviewed by Alexey Proskuryakov and Adam Roben.

https://bugs.webkit.org/show_bug.cgi?id=25348
Change WTF::ThreadIdentifier to be an actual (but wrapped) thread id, remove ThreadMap.

  • wtf/Threading.h: (WTF::ThreadIdentifier::ThreadIdentifier): (WTF::ThreadIdentifier::isValid): (WTF::ThreadIdentifier::invalidate): (WTF::ThreadIdentifier::platformId): ThreadIdentifier is now a class, containing a PlatformThreadIdentifier and methods that are used across the code on thread ids: construction, comparisons, check for 'valid' state etc. '0' is used as invalid id, which happens to just work with all platform-specific thread id implementations.

All the following files repeatedly reflect the new ThreadIdentifier for each platform.
We remove ThreadMap and threadMapMutex from all of them, remove the functions that
populated/searched/cleared the map and add platform-specific comparison operators
for ThreadIdentifier.

There are specific temporary workarounds for Safari 4 beta on OSX and Win32 since the
public build uses WTF threading functions with old type of ThreadingIdentifier.
The next time Safari 4 is rebuilt, it will 'automatically' pick up the new type and new
functions so the deprecated ones can be removed.

  • wtf/gtk/ThreadingGtk.cpp: (WTF::ThreadIdentifier::operator==): (WTF::ThreadIdentifier::operator!=): (WTF::initializeThreading): (WTF::createThreadInternal): (WTF::waitForThreadCompletion): (WTF::currentThread):
  • wtf/ThreadingNone.cpp: (WTF::ThreadIdentifier::operator==): (WTF::ThreadIdentifier::operator!=):
  • wtf/ThreadingPthreads.cpp: (WTF::ThreadIdentifier::operator==): (WTF::ThreadIdentifier::operator!=): (WTF::initializeThreading): (WTF::createThreadInternal): (WTF::waitForThreadCompletion): (WTF::detachThread): (WTF::currentThread): (WTF::waitForThreadCompletion): This is a workaround for Safari 4 beta on Mac. Safari 4 is linked against old definition of ThreadIdentifier so it treats it as uint32_t. This 'old' variant of waitForThreadCompletion takes uint32_t and has the old decorated name, so Safari can load it from JavaScriptCore library. The other functions (CurrentThread() etc) happen to match their previous decorated names and, while they return pthread_t now, it is a pointer which round-trips through a uint32_t. This function will be removed as soon as Safari 4 will release next public build.
  • wtf/qt/ThreadingQt.cpp: (WTF::ThreadIdentifier::operator==): (WTF::ThreadIdentifier::operator!=): (WTF::initializeThreading): (WTF::createThreadInternal): (WTF::waitForThreadCompletion): (WTF::currentThread):
  • wtf/ThreadingWin.cpp: (WTF::ThreadIdentifier::operator==): (WTF::ThreadIdentifier::operator!=): (WTF::initializeThreading): (WTF::createThreadInternal): All the platforms (except Windows) used a sequential counter as a thread ID and mapped it into platform ID. Windows was using native thread id and mapped it into thread handle. Since we can always obtain a thread handle by thread id, createThread now closes the handle. (WTF::waitForThreadCompletion): obtains another one using OpenThread(id) API. If can not obtain a handle, it means the thread already exited. (WTF::detachThread): (WTF::currentThread): (WTF::detachThreadDeprecated): old function, renamed (for Win Safari 4 beta which uses it for now). (WTF::waitForThreadCompletionDeprecated): same. (WTF::currentThreadDeprecated): same. (WTF::createThreadDeprecated): same.
  • bytecode/SamplingTool.h:
  • bytecode/SamplingTool.cpp: Use DEFINE_STATIC_LOCAL for a static ThreadIdentifier variable, to avoid static constructor.
  • JavaScriptCore.exp: export lists - updated decorated names of the WTF threading functions since they now take a different type as a parameter.
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.def: ditto for Windows, plus added "deprecated" functions that take old parameter type - turns out public beta of Safari 4 uses those, so they need to be kept along for a while.
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore_debug.def: ditto.

WebCore:

2009-05-11 Dmitry Titov <[email protected]>

Reviewed by Alexey Proskuryakov and Adam Roben.

https://bugs.webkit.org/show_bug.cgi?id=25348
Change WTF::ThreadIdentifier to be an actual (but wrapped) thread id, remove ThreadMap.

Most of the change is in WTF.
Unless noted, all the following files changed to use the new ThreadIdentifier::isValid()
method instead of just doing 'if(m_threadID)' kind of checks, since ThreadIdentifier
is now a class rather then an integer.
Also, there is no need to initialize threadID in constructors to 0 now.

  • dom/XMLTokenizerLibxml2.cpp: (WebCore::libxmlLoaderThread): use DEFINE_STATIC_LOCAL and accessor function for static thread id, since now ThreadIdentifier needs construction and we avoid having global initializers. (WebCore::matchFunc): use the new accessor function. (WebCore::openFunc): ditto. (WebCore::createStringParser): ditto. (WebCore::createMemoryParser): ditto.
  • loader/icon/IconDatabase.cpp: (WebCore::IconDatabase::open):
  • platform/sql/SQLiteDatabase.cpp: (WebCore::SQLiteDatabase::SQLiteDatabase): (WebCore::SQLiteDatabase::close):
  • storage/DatabaseThread.cpp: (WebCore::DatabaseThread::start): (WebCore::DatabaseThread::databaseThread): remove m_threadID from debug output.
  • storage/LocalStorageThread.cpp: (WebCore::LocalStorageThread::start): (WebCore::LocalStorageThread::scheduleImport): (WebCore::LocalStorageThread::scheduleSync): (WebCore::LocalStorageThread::terminate):
  • workers/WorkerThread.cpp: (WebCore::WorkerThread::start): (WebCore::WorkerThread::WorkerThread): (WebCore::WorkerThread::start):

WebKit/win:

2009-05-11 Dmitry Titov <[email protected]>

Reviewed by Alexey Proskuryakov and Adam Roben.

https://bugs.webkit.org/show_bug.cgi?id=25348
Change WTF::ThreadIdentifier to be an actual (but wrapped) thread id, remove ThreadMap.

Most of the change is in WTF and WebCore.

  • WebKit.vcproj/WebKit.def: replaced decorated names of WTF threading functions with new ones. Also, aliased the old implementations so the public Safari 4 beta can load the old WTF functions which it uses. Next time Safari 4 builds, it will pick up new functions and the deprecated ones can be removed.
  • WebKit.vcproj/WebKit_debug.def: same.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/ThreadingPthreads.cpp

    r43392 r43507  
    4949namespace WTF {
    5050
    51 typedef HashMap<ThreadIdentifier, pthread_t> ThreadMap;
     51bool ThreadIdentifier::operator==(const ThreadIdentifier& another) const
     52{
     53    return pthread_equal(m_platformId, another.m_platformId);
     54}
     55
     56bool ThreadIdentifier::operator!=(const ThreadIdentifier& another) const
     57{
     58    return !pthread_equal(m_platformId, another.m_platformId);
     59}
    5260
    5361static Mutex* atomicallyInitializedStaticMutex;
     
    5765#endif
    5866
    59 static Mutex& threadMapMutex()
    60 {
    61     DEFINE_STATIC_LOCAL(Mutex, mutex, ());
    62     return mutex;
    63 }
    64 
    6567void initializeThreading()
    6668{
    6769    if (!atomicallyInitializedStaticMutex) {
    6870        atomicallyInitializedStaticMutex = new Mutex;
    69         threadMapMutex();
    7071        initializeRandomNumberGenerator();
    7172#if !PLATFORM(DARWIN) || PLATFORM(CHROMIUM)
     
    8586{
    8687    atomicallyInitializedStaticMutex->unlock();
    87 }
    88 
    89 static ThreadMap& threadMap()
    90 {
    91     DEFINE_STATIC_LOCAL(ThreadMap, map, ());
    92     return map;
    93 }
    94 
    95 static ThreadIdentifier identifierByPthreadHandle(const pthread_t& pthreadHandle)
    96 {
    97     MutexLocker locker(threadMapMutex());
    98 
    99     ThreadMap::iterator i = threadMap().begin();
    100     for (; i != threadMap().end(); ++i) {
    101         if (pthread_equal(i->second, pthreadHandle))
    102             return i->first;
    103     }
    104 
    105     return 0;
    106 }
    107 
    108 static ThreadIdentifier establishIdentifierForPthreadHandle(pthread_t& pthreadHandle)
    109 {
    110     ASSERT(!identifierByPthreadHandle(pthreadHandle));
    111 
    112     MutexLocker locker(threadMapMutex());
    113 
    114     static ThreadIdentifier identifierCount = 1;
    115 
    116     threadMap().add(identifierCount, pthreadHandle);
    117    
    118     return identifierCount++;
    119 }
    120 
    121 static pthread_t pthreadHandleForIdentifier(ThreadIdentifier id)
    122 {
    123     MutexLocker locker(threadMapMutex());
    124    
    125     return threadMap().get(id);
    126 }
    127 
    128 static void clearPthreadHandleForIdentifier(ThreadIdentifier id)
    129 {
    130     MutexLocker locker(threadMapMutex());
    131 
    132     ASSERT(threadMap().contains(id));
    133    
    134     threadMap().remove(id);
    13588}
    13689
     
    165118    if (pthread_create(&threadHandle, 0, runThreadWithRegistration, static_cast<void*>(threadData))) {
    166119        LOG_ERROR("Failed to create pthread at entry point %p with data %p", entryPoint, data);
    167         return 0;
    168     }
    169     return establishIdentifierForPthreadHandle(threadHandle);
     120        return ThreadIdentifier();
     121    }
     122    return ThreadIdentifier(threadHandle);
    170123}
    171124#else
     
    175128    if (pthread_create(&threadHandle, 0, entryPoint, data)) {
    176129        LOG_ERROR("Failed to create pthread at entry point %p with data %p", entryPoint, data);
    177         return 0;
    178     }
    179 
    180     return establishIdentifierForPthreadHandle(threadHandle);
     130        return ThreadIdentifier();
     131    }
     132
     133    return ThreadIdentifier(threadHandle);
    181134}
    182135#endif
     
    193146int waitForThreadCompletion(ThreadIdentifier threadID, void** result)
    194147{
    195     ASSERT(threadID);
    196    
    197     pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID);
     148    ASSERT(threadID.isValid());
     149   
     150    pthread_t pthreadHandle = threadID.platformId();
    198151 
    199152    int joinResult = pthread_join(pthreadHandle, result);
    200153    if (joinResult == EDEADLK)
    201         LOG_ERROR("ThreadIdentifier %u was found to be deadlocked trying to quit", threadID);
     154        LOG_ERROR("ThreadIdentifier %p was found to be deadlocked trying to quit", pthreadHandle);
    202155       
    203     clearPthreadHandleForIdentifier(threadID);
    204156    return joinResult;
    205157}
     
    207159void detachThread(ThreadIdentifier threadID)
    208160{
    209     ASSERT(threadID);
    210    
    211     pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID);
     161    ASSERT(threadID.isValid());
     162   
     163    pthread_t pthreadHandle = threadID.platformId();
    212164   
    213165    pthread_detach(pthreadHandle);
    214    
    215     clearPthreadHandleForIdentifier(threadID);
    216166}
    217167
    218168ThreadIdentifier currentThread()
    219169{
    220     pthread_t currentThread = pthread_self();
    221     if (ThreadIdentifier id = identifierByPthreadHandle(currentThread))
    222         return id;
    223     return establishIdentifierForPthreadHandle(currentThread);
     170    return ThreadIdentifier(pthread_self());
    224171}
    225172
     
    315262    ASSERT_UNUSED(result, !result);
    316263}
    317    
     264
     265// Derecated function. Safari 4 beta, until recompiled next time, uses ThreadIdentifier as uint32_t.
     266// pthread_t is a pointer. So they get a pointer casted into uint32_t from CurrentThread()
     267// and then pass it here. We cast it back to pointer. This is an ugly hack which is very temporary
     268// and will be removed once next public build of Safari 4 is released.
     269int waitForThreadCompletion(uint32_t threadID, void** result)
     270{
     271    pthread_t pthreadHandle = reinterpret_cast<pthread_t>(threadID);
     272
     273    int joinResult = pthread_join(pthreadHandle, result);
     274    if (joinResult == EDEADLK)
     275        LOG_ERROR("ThreadIdentifier %p was found to be deadlocked trying to quit", pthreadHandle);
     276
     277    return joinResult;
     278}
     279
    318280} // namespace WTF
    319281
Note: See TracChangeset for help on using the changeset viewer.