Title: [245258] trunk/Source
Revision
245258
Author
ysuz...@apple.com
Date
2019-05-13 15:32:34 -0700 (Mon, 13 May 2019)

Log Message

[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.

Source/_javascript_Core:

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):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (245257 => 245258)


--- trunk/Source/_javascript_Core/ChangeLog	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/_javascript_Core/ChangeLog	2019-05-13 22:32:34 UTC (rev 245258)
@@ -1,3 +1,32 @@
+2019-05-13  Yusuke Suzuki  <ysuz...@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  <sbar...@apple.com>
 
         macro assembler code-pointer tagging has its arguments backwards

Modified: trunk/Source/_javascript_Core/heap/AlignedMemoryAllocator.cpp (245257 => 245258)


--- trunk/Source/_javascript_Core/heap/AlignedMemoryAllocator.cpp	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/_javascript_Core/heap/AlignedMemoryAllocator.cpp	2019-05-13 22:32:34 UTC (rev 245258)
@@ -45,7 +45,7 @@
     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);
     }

Modified: trunk/Source/_javascript_Core/heap/Heap.cpp (245257 => 245258)


--- trunk/Source/_javascript_Core/heap/Heap.cpp	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/_javascript_Core/heap/Heap.cpp	2019-05-13 22:32:34 UTC (rev 245258)
@@ -148,7 +148,7 @@
 
 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 @@
 
 } // 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 @@
     
     void threadDidStart() override
     {
-        WTF::registerGCThread(GCThreadType::Main);
+        Thread::registerGCThread(GCThreadType::Main);
     }
 
 private:
@@ -323,7 +323,7 @@
     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 @@
 {
     checkConn(conn);
     m_currentThreadState = currentThreadState;
-    m_currentThread = &WTF::Thread::current();
+    m_currentThread = &Thread::current();
     
     if (conn == GCConductor::Mutator)
         sanitizeStackForVM(vm());
@@ -1298,7 +1298,7 @@
                     slotVisitor = m_availableParallelSlotVisitors.takeLast();
             }
 
-            WTF::registerGCThread(GCThreadType::Helper);
+            Thread::registerGCThread(GCThreadType::Helper);
 
             {
                 ParallelModeEnabler parallelModeEnabler(*slotVisitor);
@@ -1695,7 +1695,7 @@
                 slotVisitorsToUpdate.takeLast();
             }
         }
-        WTF::Thread::yield();
+        Thread::yield();
     }
     
     for (SlotVisitor* slotVisitor : slotVisitorsToUpdate)
@@ -2105,7 +2105,7 @@
     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 @@
 
 bool Heap::isCurrentThreadBusy()
 {
-    return mayBeGCThread() || mutatorState() != MutatorState::Running;
+    return Thread::mayBeGCThread() || mutatorState() != MutatorState::Running;
 }
 
 void Heap::reportExtraMemoryVisited(size_t size)
@@ -2864,7 +2864,7 @@
     m_isSafeToCollect = true;
     
     if (Options::collectContinuously()) {
-        m_collectContinuouslyThread = WTF::Thread::create(
+        m_collectContinuouslyThread = Thread::create(
             "JSC DEBUG Continuous GC",
             [this] () {
                 MonotonicTime initialTime = MonotonicTime::now();

Modified: trunk/Source/_javascript_Core/heap/Heap.h (245257 => 245258)


--- trunk/Source/_javascript_Core/heap/Heap.h	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/_javascript_Core/heap/Heap.h	2019-05-13 22:32:34 UTC (rev 245258)
@@ -426,8 +426,8 @@
     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 @@
     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 @@
     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;

Modified: trunk/Source/_javascript_Core/heap/HeapInlines.h (245257 => 245258)


--- trunk/Source/_javascript_Core/heap/HeapInlines.h	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/_javascript_Core/heap/HeapInlines.h	2019-05-13 22:32:34 UTC (rev 245258)
@@ -173,19 +173,19 @@
 
 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)) {

Modified: trunk/Source/_javascript_Core/heap/MarkedSpace.cpp (245257 => 245258)


--- trunk/Source/_javascript_Core/heap/MarkedSpace.cpp	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/_javascript_Core/heap/MarkedSpace.cpp	2019-05-13 22:32:34 UTC (rev 245258)
@@ -259,7 +259,7 @@
 
 void MarkedSpace::prepareForAllocation()
 {
-    ASSERT(!mayBeGCThread() || m_heap->worldIsStopped());
+    ASSERT(!Thread::mayBeGCThread() || m_heap->worldIsStopped());
     for (Subspace* subspace : m_subspaces)
         subspace->prepareForAllocation();
 

Modified: trunk/Source/WTF/ChangeLog (245257 => 245258)


--- trunk/Source/WTF/ChangeLog	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WTF/ChangeLog	2019-05-13 22:32:34 UTC (rev 245258)
@@ -1,5 +1,59 @@
 2019-05-13  Yusuke Suzuki  <ysuz...@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  <ysuz...@apple.com>
+
         Unreviewed, follow-up after r245214
         https://bugs.webkit.org/show_bug.cgi?id=197730
 

Modified: trunk/Source/WTF/wtf/CompilationThread.cpp (245257 => 245258)


--- trunk/Source/WTF/wtf/CompilationThread.cpp	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WTF/wtf/CompilationThread.cpp	2019-05-13 22:32:34 UTC (rev 245258)
@@ -26,39 +26,15 @@
 #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;
+    return Thread::current().isCompilationThread();
 }
 
-bool exchangeIsCompilationThread(bool newValue)
-{
-    initializeCompilationThreads();
-    bool oldValue = isCompilationThread();
-    **s_isCompilationThread = newValue;
-    return oldValue;
-}
-
 } // namespace WTF
 

Modified: trunk/Source/WTF/wtf/CompilationThread.h (245257 => 245258)


--- trunk/Source/WTF/wtf/CompilationThread.h	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WTF/wtf/CompilationThread.h	2019-05-13 22:32:34 UTC (rev 245258)
@@ -25,25 +25,25 @@
 
 #pragma once
 
+#include <wtf/Threading.h>
+
 namespace WTF {
 
-WTF_EXPORT_PRIVATE bool exchangeIsCompilationThread(bool newValue);
-
 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 @@
 } // namespace WTF
 
 using WTF::CompilationScope;
-using WTF::exchangeIsCompilationThread;

Modified: trunk/Source/WTF/wtf/MainThread.cpp (245257 => 245258)


--- trunk/Source/WTF/wtf/MainThread.cpp	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WTF/wtf/MainThread.cpp	2019-05-13 22:32:34 UTC (rev 245258)
@@ -57,7 +57,6 @@
     std::call_once(initializeKey, [] {
         initializeThreading();
         initializeMainThreadPlatform();
-        initializeGCThreads();
     });
 }
 
@@ -68,7 +67,6 @@
     std::call_once(initializeKey, [] {
         initializeThreading();
         initializeMainThreadToProcessMainThreadPlatform();
-        initializeGCThreads();
     });
 }
 #else
@@ -157,46 +155,14 @@
         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()) {

Modified: trunk/Source/WTF/wtf/MainThread.h (245257 => 245258)


--- trunk/Source/WTF/wtf/MainThread.h	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WTF/wtf/MainThread.h	2019-05-13 22:32:34 UTC (rev 245258)
@@ -68,17 +68,6 @@
 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 @@
 
 } // namespace WTF
 
-using WTF::GCThreadType;
 using WTF::callOnMainThread;
 using WTF::callOnMainThreadAndWait;
 using WTF::canAccessThreadLocalDataForThread;
@@ -106,7 +94,6 @@
 using WTF::isMainThreadOrGCThread;
 using WTF::isUIThread;
 using WTF::isWebThread;
-using WTF::mayBeGCThread;
 using WTF::setMainThreadCallbacksPaused;
 #if PLATFORM(COCOA)
 using WTF::dispatchAsyncOnMainThreadWithWebThreadLockIfNeeded;

Modified: trunk/Source/WTF/wtf/ThreadSpecific.h (245257 => 245258)


--- trunk/Source/WTF/wtf/ThreadSpecific.h	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WTF/wtf/ThreadSpecific.h	2019-05-13 22:32:34 UTC (rev 245258)
@@ -44,29 +44,10 @@
 #include <wtf/MainThread.h>
 #include <wtf/Noncopyable.h>
 #include <wtf/StdLibExtras.h>
+#include <wtf/Threading.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
-
 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 @@
 
 #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 @@
 // 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 @@
         CRASH();
 
     m_index = InterlockedIncrement(&flsKeyCount()) - 1;
-    if (m_index >= kMaxFlsKeySize)
+    if (m_index >= maxFlsKeySize)
         CRASH();
     flsKeys()[m_index] = flsKey;
 }
@@ -274,7 +199,7 @@
 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 = "" Data(this); // Data will set itself into TLS.
     ASSERT(get() == data->storagePointer());

Modified: trunk/Source/WTF/wtf/Threading.cpp (245257 => 245258)


--- trunk/Source/WTF/wtf/Threading.cpp	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WTF/wtf/Threading.cpp	2019-05-13 22:32:34 UTC (rev 245258)
@@ -259,6 +259,24 @@
     });
 }
 
+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)

Modified: trunk/Source/WTF/wtf/Threading.h (245257 => 245258)


--- trunk/Source/WTF/wtf/Threading.h	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WTF/wtf/Threading.h	2019-05-13 22:32:34 UTC (rev 245258)
@@ -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 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 @@
     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 @@
     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 @@
         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 @@
     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 @@
     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 @@
 } // namespace WTF
 
 using WTF::Thread;
+using WTF::GCThreadType;
 
 #if OS(WINDOWS)
 using WTF::ThreadIdentifier;

Modified: trunk/Source/WTF/wtf/ThreadingPrimitives.h (245257 => 245258)


--- trunk/Source/WTF/wtf/ThreadingPrimitives.h	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WTF/wtf/ThreadingPrimitives.h	2019-05-13 22:32:34 UTC (rev 245258)
@@ -30,6 +30,7 @@
 
 #pragma once
 
+#include <limits.h>
 #include <wtf/FastMalloc.h>
 #include <wtf/Locker.h>
 #include <wtf/Noncopyable.h>
@@ -41,8 +42,18 @@
 
 #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 {
 
 using ThreadFunction = void (*)(void* argument);
@@ -51,11 +62,13 @@
 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 @@
 #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;

Modified: trunk/Source/WTF/wtf/win/ThreadSpecificWin.cpp (245257 => 245258)


--- trunk/Source/WTF/wtf/win/ThreadSpecificWin.cpp	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WTF/wtf/win/ThreadSpecificWin.cpp	2019-05-13 22:32:34 UTC (rev 245258)
@@ -34,7 +34,7 @@
 
 DWORD* flsKeys()
 {
-    static DWORD keys[kMaxFlsKeySize];
+    static DWORD keys[maxFlsKeySize];
     return keys;
 }
 

Modified: trunk/Source/WebCore/ChangeLog (245257 => 245258)


--- trunk/Source/WebCore/ChangeLog	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WebCore/ChangeLog	2019-05-13 22:32:34 UTC (rev 245258)
@@ -1,3 +1,17 @@
+2019-05-13  Yusuke Suzuki  <ysuz...@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  <gga...@apple.com>
 
         Downgrade RELEASE_ASSERT TO RELEASE_LOG_FAULT for SQLite Class A files

Modified: trunk/Source/WebCore/Modules/indexeddb/IDBDatabase.cpp (245257 => 245258)


--- trunk/Source/WebCore/Modules/indexeddb/IDBDatabase.cpp	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WebCore/Modules/indexeddb/IDBDatabase.cpp	2019-05-13 22:32:34 UTC (rev 245258)
@@ -77,7 +77,7 @@
 
 bool IDBDatabase::hasPendingActivity() const
 {
-    ASSERT(&originThread() == &Thread::current() || mayBeGCThread());
+    ASSERT(&originThread() == &Thread::current() || Thread::mayBeGCThread());
 
     if (m_closedInServer || isContextStopped())
         return false;

Modified: trunk/Source/WebCore/Modules/indexeddb/IDBRequest.cpp (245257 => 245258)


--- trunk/Source/WebCore/Modules/indexeddb/IDBRequest.cpp	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WebCore/Modules/indexeddb/IDBRequest.cpp	2019-05-13 22:32:34 UTC (rev 245258)
@@ -267,7 +267,7 @@
 
 bool IDBRequest::hasPendingActivity() const
 {
-    ASSERT(&originThread() == &Thread::current() || mayBeGCThread());
+    ASSERT(&originThread() == &Thread::current() || Thread::mayBeGCThread());
     return !m_contextStopped && m_hasPendingActivity;
 }
 

Modified: trunk/Source/WebCore/Modules/indexeddb/IDBTransaction.cpp (245257 => 245258)


--- trunk/Source/WebCore/Modules/indexeddb/IDBTransaction.cpp	2019-05-13 22:16:55 UTC (rev 245257)
+++ trunk/Source/WebCore/Modules/indexeddb/IDBTransaction.cpp	2019-05-13 22:32:34 UTC (rev 245258)
@@ -331,7 +331,7 @@
 
 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;
 }
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to