Ignore:
Timestamp:
May 7, 2009, 2:24:36 PM (16 years ago)
Author:
[email protected]
Message:

JavaScriptCore:

2009-05-07 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.

  • 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/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 the WTF threading functions decorated names 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-07 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-07 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/ThreadingWin.cpp

    r41626 r43366  
    9999namespace WTF {
    100100
     101bool ThreadIdentifier::operator==(const ThreadIdentifier& another) const
     102{
     103    return m_platformId == another.m_platformId;
     104}
     105
     106bool ThreadIdentifier::operator!=(const ThreadIdentifier& another) const
     107{
     108    return m_platformId != another.m_platformId;
     109}
     110
    101111// MS_VC_EXCEPTION, THREADNAME_INFO, and setThreadNameInternal all come from <http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx>.
    102112static const DWORD MS_VC_EXCEPTION = 0x406D1388;
     
    140150static ThreadIdentifier mainThreadIdentifier;
    141151
    142 static Mutex& threadMapMutex()
    143 {
    144     static Mutex mutex;
    145     return mutex;
    146 }
    147 
    148152void initializeThreading()
    149153{
    150154    if (!atomicallyInitializedStaticMutex) {
    151155        atomicallyInitializedStaticMutex = new Mutex;
    152         threadMapMutex();
    153156        initializeRandomNumberGenerator();
    154157        initializeMainThread();
     
    158161}
    159162
    160 static HashMap<DWORD, HANDLE>& threadMap()
    161 {
    162     static HashMap<DWORD, HANDLE> map;
    163     return map;
    164 }
    165 
    166 static void storeThreadHandleByIdentifier(DWORD threadID, HANDLE threadHandle)
    167 {
    168     MutexLocker locker(threadMapMutex());
    169     ASSERT(!threadMap().contains(threadID));
    170     threadMap().add(threadID, threadHandle);
    171 }
    172 
    173 static HANDLE threadHandleForIdentifier(ThreadIdentifier id)
    174 {
    175     MutexLocker locker(threadMapMutex());
    176     return threadMap().get(id);
    177 }
    178 
    179 static void clearThreadHandleForIdentifier(ThreadIdentifier id)
    180 {
    181     MutexLocker locker(threadMapMutex());
    182     ASSERT(threadMap().contains(id));
    183     threadMap().remove(id);
    184 }
    185 
    186163struct ThreadFunctionInvocation {
    187164    ThreadFunctionInvocation(ThreadFunction function, void* data) : function(function), data(data) {}
     
    209186{
    210187    unsigned threadIdentifier = 0;
    211     ThreadIdentifier threadID = 0;
    212188    ThreadFunctionInvocation* invocation = new ThreadFunctionInvocation(entryPoint, data);
    213189    HANDLE threadHandle = reinterpret_cast<HANDLE>(_beginthreadex(0, 0, wtfThreadEntryPoint, invocation, 0, &threadIdentifier));
    214190    if (!threadHandle) {
    215191        LOG_ERROR("Failed to create thread at entry point %p with data %p: %ld", entryPoint, data, errno);
    216         return 0;
    217     }
    218 
    219     threadID = static_cast<ThreadIdentifier>(threadIdentifier);
    220     storeThreadHandleByIdentifier(threadIdentifier, threadHandle);
    221 
    222     return threadID;
     192        return ThreadIdentifier();
     193    }
     194    // Closes handle only, since we don't need it. The new thread runs until it exits.
     195    CloseHandle(threadHandle);
     196
     197    return ThreadIdentifier(threadIdentifier);
    223198}
    224199
    225200int waitForThreadCompletion(ThreadIdentifier threadID, void** result)
    226201{
    227     ASSERT(threadID);
     202    ASSERT(threadID.isValid());
    228203   
    229     HANDLE threadHandle = threadHandleForIdentifier(threadID);
     204    unsigned threadIdentifier = threadID.platformId();
     205    HANDLE threadHandle = OpenThread(SYNCHRONIZE, FALSE, threadIdentifier);
     206    // If thread already exited, return immediately.
    230207    if (!threadHandle)
    231         LOG_ERROR("ThreadIdentifier %u did not correspond to an active thread when trying to quit", threadID);
     208        return WAIT_OBJECT_0;
    232209 
    233210    DWORD joinResult = WaitForSingleObject(threadHandle, INFINITE);
    234211    if (joinResult == WAIT_FAILED)
    235         LOG_ERROR("ThreadIdentifier %u was found to be deadlocked trying to quit", threadID);
     212        LOG_ERROR("ThreadIdentifier %p was found to be deadlocked trying to quit", threadHandle);
    236213
    237214    CloseHandle(threadHandle);
    238     clearThreadHandleForIdentifier(threadID);
    239215
    240216    return joinResult;
    241217}
    242218
    243 void detachThread(ThreadIdentifier threadID)
    244 {
    245     ASSERT(threadID);
    246    
    247     HANDLE threadHandle = threadHandleForIdentifier(threadID);
    248     if (threadHandle)
    249         CloseHandle(threadHandle);
    250     clearThreadHandleForIdentifier(threadID);
     219void detachThread(ThreadIdentifier)
     220{
    251221}
    252222
    253223ThreadIdentifier currentThread()
    254224{
    255     return static_cast<ThreadIdentifier>(GetCurrentThreadId());
     225    return ThreadIdentifier(GetCurrentThreadId());
    256226}
    257227
     
    471441}
    472442
     443// Deprecated functions. The current build of Safari 4 beta is linked with them so we need to provide
     444// the implementation taking PlatformThreadIdentifier instead of ThreadIdentifier. These are not declared
     445// in .h file so the next time Safari is built it will 'automatically' switch to new functions.
     446// Then deprecated ones can be removed from this file and from JavaScriptCore.def and WebKit.def files.
     447// In WebKit.def file, these functions are 'renamed' so their name does not have 'Deprecated' part - this
     448// is to avoid having 2 functions with only different type of return value.
     449void detachThreadDeprecated(PlatformThreadIdentifier)
     450{
     451}
     452
     453int waitForThreadCompletionDeprecated(PlatformThreadIdentifier threadID, void** result)
     454{
     455    ASSERT(threadID);
     456
     457    unsigned threadIdentifier = static_cast<unsigned>(threadID);
     458    HANDLE threadHandle = OpenThread(SYNCHRONIZE, FALSE, threadIdentifier);
     459    // If thread already exited, return immediately.
     460    if (!threadHandle)
     461        return WAIT_OBJECT_0;
     462
     463    DWORD joinResult = WaitForSingleObject(threadHandle, INFINITE);
     464    if (joinResult == WAIT_FAILED)
     465        LOG_ERROR("ThreadIdentifier %p was found to be deadlocked trying to quit", threadHandle);
     466
     467    CloseHandle(threadHandle);
     468
     469    return joinResult;
     470}
     471
     472PlatformThreadIdentifier currentThreadDeprecated()
     473{
     474    return static_cast<PlatformThreadIdentifier>(GetCurrentThreadId());
     475}
     476
     477PlatformThreadIdentifier createThreadDeprecated(ThreadFunction entryPoint, void* data, const char* name)
     478{
     479    ThreadIdentifier threadID = createThread(entryPoint, data, name);
     480    return static_cast<PlatformThreadIdentifier>(threadID.platformId());
     481}
     482
    473483} // namespace WTF
Note: See TracChangeset for help on using the changeset viewer.