Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spin Locks and Contention Management The Art of Multiprocessor Programming Spring 2007.

Similar presentations


Presentation on theme: "Spin Locks and Contention Management The Art of Multiprocessor Programming Spring 2007."— Presentation transcript:

1 Spin Locks and Contention Management The Art of Multiprocessor Programming Spring 2007

2 © Herlihy-Shavit Focus so far: Correctness Models –Accurate (we never lied to you) –But idealized (so we forgot to mention a few things) Protocols –Elegant –Important –But naïve

3 © Herlihy-Shavit New Focus: Performance Models –More complicated (not the same as complex!) –Still focus on principles (not soon obsolete) Protocols –Elegant (in their fashion) –Important (why else would we pay attention) –And realistic (your mileage may vary)

4 © Herlihy-Shavit Kinds of Architectures SISD (Uniprocessor) –Single instruction stream –Single data stream SIMD (Vector) –Single instruction –Multiple data MIMD (Multiprocessors) –Multiple instruction –Multiple data.

5 © Herlihy-Shavit Kinds of Architectures SISD (Uniprocessor) –Single instruction stream –Single data stream SIMD (Vector) –Single instruction –Multiple data MIMD (Multiprocessors) –Multiple instruction –Multiple data. Our space (1)

6 © Herlihy-Shavit MIMD Architectures Memory Contention Communication Contention Communication Latency Shared Bus memory Distributed

7 © Herlihy-Shavit Today: Revisit Mutual Exclusion Think of performance, not just correctness and progress Begin to understand how performance depends on our software properly utilizing the multipprocessor machines hardware And get to know a collection of locking algorithms… (1)

8 © Herlihy-Shavit What Should you do if you can’t get a lock? Keep trying –“spin” or “busy-wait” –Good if delays are short Give up the processor –Good if delays are long –Always good on uniprocessor (1)

9 © Herlihy-Shavit What Should you do if you can’t get a lock? Keep trying –“spin” or “busy-wait” –Good if delays are short Give up the processor –Good if delays are long –Always good on uniprocessor our focus

10 © Herlihy-Shavit Basic Spin-Lock CS Resets lock upon exit spin lock critical section...

11 © Herlihy-Shavit Basic Spin-Lock CS Resets lock upon exit spin lock critical section... …lock introduces sequntial bottleneck

12 © Herlihy-Shavit Basic Spin-Lock CS Resets lock upon exit spin lock critical section... …lock suffers from contention

13 © Herlihy-Shavit Basic Spin-Lock CS Resets lock upon exit spin lock critical section... Notice: these are two different phenomenon …lock suffers from contention Seq Bottleneck  no parallelism

14 © Herlihy-Shavit Basic Spin-Lock CS Resets lock upon exit spin lock critical section... Contention  ??? …lock suffers from contention

15 © Herlihy-Shavit Review: Test-and-Set Boolean value Test-and-set (TAS) –Swap true with current value –Return value tells if prior value was true or false Can reset just by writing false TAS aka “getAndSet”

16 © Herlihy-Shavit Review: Test-and-Set public class AtomicBoolean { boolean value; public synchronized boolean getAndSet(boolean newValue) { boolean prior = value; value = newValue; return prior; } (5)

17 © Herlihy-Shavit Review: Test-and-Set public class AtomicBoolean { boolean value; public synchronized boolean getAndSet(boolean newValue) { boolean prior = value; value = newValue; return prior; } Package java.util.concurrent.atomic

18 © Herlihy-Shavit Review: Test-and-Set public class AtomicBoolean { boolean value; public synchronized boolean getAndSet(boolean newValue) { boolean prior = value; value = newValue; return prior; } Swap old and new values

19 © Herlihy-Shavit Review: Test-and-Set AtomicBoolean lock = new AtomicBoolean(false) … boolean prior = lock.getAndSet(true)

20 © Herlihy-Shavit Review: Test-and-Set AtomicBoolean lock = new AtomicBoolean(false) … boolean prior = lock.getAndSet(true) (5) Swapping in true is called “test-and-set” or TAS

21 © Herlihy-Shavit Test-and-Set Locks Locking –Lock is free: value is false –Lock is taken: value is true Acquire lock by calling TAS –If result is false, you win –If result is true, you lose Release lock by writing false

22 © Herlihy-Shavit Test-and-set Lock class TASlock { AtomicBoolean state = new AtomicBoolean(false); void lock() { while (state.getAndSet(true)) {} } void unlock() { state.set(false); }}

23 © Herlihy-Shavit Test-and-set Lock class TASlock { AtomicBoolean state = new AtomicBoolean(false); void lock() { while (state.getAndSet(true)) {} } void unlock() { state.set(false); }} Lock state is AtomicBoolean

24 © Herlihy-Shavit Test-and-set Lock class TASlock { AtomicBoolean state = new AtomicBoolean(false); void lock() { while (state.getAndSet(true)) {} } void unlock() { state.set(false); }} Keep trying until lock acquired

25 © Herlihy-Shavit Test-and-set Lock class TASlock { AtomicBoolean state = new AtomicBoolean(false); void lock() { while (state.getAndSet(true)) {} } void unlock() { state.set(false); }} Release lock by resetting state to false

26 © Herlihy-Shavit Space Complexity TAS spin-lock has small “footprint” N thread spin-lock uses O(1) space As opposed to O(n) Peterson/Bakery How did we overcome the  (n) lower bound? We used a RMW operation…

27 © Herlihy-Shavit Performance Experiment –n threads –Increment shared counter 1 million times How long should it take? How long does it take?

28 © Herlihy-Shavit Graph ideal time threads no speedup because of sequential bottleneck

29 © Herlihy-Shavit Mystery #1 time threads TAS lock Ideal (1) What is going on? Lets try and fix it…

30 © Herlihy-Shavit Test-and-Test-and-Set Locks Lurking stage –Wait until lock “looks” free –Spin while read returns true (lock taken) Pouncing state –As soon as lock “looks” available –Read returns false (lock free) –Call TAS to acquire lock –If TAS loses, back to lurking

31 © Herlihy-Shavit Test-and-test-and-set Lock class TTASlock { AtomicBoolean state = new AtomicBoolean(false); void lock() { while (true) { while (state.get()) {} if (!state.getAndSet(true)) return; }

32 © Herlihy-Shavit Test-and-test-and-set Lock class TTASlock { AtomicBoolean state = new AtomicBoolean(false); void lock() { while (true) { while (state.get()) {} if (!state.getAndSet(true)) return; } Wait until lock looks free

33 © Herlihy-Shavit Test-and-test-and-set Lock class TTASlock { AtomicBoolean state = new AtomicBoolean(false); void lock() { while (true) { while (state.get()) {} if (!state.getAndSet(true)) return; } Then try to acquire it

34 © Herlihy-Shavit Mystery #2 TAS lock TTAS lock Ideal time threads

35 © Herlihy-Shavit Mystery Both –TAS and TTAS –Do the same thing (in our model) Except that –TTAS performs much better than TAS –Neither approaches ideal

36 © Herlihy-Shavit Opinion Our memory abstraction is broken TAS & TTAS methods –Are provably the same (in our model) –Except they aren’t (in field tests) Need a more detailed model …

37 © Herlihy-Shavit Bus-Based Architectures Bus cache memory cache

38 © Herlihy-Shavit Bus-Based Architectures Bus cache memory cache Random access memory (10s of cycles)

39 © Herlihy-Shavit Bus-Based Architectures cache memory cache Shared Bus broadcast medium One broadcaster at a time Processors and memory all “snoop” Bus

40 © Herlihy-Shavit Bus-Based Architectures Bus cache memory cache Per-Processor Caches Small Fast: 1 or 2 cycles Address & state information

41 © Herlihy-Shavit Jargon Watch Cache hit –“I found what I wanted in my cache” –Good Thing™

42 © Herlihy-Shavit Jargon Watch Cache hit –“I found what I wanted in my cache” –Good Thing™ Cache miss –“I had to shlep all the way to memory for that data” –Bad Thing™

43 © Herlihy-Shavit Cave Canem This model is still a simplification –But not in any essential way –Illustrates basic principles Will discuss complexities later

44 © Herlihy-Shavit Bus Processor Issues Load Request cache memory cache data

45 © Herlihy-Shavit Bus Processor Issues Load Request Bus cache memory cache data Gimme data

46 © Herlihy-Shavit cache Bus Memory Responds Bus memory cache data Got your data right here data

47 © Herlihy-Shavit Bus Processor Issues Load Request memory cache data Gimme data

48 © Herlihy-Shavit Bus Processor Issues Load Request Bus memory cache data Gimme data

49 © Herlihy-Shavit Bus Processor Issues Load Request Bus memory cache data I got data

50 © Herlihy-Shavit Bus Other Processor Responds memory cache data I got data data Bus

51 © Herlihy-Shavit Bus Other Processor Responds memory cache data Bus

52 © Herlihy-Shavit Modify Cached Data Bus data memory cachedata (1)

53 © Herlihy-Shavit Modify Cached Data Bus data memory cachedata (1)

54 © Herlihy-Shavit memory Bus data Modify Cached Data cachedata

55 © Herlihy-Shavit memory Bus data Modify Cached Data cache What’s up with the other copies? data

56 © Herlihy-Shavit Cache Coherence We have lots of copies of data –Original copy in memory –Cached copies at processors Some processor modifies its own copy –What do we do with the others? –How to avoid confusion?

57 © Herlihy-Shavit Write-Back Caches Accumulate changes in cache Write back when needed –Need the cache for something else –Another processor wants it On first modification –Invalidate other entries –Requires non-trivial protocol …

58 © Herlihy-Shavit Write-Back Caches Cache entry has three states –Invalid: contains raw seething bits –Valid: I can read but I can’t write –Dirty: Data has been modified Intercept other load requests Write back to memory before using cache

59 © Herlihy-Shavit Bus Invalidate memory cachedata

60 © Herlihy-Shavit Bus Invalidate Bus memory cachedata Mine, all mine!

61 © Herlihy-Shavit Bus Invalidate Bus memory cachedata cache Uh,oh

62 © Herlihy-Shavit cache Bus Invalidate memory cachedata Other caches lose read permission

63 © Herlihy-Shavit cache Bus Invalidate memory cachedata Other caches lose read permission This cache acquires write permission

64 © Herlihy-Shavit cache Bus Invalidate memory cachedata Memory provides data only if not present in any cache, so no need to change it now (expensive) (2)

65 © Herlihy-Shavit cache Bus Another Processor Asks for Data memory cachedata (2) Bus

66 © Herlihy-Shavit cache data Bus Owner Responds memory cachedata (2) Bus Here it is!

67 © Herlihy-Shavit Bus End of the Day … memory cachedata (1) Reading OK, no writing data

68 © Herlihy-Shavit Mutual Exclusion What do we want to optimize? –Bus bandwidth used by spinning threads –Release/Acquire latency –Acquire latency for idle lock

69 © Herlihy-Shavit Simple TASLock TAS invalidates cache lines Spinners –Miss in cache –Go to bus Thread wants to release lock –delayed behind spinners

70 © Herlihy-Shavit Test-and-test-and-set Wait until lock “looks” free –Spin on local cache –No bus use while lock busy Problem: when lock is released –Invalidation storm …

71 © Herlihy-Shavit Local Spinning while Lock is Busy Bus memory busy

72 © Herlihy-Shavit Bus On Release memory freeinvalid free

73 © Herlihy-Shavit On Release Bus memory freeinvalid free miss Everyone misses, rereads (1)

74 © Herlihy-Shavit On Release Bus memory freeinvalid free TAS(…) Everyone tries TAS (1)

75 © Herlihy-Shavit Problems Everyone misses –Reads satisfied sequentially Everyone does TAS –Invalidates others’ caches Eventually quiesces after lock acquired –How long does this take?

76 © Herlihy-Shavit Measuring Quiescence Time P1P1 P2P2 PnPn X = time of ops that don’t use the bus Y = time of ops that cause intensive bus traffic In critical section, run ops X then ops Y. As long as Quiescence time is less than X, no drop in performance. By gradually varying X, can determine the exact time to quiesce.

77 © Herlihy-Shavit Quiescence Time Increses linearly with the number of processors for bus architecture time threads

78 © Herlihy-Shavit Mystery Explained TAS lock TTAS lock Ideal time threads Better than TAS but still not as good as ideal

79 © Herlihy-Shavit Solution: Introduce Delay spin lock time d r1dr1d r2dr2d If the lock looks free But I fail to get it There must be lots of contention Better to back off than to collide again

80 © Herlihy-Shavit Dynamic Example: Exponential Backoff time d 2d4d spin lock If I fail to get lock –wait random duration before retry –Each subsequent failure doubles expected wait

81 © Herlihy-Shavit Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}}

82 © Herlihy-Shavit Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}} Fix minimum delay

83 © Herlihy-Shavit Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}} Wait until lock looks free

84 © Herlihy-Shavit Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}} If we win, return

85 © Herlihy-Shavit Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}} Back off for random duration

86 © Herlihy-Shavit Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}} Double max delay, within reason

87 © Herlihy-Shavit Spin-Waiting Overhead TTAS Lock Backoff lock time threads

88 © Herlihy-Shavit Backoff: Other Issues Good –Easy to implement –Beats TTAS lock Bad –Must choose parameters carefully –Not portable across platforms

89 © Herlihy-Shavit Idea Avoid useless invalidations –By keeping a queue of threads Each thread –Notifies next in line –Without bothering the others

90 © Herlihy-Shavit Anderson Queue Lock flags next TFFFFFFF idle

91 © Herlihy-Shavit Anderson Queue Lock flags next TFFFFFFF acquiring getAndIncrement

92 © Herlihy-Shavit Anderson Queue Lock flags next TFFFFFFF acquiring getAndIncrement

93 © Herlihy-Shavit Anderson Queue Lock flags next TFFFFFFF acquired Mine!

94 © Herlihy-Shavit Anderson Queue Lock flags next TFFFFFFF acquired acquiring

95 © Herlihy-Shavit Anderson Queue Lock flags next TFFFFFFF acquired acquiring getAndIncrement

96 © Herlihy-Shavit Anderson Queue Lock flags next TFFFFFFF acquired acquiring getAndIncrement

97 © Herlihy-Shavit acquired Anderson Queue Lock flags next TFFFFFFF acquiring

98 © Herlihy-Shavit released Anderson Queue Lock flags next TTFFFFFF acquired

99 © Herlihy-Shavit released Anderson Queue Lock flags next TTFFFFFF acquired Yow!

100 © Herlihy-Shavit Anderson Queue Lock class ALock implements Lock { boolean[] flags={true,false,…,false}; AtomicInteger next = new AtomicInteger(0); int[] slot = new int[n];

101 © Herlihy-Shavit Anderson Queue Lock class ALock implements Lock { boolean[] flags={true,false,…,false}; AtomicInteger next = new AtomicInteger(0); int[] slot = new int[n]; One flag per thread

102 © Herlihy-Shavit Anderson Queue Lock class ALock implements Lock { boolean[] flags={true,false,…,false}; AtomicInteger next = new AtomicInteger(0); int[] slot = new int[n]; Next flag to use

103 © Herlihy-Shavit Anderson Queue Lock class ALock implements Lock { boolean[] flags={true,false,…,false}; AtomicInteger next = new AtomicInteger(0); ThreadLocal mySlot; Thread-local variable

104 © Herlihy-Shavit Anderson Queue Lock public lock() { int mySlot = next.getAndIncrement(); while (!flags[mySlot % n]) {}; flags[mySlot % n] = false; } public unlock() { flags[(mySlot+1) % n] = true; }

105 © Herlihy-Shavit Anderson Queue Lock public lock() { int mySlot = next.getAndIncrement(); while (!flags[mySlot % n]) {}; flags[mySlot % n] = false; } public unlock() { flags[(mySlot+1) % n] = true; } Take next slot

106 © Herlihy-Shavit Anderson Queue Lock public lock() { int mySlot = next.getAndIncrement(); while (!flags[mySlot % n]) {}; flags[mySlot % n] = false; } public unlock() { flags[(mySlot+1) % n] = true; } Spin until told to go

107 © Herlihy-Shavit Anderson Queue Lock public lock() { int slot[i]=next.getAndIncrement(); while (!flags[slot[i] % n]) {}; flags[slot[i] % n] = false; } public unlock() { flags[slot[i]+1 % n] = true; } Prepare slot for re-use

108 © Herlihy-Shavit Anderson Queue Lock public lock() { int mySlot = next.getAndIncrement(); while (!flags[mySlot % n]) {}; flags[mySlot % n] = false; } public unlock() { flags[(mySlot+1) % n] = true; } Tell next thread to go

109 © Herlihy-Shavit Performance Curve is practically flat Scalable performance FIFO fairness queue TTAS

110 © Herlihy-Shavit Anderson Queue Lock Good –First truly scalable lock –Simple, easy to implement Bad –Space hog –One bit per thread Unknown number of threads? Small number of actual contenders?

111 © Herlihy-Shavit CLH Lock FIFO order Small, constant-size overhead per thread

112 © Herlihy-Shavit Initially false tail idle

113 © Herlihy-Shavit Initially false tail idle Queue tail

114 © Herlihy-Shavit Initially false tail idle Lock is free

115 © Herlihy-Shavit Initially false tail idle

116 © Herlihy-Shavit Purple Wants the Lock false tail acquiring

117 © Herlihy-Shavit Purple Wants the Lock false tail acquiring true

118 © Herlihy-Shavit Purple Wants the Lock false tail acquiring true Swap

119 © Herlihy-Shavit Purple Has the Lock false tail acquired true

120 © Herlihy-Shavit Red Wants the Lock false tail acquired acquiring true

121 © Herlihy-Shavit Red Wants the Lock false tail acquired acquiring true Swap true

122 © Herlihy-Shavit Red Wants the Lock false tail acquired acquiring true

123 © Herlihy-Shavit Red Wants the Lock false tail acquired acquiring true

124 © Herlihy-Shavit Red Wants the Lock false tail acquired acquiring true Actually, it spins on cached copy

125 © Herlihy-Shavit Purple Releases false tail release acquiring false true false Bingo!

126 © Herlihy-Shavit Purple Releases tail released acquired true

127 © Herlihy-Shavit Space Usage Let –L = number of locks –N = number of threads ALock –O(LN) CLH lock –O(L+N)

128 © Herlihy-Shavit CLH Queue Lock class Qnode { AtomicBoolean locked = new AtomicBoolean(true); }

129 © Herlihy-Shavit CLH Queue Lock class Qnode { AtomicBoolean locked = new AtomicBoolean(true); } Not released yet

130 © Herlihy-Shavit CLH Queue Lock class CLHLock implements Lock { AtomicReference tail; ThreadLocal myNode = new Qnode(); public void lock() { Qnode pred = queue.getAndSet(myNode); while (pred.locked) {} }} (3)

131 © Herlihy-Shavit CLH Queue Lock class CLHLock implements Lock { AtomicReference tail; ThreadLocal myNode = new Qnode(); public void lock() { Qnode pred = queue.getAndSet(myNode); while (pred.locked) {} }} (3) Tail of the queue

132 © Herlihy-Shavit CLH Queue Lock class CLHLock implements Lock { AtomicReference tail; ThreadLocal myNode = new Qnode(); public void lock() { Qnode pred = queue.getAndSet(myNode); while (pred.locked) {} }} (3) Thread-local Qnode

133 © Herlihy-Shavit CLH Queue Lock class CLHLock implements Lock { AtomicReference tail; ThreadLocal myNode = new Qnode(); public void lock() { Qnode pred = queue.getAndSet(myNode); while (pred.locked) {} }} (3) Swap in my node

134 © Herlihy-Shavit CLH Queue Lock class CLHLock implements Lock { AtomicReference tail; ThreadLocal myNode = new Qnode(); public void lock() { Qnode pred = queue.getAndSet(myNode); while (pred.locked) {} }} (3) Spin until predecessor releases lock

135 © Herlihy-Shavit CLH Queue Lock Class CLHLock implements Lock { … public void unlock() { myNode.locked.set(false); myNode = pred; } (3)

136 © Herlihy-Shavit CLH Queue Lock Class CLHLock implements Lock { … public void unlock() { myNode.locked.set(false); myNode = pred; } (3) Notify successor

137 © Herlihy-Shavit CLH Queue Lock Class CLHLock implements Lock { … public void unlock() { myNode.locked.set(false); myNode = pred; } (3) Recycle predecessor’s node

138 © Herlihy-Shavit CLH Lock Good –Lock release affects predecessor only –Small, constant-sized space Bad –Doesn’t work for uncached NUMA architectures

139 © Herlihy-Shavit NUMA Architecturs Acronym: –Non-Uniform Memory Architecture Illusion: –Flat shared memory Truth: –No caches (sometimes) –Some memory regions faster than others

140 © Herlihy-Shavit NUMA Machines Spinning on local memory is fast

141 © Herlihy-Shavit NUMA Machines Spinning on remote memory is slow

142 © Herlihy-Shavit CLH Lock Each thread spin’s on predecessor’s memory Could be far away …

143 © Herlihy-Shavit MCS Lock FIFO order Spin on local memory only Small, Constant-size overhead

144 © Herlihy-Shavit Initially false tail false idle

145 © Herlihy-Shavit Acquiring false queue false true acquiring (allocate Qnode)

146 © Herlihy-Shavit Acquiring false tail false true acquired swap

147 © Herlihy-Shavit Acquiring false tail false true acquired

148 © Herlihy-Shavit Acquired false tail false true acquired

149 © Herlihy-Shavit Acquiring tail true acquired acquiring true swap

150 © Herlihy-Shavit Acquiring tail acquired acquiring true

151 © Herlihy-Shavit Acquiring tail acquired acquiring true

152 © Herlihy-Shavit Acquiring tail acquired acquiring true

153 © Herlihy-Shavit Acquiring tail acquired acquiring true false

154 © Herlihy-Shavit Acquiring tail acquired acquiring true Yes! false

155 © Herlihy-Shavit MCS Queue Lock class Qnode { boolean locked = false; qnode next = null; }

156 © Herlihy-Shavit MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void lock() { Qnode qnode = new Qnode(); Qnode pred = tail.getAndSet(qnode); if (pred != null) { qnode.locked = true; pred.next = qnode; while (qnode.locked) {} }}} (3)

157 © Herlihy-Shavit MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void lock() { Qnode qnode = new Qnode(); Qnode pred = tail.getAndSet(qnode); if (pred != null) { qnode.locked = true; pred.next = qnode; while (qnode.locked) {} }}} (3) Make a QNode

158 © Herlihy-Shavit MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void lock() { Qnode qnode = new Qnode(); Qnode pred = tail.getAndSet(qnode); if (pred != null) { qnode.locked = true; pred.next = qnode; while (qnode.locked) {} }}} (3) add my Node to the tail of queue

159 © Herlihy-Shavit MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void lock() { Qnode qnode = new Qnode(); Qnode pred = tail.getAndSet(qnode); if (pred != null) { qnode.locked = true; pred.next = qnode; while (qnode.locked) {} }}} (3) Fix if queue was non-empty

160 © Herlihy-Shavit MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void lock() { Qnode qnode = new Qnode(); Qnode pred = tail.getAndSet(qnode); if (pred != null) { qnode.locked = true; pred.next = qnode; while (qnode.locked) {} }}} (3) Wait until unlocked

161 © Herlihy-Shavit MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void unlock() { if (qnode.next == null) { if (queue.CAS(qnode, null) return; while (qnode.next == null) {} } qnode.next.locked = false; }} (3)

162 © Herlihy-Shavit MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void unlock() { if (qnode.next == null) { if (queue.CAS(qnode, null) return; while (qnode.next == null) {} } qnode.next.locked = false; }} (3) Missing successor?

163 © Herlihy-Shavit MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void unlock() { if (qnode.next == null) { if (queue.CAS(qnode, null) return; while (qnode.next == null) {} } qnode.next.locked = false; }} (3) If really no successor, return

164 © Herlihy-Shavit MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void unlock() { if (qnode.next == null) { if (queue.CAS(qnode, null) return; while (qnode.next == null) {} } qnode.next.locked = false; }} (3) Otherwise wait for successor to catch up

165 © Herlihy-Shavit MCS Queue Lock class MCSLock implements Lock { AtomicReference queue; public void unlock() { if (qnode.next == null) { if (queue.CAS(qnode, null) return; while (qnode.next == null) {} } qnode.next.locked = false; }} (3) Pass lock to successor

166 © Herlihy-Shavit Purple Release false releasing swap false (2)

167 © Herlihy-Shavit Purple Release false releasing swap false By looking at the queue, I see another thread is active (2)

168 © Herlihy-Shavit Purple Release false releasing swap false By looking at the queue, I see another thread is active I have to wait for that thread to finish (2)

169 © Herlihy-Shavit Purple Release false releasing spinning true

170 © Herlihy-Shavit Purple Release false releasing spinning true false

171 © Herlihy-Shavit Purple Release false releasing spinning true locked false

172 © Herlihy-Shavit Abortable Locks What if you want to give up waiting for a lock? For example –Timeout –Database transaction aborted by user

173 © Herlihy-Shavit Back-off Lock Aborting is trivial –Just return from lock() call Extra benefit: –No cleaning up –Wait-free –Immediate return

174 © Herlihy-Shavit Queue Locks Can’t just quit –Thread in line behind will starve Need a graceful way out

175 © Herlihy-Shavit Queue Locks spinning true spinning true spinning

176 © Herlihy-Shavit Queue Locks spinning true spinning true false locked

177 © Herlihy-Shavit Queue Locks spinning true locked false

178 © Herlihy-Shavit Queue Locks locked false

179 © Herlihy-Shavit Queue Locks spinning true spinning true spinning

180 © Herlihy-Shavit Queue Locks spinning true spinning

181 © Herlihy-Shavit Queue Locks spinning true false locked

182 © Herlihy-Shavit Queue Locks spinning true false

183 © Herlihy-Shavit Queue Locks pwned true false

184 © Herlihy-Shavit Abortable CLH Lock When a thread gives up –Removing node in a wait-free way is hard Idea: –let successor deal with it.

185 © Herlihy-Shavit Initially tail idle Pointer to predecessor (or null) A

186 © Herlihy-Shavit Initially tail idle Distinguished available node means lock is free A

187 © Herlihy-Shavit Acquiring tail acquiring A

188 © Herlihy-Shavit Acquiring acquiring A Null predecessor means lock not released or aborted

189 © Herlihy-Shavit Acquiring acquiring A Swap

190 © Herlihy-Shavit Acquiring acquiring A

191 © Herlihy-Shavit Acquired locked A Pointer to AVAILABLE means lock is free.

192 © Herlihy-Shavit Normal Case spinning locked Null means lock is not free & request not aborted

193 © Herlihy-Shavit One Thread Aborts spinning Timed out locked

194 © Herlihy-Shavit Successor Notices spinning Timed out locked Non-Null means predecessor aborted

195 © Herlihy-Shavit Recycle Predecessor’s Node spinning locked

196 © Herlihy-Shavit Spin on Earlier Node spinning locked

197 © Herlihy-Shavit Spin on Earlier Node spinning released A The lock is now mine

198 © Herlihy-Shavit Time-out Lock public class TOLock implements Lock { static Qnode AVAILABLE = new Qnode(); AtomicReference tail; ThreadLocal myNode; (3)

199 © Herlihy-Shavit Time-out Lock public class TOLock implements Lock { static Qnode AVAILABLE = new Qnode(); AtomicReference tail; ThreadLocal myNode; (3) Distinguished node to signify free lock

200 © Herlihy-Shavit Time-out Lock public class TOLock implements Lock { static Qnode AVAILABLE = new Qnode(); AtomicReference tail; ThreadLocal myNode; (3) Tail of the queue

201 © Herlihy-Shavit Time-out Lock public class TOLock implements Lock { static Qnode AVAILABLE = new Qnode(); AtomicReference tail; ThreadLocal myNode; (3) Remember my node …

202 © Herlihy-Shavit Time-out Lock public boolean lock(long timeout) { Qnode qnode = new Qnode(); myNode.set(qnode); qnode.prev = null; Qnode pred = tail.getAndSet(qnode); if (pred == null || pred.prev == AVAILABLE) { return true; } … (3)

203 © Herlihy-Shavit Time-out Lock public boolean lock(long timeout) { Qnode qnode = new Qnode(); myNode.set(qnode); qnode.prev = null; Qnode pred = tail.getAndSet(qnode); if (pred == null || pred.prev == AVAILABLE) { return true; } (3) Create & initialize node

204 © Herlihy-Shavit Time-out Lock public boolean lock(long timeout) { Qnode qnode = new Qnode(); myNode.set(qnode); qnode.prev = null; Qnode pred = tail.getAndSet(qnode); if (pred == null || pred.prev == AVAILABLE) { return true; } (3) Swap with tail

205 © Herlihy-Shavit Time-out Lock public boolean lock(long timeout) { Qnode qnode = new Qnode(); myNode.set(qnode); qnode.prev = null; Qnode pred = tail.getAndSet(qnode); if (pred == null || pred.prev == AVAILABLE) { return true; }... (3) If predecessor absent or released, we are done

206 © Herlihy-Shavit Time-out Lock … long start = now(); while (now()- start < timeout) { Qnode predPred = pred.prev; if (predPred == AVAILABLE) { return true; } else if (predPred != null) { pred = predPred; } … (3)

207 © Herlihy-Shavit Time-out Lock … long start = now(); while (now()- start < timeout) { Qnode predPred = pred.prev; if (predPred == AVAILABLE) { return true; } else if (predPred != null) { pred = predPred; } … (3) Keep trying for a while …

208 © Herlihy-Shavit Time-out Lock … long start = now(); while (now()- start < timeout) { Qnode predPred = pred.prev; if (predPred == AVAILABLE) { return true; } else if (predPred != null) { pred = predPred; } … (3) Spin on predecessor’s prev field

209 © Herlihy-Shavit Time-out Lock … long start = now(); while (now()- start < timeout) { Qnode predPred = pred.prev; if (predPred == AVAILABLE) { return true; } else if (predPred != null) { pred = predPred; } … (3) Predecessor released lock

210 © Herlihy-Shavit Time-out Lock … long start = now(); while (now()- start < timeout) { Qnode predPred = pred.prev; if (predPred == AVAILABLE) { return true; } else if (predPred != null) { pred = predPred; } … (3) Predecessor aborted, advance one

211 © Herlihy-Shavit Time-out Lock … if (!tail.compareAndSet(qnode, pred)) qnode.prev = pred; return false; } (3)

212 © Herlihy-Shavit Time-out Lock … if (!tail.compareAndSet(qnode, pred)) qnode.prev = pred; return false; } (3) Try to put predecessor back

213 © Herlihy-Shavit Time-out Lock … if (!tail.compareAndSet(qnode, pred)) qnode.prev = pred; return false; } (3) Otherwise, if it’s too late, redirect to predecessor

214 © Herlihy-Shavit Time-out Lock public void unlock() { Qnode qnode = myNode.get(); if (!tail.compareAndSet(qnode, null)) qnode.prev = AVAILABLE; } (3)

215 © Herlihy-Shavit Time-out Lock public void unlock() { Qnode qnode = myNode.get(); if (!tail.compareAndSet(qnode, null)) qnode.prev = AVAILABLE; } (3) Clean up if no one waiting

216 © Herlihy-Shavit Time-out Lock public void unlock() { Qnode qnode = myNode.get(); if (!tail.compareAndSet(qnode, null)) qnode.prev = AVAILABLE; } (3) Notify successor by pointing to AVAILABLE node

217 © Herlihy-Shavit One Lock To Rule Them All? TTAS+Backoff, CLH, MCS, ToLock… Each better than others in some way There is no one solution Lock we pick really depends on: – the application – the hardware – which properties are important


Download ppt "Spin Locks and Contention Management The Art of Multiprocessor Programming Spring 2007."

Similar presentations


Ads by Google