Ignore:
Timestamp:
Dec 27, 2008, 6:54:12 PM (16 years ago)
Author:
[email protected]
Message:

<rdar://problem/6467376> Race condition in WTF::currentThread can lead to a thread using two different identifiers during its lifetime

If a newly-created thread calls WTF::currentThread() before WTF::createThread calls establishIdentifierForPthreadHandle
then more than one identifier will be used for the same thread. We can avoid this by adding some extra synchronization
during thread creation that delays the execution of the thread function until the thread identifier has been set up, and
an assertion to catch this problem should it reappear in the future.

Reviewed by Alexey Proskuryakov.

  • wtf/Threading.cpp: Added.

(WTF::NewThreadContext::NewThreadContext):
(WTF::threadEntryPoint):
(WTF::createThread): Add cross-platform createThread function that delays the execution of the thread function until
after the thread identifier has been set up.

  • wtf/Threading.h:
  • wtf/ThreadingGtk.cpp:

(WTF::establishIdentifierForThread):
(WTF::createThreadInternal):

  • wtf/ThreadingNone.cpp:

(WTF::createThreadInternal):

  • wtf/ThreadingPthreads.cpp:

(WTF::establishIdentifierForPthreadHandle):
(WTF::createThreadInternal):

  • wtf/ThreadingQt.cpp:

(WTF::identifierByQthreadHandle):
(WTF::establishIdentifierForThread):
(WTF::createThreadInternal):

  • wtf/ThreadingWin.cpp:

(WTF::storeThreadHandleByIdentifier):
(WTF::createThreadInternal):

Add Threading.cpp to the build.

File:
1 edited

Legend:

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

    r39337 r39487  
    8181}
    8282
    83 static ThreadIdentifier establishIdentifierForThread(QThread*& thread)
    84 {
    85     MutexLocker locker(threadMapMutex());
    86 
    87     static ThreadIdentifier identifierCount = 1;
    88 
    89     threadMap().add(identifierCount, thread);
    90 
    91     return identifierCount++;
    92 }
    93 
    94 static void clearThreadForIdentifier(ThreadIdentifier id)
    95 {
    96     MutexLocker locker(threadMapMutex());
    97 
    98     ASSERT(threadMap().contains(id));
    99 
    100     threadMap().remove(id);
    101 }
    102 
    10383static ThreadIdentifier identifierByQthreadHandle(QThread*& thread)
    10484{
     
    11292
    11393    return 0;
     94}
     95
     96static ThreadIdentifier establishIdentifierForThread(QThread*& thread)
     97{
     98    ASSERT(!identifierByQthreadHandle(thread));
     99
     100    MutexLocker locker(threadMapMutex());
     101
     102    static ThreadIdentifier identifierCount = 1;
     103
     104    threadMap().add(identifierCount, thread);
     105
     106    return identifierCount++;
     107}
     108
     109static void clearThreadForIdentifier(ThreadIdentifier id)
     110{
     111    MutexLocker locker(threadMapMutex());
     112
     113    ASSERT(threadMap().contains(id));
     114
     115    threadMap().remove(id);
    114116}
    115117
     
    146148}
    147149
    148 ThreadIdentifier createThread(ThreadFunction entryPoint, void* data, const char*)
     150ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*)
    149151{
    150152    ThreadPrivate* thread = new ThreadPrivate(entryPoint, data);
Note: See TracChangeset for help on using the changeset viewer.