haikuwebkit/Source/WTF/wtf/WordLock.cpp

251 lines
10 KiB
C++
Raw Permalink Normal View History

Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
/*
It should be easy to decide how WebKit yields https://bugs.webkit.org/show_bug.cgi?id=174298 Reviewed by Saam Barati. Source/bmalloc: Use sched_yield() explicitly. * bmalloc/StaticMutex.cpp: (bmalloc::StaticMutex::lockSlowCase): Source/JavaScriptCore: Use the new WTF::Thread::yield() function for yielding instead of the C++ function. * heap/Heap.cpp: (JSC::Heap::resumeThePeriphery): * heap/VisitingTimeout.h: * runtime/JSCell.cpp: (JSC::JSCell::lockSlow): (JSC::JSCell::unlockSlow): * runtime/JSCell.h: * runtime/JSCellInlines.h: (JSC::JSCell::lock): (JSC::JSCell::unlock): * runtime/JSLock.cpp: (JSC::JSLock::grabAllLocks): * runtime/SamplingProfiler.cpp: Source/WebCore: No new tests because the WebCore change is just a change to how we #include things. * inspector/InspectorPageAgent.h: * inspector/TimelineRecordFactory.h: * workers/Worker.h: * workers/WorkerGlobalScopeProxy.h: * workers/WorkerMessagingProxy.h: Source/WebKitLegacy: * Storage/StorageTracker.h: Source/WTF: Created a Thread::yield() abstraction for sched_yield(), and made WTF use it everywhere that it had previously used std::this_thread::yield(). To make it less annoying to experiment with changes to the lock algorithm in the future, this also moves the meat of the algorithm into LockAlgorithmInlines.h. Only two files include that header. Since LockAlgorithm.h no longer includes ParkingLot.h, a bunch of files in WK now need to include timing headers (Seconds, MonotonicTime, etc) manually. * WTF.xcodeproj/project.pbxproj: * benchmarks/ToyLocks.h: * wtf/CMakeLists.txt: * wtf/Lock.cpp: * wtf/LockAlgorithm.h: (WTF::LockAlgorithm::lockSlow): Deleted. (WTF::LockAlgorithm::unlockSlow): Deleted. * wtf/LockAlgorithmInlines.h: Added. (WTF::hasParkedBit>::lockSlow): (WTF::hasParkedBit>::unlockSlow): * wtf/MainThread.cpp: * wtf/RunLoopTimer.h: * wtf/Threading.cpp: * wtf/Threading.h: * wtf/ThreadingPthreads.cpp: (WTF::Thread::yield): * wtf/ThreadingWin.cpp: (WTF::Thread::yield): * wtf/WordLock.cpp: (WTF::WordLockBase::lockSlow): (WTF::WordLockBase::unlockSlow): Canonical link: https://commits.webkit.org/191572@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219763 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-22 14:36:18 +00:00
* Copyright (C) 2015-2017 Apple Inc. All rights reserved.
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
*
* 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/WordLock.h>
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
#include <condition_variable>
#include <mutex>
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/Threading.h>
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
namespace WTF {
namespace {
// This data structure serves three purposes:
//
// 1) A parking mechanism for threads that go to sleep. That involves just a system mutex and
// condition variable.
//
// 2) A queue node for when a thread is on some WordLock's queue.
//
// 3) The queue head. This is kind of funky. When a thread is the head of a queue, it also serves as
// the basic queue bookkeeping data structure. When a thread is dequeued, the next thread in the
// queue takes on the queue head duties.
struct ThreadData {
// The parking mechanism.
bool shouldPark { false };
std::mutex parkingLock;
std::condition_variable parkingCondition;
// The queue node.
ThreadData* nextInQueue { nullptr };
// The queue itself.
ThreadData* queueTail { nullptr };
};
} // anonymous namespace
[WTF] Remove XXXLockBase since constexpr constructor can initialize static variables without calling global constructors https://bugs.webkit.org/show_bug.cgi?id=180495 Reviewed by Mark Lam. Very nice feature of C++11 is that constexpr constructor can initialize static global variables without calling global constructors. We do not need to have XXXLockBase with derived XXXLock class since StaticXXXLock can have constructors as long as it is constexpr. We remove bunch of these classes, and set `XXXLock() = default;` explicitly for readability. C++11's default constructor is constexpr as long as its member's default constructor / default initializer is constexpr. * wtf/Condition.h: (WTF::ConditionBase::construct): Deleted. (WTF::ConditionBase::waitUntil): Deleted. (WTF::ConditionBase::waitFor): Deleted. (WTF::ConditionBase::wait): Deleted. (WTF::ConditionBase::notifyOne): Deleted. (WTF::ConditionBase::notifyAll): Deleted. (WTF::Condition::Condition): Deleted. * wtf/CountingLock.h: (WTF::CountingLock::CountingLock): Deleted. (WTF::CountingLock::~CountingLock): Deleted. * wtf/Lock.cpp: (WTF::Lock::lockSlow): (WTF::Lock::unlockSlow): (WTF::Lock::unlockFairlySlow): (WTF::Lock::safepointSlow): (WTF::LockBase::lockSlow): Deleted. (WTF::LockBase::unlockSlow): Deleted. (WTF::LockBase::unlockFairlySlow): Deleted. (WTF::LockBase::safepointSlow): Deleted. * wtf/Lock.h: (WTF::LockBase::construct): Deleted. (WTF::LockBase::lock): Deleted. (WTF::LockBase::tryLock): Deleted. (WTF::LockBase::try_lock): Deleted. (WTF::LockBase::unlock): Deleted. (WTF::LockBase::unlockFairly): Deleted. (WTF::LockBase::safepoint): Deleted. (WTF::LockBase::isHeld const): Deleted. (WTF::LockBase::isLocked const): Deleted. (WTF::LockBase::isFullyReset const): Deleted. (WTF::Lock::Lock): Deleted. * wtf/ReadWriteLock.cpp: (WTF::ReadWriteLock::readLock): (WTF::ReadWriteLock::readUnlock): (WTF::ReadWriteLock::writeLock): (WTF::ReadWriteLock::writeUnlock): (WTF::ReadWriteLockBase::construct): Deleted. (WTF::ReadWriteLockBase::readLock): Deleted. (WTF::ReadWriteLockBase::readUnlock): Deleted. (WTF::ReadWriteLockBase::writeLock): Deleted. (WTF::ReadWriteLockBase::writeUnlock): Deleted. * wtf/ReadWriteLock.h: (WTF::ReadWriteLock::read): (WTF::ReadWriteLock::write): (WTF::ReadWriteLockBase::ReadLock::tryLock): Deleted. (WTF::ReadWriteLockBase::ReadLock::lock): Deleted. (WTF::ReadWriteLockBase::ReadLock::unlock): Deleted. (WTF::ReadWriteLockBase::WriteLock::tryLock): Deleted. (WTF::ReadWriteLockBase::WriteLock::lock): Deleted. (WTF::ReadWriteLockBase::WriteLock::unlock): Deleted. (WTF::ReadWriteLockBase::read): Deleted. (WTF::ReadWriteLockBase::write): Deleted. (WTF::ReadWriteLock::ReadWriteLock): Deleted. * wtf/RecursiveLockAdapter.h: (WTF::RecursiveLockAdapter::RecursiveLockAdapter): Deleted. * wtf/WordLock.cpp: (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): (WTF::WordLockBase::lockSlow): Deleted. (WTF::WordLockBase::unlockSlow): Deleted. * wtf/WordLock.h: (WTF::WordLockBase::lock): Deleted. (WTF::WordLockBase::unlock): Deleted. (WTF::WordLockBase::isHeld const): Deleted. (WTF::WordLockBase::isLocked const): Deleted. (WTF::WordLockBase::isFullyReset const): Deleted. (WTF::WordLock::WordLock): Deleted. * wtf/WorkQueue.cpp: Canonical link: https://commits.webkit.org/196438@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@225617 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-12-07 03:52:09 +00:00
NEVER_INLINE void WordLock::lockSlow()
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
{
unsigned spinCount = 0;
// This magic number turns out to be optimal based on past JikesRVM experiments.
const unsigned spinLimit = 40;
for (;;) {
uintptr_t currentWordValue = m_word.load();
if (!(currentWordValue & isLockedBit)) {
// It's not possible for someone to hold the queue lock while the lock itself is no longer
// held, since we will only attempt to acquire the queue lock when the lock is held and
// the queue lock prevents unlock.
ASSERT(!(currentWordValue & isQueueLockedBit));
if (m_word.compareExchangeWeak(currentWordValue, currentWordValue | isLockedBit)) {
// Success! We acquired the lock.
return;
}
}
// If there is no queue and we haven't spun too much, we can just try to spin around again.
if (!(currentWordValue & ~queueHeadMask) && spinCount < spinLimit) {
spinCount++;
It should be easy to decide how WebKit yields https://bugs.webkit.org/show_bug.cgi?id=174298 Reviewed by Saam Barati. Source/bmalloc: Use sched_yield() explicitly. * bmalloc/StaticMutex.cpp: (bmalloc::StaticMutex::lockSlowCase): Source/JavaScriptCore: Use the new WTF::Thread::yield() function for yielding instead of the C++ function. * heap/Heap.cpp: (JSC::Heap::resumeThePeriphery): * heap/VisitingTimeout.h: * runtime/JSCell.cpp: (JSC::JSCell::lockSlow): (JSC::JSCell::unlockSlow): * runtime/JSCell.h: * runtime/JSCellInlines.h: (JSC::JSCell::lock): (JSC::JSCell::unlock): * runtime/JSLock.cpp: (JSC::JSLock::grabAllLocks): * runtime/SamplingProfiler.cpp: Source/WebCore: No new tests because the WebCore change is just a change to how we #include things. * inspector/InspectorPageAgent.h: * inspector/TimelineRecordFactory.h: * workers/Worker.h: * workers/WorkerGlobalScopeProxy.h: * workers/WorkerMessagingProxy.h: Source/WebKitLegacy: * Storage/StorageTracker.h: Source/WTF: Created a Thread::yield() abstraction for sched_yield(), and made WTF use it everywhere that it had previously used std::this_thread::yield(). To make it less annoying to experiment with changes to the lock algorithm in the future, this also moves the meat of the algorithm into LockAlgorithmInlines.h. Only two files include that header. Since LockAlgorithm.h no longer includes ParkingLot.h, a bunch of files in WK now need to include timing headers (Seconds, MonotonicTime, etc) manually. * WTF.xcodeproj/project.pbxproj: * benchmarks/ToyLocks.h: * wtf/CMakeLists.txt: * wtf/Lock.cpp: * wtf/LockAlgorithm.h: (WTF::LockAlgorithm::lockSlow): Deleted. (WTF::LockAlgorithm::unlockSlow): Deleted. * wtf/LockAlgorithmInlines.h: Added. (WTF::hasParkedBit>::lockSlow): (WTF::hasParkedBit>::unlockSlow): * wtf/MainThread.cpp: * wtf/RunLoopTimer.h: * wtf/Threading.cpp: * wtf/Threading.h: * wtf/ThreadingPthreads.cpp: (WTF::Thread::yield): * wtf/ThreadingWin.cpp: (WTF::Thread::yield): * wtf/WordLock.cpp: (WTF::WordLockBase::lockSlow): (WTF::WordLockBase::unlockSlow): Canonical link: https://commits.webkit.org/191572@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219763 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-22 14:36:18 +00:00
Thread::yield();
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
continue;
}
// Need to put ourselves on the queue. Create the queue if one does not exist. This requries
// owning the queue for a little bit. The lock that controls the queue is itself a spinlock.
ThreadData me;
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
// Reload the current word value, since some time may have passed.
currentWordValue = m_word.load();
// We proceed only if the queue lock is not held, the WordLock is held, and we succeed in
// acquiring the queue lock.
if ((currentWordValue & isQueueLockedBit)
|| !(currentWordValue & isLockedBit)
|| !m_word.compareExchangeWeak(currentWordValue, currentWordValue | isQueueLockedBit)) {
It should be easy to decide how WebKit yields https://bugs.webkit.org/show_bug.cgi?id=174298 Reviewed by Saam Barati. Source/bmalloc: Use sched_yield() explicitly. * bmalloc/StaticMutex.cpp: (bmalloc::StaticMutex::lockSlowCase): Source/JavaScriptCore: Use the new WTF::Thread::yield() function for yielding instead of the C++ function. * heap/Heap.cpp: (JSC::Heap::resumeThePeriphery): * heap/VisitingTimeout.h: * runtime/JSCell.cpp: (JSC::JSCell::lockSlow): (JSC::JSCell::unlockSlow): * runtime/JSCell.h: * runtime/JSCellInlines.h: (JSC::JSCell::lock): (JSC::JSCell::unlock): * runtime/JSLock.cpp: (JSC::JSLock::grabAllLocks): * runtime/SamplingProfiler.cpp: Source/WebCore: No new tests because the WebCore change is just a change to how we #include things. * inspector/InspectorPageAgent.h: * inspector/TimelineRecordFactory.h: * workers/Worker.h: * workers/WorkerGlobalScopeProxy.h: * workers/WorkerMessagingProxy.h: Source/WebKitLegacy: * Storage/StorageTracker.h: Source/WTF: Created a Thread::yield() abstraction for sched_yield(), and made WTF use it everywhere that it had previously used std::this_thread::yield(). To make it less annoying to experiment with changes to the lock algorithm in the future, this also moves the meat of the algorithm into LockAlgorithmInlines.h. Only two files include that header. Since LockAlgorithm.h no longer includes ParkingLot.h, a bunch of files in WK now need to include timing headers (Seconds, MonotonicTime, etc) manually. * WTF.xcodeproj/project.pbxproj: * benchmarks/ToyLocks.h: * wtf/CMakeLists.txt: * wtf/Lock.cpp: * wtf/LockAlgorithm.h: (WTF::LockAlgorithm::lockSlow): Deleted. (WTF::LockAlgorithm::unlockSlow): Deleted. * wtf/LockAlgorithmInlines.h: Added. (WTF::hasParkedBit>::lockSlow): (WTF::hasParkedBit>::unlockSlow): * wtf/MainThread.cpp: * wtf/RunLoopTimer.h: * wtf/Threading.cpp: * wtf/Threading.h: * wtf/ThreadingPthreads.cpp: (WTF::Thread::yield): * wtf/ThreadingWin.cpp: (WTF::Thread::yield): * wtf/WordLock.cpp: (WTF::WordLockBase::lockSlow): (WTF::WordLockBase::unlockSlow): Canonical link: https://commits.webkit.org/191572@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219763 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-22 14:36:18 +00:00
Thread::yield();
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
continue;
}
me.shouldPark = true;
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
// We own the queue. Nobody can enqueue or dequeue until we're done. Also, it's not possible
// to release the WordLock while we hold the queue lock.
ThreadData* queueHead = bitwise_cast<ThreadData*>(currentWordValue & ~queueHeadMask);
if (queueHead) {
// Put this thread at the end of the queue.
queueHead->queueTail->nextInQueue = &me;
queueHead->queueTail = &me;
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
// Release the queue lock.
currentWordValue = m_word.load();
ASSERT(currentWordValue & ~queueHeadMask);
ASSERT(currentWordValue & isQueueLockedBit);
ASSERT(currentWordValue & isLockedBit);
m_word.store(currentWordValue & ~isQueueLockedBit);
} else {
// Make this thread be the queue-head.
queueHead = &me;
me.queueTail = &me;
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
// Release the queue lock and install ourselves as the head. No need for a CAS loop, since
// we own the queue lock.
currentWordValue = m_word.load();
ASSERT(~(currentWordValue & ~queueHeadMask));
ASSERT(currentWordValue & isQueueLockedBit);
ASSERT(currentWordValue & isLockedBit);
uintptr_t newWordValue = currentWordValue;
newWordValue |= bitwise_cast<uintptr_t>(queueHead);
newWordValue &= ~isQueueLockedBit;
m_word.store(newWordValue);
}
// At this point everyone who acquires the queue lock will see me on the queue, and anyone who
// acquires me's lock will see that me wants to park. Note that shouldPark may have been
// cleared as soon as the queue lock was released above, but it will happen while the
// releasing thread holds me's parkingLock.
{
std::unique_lock<std::mutex> locker(me.parkingLock);
while (me.shouldPark)
me.parkingCondition.wait(locker);
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
}
ASSERT(!me.shouldPark);
ASSERT(!me.nextInQueue);
ASSERT(!me.queueTail);
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
// Now we can loop around and try to acquire the lock again.
}
}
[WTF] Remove XXXLockBase since constexpr constructor can initialize static variables without calling global constructors https://bugs.webkit.org/show_bug.cgi?id=180495 Reviewed by Mark Lam. Very nice feature of C++11 is that constexpr constructor can initialize static global variables without calling global constructors. We do not need to have XXXLockBase with derived XXXLock class since StaticXXXLock can have constructors as long as it is constexpr. We remove bunch of these classes, and set `XXXLock() = default;` explicitly for readability. C++11's default constructor is constexpr as long as its member's default constructor / default initializer is constexpr. * wtf/Condition.h: (WTF::ConditionBase::construct): Deleted. (WTF::ConditionBase::waitUntil): Deleted. (WTF::ConditionBase::waitFor): Deleted. (WTF::ConditionBase::wait): Deleted. (WTF::ConditionBase::notifyOne): Deleted. (WTF::ConditionBase::notifyAll): Deleted. (WTF::Condition::Condition): Deleted. * wtf/CountingLock.h: (WTF::CountingLock::CountingLock): Deleted. (WTF::CountingLock::~CountingLock): Deleted. * wtf/Lock.cpp: (WTF::Lock::lockSlow): (WTF::Lock::unlockSlow): (WTF::Lock::unlockFairlySlow): (WTF::Lock::safepointSlow): (WTF::LockBase::lockSlow): Deleted. (WTF::LockBase::unlockSlow): Deleted. (WTF::LockBase::unlockFairlySlow): Deleted. (WTF::LockBase::safepointSlow): Deleted. * wtf/Lock.h: (WTF::LockBase::construct): Deleted. (WTF::LockBase::lock): Deleted. (WTF::LockBase::tryLock): Deleted. (WTF::LockBase::try_lock): Deleted. (WTF::LockBase::unlock): Deleted. (WTF::LockBase::unlockFairly): Deleted. (WTF::LockBase::safepoint): Deleted. (WTF::LockBase::isHeld const): Deleted. (WTF::LockBase::isLocked const): Deleted. (WTF::LockBase::isFullyReset const): Deleted. (WTF::Lock::Lock): Deleted. * wtf/ReadWriteLock.cpp: (WTF::ReadWriteLock::readLock): (WTF::ReadWriteLock::readUnlock): (WTF::ReadWriteLock::writeLock): (WTF::ReadWriteLock::writeUnlock): (WTF::ReadWriteLockBase::construct): Deleted. (WTF::ReadWriteLockBase::readLock): Deleted. (WTF::ReadWriteLockBase::readUnlock): Deleted. (WTF::ReadWriteLockBase::writeLock): Deleted. (WTF::ReadWriteLockBase::writeUnlock): Deleted. * wtf/ReadWriteLock.h: (WTF::ReadWriteLock::read): (WTF::ReadWriteLock::write): (WTF::ReadWriteLockBase::ReadLock::tryLock): Deleted. (WTF::ReadWriteLockBase::ReadLock::lock): Deleted. (WTF::ReadWriteLockBase::ReadLock::unlock): Deleted. (WTF::ReadWriteLockBase::WriteLock::tryLock): Deleted. (WTF::ReadWriteLockBase::WriteLock::lock): Deleted. (WTF::ReadWriteLockBase::WriteLock::unlock): Deleted. (WTF::ReadWriteLockBase::read): Deleted. (WTF::ReadWriteLockBase::write): Deleted. (WTF::ReadWriteLock::ReadWriteLock): Deleted. * wtf/RecursiveLockAdapter.h: (WTF::RecursiveLockAdapter::RecursiveLockAdapter): Deleted. * wtf/WordLock.cpp: (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): (WTF::WordLockBase::lockSlow): Deleted. (WTF::WordLockBase::unlockSlow): Deleted. * wtf/WordLock.h: (WTF::WordLockBase::lock): Deleted. (WTF::WordLockBase::unlock): Deleted. (WTF::WordLockBase::isHeld const): Deleted. (WTF::WordLockBase::isLocked const): Deleted. (WTF::WordLockBase::isFullyReset const): Deleted. (WTF::WordLock::WordLock): Deleted. * wtf/WorkQueue.cpp: Canonical link: https://commits.webkit.org/196438@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@225617 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-12-07 03:52:09 +00:00
NEVER_INLINE void WordLock::unlockSlow()
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
{
// The fast path can fail either because of spurious weak CAS failure, or because someone put a
// thread on the queue, or the queue lock is held. If the queue lock is held, it can only be
// because someone *will* enqueue a thread onto the queue.
// Acquire the queue lock, or release the lock. This loop handles both lock release in case the
// fast path's weak CAS spuriously failed and it handles queue lock acquisition if there is
// actually something interesting on the queue.
for (;;) {
uintptr_t currentWordValue = m_word.load();
ASSERT(currentWordValue & isLockedBit);
if (currentWordValue == isLockedBit) {
if (m_word.compareExchangeWeak(isLockedBit, 0)) {
// The fast path's weak CAS had spuriously failed, and now we succeeded. The lock is
// unlocked and we're done!
return;
}
// Loop around and try again.
It should be easy to decide how WebKit yields https://bugs.webkit.org/show_bug.cgi?id=174298 Reviewed by Saam Barati. Source/bmalloc: Use sched_yield() explicitly. * bmalloc/StaticMutex.cpp: (bmalloc::StaticMutex::lockSlowCase): Source/JavaScriptCore: Use the new WTF::Thread::yield() function for yielding instead of the C++ function. * heap/Heap.cpp: (JSC::Heap::resumeThePeriphery): * heap/VisitingTimeout.h: * runtime/JSCell.cpp: (JSC::JSCell::lockSlow): (JSC::JSCell::unlockSlow): * runtime/JSCell.h: * runtime/JSCellInlines.h: (JSC::JSCell::lock): (JSC::JSCell::unlock): * runtime/JSLock.cpp: (JSC::JSLock::grabAllLocks): * runtime/SamplingProfiler.cpp: Source/WebCore: No new tests because the WebCore change is just a change to how we #include things. * inspector/InspectorPageAgent.h: * inspector/TimelineRecordFactory.h: * workers/Worker.h: * workers/WorkerGlobalScopeProxy.h: * workers/WorkerMessagingProxy.h: Source/WebKitLegacy: * Storage/StorageTracker.h: Source/WTF: Created a Thread::yield() abstraction for sched_yield(), and made WTF use it everywhere that it had previously used std::this_thread::yield(). To make it less annoying to experiment with changes to the lock algorithm in the future, this also moves the meat of the algorithm into LockAlgorithmInlines.h. Only two files include that header. Since LockAlgorithm.h no longer includes ParkingLot.h, a bunch of files in WK now need to include timing headers (Seconds, MonotonicTime, etc) manually. * WTF.xcodeproj/project.pbxproj: * benchmarks/ToyLocks.h: * wtf/CMakeLists.txt: * wtf/Lock.cpp: * wtf/LockAlgorithm.h: (WTF::LockAlgorithm::lockSlow): Deleted. (WTF::LockAlgorithm::unlockSlow): Deleted. * wtf/LockAlgorithmInlines.h: Added. (WTF::hasParkedBit>::lockSlow): (WTF::hasParkedBit>::unlockSlow): * wtf/MainThread.cpp: * wtf/RunLoopTimer.h: * wtf/Threading.cpp: * wtf/Threading.h: * wtf/ThreadingPthreads.cpp: (WTF::Thread::yield): * wtf/ThreadingWin.cpp: (WTF::Thread::yield): * wtf/WordLock.cpp: (WTF::WordLockBase::lockSlow): (WTF::WordLockBase::unlockSlow): Canonical link: https://commits.webkit.org/191572@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219763 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-22 14:36:18 +00:00
Thread::yield();
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
continue;
}
if (currentWordValue & isQueueLockedBit) {
It should be easy to decide how WebKit yields https://bugs.webkit.org/show_bug.cgi?id=174298 Reviewed by Saam Barati. Source/bmalloc: Use sched_yield() explicitly. * bmalloc/StaticMutex.cpp: (bmalloc::StaticMutex::lockSlowCase): Source/JavaScriptCore: Use the new WTF::Thread::yield() function for yielding instead of the C++ function. * heap/Heap.cpp: (JSC::Heap::resumeThePeriphery): * heap/VisitingTimeout.h: * runtime/JSCell.cpp: (JSC::JSCell::lockSlow): (JSC::JSCell::unlockSlow): * runtime/JSCell.h: * runtime/JSCellInlines.h: (JSC::JSCell::lock): (JSC::JSCell::unlock): * runtime/JSLock.cpp: (JSC::JSLock::grabAllLocks): * runtime/SamplingProfiler.cpp: Source/WebCore: No new tests because the WebCore change is just a change to how we #include things. * inspector/InspectorPageAgent.h: * inspector/TimelineRecordFactory.h: * workers/Worker.h: * workers/WorkerGlobalScopeProxy.h: * workers/WorkerMessagingProxy.h: Source/WebKitLegacy: * Storage/StorageTracker.h: Source/WTF: Created a Thread::yield() abstraction for sched_yield(), and made WTF use it everywhere that it had previously used std::this_thread::yield(). To make it less annoying to experiment with changes to the lock algorithm in the future, this also moves the meat of the algorithm into LockAlgorithmInlines.h. Only two files include that header. Since LockAlgorithm.h no longer includes ParkingLot.h, a bunch of files in WK now need to include timing headers (Seconds, MonotonicTime, etc) manually. * WTF.xcodeproj/project.pbxproj: * benchmarks/ToyLocks.h: * wtf/CMakeLists.txt: * wtf/Lock.cpp: * wtf/LockAlgorithm.h: (WTF::LockAlgorithm::lockSlow): Deleted. (WTF::LockAlgorithm::unlockSlow): Deleted. * wtf/LockAlgorithmInlines.h: Added. (WTF::hasParkedBit>::lockSlow): (WTF::hasParkedBit>::unlockSlow): * wtf/MainThread.cpp: * wtf/RunLoopTimer.h: * wtf/Threading.cpp: * wtf/Threading.h: * wtf/ThreadingPthreads.cpp: (WTF::Thread::yield): * wtf/ThreadingWin.cpp: (WTF::Thread::yield): * wtf/WordLock.cpp: (WTF::WordLockBase::lockSlow): (WTF::WordLockBase::unlockSlow): Canonical link: https://commits.webkit.org/191572@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219763 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-22 14:36:18 +00:00
Thread::yield();
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
continue;
}
// If it wasn't just a spurious weak CAS failure and if the queue lock is not held, then there
// must be an entry on the queue.
ASSERT(currentWordValue & ~queueHeadMask);
if (m_word.compareExchangeWeak(currentWordValue, currentWordValue | isQueueLockedBit))
break;
}
uintptr_t currentWordValue = m_word.load();
// After we acquire the queue lock, the WordLock must still be held and the queue must be
// non-empty. The queue must be non-empty since only the lockSlow() method could have held the
// queue lock and if it did then it only releases it after putting something on the queue.
ASSERT(currentWordValue & isLockedBit);
ASSERT(currentWordValue & isQueueLockedBit);
ThreadData* queueHead = bitwise_cast<ThreadData*>(currentWordValue & ~queueHeadMask);
ASSERT(queueHead);
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
ThreadData* newQueueHead = queueHead->nextInQueue;
// Either this was the only thread on the queue, in which case we delete the queue, or there
// are still more threads on the queue, in which case we create a new queue head.
if (newQueueHead)
newQueueHead->queueTail = queueHead->queueTail;
// Change the queue head, possibly removing it if newQueueHead is null. No need for a CAS loop,
// since we hold the queue lock and the lock itself so nothing about the lock can change right
// now.
currentWordValue = m_word.load();
ASSERT(currentWordValue & isLockedBit);
ASSERT(currentWordValue & isQueueLockedBit);
ASSERT((currentWordValue & ~queueHeadMask) == bitwise_cast<uintptr_t>(queueHead));
uintptr_t newWordValue = currentWordValue;
newWordValue &= ~isLockedBit; // Release the WordLock.
newWordValue &= ~isQueueLockedBit; // Release the queue lock.
newWordValue &= queueHeadMask; // Clear out the old queue head.
newWordValue |= bitwise_cast<uintptr_t>(newQueueHead); // Install new queue head.
m_word.store(newWordValue);
// Now the lock is available for acquisition. But we just have to wake up the old queue head.
// After that, we're done!
queueHead->nextInQueue = nullptr;
queueHead->queueTail = nullptr;
// We do this carefully because this may run either before or during the parkingLock critical
// section in lockSlow().
{
// Be sure to hold the lock across our call to notify_one() because a spurious wakeup could
// cause the thread at the head of the queue to exit and delete queueHead.
Remove std::lock_guard https://bugs.webkit.org/show_bug.cgi?id=206451 Reviewed by Anders Carlsson. Source/bmalloc: * bmalloc/Mutex.h: Source/JavaScriptCore: * API/JSVirtualMachine.mm: (+[JSVMWrapperCache addWrapper:forJSContextGroupRef:]): (+[JSVMWrapperCache wrapperForJSContextGroupRef:]): * API/glib/JSCVirtualMachine.cpp: (addWrapper): (removeWrapper): * heap/HeapSnapshotBuilder.cpp: (JSC::HeapSnapshotBuilder::analyzeNode): (JSC::HeapSnapshotBuilder::analyzeEdge): (JSC::HeapSnapshotBuilder::analyzePropertyNameEdge): (JSC::HeapSnapshotBuilder::analyzeVariableNameEdge): (JSC::HeapSnapshotBuilder::analyzeIndexEdge): (JSC::HeapSnapshotBuilder::setOpaqueRootReachabilityReasonForCell): * heap/MachineStackMarker.cpp: (JSC::MachineThreads::tryCopyOtherThreadStacks): * runtime/JSRunLoopTimer.cpp: (JSC::JSRunLoopTimer::timerDidFire): Source/WebCore: * Modules/webaudio/AudioBufferSourceNode.cpp: (WebCore::AudioBufferSourceNode::setBuffer): * Modules/webaudio/AudioParamTimeline.cpp: (WebCore::AudioParamTimeline::insertEvent): (WebCore::AudioParamTimeline::cancelScheduledValues): * Modules/webaudio/ConvolverNode.cpp: (WebCore::ConvolverNode::reset): (WebCore::ConvolverNode::setBuffer): * Modules/webaudio/MediaElementAudioSourceNode.cpp: (WebCore::MediaElementAudioSourceNode::setFormat): * Modules/webaudio/MediaStreamAudioSourceNode.cpp: (WebCore::MediaStreamAudioSourceNode::setFormat): * Modules/webaudio/OscillatorNode.cpp: (WebCore::OscillatorNode::setPeriodicWave): * Modules/webaudio/PannerNode.cpp: (WebCore::PannerNode::setPanningModel): * Modules/webaudio/WaveShaperProcessor.cpp: (WebCore::WaveShaperProcessor::setCurve): (WebCore::WaveShaperProcessor::setOversample): * Modules/webdatabase/Database.cpp: (WebCore::Database::Database): (WebCore::Database::performOpenAndVerify): (WebCore::Database::closeDatabase): (WebCore::Database::getCachedVersion const): (WebCore::Database::setCachedVersion): * Modules/webdatabase/DatabaseManager.cpp: (WebCore::DatabaseManager::addProposedDatabase): (WebCore::DatabaseManager::removeProposedDatabase): (WebCore::DatabaseManager::fullPathForDatabase): (WebCore::DatabaseManager::detailsForNameAndOrigin): * crypto/CryptoAlgorithmRegistry.cpp: (WebCore::CryptoAlgorithmRegistry::identifier): (WebCore::CryptoAlgorithmRegistry::name): (WebCore::CryptoAlgorithmRegistry::create): (WebCore::CryptoAlgorithmRegistry::registerAlgorithm): * inspector/agents/WebHeapAgent.cpp: (WebCore::SendGarbageCollectionEventsTask::addGarbageCollection): (WebCore::SendGarbageCollectionEventsTask::reset): (WebCore::SendGarbageCollectionEventsTask::timerFired): * page/scrolling/ScrollingThread.cpp: (WebCore::ScrollingThread::dispatch): (WebCore::ScrollingThread::dispatchFunctionsFromScrollingThread): * page/scrolling/generic/ScrollingThreadGeneric.cpp: (WebCore::ScrollingThread::initializeRunLoop): * page/scrolling/mac/ScrollingThreadMac.mm: (WebCore::ScrollingThread::initializeRunLoop): * platform/audio/ReverbConvolver.cpp: (WebCore::ReverbConvolver::~ReverbConvolver): * platform/graphics/avfoundation/AudioSourceProviderAVFObjC.mm: (WebCore::AudioSourceProviderAVFObjC::~AudioSourceProviderAVFObjC): (WebCore::AudioSourceProviderAVFObjC::finalizeCallback): (WebCore::AudioSourceProviderAVFObjC::prepareCallback): (WebCore::AudioSourceProviderAVFObjC::unprepareCallback): (WebCore::AudioSourceProviderAVFObjC::processCallback): * platform/graphics/cocoa/FontCacheCoreText.cpp: (WebCore::FontDatabase::collectionForFamily): (WebCore::FontDatabase::clear): * platform/ios/wak/WebCoreThreadRun.cpp: * platform/mediastream/mac/WebAudioSourceProviderAVFObjC.mm: (WebCore::WebAudioSourceProviderAVFObjC::~WebAudioSourceProviderAVFObjC): (WebCore::WebAudioSourceProviderAVFObjC::prepare): (WebCore::WebAudioSourceProviderAVFObjC::unprepare): * platform/network/cf/LoaderRunLoopCF.cpp: (WebCore::loaderRunLoop): * platform/sql/SQLiteDatabase.cpp: (WebCore::SQLiteDatabase::setIsDatabaseOpeningForbidden): (WebCore::SQLiteDatabase::open): * platform/sql/SQLiteDatabaseTracker.cpp: (WebCore::SQLiteDatabaseTracker::setClient): (WebCore::SQLiteDatabaseTracker::incrementTransactionInProgressCount): (WebCore::SQLiteDatabaseTracker::decrementTransactionInProgressCount): (WebCore::SQLiteDatabaseTracker::hasTransactionInProgress): * platform/text/TextEncodingRegistry.cpp: (WebCore::buildBaseTextCodecMaps): (WebCore::newTextCodec): (WebCore::atomCanonicalTextEncodingName): Source/WebKit: * NetworkProcess/cache/NetworkCacheStorage.cpp: (WebKit::NetworkCache::Storage::traverse): * Platform/IPC/Connection.cpp: (IPC::Connection::SyncMessageState::processIncomingMessage): (IPC::Connection::SyncMessageState::dispatchMessages): (IPC::Connection::SyncMessageState::dispatchMessagesAndResetDidScheduleDispatchMessagesForConnection): (IPC::Connection::addWorkQueueMessageReceiver): (IPC::Connection::removeWorkQueueMessageReceiver): (IPC::Connection::addThreadMessageReceiver): (IPC::Connection::removeThreadMessageReceiver): (IPC::Connection::sendMessage): (IPC::Connection::waitForMessage): (IPC::Connection::processIncomingMessage): (IPC::Connection::installIncomingSyncMessageCallback): (IPC::Connection::uninstallIncomingSyncMessageCallback): (IPC::Connection::hasIncomingSyncMessage): (IPC::Connection::connectionDidClose): (IPC::Connection::sendOutgoingMessages): (IPC::Connection::enqueueIncomingMessage): (IPC::Connection::dispatchMessageToWorkQueueReceiver): (IPC::Connection::dispatchMessageToThreadReceiver): (IPC::Connection::dispatchOneIncomingMessage): (IPC::Connection::dispatchIncomingMessages): * Shared/BlockingResponseMap.h: (BlockingResponseMap::didReceiveResponse): * UIProcess/mac/WKPrintingView.mm: (-[WKPrintingView _preparePDFDataForPrintingOnSecondaryThread]): (prepareDataForPrintingOnSecondaryThread): Source/WebKitLegacy/mac: * DOM/DOMInternal.mm: (getDOMWrapper): (addDOMWrapper): (removeDOMWrapper): Source/WTF: Remove use of std::lock_guard. This is deprecated in C++17. 1. For particularly low-level usage (like, bmalloc, std::mutex), use std::scoped_lock. 2. For the other purpose, use holdLock. * benchmarks/ConditionSpeedTest.cpp: * wtf/CryptographicallyRandomNumber.cpp: * wtf/HashTable.cpp: (WTF::HashTableStats::recordCollisionAtCount): (WTF::HashTableStats::dumpStats): * wtf/HashTable.h: (WTF::KeyTraits>::invalidateIterators): (WTF::addIterator): (WTF::removeIterator): * wtf/Language.cpp: (WTF::userPreferredLanguages): * wtf/MainThread.cpp: (WTF::dispatchFunctionsFromMainThread): (WTF::callOnMainThread): (WTF::callOnMainAndWait): * wtf/StackStats.cpp: (WTF::StackStats::CheckPoint::CheckPoint): (WTF::StackStats::CheckPoint::~CheckPoint): (WTF::StackStats::probe): (WTF::StackStats::LayoutCheckPoint::LayoutCheckPoint): (WTF::StackStats::LayoutCheckPoint::~LayoutCheckPoint): * wtf/WordLock.cpp: (WTF::WordLock::unlockSlow): * wtf/cf/LanguageCF.cpp: (WTF::languagePreferencesDidChange): (WTF::platformUserPreferredLanguages): * wtf/text/StringView.cpp: (WTF::StringView::invalidate): (WTF::StringView::adoptUnderlyingString): (WTF::StringView::setUnderlyingString): * wtf/unicode/icu/CollatorICU.cpp: (WTF::Collator::Collator): (WTF::Collator::~Collator): * wtf/win/LanguageWin.cpp: (WTF::platformLanguage): Tools: Add std::lock_guard lint rule to prevent from using it. * Scripts/webkitpy/style/checkers/cpp.py: (check_lock_guard): (check_style): (CppChecker): * Scripts/webkitpy/style/checkers/cpp_unittest.py: (WebKitStyleTest.test_lock_guard): * TestWebKitAPI/Tests/WTF/Condition.cpp: * TestWebKitAPI/Tests/WTF/ParkingLot.cpp: * TestWebKitAPI/Tests/WTF/bmalloc/IsoHeap.cpp: (assertHasObjects): (assertHasOnlyObjects): * WebKitTestRunner/InjectedBundle/AccessibilityController.cpp: (WTR::AXThread::dispatch): (WTR::AXThread::dispatchFunctionsFromAXThread): * WebKitTestRunner/InjectedBundle/mac/AccessibilityControllerMac.mm: (WTR::AXThread::initializeRunLoop): Canonical link: https://commits.webkit.org/221350@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@257688 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2020-03-01 05:01:30 +00:00
std::scoped_lock<std::mutex> locker(queueHead->parkingLock);
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
queueHead->shouldPark = false;
// Doesn't matter if we notify_all() or notify_one() here since the only thread that could be
// waiting is queueHead.
queueHead->parkingCondition.notify_one();
Always use a byte-sized lock implementation https://bugs.webkit.org/show_bug.cgi?id=147908 Reviewed by Geoffrey Garen. Source/JavaScriptCore: * runtime/ConcurrentJITLock.h: Lock is now byte-sized and ByteLock is gone, so use Lock. Source/WTF: At the start of my locking algorithm crusade, I implemented Lock, which is a sizeof(void*) lock implementation with some nice theoretical properties and good performance. Then I added the ParkingLot abstraction and ByteLock. ParkingLot uses Lock in its implementation. ByteLock uses ParkingLot to create a sizeof(char) lock implementation that performs like Lock. It turns out that ByteLock is always at least as good as Lock, and sometimes a lot better: it requires 8x less memory on 64-bit systems. It's hard to construct a benchmark where ByteLock is significantly slower than Lock, and when you do construct such a benchmark, tweaking it a bit can also create a scenario where ByteLock is significantly faster than Lock. So, the thing that we call "Lock" should really use ByteLock's algorithm, since it is more compact and just as fast. That's what this patch does. But we still need to keep the old Lock algorithm, because it's used to implement ParkingLot, which in turn is used to implement ByteLock. So this patch does this transformation: - Move the algorithm in Lock into files called WordLock.h|cpp. Make ParkingLot use WordLock. - Move the algorithm in ByteLock into Lock.h|cpp. Make everyone who used ByteLock use Lock instead. All other users of Lock now get the byte-sized lock implementation. - Remove the old ByteLock files. * WTF.vcxproj/WTF.vcxproj: * WTF.xcodeproj/project.pbxproj: * benchmarks/LockSpeedTest.cpp: (main): * wtf/WordLock.cpp: Added. (WTF::WordLock::lockSlow): (WTF::WordLock::unlockSlow): * wtf/WordLock.h: Added. (WTF::WordLock::WordLock): (WTF::WordLock::lock): (WTF::WordLock::unlock): (WTF::WordLock::isHeld): (WTF::WordLock::isLocked): * wtf/ByteLock.cpp: Removed. * wtf/ByteLock.h: Removed. * wtf/CMakeLists.txt: * wtf/Lock.cpp: (WTF::LockBase::lockSlow): (WTF::LockBase::unlockSlow): * wtf/Lock.h: (WTF::LockBase::lock): (WTF::LockBase::unlock): (WTF::LockBase::isHeld): (WTF::LockBase::isLocked): (WTF::Lock::Lock): * wtf/ParkingLot.cpp: Tools: All previous tests of Lock are now tests of WordLock. All previous tests of ByteLock are now tests of Lock. * TestWebKitAPI/Tests/WTF/Lock.cpp: (TestWebKitAPI::runLockTest): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/166025@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188323 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2015-08-12 04:20:24 +00:00
}
// The old queue head can now contend for the lock again. We're done!
}
} // namespace WTF