haikuwebkit/Source/WTF/wtf/ThreadGroup.cpp

57 lines
2.0 KiB
C++
Raw Permalink Normal View History

[WTF] Implement WTF::ThreadGroup https://bugs.webkit.org/show_bug.cgi?id=174081 Reviewed by Mark Lam. Source/JavaScriptCore: Large part of MachineThreads are now removed and replaced with WTF::ThreadGroup. And SamplingProfiler and others interact with WTF::Thread directly. * API/tests/ExecutionTimeLimitTest.cpp: * heap/MachineStackMarker.cpp: (JSC::MachineThreads::MachineThreads): (JSC::captureStack): (JSC::MachineThreads::tryCopyOtherThreadStack): (JSC::MachineThreads::tryCopyOtherThreadStacks): (JSC::MachineThreads::gatherConservativeRoots): (JSC::ActiveMachineThreadsManager::Locker::Locker): Deleted. (JSC::ActiveMachineThreadsManager::add): Deleted. (JSC::ActiveMachineThreadsManager::remove): Deleted. (JSC::ActiveMachineThreadsManager::contains): Deleted. (JSC::ActiveMachineThreadsManager::ActiveMachineThreadsManager): Deleted. (JSC::activeMachineThreadsManager): Deleted. (JSC::MachineThreads::~MachineThreads): Deleted. (JSC::MachineThreads::addCurrentThread): Deleted. (): Deleted. (JSC::MachineThreads::removeThread): Deleted. (JSC::MachineThreads::removeThreadIfFound): Deleted. (JSC::MachineThreads::MachineThread::MachineThread): Deleted. (JSC::MachineThreads::MachineThread::getRegisters): Deleted. (JSC::MachineThreads::MachineThread::Registers::stackPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::framePointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::instructionPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::llintPC): Deleted. (JSC::MachineThreads::MachineThread::captureStack): Deleted. * heap/MachineStackMarker.h: (JSC::MachineThreads::addCurrentThread): (JSC::MachineThreads::getLock): (JSC::MachineThreads::threads): (JSC::MachineThreads::MachineThread::suspend): Deleted. (JSC::MachineThreads::MachineThread::resume): Deleted. (JSC::MachineThreads::MachineThread::threadID): Deleted. (JSC::MachineThreads::MachineThread::stackBase): Deleted. (JSC::MachineThreads::MachineThread::stackEnd): Deleted. (JSC::MachineThreads::threadsListHead): Deleted. * runtime/SamplingProfiler.cpp: (JSC::FrameWalker::isValidFramePointer): (JSC::SamplingProfiler::SamplingProfiler): (JSC::SamplingProfiler::takeSample): (JSC::SamplingProfiler::noticeCurrentThreadAsJSCExecutionThread): * runtime/SamplingProfiler.h: * wasm/WasmMachineThreads.cpp: (JSC::Wasm::resetInstructionCacheOnAllThreads): Source/WebCore: * page/ResourceUsageThread.h: Source/WebKit: * Shared/AsyncRequest.h: Source/WTF: This patch implements WTF::ThreadGroup. It implements core of JSC::MachineThreads with more reliable way. JSC::MachineThreads was complicated because of managing dead threads. Each JSC::MachineThreads has its own TLS with a registered destructor. And everytime a thread dies, the registered TLS destructor is called. And this destructor will remove the current dying thread from JSC::MachineThreads. However the above implementation is tricky. And each JSC::MachineThreads requires own TLS space, which is not considered in WTF's Windows ThreadSpecific implementation. Current design works well since we only have small number of MachineThreads right now. Instead, we use more reliable way. After introducing WTF::Thread, WTF::Thread has WTF::Thread::didExit, which is called when associated TLS (with WTF::Thread) is destroyed. We leverage this mechanism to remove WTF::Thread from MachineThreads. This patch introduces WTF::ThreadGroup. It is tightly integrated with WTF::Thread: WTF::Thread knows ThreadGroups which includes this thread. And WTF::ThreadGroup of course knows WTF::Threads added to it. WTF::Thread::didExit carefully remove itself from WTF::ThreadGroups. The most important part of this patch is locking. WTF::Thread can die. And WTF::ThreadGroup can die. If we take a design using two fine grain locks in WTF::Thread and WTF::ThreadGroup, we easily encounter dead lock. Consider the following case. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold a lock of THG, and hold a lock of TH (locking order is THG -> TH). 2. When TH dies, TH need to hold a lock of TH to iterate THGs. And we hold a lock of THG to unregister TH from it (locking order is TH -> THG). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (locking order is THG -> TH). 4. When destroying THG, we need to hold a lock of TH to unregister THG from TH. We can hold a lock of THG before that (locking order is THG -> TH). Then, it easily causes dead lock. We cannot swap the locking order of (2) since iterating THG requires a lock of TH. To solve this problem, we use std::shared_ptr and std::weak_ptr. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold THG, and hold a lock of TH. (THG -> TH) 2. When TH dies, TH first hold lock of TH. And we use std::weak_ptr<>::lock() to retain non-destructed ThreadGroups. If some of ThreadGroups are dying, we just ignore them. It is ok because such a ThreadGroup will be destructed. So we do not need to unregister this thread from such a ThreadGroup. Then, we have Vector<std::shared_ptr<ThreadGroup>>. So we unlock a lock of TH. To unregister a thread from thread group, we first hold a lock of THG and then hold a lock of TH. Both lifetime is ensured: THG is retained by std::shared_ptr. And TH is itself. (TH), (THG -> TH). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (THG -> TH). 4. When destroying THG, we hold a lock of THG. And hold a lock of TH. During holding THG's lock, registered thread never dies because (2) holds THG lock. (THG -> TH). We also fix suspend and resume locking mechanism to avoid dead lock. We should hold the global lock when suspending and resuming. If we use per-thread lock, the suspended thread can hold the lock of the other threads. It causes dead lock. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: * wtf/CMakeLists.txt: * wtf/CrossThreadCopier.h: * wtf/ParkingLot.h: * wtf/ThreadGroup.cpp: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::~ThreadGroup): (WTF::ThreadGroup::add): (WTF::ThreadGroup::addCurrentThread): * wtf/ThreadGroup.h: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::create): (WTF::ThreadGroup::threads): (WTF::ThreadGroup::getLock): (WTF::ThreadGroup::weakFromThis): * wtf/Threading.cpp: (WTF::shouldRemoveThreadFromThreadGroup): (WTF::Thread::didExit): (WTF::Thread::addToThreadGroup): (WTF::Thread::removeFromThreadGroup): * wtf/Threading.h: * wtf/ThreadingPthreads.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): * wtf/ThreadingWin.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): Tools: Add WTF::ThreadGroup tests. * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/ThreadGroup.cpp: Added. (TestWebKitAPI::testThreadGroup): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/191464@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219653 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-19 08:43:57 +00:00
/*
* Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
Use pragma once in WTF https://bugs.webkit.org/show_bug.cgi?id=190527 Reviewed by Chris Dumez. Source/WTF: We also need to consistently include wtf headers from within wtf so we can build wtf without symbol redefinition errors from including the copy in Source and the copy in the build directory. * wtf/ASCIICType.h: * wtf/Assertions.cpp: * wtf/Assertions.h: * wtf/Atomics.h: * wtf/AutomaticThread.cpp: * wtf/AutomaticThread.h: * wtf/BackwardsGraph.h: * wtf/Bag.h: * wtf/BagToHashMap.h: * wtf/BitVector.cpp: * wtf/BitVector.h: * wtf/Bitmap.h: * wtf/BloomFilter.h: * wtf/Box.h: * wtf/BubbleSort.h: * wtf/BumpPointerAllocator.h: * wtf/ByteOrder.h: * wtf/CPUTime.cpp: * wtf/CallbackAggregator.h: * wtf/CheckedArithmetic.h: * wtf/CheckedBoolean.h: * wtf/ClockType.cpp: * wtf/ClockType.h: * wtf/CommaPrinter.h: * wtf/CompilationThread.cpp: * wtf/CompilationThread.h: * wtf/Compiler.h: * wtf/ConcurrentPtrHashSet.cpp: * wtf/ConcurrentVector.h: * wtf/Condition.h: * wtf/CountingLock.cpp: * wtf/CrossThreadTaskHandler.cpp: * wtf/CryptographicUtilities.cpp: * wtf/CryptographicUtilities.h: * wtf/CryptographicallyRandomNumber.cpp: * wtf/CryptographicallyRandomNumber.h: * wtf/CurrentTime.cpp: * wtf/DataLog.cpp: * wtf/DataLog.h: * wtf/DateMath.cpp: * wtf/DateMath.h: * wtf/DecimalNumber.cpp: * wtf/DecimalNumber.h: * wtf/Deque.h: * wtf/DisallowCType.h: * wtf/Dominators.h: * wtf/DoublyLinkedList.h: * wtf/FastBitVector.cpp: * wtf/FastMalloc.cpp: * wtf/FastMalloc.h: * wtf/FeatureDefines.h: * wtf/FilePrintStream.cpp: * wtf/FilePrintStream.h: * wtf/FlipBytes.h: * wtf/FunctionDispatcher.cpp: * wtf/FunctionDispatcher.h: * wtf/GetPtr.h: * wtf/Gigacage.cpp: * wtf/GlobalVersion.cpp: * wtf/GraphNodeWorklist.h: * wtf/GregorianDateTime.cpp: * wtf/GregorianDateTime.h: * wtf/HashFunctions.h: * wtf/HashMap.h: * wtf/HashMethod.h: * wtf/HashSet.h: * wtf/HashTable.cpp: * wtf/HashTraits.h: * wtf/Indenter.h: * wtf/IndexSparseSet.h: * wtf/InlineASM.h: * wtf/Insertion.h: * wtf/IteratorAdaptors.h: * wtf/IteratorRange.h: * wtf/JSONValues.cpp: * wtf/JSValueMalloc.cpp: * wtf/LEBDecoder.h: * wtf/Language.cpp: * wtf/ListDump.h: * wtf/Lock.cpp: * wtf/Lock.h: * wtf/LockAlgorithm.h: * wtf/LockedPrintStream.cpp: * wtf/Locker.h: * wtf/MD5.cpp: * wtf/MD5.h: * wtf/MainThread.cpp: * wtf/MainThread.h: * wtf/MallocPtr.h: * wtf/MathExtras.h: * wtf/MediaTime.cpp: * wtf/MediaTime.h: * wtf/MemoryPressureHandler.cpp: * wtf/MessageQueue.h: * wtf/MetaAllocator.cpp: * wtf/MetaAllocator.h: * wtf/MetaAllocatorHandle.h: * wtf/MonotonicTime.cpp: * wtf/MonotonicTime.h: * wtf/NakedPtr.h: * wtf/NoLock.h: * wtf/NoTailCalls.h: * wtf/Noncopyable.h: * wtf/NumberOfCores.cpp: * wtf/NumberOfCores.h: * wtf/OSAllocator.h: * wtf/OSAllocatorPosix.cpp: * wtf/OSRandomSource.cpp: * wtf/OSRandomSource.h: * wtf/ObjcRuntimeExtras.h: * wtf/OrderMaker.h: * wtf/PackedIntVector.h: * wtf/PageAllocation.h: * wtf/PageBlock.cpp: * wtf/PageBlock.h: * wtf/PageReservation.h: * wtf/ParallelHelperPool.cpp: * wtf/ParallelHelperPool.h: * wtf/ParallelJobs.h: * wtf/ParallelJobsLibdispatch.h: * wtf/ParallelVectorIterator.h: * wtf/ParkingLot.cpp: * wtf/ParkingLot.h: * wtf/Platform.h: * wtf/PointerComparison.h: * wtf/Poisoned.cpp: * wtf/PrintStream.cpp: * wtf/PrintStream.h: * wtf/ProcessID.h: * wtf/ProcessPrivilege.cpp: * wtf/RAMSize.cpp: * wtf/RAMSize.h: * wtf/RandomDevice.cpp: * wtf/RandomNumber.cpp: * wtf/RandomNumber.h: * wtf/RandomNumberSeed.h: * wtf/RangeSet.h: * wtf/RawPointer.h: * wtf/ReadWriteLock.cpp: * wtf/RedBlackTree.h: * wtf/Ref.h: * wtf/RefCountedArray.h: * wtf/RefCountedLeakCounter.cpp: * wtf/RefCountedLeakCounter.h: * wtf/RefCounter.h: * wtf/RefPtr.h: * wtf/RetainPtr.h: * wtf/RunLoop.cpp: * wtf/RunLoop.h: * wtf/RunLoopTimer.h: * wtf/RunLoopTimerCF.cpp: * wtf/SHA1.cpp: * wtf/SHA1.h: * wtf/SaturatedArithmetic.h: (saturatedSubtraction): * wtf/SchedulePair.h: * wtf/SchedulePairCF.cpp: * wtf/SchedulePairMac.mm: * wtf/ScopedLambda.h: * wtf/Seconds.cpp: * wtf/Seconds.h: * wtf/SegmentedVector.h: * wtf/SentinelLinkedList.h: * wtf/SharedTask.h: * wtf/SimpleStats.h: * wtf/SingleRootGraph.h: * wtf/SinglyLinkedList.h: * wtf/SixCharacterHash.cpp: * wtf/SixCharacterHash.h: * wtf/SmallPtrSet.h: * wtf/Spectrum.h: * wtf/StackBounds.cpp: * wtf/StackBounds.h: * wtf/StackStats.cpp: * wtf/StackStats.h: * wtf/StackTrace.cpp: * wtf/StdLibExtras.h: * wtf/StreamBuffer.h: * wtf/StringHashDumpContext.h: * wtf/StringPrintStream.cpp: * wtf/StringPrintStream.h: * wtf/ThreadGroup.cpp: * wtf/ThreadMessage.cpp: * wtf/ThreadSpecific.h: * wtf/Threading.cpp: * wtf/Threading.h: * wtf/ThreadingPrimitives.h: * wtf/ThreadingPthreads.cpp: * wtf/TimeWithDynamicClockType.cpp: * wtf/TimeWithDynamicClockType.h: * wtf/TimingScope.cpp: * wtf/TinyLRUCache.h: * wtf/TinyPtrSet.h: * wtf/TriState.h: * wtf/TypeCasts.h: * wtf/UUID.cpp: * wtf/UnionFind.h: * wtf/VMTags.h: * wtf/ValueCheck.h: * wtf/Vector.h: * wtf/VectorTraits.h: * wtf/WallTime.cpp: * wtf/WallTime.h: * wtf/WeakPtr.h: * wtf/WeakRandom.h: * wtf/WordLock.cpp: * wtf/WordLock.h: * wtf/WorkQueue.cpp: * wtf/WorkQueue.h: * wtf/WorkerPool.cpp: * wtf/cf/LanguageCF.cpp: * wtf/cf/RunLoopCF.cpp: * wtf/cocoa/Entitlements.mm: * wtf/cocoa/MachSendRight.cpp: * wtf/cocoa/MainThreadCocoa.mm: * wtf/cocoa/MemoryFootprintCocoa.cpp: * wtf/cocoa/WorkQueueCocoa.cpp: * wtf/dtoa.cpp: * wtf/dtoa.h: * wtf/ios/WebCoreThread.cpp: * wtf/ios/WebCoreThread.h: * wtf/mac/AppKitCompatibilityDeclarations.h: * wtf/mac/DeprecatedSymbolsUsedBySafari.mm: * wtf/mbmalloc.cpp: * wtf/persistence/PersistentCoders.cpp: * wtf/persistence/PersistentDecoder.cpp: * wtf/persistence/PersistentEncoder.cpp: * wtf/spi/cf/CFBundleSPI.h: * wtf/spi/darwin/CommonCryptoSPI.h: * wtf/text/ASCIIFastPath.h: * wtf/text/ASCIILiteral.cpp: * wtf/text/AtomicString.cpp: * wtf/text/AtomicString.h: * wtf/text/AtomicStringHash.h: * wtf/text/AtomicStringImpl.cpp: * wtf/text/AtomicStringImpl.h: * wtf/text/AtomicStringTable.cpp: * wtf/text/AtomicStringTable.h: * wtf/text/Base64.cpp: * wtf/text/CString.cpp: * wtf/text/CString.h: * wtf/text/ConversionMode.h: * wtf/text/ExternalStringImpl.cpp: * wtf/text/IntegerToStringConversion.h: * wtf/text/LChar.h: * wtf/text/LineEnding.cpp: * wtf/text/StringBuffer.h: * wtf/text/StringBuilder.cpp: * wtf/text/StringBuilder.h: * wtf/text/StringBuilderJSON.cpp: * wtf/text/StringCommon.h: * wtf/text/StringConcatenate.h: * wtf/text/StringHash.h: * wtf/text/StringImpl.cpp: * wtf/text/StringImpl.h: * wtf/text/StringOperators.h: * wtf/text/StringView.cpp: * wtf/text/StringView.h: * wtf/text/SymbolImpl.cpp: * wtf/text/SymbolRegistry.cpp: * wtf/text/SymbolRegistry.h: * wtf/text/TextBreakIterator.cpp: * wtf/text/TextBreakIterator.h: * wtf/text/TextBreakIteratorInternalICU.h: * wtf/text/TextPosition.h: * wtf/text/TextStream.cpp: * wtf/text/UniquedStringImpl.h: * wtf/text/WTFString.cpp: * wtf/text/WTFString.h: * wtf/text/cocoa/StringCocoa.mm: * wtf/text/cocoa/StringViewCocoa.mm: * wtf/text/cocoa/TextBreakIteratorInternalICUCocoa.cpp: * wtf/text/icu/UTextProvider.cpp: * wtf/text/icu/UTextProvider.h: * wtf/text/icu/UTextProviderLatin1.cpp: * wtf/text/icu/UTextProviderLatin1.h: * wtf/text/icu/UTextProviderUTF16.cpp: * wtf/text/icu/UTextProviderUTF16.h: * wtf/threads/BinarySemaphore.cpp: * wtf/threads/BinarySemaphore.h: * wtf/threads/Signals.cpp: * wtf/unicode/CharacterNames.h: * wtf/unicode/Collator.h: * wtf/unicode/CollatorDefault.cpp: * wtf/unicode/UTF8.cpp: * wtf/unicode/UTF8.h: Tools: Put WorkQueue in namespace DRT so it does not conflict with WTF::WorkQueue. * DumpRenderTree/TestRunner.cpp: (TestRunner::queueLoadHTMLString): (TestRunner::queueLoadAlternateHTMLString): (TestRunner::queueBackNavigation): (TestRunner::queueForwardNavigation): (TestRunner::queueLoadingScript): (TestRunner::queueNonLoadingScript): (TestRunner::queueReload): * DumpRenderTree/WorkQueue.cpp: (WorkQueue::singleton): Deleted. (WorkQueue::WorkQueue): Deleted. (WorkQueue::queue): Deleted. (WorkQueue::dequeue): Deleted. (WorkQueue::count): Deleted. (WorkQueue::clear): Deleted. (WorkQueue::processWork): Deleted. * DumpRenderTree/WorkQueue.h: (WorkQueue::setFrozen): Deleted. * DumpRenderTree/WorkQueueItem.h: * DumpRenderTree/mac/DumpRenderTree.mm: (runTest): * DumpRenderTree/mac/FrameLoadDelegate.mm: (-[FrameLoadDelegate processWork:]): (-[FrameLoadDelegate webView:locationChangeDone:forDataSource:]): * DumpRenderTree/mac/TestRunnerMac.mm: (TestRunner::notifyDone): (TestRunner::forceImmediateCompletion): (TestRunner::queueLoad): * DumpRenderTree/win/DumpRenderTree.cpp: (runTest): * DumpRenderTree/win/FrameLoadDelegate.cpp: (FrameLoadDelegate::processWork): (FrameLoadDelegate::locationChangeDone): * DumpRenderTree/win/TestRunnerWin.cpp: (TestRunner::notifyDone): (TestRunner::forceImmediateCompletion): (TestRunner::queueLoad): Canonical link: https://commits.webkit.org/205473@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@237099 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-10-15 14:24:49 +00:00
#include <wtf/ThreadGroup.h>
[WTF] Implement WTF::ThreadGroup https://bugs.webkit.org/show_bug.cgi?id=174081 Reviewed by Mark Lam. Source/JavaScriptCore: Large part of MachineThreads are now removed and replaced with WTF::ThreadGroup. And SamplingProfiler and others interact with WTF::Thread directly. * API/tests/ExecutionTimeLimitTest.cpp: * heap/MachineStackMarker.cpp: (JSC::MachineThreads::MachineThreads): (JSC::captureStack): (JSC::MachineThreads::tryCopyOtherThreadStack): (JSC::MachineThreads::tryCopyOtherThreadStacks): (JSC::MachineThreads::gatherConservativeRoots): (JSC::ActiveMachineThreadsManager::Locker::Locker): Deleted. (JSC::ActiveMachineThreadsManager::add): Deleted. (JSC::ActiveMachineThreadsManager::remove): Deleted. (JSC::ActiveMachineThreadsManager::contains): Deleted. (JSC::ActiveMachineThreadsManager::ActiveMachineThreadsManager): Deleted. (JSC::activeMachineThreadsManager): Deleted. (JSC::MachineThreads::~MachineThreads): Deleted. (JSC::MachineThreads::addCurrentThread): Deleted. (): Deleted. (JSC::MachineThreads::removeThread): Deleted. (JSC::MachineThreads::removeThreadIfFound): Deleted. (JSC::MachineThreads::MachineThread::MachineThread): Deleted. (JSC::MachineThreads::MachineThread::getRegisters): Deleted. (JSC::MachineThreads::MachineThread::Registers::stackPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::framePointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::instructionPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::llintPC): Deleted. (JSC::MachineThreads::MachineThread::captureStack): Deleted. * heap/MachineStackMarker.h: (JSC::MachineThreads::addCurrentThread): (JSC::MachineThreads::getLock): (JSC::MachineThreads::threads): (JSC::MachineThreads::MachineThread::suspend): Deleted. (JSC::MachineThreads::MachineThread::resume): Deleted. (JSC::MachineThreads::MachineThread::threadID): Deleted. (JSC::MachineThreads::MachineThread::stackBase): Deleted. (JSC::MachineThreads::MachineThread::stackEnd): Deleted. (JSC::MachineThreads::threadsListHead): Deleted. * runtime/SamplingProfiler.cpp: (JSC::FrameWalker::isValidFramePointer): (JSC::SamplingProfiler::SamplingProfiler): (JSC::SamplingProfiler::takeSample): (JSC::SamplingProfiler::noticeCurrentThreadAsJSCExecutionThread): * runtime/SamplingProfiler.h: * wasm/WasmMachineThreads.cpp: (JSC::Wasm::resetInstructionCacheOnAllThreads): Source/WebCore: * page/ResourceUsageThread.h: Source/WebKit: * Shared/AsyncRequest.h: Source/WTF: This patch implements WTF::ThreadGroup. It implements core of JSC::MachineThreads with more reliable way. JSC::MachineThreads was complicated because of managing dead threads. Each JSC::MachineThreads has its own TLS with a registered destructor. And everytime a thread dies, the registered TLS destructor is called. And this destructor will remove the current dying thread from JSC::MachineThreads. However the above implementation is tricky. And each JSC::MachineThreads requires own TLS space, which is not considered in WTF's Windows ThreadSpecific implementation. Current design works well since we only have small number of MachineThreads right now. Instead, we use more reliable way. After introducing WTF::Thread, WTF::Thread has WTF::Thread::didExit, which is called when associated TLS (with WTF::Thread) is destroyed. We leverage this mechanism to remove WTF::Thread from MachineThreads. This patch introduces WTF::ThreadGroup. It is tightly integrated with WTF::Thread: WTF::Thread knows ThreadGroups which includes this thread. And WTF::ThreadGroup of course knows WTF::Threads added to it. WTF::Thread::didExit carefully remove itself from WTF::ThreadGroups. The most important part of this patch is locking. WTF::Thread can die. And WTF::ThreadGroup can die. If we take a design using two fine grain locks in WTF::Thread and WTF::ThreadGroup, we easily encounter dead lock. Consider the following case. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold a lock of THG, and hold a lock of TH (locking order is THG -> TH). 2. When TH dies, TH need to hold a lock of TH to iterate THGs. And we hold a lock of THG to unregister TH from it (locking order is TH -> THG). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (locking order is THG -> TH). 4. When destroying THG, we need to hold a lock of TH to unregister THG from TH. We can hold a lock of THG before that (locking order is THG -> TH). Then, it easily causes dead lock. We cannot swap the locking order of (2) since iterating THG requires a lock of TH. To solve this problem, we use std::shared_ptr and std::weak_ptr. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold THG, and hold a lock of TH. (THG -> TH) 2. When TH dies, TH first hold lock of TH. And we use std::weak_ptr<>::lock() to retain non-destructed ThreadGroups. If some of ThreadGroups are dying, we just ignore them. It is ok because such a ThreadGroup will be destructed. So we do not need to unregister this thread from such a ThreadGroup. Then, we have Vector<std::shared_ptr<ThreadGroup>>. So we unlock a lock of TH. To unregister a thread from thread group, we first hold a lock of THG and then hold a lock of TH. Both lifetime is ensured: THG is retained by std::shared_ptr. And TH is itself. (TH), (THG -> TH). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (THG -> TH). 4. When destroying THG, we hold a lock of THG. And hold a lock of TH. During holding THG's lock, registered thread never dies because (2) holds THG lock. (THG -> TH). We also fix suspend and resume locking mechanism to avoid dead lock. We should hold the global lock when suspending and resuming. If we use per-thread lock, the suspended thread can hold the lock of the other threads. It causes dead lock. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: * wtf/CMakeLists.txt: * wtf/CrossThreadCopier.h: * wtf/ParkingLot.h: * wtf/ThreadGroup.cpp: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::~ThreadGroup): (WTF::ThreadGroup::add): (WTF::ThreadGroup::addCurrentThread): * wtf/ThreadGroup.h: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::create): (WTF::ThreadGroup::threads): (WTF::ThreadGroup::getLock): (WTF::ThreadGroup::weakFromThis): * wtf/Threading.cpp: (WTF::shouldRemoveThreadFromThreadGroup): (WTF::Thread::didExit): (WTF::Thread::addToThreadGroup): (WTF::Thread::removeFromThreadGroup): * wtf/Threading.h: * wtf/ThreadingPthreads.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): * wtf/ThreadingWin.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): Tools: Add WTF::ThreadGroup tests. * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/ThreadGroup.cpp: Added. (TestWebKitAPI::testThreadGroup): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/191464@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219653 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-19 08:43:57 +00:00
namespace WTF {
ThreadGroup::~ThreadGroup()
{
Stop using holdLock() in WTF as it is not compatible with Clang thread safety analysis https://bugs.webkit.org/show_bug.cgi?id=226117 Reviewed by Darin Adler. Stop using holdLock() in WTF as it is not compatible with Clang thread safety analysis (WTF::CheckedLock) and use the Locker constructor instead. This is a step towards getting rid of holdLock() completely. * benchmarks/ConditionSpeedTest.cpp: * wtf/ConcurrentPtrHashSet.cpp: (WTF::ConcurrentPtrHashSet::deleteOldTables): (WTF::ConcurrentPtrHashSet::clear): (WTF::ConcurrentPtrHashSet::containsImplSlow const): (WTF::ConcurrentPtrHashSet::sizeSlow const): (WTF::ConcurrentPtrHashSet::resizeIfNecessary): * wtf/CountingLock.h: * wtf/HashTable.cpp: (WTF::HashTableStats::recordCollisionAtCount): (WTF::HashTableStats::dumpStats): * wtf/HashTable.h: (WTF::invalidateIterators): (WTF::addIterator): (WTF::removeIterator): * wtf/LockedPrintStream.cpp: (WTF::LockedPrintStream::vprintf): (WTF::LockedPrintStream::flush): * wtf/MetaAllocator.cpp: (WTF::MetaAllocatorHandle::~MetaAllocatorHandle): * wtf/MetaAllocator.h: (WTF::MetaAllocator::allocate): (WTF::MetaAllocator::currentStatistics): * wtf/ReadWriteLock.h: * wtf/StackShotProfiler.h: (WTF::StackShotProfiler::profile): (WTF::StackShotProfiler::run): * wtf/StackStats.cpp: (WTF::StackStats::CheckPoint::CheckPoint): (WTF::StackStats::CheckPoint::~CheckPoint): (WTF::StackStats::probe): (WTF::StackStats::LayoutCheckPoint::LayoutCheckPoint): (WTF::StackStats::LayoutCheckPoint::~LayoutCheckPoint): * wtf/ThreadGroup.cpp: (WTF::ThreadGroup::~ThreadGroup): (WTF::ThreadGroup::add): * wtf/ThreadMessage.cpp: (WTF::sendMessageScoped): * wtf/Threading.cpp: (WTF::Thread::didExit): (WTF::Thread::addToThreadGroup): (WTF::Thread::removeFromThreadGroup): (WTF::Thread::numberOfThreadGroups): * wtf/TimingScope.cpp: * wtf/WTFConfig.cpp: (WTF::Config::permanentlyFreeze): * wtf/WTFSemaphore.h: * wtf/posix/ThreadingPOSIX.cpp: (WTF::Thread::changePriority): (WTF::Thread::waitForCompletion): (WTF::Thread::detach): (WTF::Thread::signal): (WTF::Thread::establishPlatformSpecificHandle): * wtf/threads/BinarySemaphore.cpp: (WTF::BinarySemaphore::signal): (WTF::BinarySemaphore::waitUntil): * wtf/threads/Signals.cpp: (WTF::SignalHandlers::add): (WTF::registerThreadForMachExceptionHandling): (WTF::activateSignalHandlersFor): * wtf/win/LanguageWin.cpp: (WTF::platformLanguage): * wtf/win/ThreadingWin.cpp: (WTF::Thread::changePriority): (WTF::Thread::waitForCompletion): (WTF::Thread::detach): (WTF::Thread::establishPlatformSpecificHandle): Canonical link: https://commits.webkit.org/238033@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@277900 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2021-05-22 00:11:37 +00:00
Locker locker { m_lock };
[WTF] Implement WTF::ThreadGroup https://bugs.webkit.org/show_bug.cgi?id=174081 Reviewed by Mark Lam. Source/JavaScriptCore: Large part of MachineThreads are now removed and replaced with WTF::ThreadGroup. And SamplingProfiler and others interact with WTF::Thread directly. * API/tests/ExecutionTimeLimitTest.cpp: * heap/MachineStackMarker.cpp: (JSC::MachineThreads::MachineThreads): (JSC::captureStack): (JSC::MachineThreads::tryCopyOtherThreadStack): (JSC::MachineThreads::tryCopyOtherThreadStacks): (JSC::MachineThreads::gatherConservativeRoots): (JSC::ActiveMachineThreadsManager::Locker::Locker): Deleted. (JSC::ActiveMachineThreadsManager::add): Deleted. (JSC::ActiveMachineThreadsManager::remove): Deleted. (JSC::ActiveMachineThreadsManager::contains): Deleted. (JSC::ActiveMachineThreadsManager::ActiveMachineThreadsManager): Deleted. (JSC::activeMachineThreadsManager): Deleted. (JSC::MachineThreads::~MachineThreads): Deleted. (JSC::MachineThreads::addCurrentThread): Deleted. (): Deleted. (JSC::MachineThreads::removeThread): Deleted. (JSC::MachineThreads::removeThreadIfFound): Deleted. (JSC::MachineThreads::MachineThread::MachineThread): Deleted. (JSC::MachineThreads::MachineThread::getRegisters): Deleted. (JSC::MachineThreads::MachineThread::Registers::stackPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::framePointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::instructionPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::llintPC): Deleted. (JSC::MachineThreads::MachineThread::captureStack): Deleted. * heap/MachineStackMarker.h: (JSC::MachineThreads::addCurrentThread): (JSC::MachineThreads::getLock): (JSC::MachineThreads::threads): (JSC::MachineThreads::MachineThread::suspend): Deleted. (JSC::MachineThreads::MachineThread::resume): Deleted. (JSC::MachineThreads::MachineThread::threadID): Deleted. (JSC::MachineThreads::MachineThread::stackBase): Deleted. (JSC::MachineThreads::MachineThread::stackEnd): Deleted. (JSC::MachineThreads::threadsListHead): Deleted. * runtime/SamplingProfiler.cpp: (JSC::FrameWalker::isValidFramePointer): (JSC::SamplingProfiler::SamplingProfiler): (JSC::SamplingProfiler::takeSample): (JSC::SamplingProfiler::noticeCurrentThreadAsJSCExecutionThread): * runtime/SamplingProfiler.h: * wasm/WasmMachineThreads.cpp: (JSC::Wasm::resetInstructionCacheOnAllThreads): Source/WebCore: * page/ResourceUsageThread.h: Source/WebKit: * Shared/AsyncRequest.h: Source/WTF: This patch implements WTF::ThreadGroup. It implements core of JSC::MachineThreads with more reliable way. JSC::MachineThreads was complicated because of managing dead threads. Each JSC::MachineThreads has its own TLS with a registered destructor. And everytime a thread dies, the registered TLS destructor is called. And this destructor will remove the current dying thread from JSC::MachineThreads. However the above implementation is tricky. And each JSC::MachineThreads requires own TLS space, which is not considered in WTF's Windows ThreadSpecific implementation. Current design works well since we only have small number of MachineThreads right now. Instead, we use more reliable way. After introducing WTF::Thread, WTF::Thread has WTF::Thread::didExit, which is called when associated TLS (with WTF::Thread) is destroyed. We leverage this mechanism to remove WTF::Thread from MachineThreads. This patch introduces WTF::ThreadGroup. It is tightly integrated with WTF::Thread: WTF::Thread knows ThreadGroups which includes this thread. And WTF::ThreadGroup of course knows WTF::Threads added to it. WTF::Thread::didExit carefully remove itself from WTF::ThreadGroups. The most important part of this patch is locking. WTF::Thread can die. And WTF::ThreadGroup can die. If we take a design using two fine grain locks in WTF::Thread and WTF::ThreadGroup, we easily encounter dead lock. Consider the following case. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold a lock of THG, and hold a lock of TH (locking order is THG -> TH). 2. When TH dies, TH need to hold a lock of TH to iterate THGs. And we hold a lock of THG to unregister TH from it (locking order is TH -> THG). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (locking order is THG -> TH). 4. When destroying THG, we need to hold a lock of TH to unregister THG from TH. We can hold a lock of THG before that (locking order is THG -> TH). Then, it easily causes dead lock. We cannot swap the locking order of (2) since iterating THG requires a lock of TH. To solve this problem, we use std::shared_ptr and std::weak_ptr. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold THG, and hold a lock of TH. (THG -> TH) 2. When TH dies, TH first hold lock of TH. And we use std::weak_ptr<>::lock() to retain non-destructed ThreadGroups. If some of ThreadGroups are dying, we just ignore them. It is ok because such a ThreadGroup will be destructed. So we do not need to unregister this thread from such a ThreadGroup. Then, we have Vector<std::shared_ptr<ThreadGroup>>. So we unlock a lock of TH. To unregister a thread from thread group, we first hold a lock of THG and then hold a lock of TH. Both lifetime is ensured: THG is retained by std::shared_ptr. And TH is itself. (TH), (THG -> TH). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (THG -> TH). 4. When destroying THG, we hold a lock of THG. And hold a lock of TH. During holding THG's lock, registered thread never dies because (2) holds THG lock. (THG -> TH). We also fix suspend and resume locking mechanism to avoid dead lock. We should hold the global lock when suspending and resuming. If we use per-thread lock, the suspended thread can hold the lock of the other threads. It causes dead lock. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: * wtf/CMakeLists.txt: * wtf/CrossThreadCopier.h: * wtf/ParkingLot.h: * wtf/ThreadGroup.cpp: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::~ThreadGroup): (WTF::ThreadGroup::add): (WTF::ThreadGroup::addCurrentThread): * wtf/ThreadGroup.h: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::create): (WTF::ThreadGroup::threads): (WTF::ThreadGroup::getLock): (WTF::ThreadGroup::weakFromThis): * wtf/Threading.cpp: (WTF::shouldRemoveThreadFromThreadGroup): (WTF::Thread::didExit): (WTF::Thread::addToThreadGroup): (WTF::Thread::removeFromThreadGroup): * wtf/Threading.h: * wtf/ThreadingPthreads.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): * wtf/ThreadingWin.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): Tools: Add WTF::ThreadGroup tests. * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/ThreadGroup.cpp: Added. (TestWebKitAPI::testThreadGroup): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/191464@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219653 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-19 08:43:57 +00:00
for (auto& thread : m_threads)
thread->removeFromThreadGroup(locker, *this);
}
ThreadGroupAddResult ThreadGroup::add(const AbstractLocker& locker, Thread& thread)
[WTF] Implement WTF::ThreadGroup https://bugs.webkit.org/show_bug.cgi?id=174081 Reviewed by Mark Lam. Source/JavaScriptCore: Large part of MachineThreads are now removed and replaced with WTF::ThreadGroup. And SamplingProfiler and others interact with WTF::Thread directly. * API/tests/ExecutionTimeLimitTest.cpp: * heap/MachineStackMarker.cpp: (JSC::MachineThreads::MachineThreads): (JSC::captureStack): (JSC::MachineThreads::tryCopyOtherThreadStack): (JSC::MachineThreads::tryCopyOtherThreadStacks): (JSC::MachineThreads::gatherConservativeRoots): (JSC::ActiveMachineThreadsManager::Locker::Locker): Deleted. (JSC::ActiveMachineThreadsManager::add): Deleted. (JSC::ActiveMachineThreadsManager::remove): Deleted. (JSC::ActiveMachineThreadsManager::contains): Deleted. (JSC::ActiveMachineThreadsManager::ActiveMachineThreadsManager): Deleted. (JSC::activeMachineThreadsManager): Deleted. (JSC::MachineThreads::~MachineThreads): Deleted. (JSC::MachineThreads::addCurrentThread): Deleted. (): Deleted. (JSC::MachineThreads::removeThread): Deleted. (JSC::MachineThreads::removeThreadIfFound): Deleted. (JSC::MachineThreads::MachineThread::MachineThread): Deleted. (JSC::MachineThreads::MachineThread::getRegisters): Deleted. (JSC::MachineThreads::MachineThread::Registers::stackPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::framePointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::instructionPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::llintPC): Deleted. (JSC::MachineThreads::MachineThread::captureStack): Deleted. * heap/MachineStackMarker.h: (JSC::MachineThreads::addCurrentThread): (JSC::MachineThreads::getLock): (JSC::MachineThreads::threads): (JSC::MachineThreads::MachineThread::suspend): Deleted. (JSC::MachineThreads::MachineThread::resume): Deleted. (JSC::MachineThreads::MachineThread::threadID): Deleted. (JSC::MachineThreads::MachineThread::stackBase): Deleted. (JSC::MachineThreads::MachineThread::stackEnd): Deleted. (JSC::MachineThreads::threadsListHead): Deleted. * runtime/SamplingProfiler.cpp: (JSC::FrameWalker::isValidFramePointer): (JSC::SamplingProfiler::SamplingProfiler): (JSC::SamplingProfiler::takeSample): (JSC::SamplingProfiler::noticeCurrentThreadAsJSCExecutionThread): * runtime/SamplingProfiler.h: * wasm/WasmMachineThreads.cpp: (JSC::Wasm::resetInstructionCacheOnAllThreads): Source/WebCore: * page/ResourceUsageThread.h: Source/WebKit: * Shared/AsyncRequest.h: Source/WTF: This patch implements WTF::ThreadGroup. It implements core of JSC::MachineThreads with more reliable way. JSC::MachineThreads was complicated because of managing dead threads. Each JSC::MachineThreads has its own TLS with a registered destructor. And everytime a thread dies, the registered TLS destructor is called. And this destructor will remove the current dying thread from JSC::MachineThreads. However the above implementation is tricky. And each JSC::MachineThreads requires own TLS space, which is not considered in WTF's Windows ThreadSpecific implementation. Current design works well since we only have small number of MachineThreads right now. Instead, we use more reliable way. After introducing WTF::Thread, WTF::Thread has WTF::Thread::didExit, which is called when associated TLS (with WTF::Thread) is destroyed. We leverage this mechanism to remove WTF::Thread from MachineThreads. This patch introduces WTF::ThreadGroup. It is tightly integrated with WTF::Thread: WTF::Thread knows ThreadGroups which includes this thread. And WTF::ThreadGroup of course knows WTF::Threads added to it. WTF::Thread::didExit carefully remove itself from WTF::ThreadGroups. The most important part of this patch is locking. WTF::Thread can die. And WTF::ThreadGroup can die. If we take a design using two fine grain locks in WTF::Thread and WTF::ThreadGroup, we easily encounter dead lock. Consider the following case. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold a lock of THG, and hold a lock of TH (locking order is THG -> TH). 2. When TH dies, TH need to hold a lock of TH to iterate THGs. And we hold a lock of THG to unregister TH from it (locking order is TH -> THG). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (locking order is THG -> TH). 4. When destroying THG, we need to hold a lock of TH to unregister THG from TH. We can hold a lock of THG before that (locking order is THG -> TH). Then, it easily causes dead lock. We cannot swap the locking order of (2) since iterating THG requires a lock of TH. To solve this problem, we use std::shared_ptr and std::weak_ptr. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold THG, and hold a lock of TH. (THG -> TH) 2. When TH dies, TH first hold lock of TH. And we use std::weak_ptr<>::lock() to retain non-destructed ThreadGroups. If some of ThreadGroups are dying, we just ignore them. It is ok because such a ThreadGroup will be destructed. So we do not need to unregister this thread from such a ThreadGroup. Then, we have Vector<std::shared_ptr<ThreadGroup>>. So we unlock a lock of TH. To unregister a thread from thread group, we first hold a lock of THG and then hold a lock of TH. Both lifetime is ensured: THG is retained by std::shared_ptr. And TH is itself. (TH), (THG -> TH). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (THG -> TH). 4. When destroying THG, we hold a lock of THG. And hold a lock of TH. During holding THG's lock, registered thread never dies because (2) holds THG lock. (THG -> TH). We also fix suspend and resume locking mechanism to avoid dead lock. We should hold the global lock when suspending and resuming. If we use per-thread lock, the suspended thread can hold the lock of the other threads. It causes dead lock. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: * wtf/CMakeLists.txt: * wtf/CrossThreadCopier.h: * wtf/ParkingLot.h: * wtf/ThreadGroup.cpp: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::~ThreadGroup): (WTF::ThreadGroup::add): (WTF::ThreadGroup::addCurrentThread): * wtf/ThreadGroup.h: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::create): (WTF::ThreadGroup::threads): (WTF::ThreadGroup::getLock): (WTF::ThreadGroup::weakFromThis): * wtf/Threading.cpp: (WTF::shouldRemoveThreadFromThreadGroup): (WTF::Thread::didExit): (WTF::Thread::addToThreadGroup): (WTF::Thread::removeFromThreadGroup): * wtf/Threading.h: * wtf/ThreadingPthreads.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): * wtf/ThreadingWin.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): Tools: Add WTF::ThreadGroup tests. * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/ThreadGroup.cpp: Added. (TestWebKitAPI::testThreadGroup): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/191464@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219653 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-19 08:43:57 +00:00
{
return thread.addToThreadGroup(locker, *this);
}
ThreadGroupAddResult ThreadGroup::add(Thread& thread)
{
Stop using holdLock() in WTF as it is not compatible with Clang thread safety analysis https://bugs.webkit.org/show_bug.cgi?id=226117 Reviewed by Darin Adler. Stop using holdLock() in WTF as it is not compatible with Clang thread safety analysis (WTF::CheckedLock) and use the Locker constructor instead. This is a step towards getting rid of holdLock() completely. * benchmarks/ConditionSpeedTest.cpp: * wtf/ConcurrentPtrHashSet.cpp: (WTF::ConcurrentPtrHashSet::deleteOldTables): (WTF::ConcurrentPtrHashSet::clear): (WTF::ConcurrentPtrHashSet::containsImplSlow const): (WTF::ConcurrentPtrHashSet::sizeSlow const): (WTF::ConcurrentPtrHashSet::resizeIfNecessary): * wtf/CountingLock.h: * wtf/HashTable.cpp: (WTF::HashTableStats::recordCollisionAtCount): (WTF::HashTableStats::dumpStats): * wtf/HashTable.h: (WTF::invalidateIterators): (WTF::addIterator): (WTF::removeIterator): * wtf/LockedPrintStream.cpp: (WTF::LockedPrintStream::vprintf): (WTF::LockedPrintStream::flush): * wtf/MetaAllocator.cpp: (WTF::MetaAllocatorHandle::~MetaAllocatorHandle): * wtf/MetaAllocator.h: (WTF::MetaAllocator::allocate): (WTF::MetaAllocator::currentStatistics): * wtf/ReadWriteLock.h: * wtf/StackShotProfiler.h: (WTF::StackShotProfiler::profile): (WTF::StackShotProfiler::run): * wtf/StackStats.cpp: (WTF::StackStats::CheckPoint::CheckPoint): (WTF::StackStats::CheckPoint::~CheckPoint): (WTF::StackStats::probe): (WTF::StackStats::LayoutCheckPoint::LayoutCheckPoint): (WTF::StackStats::LayoutCheckPoint::~LayoutCheckPoint): * wtf/ThreadGroup.cpp: (WTF::ThreadGroup::~ThreadGroup): (WTF::ThreadGroup::add): * wtf/ThreadMessage.cpp: (WTF::sendMessageScoped): * wtf/Threading.cpp: (WTF::Thread::didExit): (WTF::Thread::addToThreadGroup): (WTF::Thread::removeFromThreadGroup): (WTF::Thread::numberOfThreadGroups): * wtf/TimingScope.cpp: * wtf/WTFConfig.cpp: (WTF::Config::permanentlyFreeze): * wtf/WTFSemaphore.h: * wtf/posix/ThreadingPOSIX.cpp: (WTF::Thread::changePriority): (WTF::Thread::waitForCompletion): (WTF::Thread::detach): (WTF::Thread::signal): (WTF::Thread::establishPlatformSpecificHandle): * wtf/threads/BinarySemaphore.cpp: (WTF::BinarySemaphore::signal): (WTF::BinarySemaphore::waitUntil): * wtf/threads/Signals.cpp: (WTF::SignalHandlers::add): (WTF::registerThreadForMachExceptionHandling): (WTF::activateSignalHandlersFor): * wtf/win/LanguageWin.cpp: (WTF::platformLanguage): * wtf/win/ThreadingWin.cpp: (WTF::Thread::changePriority): (WTF::Thread::waitForCompletion): (WTF::Thread::detach): (WTF::Thread::establishPlatformSpecificHandle): Canonical link: https://commits.webkit.org/238033@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@277900 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2021-05-22 00:11:37 +00:00
Locker locker { m_lock };
return add(locker, thread);
}
ThreadGroupAddResult ThreadGroup::addCurrentThread()
[WTF] Implement WTF::ThreadGroup https://bugs.webkit.org/show_bug.cgi?id=174081 Reviewed by Mark Lam. Source/JavaScriptCore: Large part of MachineThreads are now removed and replaced with WTF::ThreadGroup. And SamplingProfiler and others interact with WTF::Thread directly. * API/tests/ExecutionTimeLimitTest.cpp: * heap/MachineStackMarker.cpp: (JSC::MachineThreads::MachineThreads): (JSC::captureStack): (JSC::MachineThreads::tryCopyOtherThreadStack): (JSC::MachineThreads::tryCopyOtherThreadStacks): (JSC::MachineThreads::gatherConservativeRoots): (JSC::ActiveMachineThreadsManager::Locker::Locker): Deleted. (JSC::ActiveMachineThreadsManager::add): Deleted. (JSC::ActiveMachineThreadsManager::remove): Deleted. (JSC::ActiveMachineThreadsManager::contains): Deleted. (JSC::ActiveMachineThreadsManager::ActiveMachineThreadsManager): Deleted. (JSC::activeMachineThreadsManager): Deleted. (JSC::MachineThreads::~MachineThreads): Deleted. (JSC::MachineThreads::addCurrentThread): Deleted. (): Deleted. (JSC::MachineThreads::removeThread): Deleted. (JSC::MachineThreads::removeThreadIfFound): Deleted. (JSC::MachineThreads::MachineThread::MachineThread): Deleted. (JSC::MachineThreads::MachineThread::getRegisters): Deleted. (JSC::MachineThreads::MachineThread::Registers::stackPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::framePointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::instructionPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::llintPC): Deleted. (JSC::MachineThreads::MachineThread::captureStack): Deleted. * heap/MachineStackMarker.h: (JSC::MachineThreads::addCurrentThread): (JSC::MachineThreads::getLock): (JSC::MachineThreads::threads): (JSC::MachineThreads::MachineThread::suspend): Deleted. (JSC::MachineThreads::MachineThread::resume): Deleted. (JSC::MachineThreads::MachineThread::threadID): Deleted. (JSC::MachineThreads::MachineThread::stackBase): Deleted. (JSC::MachineThreads::MachineThread::stackEnd): Deleted. (JSC::MachineThreads::threadsListHead): Deleted. * runtime/SamplingProfiler.cpp: (JSC::FrameWalker::isValidFramePointer): (JSC::SamplingProfiler::SamplingProfiler): (JSC::SamplingProfiler::takeSample): (JSC::SamplingProfiler::noticeCurrentThreadAsJSCExecutionThread): * runtime/SamplingProfiler.h: * wasm/WasmMachineThreads.cpp: (JSC::Wasm::resetInstructionCacheOnAllThreads): Source/WebCore: * page/ResourceUsageThread.h: Source/WebKit: * Shared/AsyncRequest.h: Source/WTF: This patch implements WTF::ThreadGroup. It implements core of JSC::MachineThreads with more reliable way. JSC::MachineThreads was complicated because of managing dead threads. Each JSC::MachineThreads has its own TLS with a registered destructor. And everytime a thread dies, the registered TLS destructor is called. And this destructor will remove the current dying thread from JSC::MachineThreads. However the above implementation is tricky. And each JSC::MachineThreads requires own TLS space, which is not considered in WTF's Windows ThreadSpecific implementation. Current design works well since we only have small number of MachineThreads right now. Instead, we use more reliable way. After introducing WTF::Thread, WTF::Thread has WTF::Thread::didExit, which is called when associated TLS (with WTF::Thread) is destroyed. We leverage this mechanism to remove WTF::Thread from MachineThreads. This patch introduces WTF::ThreadGroup. It is tightly integrated with WTF::Thread: WTF::Thread knows ThreadGroups which includes this thread. And WTF::ThreadGroup of course knows WTF::Threads added to it. WTF::Thread::didExit carefully remove itself from WTF::ThreadGroups. The most important part of this patch is locking. WTF::Thread can die. And WTF::ThreadGroup can die. If we take a design using two fine grain locks in WTF::Thread and WTF::ThreadGroup, we easily encounter dead lock. Consider the following case. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold a lock of THG, and hold a lock of TH (locking order is THG -> TH). 2. When TH dies, TH need to hold a lock of TH to iterate THGs. And we hold a lock of THG to unregister TH from it (locking order is TH -> THG). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (locking order is THG -> TH). 4. When destroying THG, we need to hold a lock of TH to unregister THG from TH. We can hold a lock of THG before that (locking order is THG -> TH). Then, it easily causes dead lock. We cannot swap the locking order of (2) since iterating THG requires a lock of TH. To solve this problem, we use std::shared_ptr and std::weak_ptr. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold THG, and hold a lock of TH. (THG -> TH) 2. When TH dies, TH first hold lock of TH. And we use std::weak_ptr<>::lock() to retain non-destructed ThreadGroups. If some of ThreadGroups are dying, we just ignore them. It is ok because such a ThreadGroup will be destructed. So we do not need to unregister this thread from such a ThreadGroup. Then, we have Vector<std::shared_ptr<ThreadGroup>>. So we unlock a lock of TH. To unregister a thread from thread group, we first hold a lock of THG and then hold a lock of TH. Both lifetime is ensured: THG is retained by std::shared_ptr. And TH is itself. (TH), (THG -> TH). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (THG -> TH). 4. When destroying THG, we hold a lock of THG. And hold a lock of TH. During holding THG's lock, registered thread never dies because (2) holds THG lock. (THG -> TH). We also fix suspend and resume locking mechanism to avoid dead lock. We should hold the global lock when suspending and resuming. If we use per-thread lock, the suspended thread can hold the lock of the other threads. It causes dead lock. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: * wtf/CMakeLists.txt: * wtf/CrossThreadCopier.h: * wtf/ParkingLot.h: * wtf/ThreadGroup.cpp: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::~ThreadGroup): (WTF::ThreadGroup::add): (WTF::ThreadGroup::addCurrentThread): * wtf/ThreadGroup.h: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::create): (WTF::ThreadGroup::threads): (WTF::ThreadGroup::getLock): (WTF::ThreadGroup::weakFromThis): * wtf/Threading.cpp: (WTF::shouldRemoveThreadFromThreadGroup): (WTF::Thread::didExit): (WTF::Thread::addToThreadGroup): (WTF::Thread::removeFromThreadGroup): * wtf/Threading.h: * wtf/ThreadingPthreads.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): * wtf/ThreadingWin.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): Tools: Add WTF::ThreadGroup tests. * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/ThreadGroup.cpp: Added. (TestWebKitAPI::testThreadGroup): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/191464@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219653 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-19 08:43:57 +00:00
{
auto result = add(Thread::current());
ASSERT(result != ThreadGroupAddResult::NotAdded);
return result;
[WTF] Implement WTF::ThreadGroup https://bugs.webkit.org/show_bug.cgi?id=174081 Reviewed by Mark Lam. Source/JavaScriptCore: Large part of MachineThreads are now removed and replaced with WTF::ThreadGroup. And SamplingProfiler and others interact with WTF::Thread directly. * API/tests/ExecutionTimeLimitTest.cpp: * heap/MachineStackMarker.cpp: (JSC::MachineThreads::MachineThreads): (JSC::captureStack): (JSC::MachineThreads::tryCopyOtherThreadStack): (JSC::MachineThreads::tryCopyOtherThreadStacks): (JSC::MachineThreads::gatherConservativeRoots): (JSC::ActiveMachineThreadsManager::Locker::Locker): Deleted. (JSC::ActiveMachineThreadsManager::add): Deleted. (JSC::ActiveMachineThreadsManager::remove): Deleted. (JSC::ActiveMachineThreadsManager::contains): Deleted. (JSC::ActiveMachineThreadsManager::ActiveMachineThreadsManager): Deleted. (JSC::activeMachineThreadsManager): Deleted. (JSC::MachineThreads::~MachineThreads): Deleted. (JSC::MachineThreads::addCurrentThread): Deleted. (): Deleted. (JSC::MachineThreads::removeThread): Deleted. (JSC::MachineThreads::removeThreadIfFound): Deleted. (JSC::MachineThreads::MachineThread::MachineThread): Deleted. (JSC::MachineThreads::MachineThread::getRegisters): Deleted. (JSC::MachineThreads::MachineThread::Registers::stackPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::framePointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::instructionPointer): Deleted. (JSC::MachineThreads::MachineThread::Registers::llintPC): Deleted. (JSC::MachineThreads::MachineThread::captureStack): Deleted. * heap/MachineStackMarker.h: (JSC::MachineThreads::addCurrentThread): (JSC::MachineThreads::getLock): (JSC::MachineThreads::threads): (JSC::MachineThreads::MachineThread::suspend): Deleted. (JSC::MachineThreads::MachineThread::resume): Deleted. (JSC::MachineThreads::MachineThread::threadID): Deleted. (JSC::MachineThreads::MachineThread::stackBase): Deleted. (JSC::MachineThreads::MachineThread::stackEnd): Deleted. (JSC::MachineThreads::threadsListHead): Deleted. * runtime/SamplingProfiler.cpp: (JSC::FrameWalker::isValidFramePointer): (JSC::SamplingProfiler::SamplingProfiler): (JSC::SamplingProfiler::takeSample): (JSC::SamplingProfiler::noticeCurrentThreadAsJSCExecutionThread): * runtime/SamplingProfiler.h: * wasm/WasmMachineThreads.cpp: (JSC::Wasm::resetInstructionCacheOnAllThreads): Source/WebCore: * page/ResourceUsageThread.h: Source/WebKit: * Shared/AsyncRequest.h: Source/WTF: This patch implements WTF::ThreadGroup. It implements core of JSC::MachineThreads with more reliable way. JSC::MachineThreads was complicated because of managing dead threads. Each JSC::MachineThreads has its own TLS with a registered destructor. And everytime a thread dies, the registered TLS destructor is called. And this destructor will remove the current dying thread from JSC::MachineThreads. However the above implementation is tricky. And each JSC::MachineThreads requires own TLS space, which is not considered in WTF's Windows ThreadSpecific implementation. Current design works well since we only have small number of MachineThreads right now. Instead, we use more reliable way. After introducing WTF::Thread, WTF::Thread has WTF::Thread::didExit, which is called when associated TLS (with WTF::Thread) is destroyed. We leverage this mechanism to remove WTF::Thread from MachineThreads. This patch introduces WTF::ThreadGroup. It is tightly integrated with WTF::Thread: WTF::Thread knows ThreadGroups which includes this thread. And WTF::ThreadGroup of course knows WTF::Threads added to it. WTF::Thread::didExit carefully remove itself from WTF::ThreadGroups. The most important part of this patch is locking. WTF::Thread can die. And WTF::ThreadGroup can die. If we take a design using two fine grain locks in WTF::Thread and WTF::ThreadGroup, we easily encounter dead lock. Consider the following case. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold a lock of THG, and hold a lock of TH (locking order is THG -> TH). 2. When TH dies, TH need to hold a lock of TH to iterate THGs. And we hold a lock of THG to unregister TH from it (locking order is TH -> THG). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (locking order is THG -> TH). 4. When destroying THG, we need to hold a lock of TH to unregister THG from TH. We can hold a lock of THG before that (locking order is THG -> TH). Then, it easily causes dead lock. We cannot swap the locking order of (2) since iterating THG requires a lock of TH. To solve this problem, we use std::shared_ptr and std::weak_ptr. 1. When adding WTF::Thread (TH) to WTF::ThreadGroup (THG), we first hold THG, and hold a lock of TH. (THG -> TH) 2. When TH dies, TH first hold lock of TH. And we use std::weak_ptr<>::lock() to retain non-destructed ThreadGroups. If some of ThreadGroups are dying, we just ignore them. It is ok because such a ThreadGroup will be destructed. So we do not need to unregister this thread from such a ThreadGroup. Then, we have Vector<std::shared_ptr<ThreadGroup>>. So we unlock a lock of TH. To unregister a thread from thread group, we first hold a lock of THG and then hold a lock of TH. Both lifetime is ensured: THG is retained by std::shared_ptr. And TH is itself. (TH), (THG -> TH). 3. When suspending and resuming THs in THG, we first hold a lock of THG. And then, we hold a lock of TH to suspend and resume it (THG -> TH). 4. When destroying THG, we hold a lock of THG. And hold a lock of TH. During holding THG's lock, registered thread never dies because (2) holds THG lock. (THG -> TH). We also fix suspend and resume locking mechanism to avoid dead lock. We should hold the global lock when suspending and resuming. If we use per-thread lock, the suspended thread can hold the lock of the other threads. It causes dead lock. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: * wtf/CMakeLists.txt: * wtf/CrossThreadCopier.h: * wtf/ParkingLot.h: * wtf/ThreadGroup.cpp: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::~ThreadGroup): (WTF::ThreadGroup::add): (WTF::ThreadGroup::addCurrentThread): * wtf/ThreadGroup.h: Copied from Source/JavaScriptCore/wasm/WasmMachineThreads.cpp. (WTF::ThreadGroup::create): (WTF::ThreadGroup::threads): (WTF::ThreadGroup::getLock): (WTF::ThreadGroup::weakFromThis): * wtf/Threading.cpp: (WTF::shouldRemoveThreadFromThreadGroup): (WTF::Thread::didExit): (WTF::Thread::addToThreadGroup): (WTF::Thread::removeFromThreadGroup): * wtf/Threading.h: * wtf/ThreadingPthreads.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): * wtf/ThreadingWin.cpp: (WTF::Thread::resume): (WTF::Thread::getRegisters): Tools: Add WTF::ThreadGroup tests. * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/ThreadGroup.cpp: Added. (TestWebKitAPI::testThreadGroup): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/191464@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219653 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-19 08:43:57 +00:00
}
} // namespace WTF