[WTF] Simplify GCThread and CompilationThread flags by adding them to WTF::Thread
authorysuzuki@apple.com <ysuzuki@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 13 May 2019 22:32:34 +0000 (22:32 +0000)
committerysuzuki@apple.com <ysuzuki@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 13 May 2019 22:32:34 +0000 (22:32 +0000)
https://bugs.webkit.org/show_bug.cgi?id=197146

Reviewed by Saam Barati.

Source/JavaScriptCore:

Rename Heap::Thread to Heap::HeapThread to remove conflict between WTF::Thread.

* heap/AlignedMemoryAllocator.cpp:
(JSC::AlignedMemoryAllocator::registerDirectory):
* heap/Heap.cpp:
(JSC::Heap::HeapThread::HeapThread):
(JSC::Heap::Heap):
(JSC::Heap::runCurrentPhase):
(JSC::Heap::runBeginPhase):
(JSC::Heap::resumeThePeriphery):
(JSC::Heap::requestCollection):
(JSC::Heap::isCurrentThreadBusy):
(JSC::Heap::notifyIsSafeToCollect):
(JSC::Heap::Thread::Thread): Deleted.
* heap/Heap.h:
* heap/HeapInlines.h:
(JSC::Heap::incrementDeferralDepth):
(JSC::Heap::decrementDeferralDepth):
(JSC::Heap::decrementDeferralDepthAndGCIfNeeded):
* heap/MarkedSpace.cpp:
(JSC::MarkedSpace::prepareForAllocation):

Source/WebCore:

* Modules/indexeddb/IDBDatabase.cpp:
(WebCore::IDBDatabase::hasPendingActivity const):
* Modules/indexeddb/IDBRequest.cpp:
(WebCore::IDBRequest::hasPendingActivity const):
* Modules/indexeddb/IDBTransaction.cpp:
(WebCore::IDBTransaction::hasPendingActivity const):

Source/WTF:

Since GCThread and CompilationThread flags exist in WTF, we put these flags into WTF::Thread directly instead of holding them in ThreadSpecific<>.
And this patch removes dependency from Threading.h to ThreadSpecific.h. ThreadSpecific.h's OS threading primitives are moved to ThreadingPrimitives.h,
and Threading.h relies on it instead.

* wtf/CompilationThread.cpp:
(WTF::isCompilationThread):
(WTF::initializeCompilationThreads): Deleted.
(WTF::exchangeIsCompilationThread): Deleted.
* wtf/CompilationThread.h:
(WTF::CompilationScope::CompilationScope):
(WTF::CompilationScope::~CompilationScope):
(WTF::CompilationScope::leaveEarly):
* wtf/MainThread.cpp:
(WTF::initializeMainThread):
(WTF::initializeMainThreadToProcessMainThread):
(WTF::isMainThreadOrGCThread):
(WTF::initializeGCThreads): Deleted.
(WTF::registerGCThread): Deleted.
(WTF::mayBeGCThread): Deleted.
* wtf/MainThread.h:
* wtf/ThreadSpecific.h:
(WTF::canBeGCThread>::ThreadSpecific):
(WTF::canBeGCThread>::set):
(WTF::threadSpecificKeyCreate): Deleted.
(WTF::threadSpecificKeyDelete): Deleted.
(WTF::threadSpecificSet): Deleted.
(WTF::threadSpecificGet): Deleted.
* wtf/Threading.cpp:
(WTF::Thread::exchangeIsCompilationThread):
(WTF::Thread::registerGCThread):
(WTF::Thread::mayBeGCThread):
* wtf/Threading.h:
(WTF::Thread::isCompilationThread const):
(WTF::Thread::gcThreadType const):
(WTF::Thread::joinableState const):
(WTF::Thread::hasExited const):
(WTF::Thread::Thread):
(WTF::Thread::joinableState): Deleted.
(WTF::Thread::hasExited): Deleted.
* wtf/ThreadingPrimitives.h:
(WTF::threadSpecificKeyCreate):
(WTF::threadSpecificKeyDelete):
(WTF::threadSpecificSet):
(WTF::threadSpecificGet):
* wtf/win/ThreadSpecificWin.cpp:
(WTF::flsKeys):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@245258 268f45cc-cd09-0410-ab3c-d52691b4dbfc

20 files changed:
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/heap/AlignedMemoryAllocator.cpp
Source/JavaScriptCore/heap/Heap.cpp
Source/JavaScriptCore/heap/Heap.h
Source/JavaScriptCore/heap/HeapInlines.h
Source/JavaScriptCore/heap/MarkedSpace.cpp
Source/WTF/ChangeLog
Source/WTF/wtf/CompilationThread.cpp
Source/WTF/wtf/CompilationThread.h
Source/WTF/wtf/MainThread.cpp
Source/WTF/wtf/MainThread.h
Source/WTF/wtf/ThreadSpecific.h
Source/WTF/wtf/Threading.cpp
Source/WTF/wtf/Threading.h
Source/WTF/wtf/ThreadingPrimitives.h
Source/WTF/wtf/win/ThreadSpecificWin.cpp
Source/WebCore/ChangeLog
Source/WebCore/Modules/indexeddb/IDBDatabase.cpp
Source/WebCore/Modules/indexeddb/IDBRequest.cpp
Source/WebCore/Modules/indexeddb/IDBTransaction.cpp

index adbfbd2..f075585 100644 (file)
@@ -1,3 +1,32 @@
+2019-05-13  Yusuke Suzuki  <ysuzuki@apple.com>
+
+        [WTF] Simplify GCThread and CompilationThread flags by adding them to WTF::Thread
+        https://bugs.webkit.org/show_bug.cgi?id=197146
+
+        Reviewed by Saam Barati.
+
+        Rename Heap::Thread to Heap::HeapThread to remove conflict between WTF::Thread.
+
+        * heap/AlignedMemoryAllocator.cpp:
+        (JSC::AlignedMemoryAllocator::registerDirectory):
+        * heap/Heap.cpp:
+        (JSC::Heap::HeapThread::HeapThread):
+        (JSC::Heap::Heap):
+        (JSC::Heap::runCurrentPhase):
+        (JSC::Heap::runBeginPhase):
+        (JSC::Heap::resumeThePeriphery):
+        (JSC::Heap::requestCollection):
+        (JSC::Heap::isCurrentThreadBusy):
+        (JSC::Heap::notifyIsSafeToCollect):
+        (JSC::Heap::Thread::Thread): Deleted.
+        * heap/Heap.h:
+        * heap/HeapInlines.h:
+        (JSC::Heap::incrementDeferralDepth):
+        (JSC::Heap::decrementDeferralDepth):
+        (JSC::Heap::decrementDeferralDepthAndGCIfNeeded):
+        * heap/MarkedSpace.cpp:
+        (JSC::MarkedSpace::prepareForAllocation):
+
 2019-05-13  Saam Barati  <sbarati@apple.com>
 
         macro assembler code-pointer tagging has its arguments backwards
index 9fb6f22..e7c93c2 100644 (file)
@@ -45,7 +45,7 @@ void AlignedMemoryAllocator::registerDirectory(BlockDirectory* directory)
     RELEASE_ASSERT(!directory->nextDirectoryInAlignedMemoryAllocator());
     
     if (m_directories.isEmpty()) {
-        ASSERT(!mayBeGCThread() || directory->heap()->worldIsStopped());
+        ASSERT(!Thread::mayBeGCThread() || directory->heap()->worldIsStopped());
         for (Subspace* subspace = m_subspaces.first(); subspace; subspace = subspace->nextSubspaceInAlignedMemoryAllocator())
             subspace->didCreateFirstDirectory(directory);
     }
index 8d92313..987f9f8 100644 (file)
@@ -148,7 +148,7 @@ bool isValidSharedInstanceThreadState(VM* vm)
 
 bool isValidThreadState(VM* vm)
 {
-    if (vm->atomicStringTable() != WTF::Thread::current().atomicStringTable())
+    if (vm->atomicStringTable() != Thread::current().atomicStringTable())
         return false;
 
     if (vm->isSharedInstance() && !isValidSharedInstanceThreadState(vm))
@@ -231,9 +231,9 @@ private:
 
 } // anonymous namespace
 
-class Heap::Thread : public AutomaticThread {
+class Heap::HeapThread : public AutomaticThread {
 public:
-    Thread(const AbstractLocker& locker, Heap& heap)
+    HeapThread(const AbstractLocker& locker, Heap& heap)
         : AutomaticThread(locker, heap.m_threadLock, heap.m_threadCondition.copyRef())
         , m_heap(heap)
     {
@@ -264,7 +264,7 @@ protected:
     
     void threadDidStart() override
     {
-        WTF::registerGCThread(GCThreadType::Main);
+        Thread::registerGCThread(GCThreadType::Main);
     }
 
 private:
@@ -323,7 +323,7 @@ Heap::Heap(VM* vm, HeapType heapType)
     m_maxEdenSizeWhenCritical = memoryAboveCriticalThreshold / 4;
 
     LockHolder locker(*m_threadLock);
-    m_thread = adoptRef(new Thread(locker, *this));
+    m_thread = adoptRef(new HeapThread(locker, *this));
 }
 
 Heap::~Heap()
@@ -1169,7 +1169,7 @@ auto Heap::runCurrentPhase(GCConductor conn, CurrentThreadState* currentThreadSt
 {
     checkConn(conn);
     m_currentThreadState = currentThreadState;
-    m_currentThread = &WTF::Thread::current();
+    m_currentThread = &Thread::current();
     
     if (conn == GCConductor::Mutator)
         sanitizeStackForVM(vm());
@@ -1298,7 +1298,7 @@ NEVER_INLINE bool Heap::runBeginPhase(GCConductor conn)
                     slotVisitor = m_availableParallelSlotVisitors.takeLast();
             }
 
-            WTF::registerGCThread(GCThreadType::Helper);
+            Thread::registerGCThread(GCThreadType::Helper);
 
             {
                 ParallelModeEnabler parallelModeEnabler(*slotVisitor);
@@ -1695,7 +1695,7 @@ NEVER_INLINE void Heap::resumeThePeriphery()
                 slotVisitorsToUpdate.takeLast();
             }
         }
-        WTF::Thread::yield();
+        Thread::yield();
     }
     
     for (SlotVisitor* slotVisitor : slotVisitorsToUpdate)
@@ -2105,7 +2105,7 @@ Heap::Ticket Heap::requestCollection(GCRequest request)
     stopIfNecessary();
     
     ASSERT(vm()->currentThreadIsHoldingAPILock());
-    RELEASE_ASSERT(vm()->atomicStringTable() == WTF::Thread::current().atomicStringTable());
+    RELEASE_ASSERT(vm()->atomicStringTable() == Thread::current().atomicStringTable());
     
     LockHolder locker(*m_threadLock);
     // We may be able to steal the conn. That only works if the collector is definitely not running
@@ -2548,7 +2548,7 @@ void Heap::writeBarrierSlowPath(const JSCell* from)
 
 bool Heap::isCurrentThreadBusy()
 {
-    return mayBeGCThread() || mutatorState() != MutatorState::Running;
+    return Thread::mayBeGCThread() || mutatorState() != MutatorState::Running;
 }
 
 void Heap::reportExtraMemoryVisited(size_t size)
@@ -2864,7 +2864,7 @@ void Heap::notifyIsSafeToCollect()
     m_isSafeToCollect = true;
     
     if (Options::collectContinuously()) {
-        m_collectContinuouslyThread = WTF::Thread::create(
+        m_collectContinuouslyThread = Thread::create(
             "JSC DEBUG Continuous GC",
             [this] () {
                 MonotonicTime initialTime = MonotonicTime::now();
index 38f5321..0d09999 100644 (file)
@@ -426,8 +426,8 @@ private:
     friend class VM;
     friend class WeakSet;
 
-    class Thread;
-    friend class Thread;
+    class HeapThread;
+    friend class HeapThread;
 
     static const size_t minExtraMemory = 256;
     
@@ -727,7 +727,7 @@ private:
     Ref<AutomaticThreadCondition> m_threadCondition; // The mutator must not wait on this. It would cause a deadlock.
     RefPtr<AutomaticThread> m_thread;
 
-    RefPtr<WTF::Thread> m_collectContinuouslyThread { nullptr };
+    RefPtr<Thread> m_collectContinuouslyThread { nullptr };
     
     MonotonicTime m_lastGCStartTime;
     MonotonicTime m_lastGCEndTime;
@@ -737,7 +737,7 @@ private:
     uintptr_t m_barriersExecuted { 0 };
     
     CurrentThreadState* m_currentThreadState { nullptr };
-    WTF::Thread* m_currentThread { nullptr }; // It's OK if this becomes a dangling pointer.
+    Thread* m_currentThread { nullptr }; // It's OK if this becomes a dangling pointer.
 
 #if PLATFORM(IOS_FAMILY)
     unsigned m_precentAvailableMemoryCachedCallCount;
index 1b8b192..7e75be4 100644 (file)
@@ -173,19 +173,19 @@ inline void Heap::releaseSoon(std::unique_ptr<JSCGLibWrapperObject>&& object)
 
 inline void Heap::incrementDeferralDepth()
 {
-    ASSERT(!mayBeGCThread() || m_worldIsStopped);
+    ASSERT(!Thread::mayBeGCThread() || m_worldIsStopped);
     m_deferralDepth++;
 }
 
 inline void Heap::decrementDeferralDepth()
 {
-    ASSERT(!mayBeGCThread() || m_worldIsStopped);
+    ASSERT(!Thread::mayBeGCThread() || m_worldIsStopped);
     m_deferralDepth--;
 }
 
 inline void Heap::decrementDeferralDepthAndGCIfNeeded()
 {
-    ASSERT(!mayBeGCThread() || m_worldIsStopped);
+    ASSERT(!Thread::mayBeGCThread() || m_worldIsStopped);
     m_deferralDepth--;
     
     if (UNLIKELY(m_didDeferGCWork)) {
index 7ebac36..ca0f126 100644 (file)
@@ -259,7 +259,7 @@ void MarkedSpace::sweepLargeAllocations()
 
 void MarkedSpace::prepareForAllocation()
 {
-    ASSERT(!mayBeGCThread() || m_heap->worldIsStopped());
+    ASSERT(!Thread::mayBeGCThread() || m_heap->worldIsStopped());
     for (Subspace* subspace : m_subspaces)
         subspace->prepareForAllocation();
 
index 846d1cb..7124474 100644 (file)
@@ -1,5 +1,59 @@
 2019-05-13  Yusuke Suzuki  <ysuzuki@apple.com>
 
+        [WTF] Simplify GCThread and CompilationThread flags by adding them to WTF::Thread
+        https://bugs.webkit.org/show_bug.cgi?id=197146
+
+        Reviewed by Saam Barati.
+
+        Since GCThread and CompilationThread flags exist in WTF, we put these flags into WTF::Thread directly instead of holding them in ThreadSpecific<>.
+        And this patch removes dependency from Threading.h to ThreadSpecific.h. ThreadSpecific.h's OS threading primitives are moved to ThreadingPrimitives.h,
+        and Threading.h relies on it instead.
+
+        * wtf/CompilationThread.cpp:
+        (WTF::isCompilationThread):
+        (WTF::initializeCompilationThreads): Deleted.
+        (WTF::exchangeIsCompilationThread): Deleted.
+        * wtf/CompilationThread.h:
+        (WTF::CompilationScope::CompilationScope):
+        (WTF::CompilationScope::~CompilationScope):
+        (WTF::CompilationScope::leaveEarly):
+        * wtf/MainThread.cpp:
+        (WTF::initializeMainThread):
+        (WTF::initializeMainThreadToProcessMainThread):
+        (WTF::isMainThreadOrGCThread):
+        (WTF::initializeGCThreads): Deleted.
+        (WTF::registerGCThread): Deleted.
+        (WTF::mayBeGCThread): Deleted.
+        * wtf/MainThread.h:
+        * wtf/ThreadSpecific.h:
+        (WTF::canBeGCThread>::ThreadSpecific):
+        (WTF::canBeGCThread>::set):
+        (WTF::threadSpecificKeyCreate): Deleted.
+        (WTF::threadSpecificKeyDelete): Deleted.
+        (WTF::threadSpecificSet): Deleted.
+        (WTF::threadSpecificGet): Deleted.
+        * wtf/Threading.cpp:
+        (WTF::Thread::exchangeIsCompilationThread):
+        (WTF::Thread::registerGCThread):
+        (WTF::Thread::mayBeGCThread):
+        * wtf/Threading.h:
+        (WTF::Thread::isCompilationThread const):
+        (WTF::Thread::gcThreadType const):
+        (WTF::Thread::joinableState const):
+        (WTF::Thread::hasExited const):
+        (WTF::Thread::Thread):
+        (WTF::Thread::joinableState): Deleted.
+        (WTF::Thread::hasExited): Deleted.
+        * wtf/ThreadingPrimitives.h:
+        (WTF::threadSpecificKeyCreate):
+        (WTF::threadSpecificKeyDelete):
+        (WTF::threadSpecificSet):
+        (WTF::threadSpecificGet):
+        * wtf/win/ThreadSpecificWin.cpp:
+        (WTF::flsKeys):
+
+2019-05-13  Yusuke Suzuki  <ysuzuki@apple.com>
+
         Unreviewed, follow-up after r245214
         https://bugs.webkit.org/show_bug.cgi?id=197730
 
index dc4f161..162c504 100644 (file)
 #include "config.h"
 #include <wtf/CompilationThread.h>
 
-#include <mutex>
 #include <wtf/StdLibExtras.h>
-#include <wtf/ThreadSpecific.h>
 #include <wtf/Threading.h>
 
 namespace WTF {
 
-static ThreadSpecific<bool, CanBeGCThread::True>* s_isCompilationThread;
-
-static void initializeCompilationThreads()
-{
-    static std::once_flag initializeCompilationThreadsOnceFlag;
-    std::call_once(initializeCompilationThreadsOnceFlag, []{
-        s_isCompilationThread = new ThreadSpecific<bool, CanBeGCThread::True>();
-    });
-}
-
 bool isCompilationThread()
 {
-    if (!s_isCompilationThread)
-        return false;
-    if (!s_isCompilationThread->isSet())
-        return false;
-    return **s_isCompilationThread;
-}
-
-bool exchangeIsCompilationThread(bool newValue)
-{
-    initializeCompilationThreads();
-    bool oldValue = isCompilationThread();
-    **s_isCompilationThread = newValue;
-    return oldValue;
+    return Thread::current().isCompilationThread();
 }
 
 } // namespace WTF
index 5d5e174..06e7160 100644 (file)
 
 #pragma once
 
-namespace WTF {
+#include <wtf/Threading.h>
 
-WTF_EXPORT_PRIVATE bool exchangeIsCompilationThread(bool newValue);
+namespace WTF {
 
 class CompilationScope {
 public:
     CompilationScope()
-        : m_oldValue(exchangeIsCompilationThread(true))
+        : m_oldValue(Thread::exchangeIsCompilationThread(true))
     {
     }
     
     ~CompilationScope()
     {
-        exchangeIsCompilationThread(m_oldValue);
+        Thread::exchangeIsCompilationThread(m_oldValue);
     }
     
     void leaveEarly()
     {
-        exchangeIsCompilationThread(m_oldValue);
+        Thread::exchangeIsCompilationThread(m_oldValue);
     }
 private:
     bool m_oldValue;
@@ -52,4 +52,3 @@ private:
 } // namespace WTF
 
 using WTF::CompilationScope;
-using WTF::exchangeIsCompilationThread;
index 7831201..fce7b46 100644 (file)
@@ -57,7 +57,6 @@ void initializeMainThread()
     std::call_once(initializeKey, [] {
         initializeThreading();
         initializeMainThreadPlatform();
-        initializeGCThreads();
     });
 }
 
@@ -68,7 +67,6 @@ void initializeMainThreadToProcessMainThread()
     std::call_once(initializeKey, [] {
         initializeThreading();
         initializeMainThreadToProcessMainThreadPlatform();
-        initializeGCThreads();
     });
 }
 #else
@@ -157,46 +155,14 @@ void setMainThreadCallbacksPaused(bool paused)
         scheduleDispatchFunctionsOnMainThread();
 }
 
-static ThreadSpecific<Optional<GCThreadType>, CanBeGCThread::True>* isGCThread;
-
-void initializeGCThreads()
-{
-    static std::once_flag flag;
-    std::call_once(
-        flag,
-        [] {
-            isGCThread = new ThreadSpecific<Optional<GCThreadType>, CanBeGCThread::True>();
-        });
-}
-
-void registerGCThread(GCThreadType type)
-{
-    if (!isGCThread) {
-        // This happens if we're running in a process that doesn't care about
-        // MainThread.
-        return;
-    }
-
-    **isGCThread = type;
-}
-
 bool isMainThreadOrGCThread()
 {
-    if (mayBeGCThread())
+    if (Thread::mayBeGCThread())
         return true;
 
     return isMainThread();
 }
 
-Optional<GCThreadType> mayBeGCThread()
-{
-    if (!isGCThread)
-        return WTF::nullopt;
-    if (!isGCThread->isSet())
-        return WTF::nullopt;
-    return **isGCThread;
-}
-
 void callOnMainThreadAndWait(WTF::Function<void()>&& function)
 {
     if (isMainThread()) {
index c8ef1b5..4e45c68 100644 (file)
@@ -68,17 +68,6 @@ inline bool isWebThread() { return isMainThread(); }
 inline bool isUIThread() { return isMainThread(); }
 #endif // USE(WEB_THREAD)
 
-WTF_EXPORT_PRIVATE void initializeGCThreads();
-
-enum class GCThreadType {
-    Main,
-    Helper
-};
-
-void printInternal(PrintStream&, GCThreadType);
-
-WTF_EXPORT_PRIVATE void registerGCThread(GCThreadType);
-WTF_EXPORT_PRIVATE Optional<GCThreadType> mayBeGCThread();
 WTF_EXPORT_PRIVATE bool isMainThreadOrGCThread();
 
 // NOTE: these functions are internal to the callOnMainThread implementation.
@@ -98,7 +87,6 @@ void initializeMainThreadToProcessMainThreadPlatform();
 
 } // namespace WTF
 
-using WTF::GCThreadType;
 using WTF::callOnMainThread;
 using WTF::callOnMainThreadAndWait;
 using WTF::canAccessThreadLocalDataForThread;
@@ -106,7 +94,6 @@ using WTF::isMainThread;
 using WTF::isMainThreadOrGCThread;
 using WTF::isUIThread;
 using WTF::isWebThread;
-using WTF::mayBeGCThread;
 using WTF::setMainThreadCallbacksPaused;
 #if PLATFORM(COCOA)
 using WTF::dispatchAsyncOnMainThreadWithWebThreadLockIfNeeded;
index b1f1f71..c70df74 100644 (file)
 #include <wtf/MainThread.h>
 #include <wtf/Noncopyable.h>
 #include <wtf/StdLibExtras.h>
-
-#if USE(PTHREADS)
-#include <pthread.h>
-
-#if OS(HURD)
-// PTHREAD_KEYS_MAX is not defined in bionic nor in Hurd, so explicitly define it here.
-#define PTHREAD_KEYS_MAX 1024
-#else
-#include <limits.h>
-#endif
-
-#elif OS(WINDOWS)
-#include <windows.h>
-#endif
+#include <wtf/Threading.h>
 
 namespace WTF {
 
-#if OS(WINDOWS) && CPU(X86)
-#define THREAD_SPECIFIC_CALL __stdcall
-#else
-#define THREAD_SPECIFIC_CALL
-#endif
-
 enum class CanBeGCThread {
     False,
     True
@@ -129,34 +110,6 @@ private:
 
 #if USE(PTHREADS)
 
-typedef pthread_key_t ThreadSpecificKey;
-
-static const constexpr ThreadSpecificKey InvalidThreadSpecificKey = PTHREAD_KEYS_MAX;
-
-inline void threadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void *))
-{
-    int error = pthread_key_create(key, destructor);
-    if (error)
-        CRASH();
-}
-
-inline void threadSpecificKeyDelete(ThreadSpecificKey key)
-{
-    int error = pthread_key_delete(key);
-    if (error)
-        CRASH();
-}
-
-inline void threadSpecificSet(ThreadSpecificKey key, void* value)
-{
-    pthread_setspecific(key, value);
-}
-
-inline void* threadSpecificGet(ThreadSpecificKey key)
-{
-    return pthread_getspecific(key);
-}
-
 template<typename T, CanBeGCThread canBeGCThread>
 inline ThreadSpecific<T, canBeGCThread>::ThreadSpecific()
 {
@@ -185,39 +138,11 @@ inline void ThreadSpecific<T, canBeGCThread>::setInTLS(Data* data)
 // The maximum number of FLS keys that can be created. For simplification, we assume that:
 // 1) Once the instance of ThreadSpecific<> is created, it will not be destructed until the program dies.
 // 2) We do not need to hold many instances of ThreadSpecific<> data. This fixed number should be far enough.
-const int kMaxFlsKeySize = 128;
+static constexpr int maxFlsKeySize = 128;
 
 WTF_EXPORT_PRIVATE long& flsKeyCount();
 WTF_EXPORT_PRIVATE DWORD* flsKeys();
 
-typedef DWORD ThreadSpecificKey;
-
-static const constexpr ThreadSpecificKey InvalidThreadSpecificKey = FLS_OUT_OF_INDEXES;
-
-inline void threadSpecificKeyCreate(ThreadSpecificKey* key, void (THREAD_SPECIFIC_CALL *destructor)(void *))
-{
-    DWORD flsKey = FlsAlloc(destructor);
-    if (flsKey == FLS_OUT_OF_INDEXES)
-        CRASH();
-
-    *key = flsKey;
-}
-
-inline void threadSpecificKeyDelete(ThreadSpecificKey key)
-{
-    FlsFree(key);
-}
-
-inline void threadSpecificSet(ThreadSpecificKey key, void* data)
-{
-    FlsSetValue(key, data);
-}
-
-inline void* threadSpecificGet(ThreadSpecificKey key)
-{
-    return FlsGetValue(key);
-}
-
 template<typename T, CanBeGCThread canBeGCThread>
 inline ThreadSpecific<T, canBeGCThread>::ThreadSpecific()
     : m_index(-1)
@@ -227,7 +152,7 @@ inline ThreadSpecific<T, canBeGCThread>::ThreadSpecific()
         CRASH();
 
     m_index = InterlockedIncrement(&flsKeyCount()) - 1;
-    if (m_index >= kMaxFlsKeySize)
+    if (m_index >= maxFlsKeySize)
         CRASH();
     flsKeys()[m_index] = flsKey;
 }
@@ -274,7 +199,7 @@ inline void THREAD_SPECIFIC_CALL ThreadSpecific<T, canBeGCThread>::destroy(void*
 template<typename T, CanBeGCThread canBeGCThread>
 inline T* ThreadSpecific<T, canBeGCThread>::set()
 {
-    RELEASE_ASSERT(canBeGCThread == CanBeGCThread::True || !mayBeGCThread());
+    RELEASE_ASSERT(canBeGCThread == CanBeGCThread::True || !Thread::mayBeGCThread());
     ASSERT(!get());
     Data* data = new Data(this); // Data will set itself into TLS.
     ASSERT(get() == data->storagePointer());
index bf11b03..908bafe 100644 (file)
@@ -259,6 +259,24 @@ void Thread::removeFromThreadGroup(const AbstractLocker& threadGroupLocker, Thre
     });
 }
 
+bool Thread::exchangeIsCompilationThread(bool newValue)
+{
+    auto& thread = Thread::current();
+    bool oldValue = thread.m_isCompilationThread;
+    thread.m_isCompilationThread = newValue;
+    return oldValue;
+}
+
+void Thread::registerGCThread(GCThreadType gcThreadType)
+{
+    Thread::current().m_gcThreadType = static_cast<unsigned>(gcThreadType);
+}
+
+bool Thread::mayBeGCThread()
+{
+    return Thread::current().gcThreadType() != GCThreadType::None;
+}
+
 void Thread::setCurrentThreadIsUserInteractive(int relativePriority)
 {
 #if HAVE(QOS_CLASSES)
index 3a579b3..81eb386 100644 (file)
@@ -43,7 +43,6 @@
 #include <wtf/StackBounds.h>
 #include <wtf/StackStats.h>
 #include <wtf/ThreadSafeRefCounted.h>
-#include <wtf/ThreadSpecific.h>
 #include <wtf/Vector.h>
 #include <wtf/WordLock.h>
 #include <wtf/text/AtomicStringTable.h>
@@ -81,6 +80,12 @@ WTF_EXPORT_PRIVATE ThreadIdentifier createThread(ThreadFunction, void*, const ch
 WTF_EXPORT_PRIVATE int waitForThreadCompletion(ThreadIdentifier);
 #endif
 
+enum class GCThreadType : uint8_t {
+    None = 0,
+    Main,
+    Helper,
+};
+
 class Thread : public ThreadSafeRefCounted<Thread> {
 public:
     friend class ThreadGroup;
@@ -150,6 +155,10 @@ public:
     WTF_EXPORT_PRIVATE static const unsigned lockSpinLimit;
     WTF_EXPORT_PRIVATE static void yield();
 
+    WTF_EXPORT_PRIVATE static bool exchangeIsCompilationThread(bool newValue);
+    WTF_EXPORT_PRIVATE static void registerGCThread(GCThreadType);
+    WTF_EXPORT_PRIVATE static bool mayBeGCThread();
+
     WTF_EXPORT_PRIVATE void dump(PrintStream& out) const;
 
     static void initializePlatformThreading();
@@ -202,10 +211,13 @@ public:
     mach_port_t machThread() { return m_platformThread; }
 #endif
 
+    bool isCompilationThread() const { return m_isCompilationThread; }
+    GCThreadType gcThreadType() const { return static_cast<GCThreadType>(m_gcThreadType); }
+
     struct NewThreadContext;
     static void entryPoint(NewThreadContext*);
 protected:
-    Thread() = default;
+    Thread();
 
     void initializeInThread();
 
@@ -238,11 +250,11 @@ protected:
         Detached,
     };
 
-    JoinableState joinableState() { return m_joinableState; }
+    JoinableState joinableState() const { return m_joinableState; }
     void didBecomeDetached() { m_joinableState = Detached; }
     void didExit();
     void didJoin() { m_joinableState = Joined; }
-    bool hasExited() { return m_didExit; }
+    bool hasExited() const { return m_didExit; }
 
     // These functions are only called from ThreadGroup.
     ThreadGroupAddResult addToThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup&);
@@ -280,9 +292,11 @@ protected:
     static void THREAD_SPECIFIC_CALL destructTLS(void* data);
 
     JoinableState m_joinableState { Joinable };
-    bool m_isShuttingDown { false };
-    bool m_didExit { false };
-    bool m_isDestroyedOnce { false };
+    bool m_isShuttingDown : 1;
+    bool m_didExit : 1;
+    bool m_isDestroyedOnce : 1;
+    bool m_isCompilationThread: 1;
+    unsigned m_gcThreadType : 2;
 
     // Lock & ParkingLot rely on ThreadSpecific. But Thread object can be destroyed even after ThreadSpecific things are destroyed.
     // Use WordLock since WordLock does not depend on ThreadSpecific and this "Thread".
@@ -311,6 +325,15 @@ public:
     void* m_apiData { nullptr };
 };
 
+inline Thread::Thread()
+    : m_isShuttingDown(false)
+    , m_didExit(false)
+    , m_isDestroyedOnce(false)
+    , m_isCompilationThread(false)
+    , m_gcThreadType(static_cast<unsigned>(GCThreadType::None))
+{
+}
+
 inline Thread* Thread::currentMayBeNull()
 {
 #if !HAVE(FAST_TLS)
@@ -345,6 +368,7 @@ inline Thread& Thread::current()
 } // namespace WTF
 
 using WTF::Thread;
+using WTF::GCThreadType;
 
 #if OS(WINDOWS)
 using WTF::ThreadIdentifier;
index 2693005..b42b52f 100644 (file)
@@ -30,6 +30,7 @@
 
 #pragma once
 
+#include <limits.h>
 #include <wtf/FastMalloc.h>
 #include <wtf/Locker.h>
 #include <wtf/Noncopyable.h>
 
 #if USE(PTHREADS)
 #include <pthread.h>
+#if !defined(PTHREAD_KEYS_MAX)
+// PTHREAD_KEYS_MAX is not defined in bionic nor in Hurd, so explicitly define it here.
+#define PTHREAD_KEYS_MAX 1024
+#endif
+#endif
+
+#if OS(WINDOWS) && CPU(X86)
+#define THREAD_SPECIFIC_CALL __stdcall
+#else
+#define THREAD_SPECIFIC_CALL
 #endif
 
 namespace WTF {
@@ -51,11 +62,13 @@ using ThreadFunction = void (*)(void* argument);
 using PlatformThreadHandle = pthread_t;
 using PlatformMutex = pthread_mutex_t;
 using PlatformCondition = pthread_cond_t;
+using ThreadSpecificKey = pthread_key_t;
 #elif OS(WINDOWS)
 using ThreadIdentifier = uint32_t;
 using PlatformThreadHandle = HANDLE;
 using PlatformMutex = SRWLOCK;
 using PlatformCondition = CONDITION_VARIABLE;
+using ThreadSpecificKey = DWORD;
 #else
 #error "Not supported platform"
 #endif
@@ -104,6 +117,64 @@ private:
 #endif
 };
 
+#if USE(PTHREADS)
+
+static constexpr ThreadSpecificKey InvalidThreadSpecificKey = PTHREAD_KEYS_MAX;
+
+inline void threadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void *))
+{
+    int error = pthread_key_create(key, destructor);
+    if (error)
+        CRASH();
+}
+
+inline void threadSpecificKeyDelete(ThreadSpecificKey key)
+{
+    int error = pthread_key_delete(key);
+    if (error)
+        CRASH();
+}
+
+inline void threadSpecificSet(ThreadSpecificKey key, void* value)
+{
+    pthread_setspecific(key, value);
+}
+
+inline void* threadSpecificGet(ThreadSpecificKey key)
+{
+    return pthread_getspecific(key);
+}
+
+#elif OS(WINDOWS)
+
+static constexpr ThreadSpecificKey InvalidThreadSpecificKey = FLS_OUT_OF_INDEXES;
+
+inline void threadSpecificKeyCreate(ThreadSpecificKey* key, void (THREAD_SPECIFIC_CALL *destructor)(void *))
+{
+    DWORD flsKey = FlsAlloc(destructor);
+    if (flsKey == FLS_OUT_OF_INDEXES)
+        CRASH();
+
+    *key = flsKey;
+}
+
+inline void threadSpecificKeyDelete(ThreadSpecificKey key)
+{
+    FlsFree(key);
+}
+
+inline void threadSpecificSet(ThreadSpecificKey key, void* data)
+{
+    FlsSetValue(key, data);
+}
+
+inline void* threadSpecificGet(ThreadSpecificKey key)
+{
+    return FlsGetValue(key);
+}
+
+#endif
+
 } // namespace WTF
 
 using WTF::Mutex;
index 2a8ef32..d11cebf 100644 (file)
@@ -34,7 +34,7 @@ long& flsKeyCount()
 
 DWORD* flsKeys()
 {
-    static DWORD keys[kMaxFlsKeySize];
+    static DWORD keys[maxFlsKeySize];
     return keys;
 }
 
index dbaa1fe..92c120b 100644 (file)
@@ -1,3 +1,17 @@
+2019-05-13  Yusuke Suzuki  <ysuzuki@apple.com>
+
+        [WTF] Simplify GCThread and CompilationThread flags by adding them to WTF::Thread
+        https://bugs.webkit.org/show_bug.cgi?id=197146
+
+        Reviewed by Saam Barati.
+
+        * Modules/indexeddb/IDBDatabase.cpp:
+        (WebCore::IDBDatabase::hasPendingActivity const):
+        * Modules/indexeddb/IDBRequest.cpp:
+        (WebCore::IDBRequest::hasPendingActivity const):
+        * Modules/indexeddb/IDBTransaction.cpp:
+        (WebCore::IDBTransaction::hasPendingActivity const):
+
 2019-05-13  Geoffrey Garen  <ggaren@apple.com>
 
         Downgrade RELEASE_ASSERT TO RELEASE_LOG_FAULT for SQLite Class A files
index 5b34eaf..0c7ac17 100644 (file)
@@ -77,7 +77,7 @@ IDBDatabase::~IDBDatabase()
 
 bool IDBDatabase::hasPendingActivity() const
 {
-    ASSERT(&originThread() == &Thread::current() || mayBeGCThread());
+    ASSERT(&originThread() == &Thread::current() || Thread::mayBeGCThread());
 
     if (m_closedInServer || isContextStopped())
         return false;
index ce4f61a..a828ce0 100644 (file)
@@ -267,7 +267,7 @@ bool IDBRequest::canSuspendForDocumentSuspension() const
 
 bool IDBRequest::hasPendingActivity() const
 {
-    ASSERT(&originThread() == &Thread::current() || mayBeGCThread());
+    ASSERT(&originThread() == &Thread::current() || Thread::mayBeGCThread());
     return !m_contextStopped && m_hasPendingActivity;
 }
 
index 157085a..1a7aebb 100644 (file)
@@ -331,7 +331,7 @@ bool IDBTransaction::canSuspendForDocumentSuspension() const
 
 bool IDBTransaction::hasPendingActivity() const
 {
-    ASSERT(&m_database->originThread() == &Thread::current() || mayBeGCThread());
+    ASSERT(&m_database->originThread() == &Thread::current() || Thread::mayBeGCThread());
     return !m_contextStopped && m_state != IndexedDB::TransactionState::Finished;
 }