Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spin Locks and Contention Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Similar presentations


Presentation on theme: "Spin Locks and Contention Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit."— Presentation transcript:

1 Spin Locks and Contention Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

2 Art of Multiprocessor Programming2 Focus so far: Correctness and Progress Models –Accurate (we never lied to you) –But idealized (so we forgot to mention a few things) Protocols –Elegant –Important –But naïve

3 Art of Multiprocessor Programming3 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 Art of Multiprocessor Programming4 Kinds of Architectures SISD (Uniprocessor) –Single instruction stream –Single data stream SIMD (Vector) –Single instruction –Multiple data MIMD (Multiprocessors) –Multiple instruction –Multiple data.

5 Art of Multiprocessor Programming5 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 Art of Multiprocessor Programming6 MIMD Architectures Memory Contention Communication Contention Communication Latency Shared Bus memory Distributed

7 Art of Multiprocessor Programming7 Today: Revisit Mutual Exclusion Think of performance, not just correctness and progress Begin to understand how performance depends on our software properly utilizing the multiprocessor machine’s hardware And get to know a collection of locking algorithms… (1)

8 Art of Multiprocessor Programming8 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 Art of Multiprocessor Programming9 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 Art of Multiprocessor Programming10 Basic Spin-Lock CS Resets lock upon exit spin lock critical section...

11 Art of Multiprocessor Programming11 Basic Spin-Lock CS Resets lock upon exit spin lock critical section... …lock introduces sequential bottleneck

12 Art of Multiprocessor Programming12 Basic Spin-Lock CS Resets lock upon exit spin lock critical section... …lock suffers from contention

13 Art of Multiprocessor Programming13 Basic Spin-Lock CS Resets lock upon exit spin lock critical section... Notice: these are distinct phenomena …lock suffers from contention

14 Art of Multiprocessor Programming14 Basic Spin-Lock CS Resets lock upon exit spin lock critical section... …lock suffers from contention Seq Bottleneck  no parallelism

15 Art of Multiprocessor Programming15 Basic Spin-Lock CS Resets lock upon exit spin lock critical section... Contention  ??? …lock suffers from contention

16 Art of Multiprocessor Programming16 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”

17 Art of Multiprocessor Programming17 Review: Test-and-Set public class AtomicBoolean { boolean value; public synchronized boolean getAndSet(boolean newValue) { boolean prior = value; value = newValue; return prior; } (5)

18 Art of Multiprocessor Programming18 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

19 Art of Multiprocessor Programming19 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

20 Art of Multiprocessor Programming20 Review: Test-and-Set AtomicBoolean lock = new AtomicBoolean(false) … boolean prior = lock.getAndSet(true)

21 Art of Multiprocessor Programming21 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

22 Art of Multiprocessor Programming22 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

23 Art of Multiprocessor Programming23 Test-and-set Lock class TASlock { AtomicBoolean state = new AtomicBoolean(false); void lock() { while (state.getAndSet(true)) {} } void unlock() { state.set(false); }}

24 Art of Multiprocessor Programming24 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

25 Art of Multiprocessor Programming25 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

26 Art of Multiprocessor Programming26 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

27 Art of Multiprocessor Programming27 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…

28 Art of Multiprocessor Programming28 Performance Experiment –n threads –Increment shared counter 1 million times How long should it take? How long does it take?

29 Art of Multiprocessor Programming29 Graph ideal time threads no speedup because of sequential bottleneck

30 Art of Multiprocessor Programming30 Mystery #1 time threads TAS lock Ideal (1) What is going on?

31 Art of Multiprocessor Programming31 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

32 Art of Multiprocessor Programming32 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; }

33 Art of Multiprocessor Programming33 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

34 Art of Multiprocessor Programming34 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

35 Art of Multiprocessor Programming35 Mystery #2 TAS lock TTAS lock Ideal time threads

36 Art of Multiprocessor Programming36 Mystery Both –TAS and TTAS –Do the same thing (in our model) Except that –TTAS performs much better than TAS –Neither approaches ideal

37 Art of Multiprocessor Programming37 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 …

38 Art of Multiprocessor Programming38 Bus-Based Architectures Bus cache memory cache

39 Art of Multiprocessor Programming39 Bus-Based Architectures Bus cache memory cache Random access memory (10s of cycles)

40 Art of Multiprocessor Programming40 Bus-Based Architectures cache memory cache Shared Bus Broadcast medium One broadcaster at a time Processors and memory all “snoop” Bus

41 Art of Multiprocessor Programming41 Bus-Based Architectures Bus cache memory cache Per-Processor Caches Small Fast: 1 or 2 cycles Address & state information

42 Art of Multiprocessor Programming42 Jargon Watch Cache hit –“I found what I wanted in my cache” –Good Thing™

43 Art of Multiprocessor Programming43 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™

44 Art of Multiprocessor Programming44 Cave Canem This model is still a simplification –But not in any essential way –Illustrates basic principles Will discuss complexities later

45 Art of Multiprocessor Programming45 Bus Processor Issues Load Request cache memory cache data

46 Art of Multiprocessor Programming46 Bus Processor Issues Load Request Bus cache memory cache data Gimme data

47 Art of Multiprocessor Programming47 cache Bus Memory Responds Bus memory cache data Got your data right here data

48 Art of Multiprocessor Programming48 Bus Processor Issues Load Request memory cache data Gimme data

49 Art of Multiprocessor Programming49 Bus Processor Issues Load Request Bus memory cache data Gimme data

50 Art of Multiprocessor Programming50 Bus Processor Issues Load Request Bus memory cache data I got data

51 Art of Multiprocessor Programming51 Bus Other Processor Responds memory cache data I got data data Bus

52 Art of Multiprocessor Programming52 Bus Other Processor Responds memory cache data Bus

53 Art of Multiprocessor Programming53 Modify Cached Data Bus data memory cachedata (1)

54 Art of Multiprocessor Programming54 Modify Cached Data Bus data memory cachedata (1)

55 Art of Multiprocessor Programming55 memory Bus data Modify Cached Data cachedata

56 Art of Multiprocessor Programming56 memory Bus data Modify Cached Data cache What’s up with the other copies? data

57 Art of Multiprocessor Programming57 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?

58 Art of Multiprocessor Programming58 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 …

59 Art of Multiprocessor Programming59 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

60 Art of Multiprocessor Programming60 Bus Invalidate memory cachedata

61 Art of Multiprocessor Programming61 Bus Invalidate Bus memory cachedata Mine, all mine!

62 Art of Multiprocessor Programming62 Bus Invalidate Bus memory cachedata cache Uh,oh

63 Art of Multiprocessor Programming63 cache Bus Invalidate memory cachedata Other caches lose read permission

64 Art of Multiprocessor Programming64 cache Bus Invalidate memory cachedata Other caches lose read permission This cache acquires write permission

65 Art of Multiprocessor Programming65 cache Bus Invalidate memory cachedata Memory provides data only if not present in any cache, so no need to change it now (expensive) (2)

66 Art of Multiprocessor Programming66 cache Bus Another Processor Asks for Data memory cachedata (2) Bus

67 Art of Multiprocessor Programming67 cache data Bus Owner Responds memory cachedata (2) Bus Here it is!

68 Art of Multiprocessor Programming68 Bus End of the Day … memory cachedata (1) Reading OK, no writing data

69 Art of Multiprocessor Programming69 Mutual Exclusion What do we want to optimize? –Bus bandwidth used by spinning threads –Release/Acquire latency –Acquire latency for idle lock

70 Art of Multiprocessor Programming70 Simple TASLock TAS invalidates cache lines Spinners –Miss in cache –Go to bus Thread wants to release lock –delayed behind spinners

71 Art of Multiprocessor Programming71 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 …

72 Art of Multiprocessor Programming72 Local Spinning while Lock is Busy Bus memory busy

73 Art of Multiprocessor Programming73 Bus On Release memory freeinvalid free

74 Art of Multiprocessor Programming74 On Release Bus memory freeinvalid free miss Everyone misses, rereads (1)

75 Art of Multiprocessor Programming75 On Release Bus memory freeinvalid free TAS(…) Everyone tries TAS (1)

76 Art of Multiprocessor Programming76 Problems Everyone misses –Reads satisfied sequentially Everyone does TAS –Invalidates others’ caches Eventually quiesces after lock acquired –How long does this take?

77 Art of Multiprocessor Programming77 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.

78 Art of Multiprocessor Programming78 Quiescence Time Increses linearly with the number of processors for bus architecture time threads

79 Art of Multiprocessor Programming79 Mystery Explained TAS lock TTAS lock Ideal time threads Better than TAS but still not as good as ideal

80 Art of Multiprocessor Programming80 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

81 Art of Multiprocessor Programming81 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

82 Art of Multiprocessor Programming82 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; }}}

83 Art of Multiprocessor Programming83 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

84 Art of Multiprocessor Programming84 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

85 Art of Multiprocessor Programming85 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

86 Art of Multiprocessor Programming86 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

87 Art of Multiprocessor Programming87 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

88 Art of Multiprocessor Programming88 Spin-Waiting Overhead TTAS Lock Backoff lock time threads

89 Art of Multiprocessor Programming89 Backoff: Other Issues Good –Easy to implement –Beats TTAS lock Bad –Must choose parameters carefully –Not portable across platforms

90 Art of Multiprocessor Programming90 Idea Avoid useless invalidations –By keeping a queue of threads Each thread –Notifies next in line –Without bothering the others

91 Art of Multiprocessor Programming91 Anderson Queue Lock flags next TFFFFFFF idle

92 Art of Multiprocessor Programming92 Anderson Queue Lock flags next TFFFFFFF acquiring getAndIncrement

93 Art of Multiprocessor Programming93 Anderson Queue Lock flags next TFFFFFFF acquiring getAndIncrement

94 Art of Multiprocessor Programming94 Anderson Queue Lock flags next TFFFFFFF acquired Mine!

95 Art of Multiprocessor Programming95 Anderson Queue Lock flags next TFFFFFFF acquired acquiring

96 Art of Multiprocessor Programming96 Anderson Queue Lock flags next TFFFFFFF acquired acquiring getAndIncrement

97 Art of Multiprocessor Programming97 Anderson Queue Lock flags next TFFFFFFF acquired acquiring getAndIncrement

98 Art of Multiprocessor Programming98 acquired Anderson Queue Lock flags next TFFFFFFF acquiring

99 Art of Multiprocessor Programming99 released Anderson Queue Lock flags next TTFFFFFF acquired

100 Art of Multiprocessor Programming100 released Anderson Queue Lock flags next TTFFFFFF acquired Yow!

101 Art of Multiprocessor Programming101 Anderson Queue Lock class ALock implements Lock { boolean[] flags={true,false,…,false}; AtomicInteger next = new AtomicInteger(0); int[] slot = new int[n];

102 Art of Multiprocessor Programming102 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

103 Art of Multiprocessor Programming103 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

104 Art of Multiprocessor Programming104 Anderson Queue Lock class ALock implements Lock { boolean[] flags={true,false,…,false}; AtomicInteger next = new AtomicInteger(0); ThreadLocal mySlot; Thread-local variable

105 Art of Multiprocessor Programming105 Anderson Queue Lock public lock() { mySlot = next.getAndIncrement(); while (!flags[mySlot % n]) {}; flags[mySlot % n] = false; } public unlock() { flags[(mySlot+1) % n] = true; }

106 Art of Multiprocessor Programming106 Anderson Queue Lock public lock() { mySlot = next.getAndIncrement(); while (!flags[mySlot % n]) {}; flags[mySlot % n] = false; } public unlock() { flags[(mySlot+1) % n] = true; } Take next slot

107 Art of Multiprocessor Programming107 Anderson Queue Lock public lock() { mySlot = next.getAndIncrement(); while (!flags[mySlot % n]) {}; flags[mySlot % n] = false; } public unlock() { flags[(mySlot+1) % n] = true; } Spin until told to go

108 Art of Multiprocessor Programming108 Anderson Queue Lock public lock() { myslot = next.getAndIncrement(); while (!flags[myslot % n]) {}; flags[myslot % n] = false; } public unlock() { flags[(myslot+1) % n] = true; } Prepare slot for re-use

109 Art of Multiprocessor Programming109 Anderson Queue Lock public lock() { mySlot = next.getAndIncrement(); while (!flags[mySlot % n]) {}; flags[mySlot % n] = false; } public unlock() { flags[(mySlot+1) % n] = true; } Tell next thread to go

110 Art of Multiprocessor Programming110 Performance Shorter handover than backoff Curve is practically flat Scalable performance FIFO fairness queue TTAS

111 Art of Multiprocessor Programming111 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?

112 Art of Multiprocessor Programming112 CLH Lock FIFO order Small, constant-size overhead per thread

113 Art of Multiprocessor Programming113 Initially false tail idle

114 Art of Multiprocessor Programming114 Initially false tail idle Queue tail

115 Art of Multiprocessor Programming115 Initially false tail idle Lock is free

116 Art of Multiprocessor Programming116 Initially false tail idle

117 Art of Multiprocessor Programming117 Purple Wants the Lock false tail acquiring

118 Art of Multiprocessor Programming118 Purple Wants the Lock false tail acquiring true

119 Art of Multiprocessor Programming119 Purple Wants the Lock false tail acquiring true Swap

120 Art of Multiprocessor Programming120 Purple Has the Lock false tail acquired true

121 Art of Multiprocessor Programming121 Red Wants the Lock false tail acquired acquiring true

122 Art of Multiprocessor Programming122 Red Wants the Lock false tail acquired acquiring true Swap true

123 Art of Multiprocessor Programming123 Red Wants the Lock false tail acquired acquiring true

124 Art of Multiprocessor Programming124 Red Wants the Lock false tail acquired acquiring true

125 Art of Multiprocessor Programming125 Red Wants the Lock false tail acquired acquiring true Implicitely Linked list

126 Art of Multiprocessor Programming126 Red Wants the Lock false tail acquired acquiring true

127 Art of Multiprocessor Programming127 Red Wants the Lock false tail acquired acquiring true Actually, it spins on cached copy

128 Art of Multiprocessor Programming128 Purple Releases false tail release acquiring false true false Bingo!

129 Art of Multiprocessor Programming129 Purple Releases tail released acquired true

130 Art of Multiprocessor Programming130 Space Usage Let –L = number of locks –N = number of threads ALock –O(LN) CLH lock –O(L+N)

131 Art of Multiprocessor Programming131 CLH Queue Lock class Qnode { AtomicBoolean locked = new AtomicBoolean(true); }

132 Art of Multiprocessor Programming132 CLH Queue Lock class Qnode { AtomicBoolean locked = new AtomicBoolean(true); } Not released yet

133 Art of Multiprocessor Programming133 CLH Queue Lock class CLHLock implements Lock { AtomicReference tail; ThreadLocal myNode = new Qnode(); public void lock() { Qnode pred = tail.getAndSet(myNode); while (pred.locked) {} }} (3)

134 Art of Multiprocessor Programming134 CLH Queue Lock class CLHLock implements Lock { AtomicReference tail; ThreadLocal myNode = new Qnode(); public void lock() { Qnode pred = tail.getAndSet(myNode); while (pred.locked) {} }} (3) Tail of the queue

135 Art of Multiprocessor Programming135 CLH Queue Lock class CLHLock implements Lock { AtomicReference tail; ThreadLocal myNode = new Qnode(); public void lock() { Qnode pred = tail.getAndSet(myNode); while (pred.locked) {} }} (3) Thread-local Qnode

136 Art of Multiprocessor Programming136 CLH Queue Lock class CLHLock implements Lock { AtomicReference tail; ThreadLocal myNode = new Qnode(); public void lock() { Qnode pred = tail.getAndSet(myNode); while (pred.locked) {} }} (3) Swap in my node

137 Art of Multiprocessor Programming137 CLH Queue Lock class CLHLock implements Lock { AtomicReference tail; ThreadLocal myNode = new Qnode(); public void lock() { Qnode pred = tail.getAndSet(myNode); while (pred.locked) {} }} (3) Spin until predecessor releases lock

138 Art of Multiprocessor Programming138 CLH Queue Lock Class CLHLock implements Lock { … public void unlock() { myNode.locked.set(false); myNode = pred; } (3)

139 Art of Multiprocessor Programming139 CLH Queue Lock Class CLHLock implements Lock { … public void unlock() { myNode.locked.set(false); myNode = pred; } (3) Notify successor

140 Art of Multiprocessor Programming140 CLH Queue Lock Class CLHLock implements Lock { … public void unlock() { myNode.locked.set(false); myNode = pred; } (3) Recycle predecessor’s node

141 Art of Multiprocessor Programming141 CLH Queue Lock Class CLHLock implements Lock { … public void unlock() { myNode.locked.set(false); myNode = pred; } (3) (notice that we actually don’t reuse myNode. Code in book shows how its done.)

142 Art of Multiprocessor Programming142 CLH Lock Good –Lock release affects predecessor only –Small, constant-sized space Bad –Doesn’t work for uncached NUMA architectures

143 Art of Multiprocessor Programming143 NUMA Architecturs Acronym: –Non-Uniform Memory Architecture Illusion: –Flat shared memory Truth: –No caches (sometimes) –Some memory regions faster than others

144 Art of Multiprocessor Programming144 NUMA Machines Spinning on local memory is fast

145 Art of Multiprocessor Programming145 NUMA Machines Spinning on remote memory is slow

146 Art of Multiprocessor Programming146 CLH Lock Each thread spin’s on predecessor’s memory Could be far away …

147 Art of Multiprocessor Programming147 MCS Lock FIFO order Spin on local memory only Small, Constant-size overhead

148 Art of Multiprocessor Programming148 Initially false tail false idle

149 Art of Multiprocessor Programming149 Acquiring false queue false true acquiring (allocate Qnode)

150 Art of Multiprocessor Programming150 Acquiring false tail false true acquired swap

151 Art of Multiprocessor Programming151 Acquiring false tail false true acquired

152 Art of Multiprocessor Programming152 Acquired false tail false true acquired

153 Art of Multiprocessor Programming153 Acquiring tail false acquired acquiring true swap

154 Art of Multiprocessor Programming154 Acquiring tail acquired acquiring true false

155 Art of Multiprocessor Programming155 Acquiring tail acquired acquiring true false

156 Art of Multiprocessor Programming156 Acquiring tail acquired acquiring true false

157 Art of Multiprocessor Programming157 Acquiring tail acquired acquiring true false

158 Art of Multiprocessor Programming158 Acquiring tail acquired acquiring true Yes! false

159 Art of Multiprocessor Programming159 MCS Queue Lock class Qnode { boolean locked = false; qnode next = null; }

160 Art of Multiprocessor Programming160 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)

161 Art of Multiprocessor Programming161 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

162 Art of Multiprocessor Programming162 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

163 Art of Multiprocessor Programming163 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

164 Art of Multiprocessor Programming164 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

165 Art of Multiprocessor Programming165 MCS Queue Unlock class MCSLock implements Lock { AtomicReference tail; public void unlock() { if (qnode.next == null) { if (tail.CAS(qnode, null) return; while (qnode.next == null) {} } qnode.next.locked = false; }} (3)

166 Art of Multiprocessor Programming166 MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void unlock() { if (qnode.next == null) { if (tail.CAS(qnode, null) return; while (qnode.next == null) {} } qnode.next.locked = false; }} (3) Missing successor?

167 Art of Multiprocessor Programming167 MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void unlock() { if (qnode.next == null) { if (tail.CAS(qnode, null) return; while (qnode.next == null) {} } qnode.next.locked = false; }} (3) If really no successor, return

168 Art of Multiprocessor Programming168 MCS Queue Lock class MCSLock implements Lock { AtomicReference tail; public void unlock() { if (qnode.next == null) { if (tail.CAS(qnode, null) return; while (qnode.next == null) {} } qnode.next.locked = false; }} (3) Otherwise wait for successor to catch up

169 Art of Multiprocessor Programming169 MCS Queue Lock class MCSLock implements Lock { AtomicReference queue; public void unlock() { if (qnode.next == null) { if (tail.CAS(qnode, null) return; while (qnode.next == null) {} } qnode.next.locked = false; }} (3) Pass lock to successor

170 Art of Multiprocessor Programming170 Purple Release false releasing swap false (2)

171 Art of Multiprocessor Programming171 Purple Release false releasing swap false By looking at the queue, I see another thread is active (2)

172 Art of Multiprocessor Programming172 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)

173 Art of Multiprocessor Programming173 Purple Release false releasing prepare to spin true

174 Art of Multiprocessor Programming174 Purple Release false releasing spinning true

175 Art of Multiprocessor Programming175 Purple Release false releasing spinning true false

176 Art of Multiprocessor Programming176 Purple Release false releasing true Acquired lock false

177 Art of Multiprocessor Programming177 Abortable Locks What if you want to give up waiting for a lock? For example –Timeout –Database transaction aborted by user

178 Art of Multiprocessor Programming178 Back-off Lock Aborting is trivial –Just return from lock() call Extra benefit: –No cleaning up –Wait-free –Immediate return

179 Art of Multiprocessor Programming179 Queue Locks Can’t just quit –Thread in line behind will starve Need a graceful way out

180 Art of Multiprocessor Programming180 Queue Locks spinning true spinning true spinning

181 Art of Multiprocessor Programming181 Queue Locks spinning true spinning true false locked

182 Art of Multiprocessor Programming182 Queue Locks spinning true locked false

183 Art of Multiprocessor Programming183 Queue Locks locked false

184 Art of Multiprocessor Programming184 Queue Locks spinning true spinning true spinning

185 Art of Multiprocessor Programming185 Queue Locks spinning true spinning

186 Art of Multiprocessor Programming186 Queue Locks spinning true false locked

187 Art of Multiprocessor Programming187 Queue Locks spinning true false

188 Art of Multiprocessor Programming188 Queue Locks pwned true false

189 Art of Multiprocessor Programming189 Abortable CLH Lock When a thread gives up –Removing node in a wait-free way is hard Idea: –let successor deal with it.

190 Art of Multiprocessor Programming190 Initially tail idle Pointer to predecessor (or null) A

191 Art of Multiprocessor Programming191 Initially tail idle Distinguished available node means lock is free A

192 Art of Multiprocessor Programming192 Acquiring tail acquiring A

193 Art of Multiprocessor Programming193 Acquiring acquiring A Null predecessor means lock not released or aborted

194 Art of Multiprocessor Programming194 Acquiring acquiring A Swap

195 Art of Multiprocessor Programming195 Acquiring acquiring A

196 Art of Multiprocessor Programming196 Acquired locked A Pointer to AVAILABLE means lock is free.

197 spinning locked Art of Multiprocessor Programming197 Normal Case Null means lock is not free & request not aborted

198 Art of Multiprocessor Programming198 One Thread Aborts spinning Timed out locked

199 Art of Multiprocessor Programming199 Successor Notices spinning Timed out locked Non-Null means predecessor aborted

200 Art of Multiprocessor Programming200 Recycle Predecessor’s Node spinning locked

201 Art of Multiprocessor Programming201 Spin on Earlier Node spinning locked

202 Art of Multiprocessor Programming202 Spin on Earlier Node spinning released A The lock is now mine

203 Art of Multiprocessor Programming203 Time-out Lock public class TOLock implements Lock { static Qnode AVAILABLE = new Qnode(); AtomicReference tail; ThreadLocal myNode;

204 Art of Multiprocessor Programming204 Time-out Lock public class TOLock implements Lock { static Qnode AVAILABLE = new Qnode(); AtomicReference tail; ThreadLocal myNode; Distinguished node to signify free lock

205 Art of Multiprocessor Programming205 Time-out Lock public class TOLock implements Lock { static Qnode AVAILABLE = new Qnode(); AtomicReference tail; ThreadLocal myNode; Tail of the queue

206 Art of Multiprocessor Programming206 Time-out Lock public class TOLock implements Lock { static Qnode AVAILABLE = new Qnode(); AtomicReference tail; ThreadLocal myNode; Remember my node …

207 Art of Multiprocessor Programming207 Time-out Lock public boolean lock(long timeout) { Qnode qnode = new Qnode(); myNode.set(qnode); qnode.prev = null; Qnode myPred = tail.getAndSet(qnode); if (myPred== null || myPred.prev == AVAILABLE) { return true; } …

208 Art of Multiprocessor Programming208 Time-out Lock public boolean lock(long timeout) { Qnode qnode = new Qnode(); myNode.set(qnode); qnode.prev = null; Qnode myPred = tail.getAndSet(qnode); if (myPred == null || myPred.prev == AVAILABLE) { return true; } Create & initialize node

209 Art of Multiprocessor Programming209 Time-out Lock public boolean lock(long timeout) { Qnode qnode = new Qnode(); myNode.set(qnode); qnode.prev = null; Qnode myPred = tail.getAndSet(qnode); if (myPred == null || myPred.prev == AVAILABLE) { return true; } Swap with tail

210 Art of Multiprocessor Programming210 Time-out Lock public boolean lock(long timeout) { Qnode qnode = new Qnode(); myNode.set(qnode); qnode.prev = null; Qnode myPred = tail.getAndSet(qnode); if (myPred == null || myPred.prev == AVAILABLE) { return true; }... If predecessor absent or released, we are done

211 Art of Multiprocessor Programming211 Time-out Lock … long start = now(); while (now()- start < timeout) { Qnode predPred = myPred.prev; if (predPred == AVAILABLE) { return true; } else if (predPred != null) { myPred = predPred; } … spinning locked

212 Art of Multiprocessor Programming212 Time-out Lock … long start = now(); while (now()- start < timeout) { Qnode predPred = myPred.prev; if (predPred == AVAILABLE) { return true; } else if (predPred != null) { myPred = predPred; } … Keep trying for a while …

213 Art of Multiprocessor Programming213 Time-out Lock … long start = now(); while (now()- start < timeout) { Qnode predPred = myPred.prev; if (predPred == AVAILABLE) { return true; } else if (predPred != null) { myPred = predPred; } … Spin on predecessor’s prev field

214 Art of Multiprocessor Programming214 Time-out Lock … long start = now(); while (now()- start < timeout) { Qnode predPred = myPred.prev; if (predPred == AVAILABLE) { return true; } else if (predPred != null) { myPred = predPred; } … Predecessor released lock

215 Art of Multiprocessor Programming215 Time-out Lock … long start = now(); while (now()- start < timeout) { Qnode predPred = myPred.prev; if (predPred == AVAILABLE) { return true; } else if (predPred != null) { myPred = predPred; } … Predecessor aborted, advance one

216 Art of Multiprocessor Programming216 Time-out Lock … if (!tail.compareAndSet(qnode, myPred)) qnode.prev = myPred; return false; } What do I do when I time out?

217 Art of Multiprocessor Programming217 Time-out Lock … if (!tail.compareAndSet(qnode, myPred)) qnode.prev = myPred; return false; } Do I have a successor? If CAS fails: I do have a successor, tell it about myPred

218 Art of Multiprocessor Programming218 Time-out Lock … if (!tail.compareAndSet(qnode, myPred)) qnode.prev = myPred; return false; } If CAS succeeds: no successor, simply return false

219 Art of Multiprocessor Programming219 Time-Out Unlock public void unlock() { Qnode qnode = myNode.get(); if (!tail.compareAndSet(qnode, null)) qnode.prev = AVAILABLE; }

220 Art of Multiprocessor Programming220 public void unlock() { Qnode qnode = myNode.get(); if (!tail.compareAndSet(qnode, null)) qnode.prev = AVAILABLE; } Time-out Unlock If CAS failed: exists successor, notify successor it can enter

221 Art of Multiprocessor Programming221 public void unlock() { Qnode qnode = myNode.get(); if (!tail.compareAndSet(qnode, null)) qnode.prev = AVAILABLE; } Timing-out Lock CAS successful: set tail to null, no clean up since no successor waiting

222 Art of Multiprocessor Programming222 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

223 Art of Multiprocessor Programming223 This work is licensed under a Creative Commons Attribution- ShareAlike 2.5 License.Creative Commons Attribution- ShareAlike 2.5 License You are free: –to Share — to copy, distribute and transmit the work –to Remix — to adapt the work Under the following conditions: –Attribution. You must attribute the work to “The Art of Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work). –Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to –http://creativecommons.org/licenses/by-sa/3.0/. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights.


Download ppt "Spin Locks and Contention Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit."

Similar presentations


Ads by Google