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 Performance, not just correctness Proper use of multiprocessor architectures A collection of locking algorithms… (1)

8 Art of Multiprocessor Programming8 What Should you do if you cant 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 cant 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... Seq Bottleneck no parallelism …lock suffers from contention

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 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 arent (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 Whats 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 cant 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)

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

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

68 Art of Multiprocessor Programming68 Bus End of the Day … memory cachedata 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 Measuring Quiescence Time Acquire lock Pause without using bus Use bus heavily Art of Multiprocessor Programming77 P1P1 P2P2 PnPn If pause > quiescence time, critical section duration independent of number of threads If pause < quiescence time, critical section duration slower with more threads

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 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); ThreadLocal mySlot;

102 Art of Multiprocessor Programming102 Anderson Queue Lock class ALock implements Lock { boolean[] flags={true,false,…,false}; AtomicInteger next = new AtomicInteger(0); ThreadLocal mySlot; 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); ThreadLocal mySlot; 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 released Local Spinning flags next TFFFFFFF acquired Spin on my bit Unfortunately many bits share cache line

111 111 released False Sharing flags next TFFFFFFF acquired Spin on my bit Line 1 Line 2 Spinning thread gets cache invalidation on account of store by threads it is not waiting for Result: contention Art of Multiprocessor Programming

112 112 released The Solution: Padding flags next T///F/// acquired Line 1 Line 2 Art of Multiprocessor Programming Spin on my line

113 Art of Multiprocessor Programming113 Performance Shorter handover than backoff Curve is practically flat Scalable performance queue TTAS

114 Art of Multiprocessor Programming114 Anderson Queue Lock Good –First truly scalable lock –Simple, easy to implement –Back to FIFO order (like Bakery)

115 Art of Multiprocessor Programming115 Anderson Queue Lock Bad –Space hog… –One bit per thread one cache line per thread What if unknown number of threads? What if small number of actual contenders?

116 Art of Multiprocessor Programming116 CLH Lock FIFO order Small, constant-size overhead per thread

117 Art of Multiprocessor Programming117 Initially false tail idle

118 Art of Multiprocessor Programming118 Initially false tail idle Queue tail

119 Art of Multiprocessor Programming119 Initially false tail idle Lock is free

120 Art of Multiprocessor Programming120 Initially false tail idle

121 Art of Multiprocessor Programming121 Purple Wants the Lock false tail acquiring

122 Art of Multiprocessor Programming122 Purple Wants the Lock false tail acquiring true

123 Art of Multiprocessor Programming123 Purple Wants the Lock false tail acquiring true Swap

124 Art of Multiprocessor Programming124 Purple Has the Lock false tail acquired true

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

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

127 Art of Multiprocessor Programming127 Red Wants the Lock false tail acquired acquiring true

128 Art of Multiprocessor Programming128 Red Wants the Lock false tail acquired acquiring true

129 Art of Multiprocessor Programming129 Red Wants the Lock false tail acquired acquiring true Implicit Linked list

130 Art of Multiprocessor Programming130 Red Wants the Lock false tail acquired acquiring true

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

132 Art of Multiprocessor Programming132 Purple Releases false tail release acquiring false true false Bingo!

133 Art of Multiprocessor Programming133 Purple Releases tail released acquired true

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

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

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

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) {} }}

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

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

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

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

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

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

144 Art of Multiprocessor Programming144 CLH Queue Lock Class CLHLock implements Lock { … public void unlock() { myNode.locked.set(false); myNode = pred; } Recycle predecessors node

145 Art of Multiprocessor Programming145 CLH Queue Lock Class CLHLock implements Lock { … public void unlock() { myNode.locked.set(false); myNode = pred; } (we dont actually reuse myNode. Code in book shows how its done.)

146 Art of Multiprocessor Programming146 CLH Lock Good –Lock release affects predecessor only –Small, constant-sized space Bad –Doesnt work for uncached NUMA architectures

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

148 Art of Multiprocessor Programming148 NUMA Machines Spinning on local memory is fast

149 Art of Multiprocessor Programming149 NUMA Machines Spinning on remote memory is slow

150 Art of Multiprocessor Programming150 CLH Lock Each thread spins on predecessors memory Could be far away …

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

152 Art of Multiprocessor Programming152 Initially false idle tail

153 Art of Multiprocessor Programming153 Acquiring false true acquiring (allocate Qnode) tail

154 Art of Multiprocessor Programming154 Acquiring false tail false true acquired swap

155 Art of Multiprocessor Programming155 Acquiring false tail false true acquired

156 Art of Multiprocessor Programming156 Acquired false tail false true acquired

157 Art of Multiprocessor Programming157 Acquiring tail false acquired acquiring true swap

158 Art of Multiprocessor Programming158 Acquiring tail acquired acquiring true false

159 Art of Multiprocessor Programming159 Acquiring tail acquired acquiring true false

160 Art of Multiprocessor Programming160 Acquiring tail acquired acquiring true false

161 Art of Multiprocessor Programming161 Acquiring tail acquired acquiring true false

162 Art of Multiprocessor Programming162 Acquiring tail acquired acquiring true Yes! false

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

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) {} }}}

165 Art of Multiprocessor Programming165 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) {} }}} Make a QNode

166 Art of Multiprocessor Programming166 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) {} }}} add my Node to the tail of queue

167 Art of Multiprocessor Programming167 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) {} }}} Fix if queue was non-empty

168 Art of Multiprocessor Programming168 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) {} }}} Wait until unlocked

169 Art of Multiprocessor Programming169 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; }}

170 Art of Multiprocessor Programming170 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; }} Missing successor ?

171 Art of Multiprocessor Programming171 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; }} If really no successor, return

172 Art of Multiprocessor Programming172 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; }} Otherwise wait for successor to catch up

173 Art of Multiprocessor Programming173 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; }} Pass lock to successor

174 Art of Multiprocessor Programming174 Purple Release false releasing swap false

175 Art of Multiprocessor Programming175 Purple Release false releasing swap false By looking at the queue, I see another thread is active

176 Art of Multiprocessor Programming176 Purple Release false releasing swap false I have to wait for that thread to finish By looking at the queue, I see another thread is active

177 Art of Multiprocessor Programming177 Purple Release false releasing prepare to spin true

178 Art of Multiprocessor Programming178 Purple Release false releasing spinning true

179 Art of Multiprocessor Programming179 Purple Release false releasing spinning true false

180 Art of Multiprocessor Programming180 Purple Release false releasing true Acquired lock false

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

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

183 Art of Multiprocessor Programming183 Queue Locks Cant just quit –Thread in line behind will starve Need a graceful way out

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

185 Art of Multiprocessor Programming185 Queue Locks spinning true spinning true false locked

186 Art of Multiprocessor Programming186 Queue Locks spinning true locked false

187 Art of Multiprocessor Programming187 Queue Locks locked false

188 Art of Multiprocessor Programming188 Queue Locks spinning true spinning true spinning

189 Art of Multiprocessor Programming189 Queue Locks spinning true spinning

190 Art of Multiprocessor Programming190 Queue Locks spinning true false locked

191 Art of Multiprocessor Programming191 Queue Locks spinning true false

192 Art of Multiprocessor Programming192 Queue Locks pwned true false

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

194 Art of Multiprocessor Programming194 Initially tail idle Pointer to predecessor (or null) A

195 Art of Multiprocessor Programming195 Initially tail idle Distinguished available node means lock is free A

196 Art of Multiprocessor Programming196 Acquiring tail acquiring A

197 Art of Multiprocessor Programming197 Acquiring acquiring A Null predecessor means lock not released or aborted

198 Art of Multiprocessor Programming198 Acquiring acquiring A Swap

199 Art of Multiprocessor Programming199 Acquiring acquiring A

200 Art of Multiprocessor Programming200 Acquired locked A Reference to AVAILABLE means lock is free.

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

202 Art of Multiprocessor Programming202 One Thread Aborts spinning Timed out locked

203 Art of Multiprocessor Programming203 Successor Notices spinning Timed out locked Non-Null means predecessor aborted

204 Art of Multiprocessor Programming204 Recycle Predecessors Node spinning locked

205 Art of Multiprocessor Programming205 Spin on Earlier Node spinning locked

206 Art of Multiprocessor Programming206 Spin on Earlier Node spinning released A The lock is now mine

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

208 Art of Multiprocessor Programming208 Time-out Lock public class TOLock implements Lock { static Qnode AVAILABLE = new Qnode(); AtomicReference tail; ThreadLocal myNode; AVAILABLE node signifies free lock

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

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

211 Art of Multiprocessor Programming211 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; } …

212 Art of Multiprocessor Programming212 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

213 Art of Multiprocessor Programming213 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

214 Art of Multiprocessor Programming214 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

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; } … spinning locked

216 Art of Multiprocessor Programming216 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 …

217 Art of Multiprocessor Programming217 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 predecessors prev field

218 Art of Multiprocessor Programming218 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

219 Art of Multiprocessor Programming219 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

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

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

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

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

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

225 Art of Multiprocessor Programming225 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

226 Art of Multiprocessor Programming226 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

227 Art of Multiprocessor Programming227 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