blob: 8438d2e92aad31efd459983b9c52c09a069fd0da [file] [log] [blame]
jfbastien@apple.coma07ee7f2017-08-22 21:54:59 +00001/*
2 * Copyright (C) 2015-2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/LockAlgorithm.h>
29#include <wtf/ParkingLot.h>
30
31// It's a good idea to avoid including this header in too many places, so that it's possible to change
32// the lock algorithm slow path without recompiling the world. Right now this should be included in two
33// places (Lock.cpp and JSCell.cpp).
34
35namespace WTF {
36
37template<typename LockType, LockType isHeldBit, LockType hasParkedBit>
38void LockAlgorithm<LockType, isHeldBit, hasParkedBit>::lockSlow(Atomic<LockType>& lock)
39{
40 // This magic number turns out to be optimal based on past JikesRVM experiments.
41 static const unsigned spinLimit = 40;
42
43 unsigned spinCount = 0;
44
45 for (;;) {
46 uint8_t currentByteValue = lock.load();
47
48 // We allow ourselves to barge in.
49 if (!(currentByteValue & isHeldBit)
50 && lock.compareExchangeWeak(currentByteValue, currentByteValue | isHeldBit))
51 return;
52
53 // If there is nobody parked and we haven't spun too much, we can just try to spin around.
54 if (!(currentByteValue & hasParkedBit) && spinCount < spinLimit) {
55 spinCount++;
56 Thread::yield();
57 continue;
58 }
59
60 // Need to park. We do this by setting the parked bit first, and then parking. We spin around
61 // if the parked bit wasn't set and we failed at setting it.
62 if (!(currentByteValue & hasParkedBit)
63 && !lock.compareExchangeWeak(currentByteValue, currentByteValue | hasParkedBit))
64 continue;
65
66 // We now expect the value to be isHeld|hasParked. So long as that's the case, we can park.
67 ParkingLot::ParkResult parkResult =
68 ParkingLot::compareAndPark(&lock, currentByteValue | isHeldBit | hasParkedBit);
69 if (parkResult.wasUnparked) {
70 switch (static_cast<Token>(parkResult.token)) {
71 case DirectHandoff:
72 // The lock was never released. It was handed to us directly by the thread that did
73 // unlock(). This means we're done!
74 RELEASE_ASSERT(isLocked(lock));
75 return;
76 case BargingOpportunity:
77 // This is the common case. The thread that called unlock() has released the lock,
78 // and we have been woken up so that we may get an opportunity to grab the lock. But
79 // other threads may barge, so the best that we can do is loop around and try again.
80 break;
81 }
82 }
83
84 // We have awoken, or we never parked because the byte value changed. Either way, we loop
85 // around and try again.
86 }
87}
88
89template<typename LockType, LockType isHeldBit, LockType hasParkedBit>
90void LockAlgorithm<LockType, isHeldBit, hasParkedBit>::unlockSlow(Atomic<LockType>& lock, Fairness fairness)
91{
92 // We could get here because the weak CAS in unlock() failed spuriously, or because there is
93 // someone parked. So, we need a CAS loop: even if right now the lock is just held, it could
94 // be held and parked if someone attempts to lock just as we are unlocking.
95 for (;;) {
96 uint8_t oldByteValue = lock.load();
97 RELEASE_ASSERT(
98 (oldByteValue & mask) == isHeldBit
99 || (oldByteValue & mask) == (isHeldBit | hasParkedBit));
100
101 if ((oldByteValue & mask) == isHeldBit) {
102 if (lock.compareExchangeWeak(oldByteValue, oldByteValue & ~isHeldBit))
103 return;
104 continue;
105 }
106
107 // Someone is parked. Unpark exactly one thread. We may hand the lock to that thread
108 // directly, or we will unlock the lock at the same time as we unpark to allow for barging.
109 // When we unlock, we may leave the parked bit set if there is a chance that there are still
110 // other threads parked.
111 ASSERT((oldByteValue & mask) == (isHeldBit | hasParkedBit));
112 ParkingLot::unparkOne(
113 &lock,
114 [&] (ParkingLot::UnparkResult result) -> intptr_t {
115 // We are the only ones that can clear either the isHeldBit or the hasParkedBit,
116 // so we should still see both bits set right now.
117 ASSERT((lock.load() & mask) == (isHeldBit | hasParkedBit));
118
119 if (result.didUnparkThread && (fairness == Fair || result.timeToBeFair)) {
120 // We don't unlock anything. Instead, we hand the lock to the thread that was
121 // waiting.
122 return DirectHandoff;
123 }
124
125 lock.transaction(
126 [&] (LockType& value) -> bool {
127 value &= ~mask;
128 if (result.mayHaveMoreThreads)
129 value |= hasParkedBit;
130 return true;
131 });
132 return BargingOpportunity;
133 });
134 return;
135 }
136}
137
138} // namespace WTF
139