Changeset 203350 in webkit
- Timestamp:
- Jul 18, 2016, 11:32:52 AM (9 years ago)
- Location:
- trunk
- Files:
-
- 10 edited
-
Source/WTF/ChangeLog (modified) (1 diff)
-
Source/WTF/benchmarks/LockFairnessTest.cpp (modified) (5 diffs)
-
Source/WTF/benchmarks/ToyLocks.h (modified) (2 diffs)
-
Source/WTF/wtf/Condition.h (modified) (2 diffs)
-
Source/WTF/wtf/Lock.cpp (modified) (5 diffs)
-
Source/WTF/wtf/Lock.h (modified) (5 diffs)
-
Source/WTF/wtf/ParkingLot.cpp (modified) (19 diffs)
-
Source/WTF/wtf/ParkingLot.h (modified) (5 diffs)
-
Tools/ChangeLog (modified) (1 diff)
-
Tools/TestWebKitAPI/Tests/WTF/ParkingLot.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r203330 r203350 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 1 138 2016-07-17 Myles C. Maxfield <mmaxfield@apple.com> 2 139 -
trunk/Source/WTF/benchmarks/LockFairnessTest.cpp
r202791 r203350 49 49 NO_RETURN void usage() 50 50 { 51 printf("Usage: LockFairnessTest yieldspinlock|pausespinlock|wordlock|lock|barginglock|bargingwordlock|thunderlock|thunderwordlock|cascadelock|cascadewordlockhandofflock|mutex|all <num threads> <seconds per test> \n");51 printf("Usage: LockFairnessTest yieldspinlock|pausespinlock|wordlock|lock|barginglock|bargingwordlock|thunderlock|thunderwordlock|cascadelock|cascadewordlockhandofflock|mutex|all <num threads> <seconds per test>\n"); 52 52 exit(1); 53 53 } … … 55 55 unsigned numThreads; 56 56 double secondsPerTest; 57 57 58 58 59 struct Benchmark { … … 73 74 "Benchmark Thread", 74 75 [&, threadIndex] () { 76 77 78 79 80 81 82 83 84 75 85 while (keepGoing) { 76 86 lock.lock(); 77 87 counts[threadIndex]++; 88 78 89 lock.unlock(); 79 90 } … … 86 97 sleep(secondsPerTest); 87 98 99 88 100 lock.lock(); 89 keepGoing = false;90 101 91 102 dataLog(name, ": "); … … 107 118 WTF::initializeThreading(); 108 119 109 if (argc != 4120 if (argc != 110 121 || sscanf(argv[2], "%u", &numThreads) != 1 111 || sscanf(argv[3], "%lf", &secondsPerTest) != 1) 122 || sscanf(argv[3], "%lf", &secondsPerTest) != 1 123 || sscanf(argv[4], "%u", µsecondsInCriticalSection) != 1) 112 124 usage(); 113 125 -
trunk/Source/WTF/benchmarks/ToyLocks.h
r200444 r203350 236 236 ParkingLot::unparkOne( 237 237 &m_state, 238 [this] (ParkingLot::UnparkResult result) {238 [this] (ParkingLot::UnparkResult result) { 239 239 if (result.mayHaveMoreThreads) 240 240 m_state.store(hasParkedBit); 241 241 else 242 242 m_state.store(0); 243 243 244 }); 244 245 } … … 431 432 432 433 if (m_state.compareExchangeWeak(state, state + parkedCountUnit)) { 433 bool result = ParkingLot::compareAndPark(&m_state, state + parkedCountUnit) ;434 bool result = ParkingLot::compareAndPark(&m_state, state + parkedCountUnit); 434 435 m_state.exchangeAndAdd(-parkedCountUnit); 435 436 if (result) -
trunk/Source/WTF/wtf/Condition.h
r199760 r203350 81 81 }, 82 82 [&lock] () { lock.unlock(); }, 83 timeout) ;83 timeout); 84 84 } 85 85 lock.lock(); … … 181 181 ParkingLot::unparkOne( 182 182 &m_hasWaiters, 183 [this] (ParkingLot::UnparkResult result) {183 [this] (ParkingLot::UnparkResult result) { 184 184 if (!result.mayHaveMoreThreads) 185 185 m_hasWaiters.store(false); 186 186 187 }); 187 188 } -
trunk/Source/WTF/wtf/Lock.cpp
r199760 r203350 1 1 /* 2 * Copyright (C) 2015 Apple Inc. All rights reserved.2 * Copyright (C) 2015 Apple Inc. All rights reserved. 3 3 * 4 4 * Redistribution and use in source and binary forms, with or without … … 68 68 69 69 // We now expect the value to be isHeld|hasParked. So long as that's the case, we can park. 70 ParkingLot::compareAndPark(&m_byte, isHeldBit | hasParkedBit); 70 ParkingLot::ParkResult parkResult = 71 ParkingLot::compareAndPark(&m_byte, isHeldBit | hasParkedBit); 72 if (parkResult.wasUnparked) { 73 switch (static_cast<Token>(parkResult.token)) { 74 case DirectHandoff: 75 // The lock was never released. It was handed to us directly by the thread that did 76 // unlock(). This means we're done! 77 RELEASE_ASSERT(isHeld()); 78 return; 79 case BargingOpportunity: 80 // This is the common case. The thread that called unlock() has released the lock, 81 // and we have been woken up so that we may get an opportunity to grab the lock. But 82 // other threads may barge, so the best that we can do is loop around and try again. 83 break; 84 } 85 } 71 86 72 87 // We have awoken, or we never parked because the byte value changed. Either way, we loop … … 75 90 } 76 91 77 NEVER_INLINE void LockBase::unlockSlow() 92 void LockBase::unlockSlow() 93 { 94 unlockSlowImpl(Unfair); 95 } 96 97 void LockBase::unlockFairlySlow() 98 { 99 unlockSlowImpl(Fair); 100 } 101 102 NEVER_INLINE void LockBase::unlockSlowImpl(Fairness fairness) 78 103 { 79 104 // We could get here because the weak CAS in unlock() failed spuriously, or because there is … … 90 115 } 91 116 92 // Someone is parked. Unpark exactly one thread, possibly leaving the parked bit set if 93 // there is a chance that there are still other threads parked. 117 // Someone is parked. Unpark exactly one thread. We may hand the lock to that thread 118 // directly, or we will unlock the lock at the same time as we unpark to allow for barging. 119 // When we unlock, we may leave the parked bit set if there is a chance that there are still 120 // other threads parked. 94 121 ASSERT(oldByteValue == (isHeldBit | hasParkedBit)); 95 122 ParkingLot::unparkOne( 96 123 &m_byte, 97 [ this] (ParkingLot::UnparkResult result){124 [ { 98 125 // We are the only ones that can clear either the isHeldBit or the hasParkedBit, 99 126 // so we should still see both bits set right now. 100 127 ASSERT(m_byte.load() == (isHeldBit | hasParkedBit)); 128 129 130 131 132 133 101 134 102 135 if (result.mayHaveMoreThreads) … … 104 137 else 105 138 m_byte.store(0); 139 106 140 }); 107 141 return; -
trunk/Source/WTF/wtf/Lock.h
r200068 r203350 1 1 /* 2 * Copyright (C) 2015 Apple Inc. All rights reserved.2 * Copyright (C) 2015 Apple Inc. All rights reserved. 3 3 * 4 4 * Redistribution and use in source and binary forms, with or without … … 41 41 // competetive to a spinlock (uncontended locking is inlined and is just a CAS, microcontention is 42 42 // handled by spinning and yielding), and a slow path that is competetive to std::mutex (if a lock 43 // cannot be acquired in a short period of time, the thread is put to sleep until the lock is available 44 // again). It uses less memory than a std::mutex. 43 // cannot be acquired in a short period of time, the thread is put to sleep until the lock is 44 // available again). It uses less memory than a std::mutex. This lock guarantees eventual stochastic 45 // fairness, even in programs that relock the lock immediately after unlocking it. Except when there 46 // are collisions between this lock and other locks in the ParkingLot, this lock will guarantee that 47 // at worst one call to unlock() per millisecond will do a direct hand-off to the thread that is at 48 // the head of the queue. When there are collisions, each collision increases the fair unlock delay 49 // by one millisecond in the worst case. 45 50 46 51 // This is a struct without a constructor or destructor so that it can be statically initialized. … … 74 79 } 75 80 81 82 83 84 85 86 87 88 76 89 void unlock() 77 90 { … … 82 95 83 96 unlockSlow(); 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 84 112 } 85 113 … … 102 130 WTF_EXPORT_PRIVATE void lockSlow(); 103 131 WTF_EXPORT_PRIVATE void unlockSlow(); 132 133 134 135 136 137 138 139 140 141 142 143 104 144 105 145 // Method used for testing only. -
trunk/Source/WTF/wtf/ParkingLot.cpp
r201433 r203350 27 27 #include "ParkingLot.h" 28 28 29 29 30 #include "DataLog.h" 30 31 #include "HashFunctions.h" … … 33 34 #include "ThreadingPrimitives.h" 34 35 #include "Vector.h" 36 35 37 #include "WordLock.h" 36 38 #include <condition_variable> … … 59 61 60 62 ThreadData* nextInQueue { nullptr }; 63 64 61 65 }; 62 66 … … 70 74 WTF_MAKE_FAST_ALLOCATED; 71 75 public: 76 77 78 79 80 72 81 void enqueue(ThreadData* data) 73 82 { … … 124 133 ThreadData** currentPtr = &queueHead; 125 134 ThreadData* previous = nullptr; 135 136 137 138 139 140 141 142 126 143 while (shouldContinue) { 127 144 ThreadData* current = *currentPtr; … … 130 147 if (!current) 131 148 break; 132 DequeueResult result = functor(current );149 DequeueResult result = functor(current); 133 150 switch (result) { 134 151 case DequeueResult::Ignore: … … 144 161 if (current == queueTail) 145 162 queueTail = previous; 163 146 164 *currentPtr = current->nextInQueue; 147 165 current->nextInQueue = nullptr; … … 151 169 } 152 170 } 171 172 173 153 174 154 175 ASSERT(!!queueHead == !!queueTail); … … 159 180 ThreadData* result = nullptr; 160 181 genericDequeue( 161 [&] (ThreadData* element ) -> DequeueResult {182 [&] (ThreadData* element) -> DequeueResult { 162 183 result = element; 163 184 return DequeueResult::RemoveAndStop; … … 172 193 // this lock. 173 194 WordLock lock; 195 196 197 198 174 199 175 200 // Put some distane between buckets in memory. This is one of several mitigations against false … … 531 556 } // anonymous namespace 532 557 533 NEVER_INLINE boolParkingLot::parkConditionallyImpl(558 NEVER_INLINE ParkingLot::parkConditionallyImpl( 534 559 const void* address, 535 560 const ScopedLambda<bool()>& validation, … … 541 566 542 567 ThreadData* me = myThreadData(); 568 543 569 544 570 // Guard against someone calling parkConditionally() recursively from beforeSleep(). 545 571 RELEASE_ASSERT(!me->address); 546 572 547 bool result = enqueue(573 bool esult = enqueue( 548 574 address, 549 575 [&] () -> ThreadData* { … … 555 581 }); 556 582 557 if (! result)558 return false;583 if (!esult) 584 return ; 559 585 560 586 beforeSleep(); … … 583 609 if (didGetDequeued) { 584 610 // Great! We actually got dequeued rather than the timeout expiring. 585 return true; 611 ParkResult result; 612 result.wasUnparked = true; 613 result.token = me->token; 614 return result; 586 615 } 587 616 588 617 // Have to remove ourselves from the queue since we timed out and nobody has dequeued us yet. 589 618 590 // It's possible that we get unparked right here, just before dequeue() grabs a lock. It's 591 // probably worthwhile to detect when this happens, and return true in that case, to ensure 592 // that when we return false it really means that no unpark could have been responsible for us 593 // waking up, and that if an unpark call did happen, it woke someone else up. 619 bool didDequeue = false; 594 620 dequeue( 595 621 address, BucketMode::IgnoreEmpty, 596 [&] (ThreadData* element) { 597 if (element == me) 622 [&] (ThreadData* element, bool) { 623 if (element == me) { 624 didDequeue = true; 598 625 return DequeueResult::RemoveAndStop; 626 599 627 return DequeueResult::Ignore; 600 628 }, 601 629 [] (bool) { }); 602 603 ASSERT(!me->nextInQueue); 630 631 // If didDequeue is true, then we dequeued ourselves. This means that we were not unparked. 632 // If didDequeue is false, then someone unparked us. 633 634 RELEASE_ASSERT(!me->nextInQueue); 604 635 605 636 // Make sure that no matter what, me->address is null after this point. 606 637 { 607 638 std::lock_guard<std::mutex> locker(me->parkingLock); 639 640 641 642 608 643 me->address = nullptr; 609 644 } 610 645 611 // If we were not found in the search above, then we know that someone unparked us. 612 return false; 646 ParkResult result; 647 result.wasUnparked = !didDequeue; 648 if (!didDequeue) { 649 // If we were unparked then there should be a token. 650 result.token = me->token; 651 } 652 return result; 613 653 } 614 654 … … 624 664 address, 625 665 BucketMode::EnsureNonEmpty, 626 [&] (ThreadData* element ) {666 [&] (ThreadData* element) { 627 667 if (element->address != address) 628 668 return DequeueResult::Ignore; … … 644 684 std::unique_lock<std::mutex> locker(threadData->parkingLock); 645 685 threadData->address = nullptr; 686 646 687 } 647 688 threadData->parkingCondition.notify_one(); … … 652 693 NEVER_INLINE void ParkingLot::unparkOneImpl( 653 694 const void* address, 654 const ScopedLambda< void(ParkingLot::UnparkResult)>& callback)695 const ScopedLambda<(ParkingLot::UnparkResult)>& callback) 655 696 { 656 697 if (verbose) 657 698 dataLog(toString(currentThread(), ": unparking one the hard way.\n")); 658 699 659 700 ThreadData* threadData = nullptr; 701 660 702 dequeue( 661 703 address, 662 704 BucketMode::EnsureNonEmpty, 663 [&] (ThreadData* element ) {705 [&] (ThreadData* element) { 664 706 if (element->address != address) 665 707 return DequeueResult::Ignore; 666 708 threadData = element; 709 667 710 return DequeueResult::RemoveAndStop; 668 711 }, … … 671 714 result.didUnparkThread = !!threadData; 672 715 result.mayHaveMoreThreads = result.didUnparkThread && mayHaveMoreThreads; 673 callback(result); 716 if (timeToBeFair) 717 RELEASE_ASSERT(threadData); 718 result.timeToBeFair = timeToBeFair; 719 intptr_t token = callback(result); 720 if (threadData) 721 threadData->token = token; 674 722 }); 675 723 … … 695 743 address, 696 744 BucketMode::IgnoreEmpty, 697 [&] (ThreadData* element ) {745 [&] (ThreadData* element) { 698 746 if (verbose) 699 747 dataLog(toString(currentThread(), ": Observing element with address = ", RawPointer(element->address), "\n")); -
trunk/Source/WTF/wtf/ParkingLot.h
r201433 r203350 44 44 // Parks the thread in a queue associated with the given address, which cannot be null. The 45 45 // parking only succeeds if the validation function returns true while the queue lock is held. 46 46 47 // If validation returns false, it will unlock the internal parking queue and then it will 47 // return without doing anything else. If validation returns true, it will enqueue the thread, 48 // unlock the parking queue lock, call the beforeSleep function, and then it will sleep so long 49 // as the thread continues to be on the queue and the timeout hasn't fired. Finally, this 50 // returns true if we actually got unparked or false if the timeout was hit. Note that 51 // beforeSleep is called with no locks held, so it's OK to do pretty much anything so long as 52 // you don't recursively call parkConditionally(). You can call unparkOne()/unparkAll() though. 53 // It's useful to use beforeSleep() to unlock some mutex in the implementation of 48 // return a null ParkResult (wasUnparked = false, token = 0) without doing anything else. 49 // 50 // If validation returns true, it will enqueue the thread, unlock the parking queue lock, call 51 // the beforeSleep function, and then it will sleep so long as the thread continues to be on the 52 // queue and the timeout hasn't fired. Finally, this returns wasUnparked = true if we actually 53 // got unparked or wasUnparked = false if the timeout was hit. When wasUnparked = true, the 54 // token will contain whatever token was returned from the callback to unparkOne(), or 0 if the 55 // thread was unparked using unparkAll() or the form of unparkOne() that doesn't take a 56 // callback. 57 // 58 // Note that beforeSleep is called with no locks held, so it's OK to do pretty much anything so 59 // long as you don't recursively call parkConditionally(). You can call unparkOne()/unparkAll() 60 // though. It's useful to use beforeSleep() to unlock some mutex in the implementation of 54 61 // Condition::wait(). 62 63 64 65 55 66 template<typename ValidationFunctor, typename BeforeSleepFunctor> 56 static boolparkConditionally(67 static parkConditionally( 57 68 const void* address, 58 69 ValidationFunctor&& validation, … … 70 81 // indefinitely so long as the value at the given address hasn't changed. 71 82 template<typename T, typename U> 72 static boolcompareAndPark(const Atomic<T>* address, U expected)83 static compareAndPark(const Atomic<T>* address, U expected) 73 84 { 74 85 return parkConditionally( … … 82 93 } 83 94 95 96 97 98 99 100 101 102 103 104 105 106 84 107 // Unparks one thread from the queue associated with the given address, which cannot be null. 85 108 // Returns true if there may still be other threads on that queue, or false if there definitely 86 109 // are no more threads on the queue. 87 struct UnparkResult {88 bool didUnparkThread { false };89 bool mayHaveMoreThreads { false };90 };91 110 WTF_EXPORT_PRIVATE static UnparkResult unparkOne(const void* address); 92 111 112 113 114 93 115 // Unparks one thread from the queue associated with the given address, and calls the given 94 // functor while the address is locked. Reports to the callback whether any thread got unparked95 // and whether there may be any other threads still on the queue. This is an expert-mode version96 // of unparkOne() that allows for really good thundering herd avoidance in adaptive mutexes.97 // Without this, a lock implementation that uses unparkOne() has to have some trick for knowing98 // if there are still threads parked on the queue, so that it can set some bit in its lock word99 // to indicate that the next unlock() also needs to unparkOne(). But there is a race between100 // manipulating that bit and some other thread acquiring the lock. It's possible to work around101 // that race - see Rusty Russel's well-known usersem library - but it's not pretty. This form102 // allows that race to be completely avoided, since there is no way that a thread can be parked103 // while the callback is running.116 // 117 // 118 // 119 // 120 // 121 // 122 // 123 // 124 // 125 // . 104 126 template<typename Callback> 105 127 static void unparkOne(const void* address, Callback&& callback) 106 128 { 107 unparkOneImpl(address, scopedLambda< void(UnparkResult)>(std::forward<Callback>(callback)));129 unparkOneImpl(address, scopedLambda<(UnparkResult)>(std::forward<Callback>(callback))); 108 130 } 109 131 … … 127 149 128 150 private: 129 WTF_EXPORT_PRIVATE static boolparkConditionallyImpl(151 WTF_EXPORT_PRIVATE static parkConditionallyImpl( 130 152 const void* address, 131 153 const ScopedLambda<bool()>& validation, … … 134 156 135 157 WTF_EXPORT_PRIVATE static void unparkOneImpl( 136 const void* address, const ScopedLambda< void(UnparkResult)>& callback);158 const void* address, const ScopedLambda<(UnparkResult)>& callback); 137 159 138 160 WTF_EXPORT_PRIVATE static void forEachImpl(const std::function<void(ThreadIdentifier, const void*)>&); -
trunk/Tools/ChangeLog
r203338 r203350 1 2 3 4 5 6 7 8 9 1 10 2016-07-17 Sam Weinig <sam@webkit.org> 2 11 -
trunk/Tools/TestWebKitAPI/Tests/WTF/ParkingLot.cpp
r198053 r203350 149 149 150 150 // We need to wait. 151 if (ParkingLot::compareAndPark(&semaphore, newSemaphoreValue) ) {151 if (ParkingLot::compareAndPark(&semaphore, newSemaphoreValue)) { 152 152 // We did wait, and then got woken up. This means that someone who up'd the semaphore 153 153 // passed ownership onto us.
Note:
See TracChangeset
for help on using the changeset viewer.