Download presentation
Presentation is loading. Please wait.
Published byHarriet Poole Modified over 9 years ago
1
The Relative Power of Synchronization Operations The Art of Multiprocessor Programming Spring 2007
2
© 2005 Herlihy and Shavit2 Last Lecture: Shared-Memory Computability Mathematical model of concurrent computation What is (and is not) concurrently computable Efficiency (mostly) irrelevant 10011 Shared Memory
3
© 2005 Herlihy and Shavit3 Wait-Free Implementation Every method call completes in finite number of steps Implies no mutual exclusion (2)
4
© 2005 Herlihy and Shavit4 From Weakest Register 1 0 1 1 Single readerSingle writer Safe Boolean register
5
© 2005 Herlihy and Shavit5 Construct Atomic Snapshots MRMW MRSW SRSW Safe Regular Atomic M-valued Boolean Snapshot FIFO Queue?
6
© 2005 Herlihy and Shavit6 Rationale for wait-freedom We wanted atomic registers to implement mutual exclusion So we couldn’t use mutual exclusion to implement atomic registers But wait, there’s more!
7
Why is Mutual Exclusion so wrong? (2)
8
© 2005 Herlihy and Shavit8 Asynchronous Interrupts Swapped out back at ??? (2)
9
© 2005 Herlihy and Shavit9 Heterogeneous Processors ??? yawn (1) Pentium 286
10
© 2005 Herlihy and Shavit10 Fault-tolerance ??? (2)
11
© 2005 Herlihy and Shavit11 Machine Level Instruction Granularity Eugene Amdahl (2) Wait-free implementations provide word level granularity
12
© 2005 Herlihy and Shavit12 Basic Questions Wait-Free synchronization might be a good idea in principle But how do you do it –Systematically? –Correctly? –Efficiently?
13
© 2005 Herlihy and Shavit13 Today: Focus on Wait-free The rest of today’s discussion will focus on wait-free implementations But the results we present apply almost verbatim to lock-free ones
14
© 2005 Herlihy and Shavit14 FIFO Queue: Enqueue Method q.enq ( )
15
© 2005 Herlihy and Shavit15 FIFO Queue: Dequeue Method q.deq()/
16
© 2005 Herlihy and Shavit16 Two-Thread Wait-Free Queue public class LockFreeQueue { int head = 0, tail = 0; Item[QSIZE] items; public void enq(Item x) { while (tail - head == QSIZE) {}; items[tail % QSIZE] = x; tail++; } public Item deq() { while (tail - head == 0) {} Item item = items[head % QSIZE]; head++; return item; }}
17
© 2005 Herlihy and Shavit17 Two-Thread Wait-Free Queue public class LockFreeQueue { int head = 0, tail = 0; Item[QSIZE] items; public void enq(Item x) { while (tail-head == QSIZE) {}; items[tail % QSIZE] = x; tail++; } public Item deq() { while (tail-head == 0) {} Item item = items[head % QSIZE]; head++; return item; }} Put object in quue
18
© 2005 Herlihy and Shavit18 Two-Thread Wait-Free Queue public class LockFreeQueue { int head = 0, tail = 0; Item[QSIZE] items; public void enq(Item x) { while (tail-head == QSIZE) {}; items[tail % QSIZE] = x; tail++; } public Item deq() { while (tail-head == 0) {} Item item = items[head % QSIZE]; head++; return item; }} Increment tail counter
19
© 2005 Herlihy and Shavit19 What About Multiple Dequeuers?
20
© 2005 Herlihy and Shavit20 Grand Challenge Implement a FIFO queue –Wait-free –Linearizable –From atomic read-write registers –Multiple dequeuers Only new aspect (1)
21
© 2005 Herlihy and Shavit21 Consensus While you are ruminating on the grand challenge… We will give you another puzzle –Consensus –Will be important …
22
© 2005 Herlihy and Shavit22 Consensus: Each Thread has a Private Input 32 19 21
23
© 2005 Herlihy and Shavit23 They Communicate
24
© 2005 Herlihy and Shavit24 They Agree on One Thread’s Input 19
25
© 2005 Herlihy and Shavit25 Formally: Consensus Consistent: all threads decide the same value Valid: the common decision value is some thread's input Wait-free: each thread decides after a finite number of steps
26
© 2005 Herlihy and Shavit26 No Wait-Free Consensus using Registers ???
27
© 2005 Herlihy and Shavit27 Formally Theorem [adapted from Fischer, Lynch, Paterson]: There is no wait- free implementation of n-thread consensus, n>1, from read-write registers Implication: asynchronous computability fundamentally different from Turing computability
28
© 2005 Herlihy and Shavit28 Proof Strategy Assume otherwise Reason about the properties of any such protocol Derive a contradiction Quod Erat Demonstrandum Suffices to prove for binary consensus and n=2
29
© 2005 Herlihy and Shavit29 Wait-Free Computation Either A or B “moves” Moving means –Register read –Register write A movesB moves
30
© 2005 Herlihy and Shavit30 The Two-Move Tree Initial state Final states (2)
31
© 2005 Herlihy and Shavit31 Decision Values 10 0111
32
© 2005 Herlihy and Shavit32 Bivalent: Both Possible 111 bivalent 10 0
33
© 2005 Herlihy and Shavit33 Univalent: Single Value Possible 111 univalent 10 0
34
© 2005 Herlihy and Shavit34 x-valent: x Only Possible Decision 0 111 1-valent 0 1
35
© 2005 Herlihy and Shavit35 Summary Wait-free computation is a tree Bivalent system states –Outcome not fixed Univalent states –Outcome is fixed –May not be “known” yet 1-Valent and 0-Valent states
36
© 2005 Herlihy and Shavit36 Claim Some initial state is bivalent Outcome depends on –Chance –Whim of the scheduler Multiprocessor gods do play dice … Lets prove this claim
37
© 2005 Herlihy and Shavit37 Both Inputs 0 Univalent: all executions must decide 0 0 0 (2) (1)
38
© 2005 Herlihy and Shavit38 Both Inputs 0 Including this solo execution by A (1) 0
39
© 2005 Herlihy and Shavit39 Both Inputs 1 All executions must decide 1 1 1 (2) (1)
40
© 2005 Herlihy and Shavit40 Both Inputs 1 Including this solo execution by B (1) 1
41
© 2005 Herlihy and Shavit41 What if inputs differ? (2) 1 0 By Way of contradiction: If univalent all executions must decide on same value
42
© 2005 Herlihy and Shavit42 The Possible Executions 0 1 (2) Include the solo execution by A that decides 0
43
© 2005 Herlihy and Shavit43 The Possible Executions 0 1 (2) Also include the solo execution by B which we know decides 1
44
© 2005 Herlihy and Shavit44 Possible Executions Include Solo execution by A must decide 0 Solo execution by B must decide 1 0 1 How univalent is that? (QED) How univalent is that? (QED)
45
© 2005 Herlihy and Shavit45 0-valent Critical States 1-valent critical (3)
46
© 2005 Herlihy and Shavit46 From a Critical State c If A goes first, protocol decides 0 If B goes first, protocol decides 1 0-valent 1-valent
47
© 2005 Herlihy and Shavit47 Reaching Critical State CACA CACA CBCB c CBCB univalent 0-valent 1-valent initially bivalent
48
© 2005 Herlihy and Shavit48 Critical States Starting from a bivalent initial state The protocol can reach a critical state –Otherwise we could stay bivalent forever –And the protocol is not wait-free
49
© 2005 Herlihy and Shavit49 Model Dependency So far, memory-independent! True for –Registers –Message-passing –Carrier pigeons –Any kind of asynchronous computation
50
© 2005 Herlihy and Shavit50 What are the Threads Doing? Reads and/or writes To same/different registers
51
© 2005 Herlihy and Shavit51 Completing the Proof Lets look at executions that: –Start from a critical state –Threads cause state to become univalent by reading or writing to same/different registers –End within a finite number of steps deciding either 0 or 1 Show this leads to a contradiction
52
© 2005 Herlihy and Shavit52 Possible Interactions x.read()y.read()x.write()y.write() x.read() ???? y.read() ???? x.write() ???? y.write() ???? A reads x A reads y
53
© 2005 Herlihy and Shavit53 Some Thread Reads A runs solo, eventually decides 0 B reads x 1 0 A runs solo, eventually decides 1 c States look the same to A Contradiction
54
© 2005 Herlihy and Shavit54 Possible Interactions x.read()y.read()x.write()y.write() x.read() no y.read() no x.write() no ?? y.write() no ??
55
© 2005 Herlihy and Shavit55 Writing Distinct Registers A writes yB writes x 10 c The song is the same A writes y B writes x Contradiction
56
© 2005 Herlihy and Shavit56 Possible Interactions x.read()y.read()x.write()y.write() x.read() no y.read() no x.write() no ? y.write() no ?
57
© 2005 Herlihy and Shavit57 Writing Same Registers States look the same to A A writes x B writes x 1 A runs solo, eventually decides 1 c 0 A runs solo, eventually decides 0 A writes x Contradiction
58
© 2005 Herlihy and Shavit58 That’s All, Folks! x.read()y.read()x.write()y.write() x.read() no y.read() no x.write() no y.write() no QED
59
© 2005 Herlihy and Shavit59 Recap: Atomic Registers Can’t Do Consensus If protocol exists –It has a bivalent initial state –Leading to a critical state What’s up with the critical state? –Case analysis for each pair of methods –As we showed, all lead to a contradiction
60
© 2005 Herlihy and Shavit60 What Does Consensus have to do with Concurrent Objects?
61
© 2005 Herlihy and Shavit61 Consensus Object public interface Consensus { Object decide(object value); } (4)
62
© 2005 Herlihy and Shavit62 Concurrent Consensus Object We consider only one time objects: each thread can execute a method only once Linearizable to sequential consensus object in which –the thread who’s input was decided on completed its method first
63
© 2005 Herlihy and Shavit63 Java Jargon Watch Define Consensus protocol as an abstract class We implement some methods Leave you to do the rest …
64
© 2005 Herlihy and Shavit64 Generic Consensus Protocol abstract class ConsensusProtocol implements Consensus { protected Object[] proposed = new Object[N]; private void propose(Object value) { proposed[ThreadID.get()] = value; } abstract public Object decide(object value); }} (4)
65
© 2005 Herlihy and Shavit65 abstract class ConsensusProtocol implements Consensus { protected Object[] proposed = new Object[N]; private void propose(Object value) { proposed[ThreadID.get()] = value; } abstract public Object decide(object value); }} Generic Consensus Protocol (4) Each thread’s proposed value
66
© 2005 Herlihy and Shavit66 abstract class ConsensusProtocol implements Consensus { protected Object[] proposed = new Object[N]; private void propose(Object value) { proposed[ThreadID.get()] = value; } abstract public Object decide(object value); }} Generic Consensus Protocol (4) Propose a value
67
© 2005 Herlihy and Shavit67 abstract class ConsensusProtocol implements Consensus { protected Object[] proposed = new Object[N]; private void propose(Object value) { proposed[ThreadID.get()] = value; } abstract public Object decide(object value); }} Generic Consensus Protocol (4) Decide a value: abstract method means subclass does the heavy lifting (real work)
68
Can FIFO Queue Implement Consensus?
69
© 2005 Herlihy and Shavit69 FIFO Consensus announce array FIFO Queue with red and black balls 8 Coveted red ballDreaded black ball
70
© 2005 Herlihy and Shavit70 Protocol: Write Value to Array 01 0
71
© 2005 Herlihy and Shavit71 0 Protocol: Take Next Item from Queue 01 8
72
© 2005 Herlihy and Shavit72 01 Protocol: Take Next Item from Queue I got the coveted red ball, so I will decide my value I got the dreaded black ball, so I will decide the other’s value from the array 8
73
© 2005 Herlihy and Shavit73 public class QueueConsensus extends ConsensusProtocol { private Queue queue; public QueueConsensus() { queue = new Queue(); queue.enq(Ball.RED); queue.enq(Ball.BLACK); } … } Consensus Using FIFO Queue
74
© 2005 Herlihy and Shavit74 public class QueueConsensus extends ConsensusProtocol { private Queue queue; public QueueConsensus() { this.queue = new Queue(); this.queue.enq(Ball.RED); this.queue.enq(Ball.BLACK); } … } Initialize Queue 8
75
© 2005 Herlihy and Shavit75 public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); Ball ball = this.queue.deq(); if (ball == Ball.RED) return proposed[i]; else return proposed[1-i]; } Who Won?
76
© 2005 Herlihy and Shavit76 public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); Ball ball = this.queue.deq(); if (ball == Ball.RED) return proposed[i]; else return proposed[1-ij]; } Who Won? Race to dequeue first queue item
77
© 2005 Herlihy and Shavit77 public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); Ball ball = this.queue.deq(); if (ball == Ball.RED) return proposed[i]; else return proposed[1-i]; } Who Won? i = ThreadID.get(); I win if I was first
78
© 2005 Herlihy and Shavit78 public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); Ball ball = this.queue.deq(); if (ball == Ball.RED) return proposed[i]; else return proposed[1-i]; } Who Won? Other thread wins if I was second
79
© 2005 Herlihy and Shavit79 Why does this Work? If one thread gets the red ball Then the other gets the black ball Winner decides her own value Loser can find winner’s value in array –Because threads write array –Before dequeueing from queue
80
© 2005 Herlihy and Shavit80 Theorem We can solve 2-thread consensus using only –A two-dequeuer queue, and –Some atomic registers
81
© 2005 Herlihy and Shavit81 Implications Given –A consensus protocol from queue and registers Assume there exists –A queue implementation from atomic registers Substitution yields: –A wait-free consensus protocol from atomic registers contradiction (1)
82
© 2005 Herlihy and Shavit82 Corollary It is impossible to implement –a two-dequeuer wait-free FIFO queue –from read/write memory.
83
© 2005 Herlihy and Shavit83 Consensus Numbers An object X has consensus number n –If it can be used to solve n-thread consensus Taking any number of instances of X together with atomic read/write registers and implement n-thread consensus –But not (n+1)-thread consensus
84
© 2005 Herlihy and Shavit84 Consensus Numbers Theorem –Atomic read/write registers have consensus number 1 Theorem –Multi-dequeuer FIFO queues have consensus number at least 2
85
© 2005 Herlihy and Shavit85 Consensus Numbers Measure Synchronization Power Theorem –If you can implement X from Y –And X has consensus number c –Then Y has consensus number at least c
86
© 2005 Herlihy and Shavit86 Synchronization Speed Limit Conversely –If X has consensus number c –And Y has consensus number d < c –Then there is no way to construct a wait-free implementation of X by Y This theorem will be very useful –Unforeseen practical implications! Theoretical Caveat: Certain weird exceptions exist
87
© 2005 Herlihy and Shavit87 Earlier Grand Challenge Snapshot means –Write any array element –Read multiple array elements atomically What about –Write multiple array elements atomically –Scan any array elements Call this problem multiple assignment
88
© 2005 Herlihy and Shavit88 Multiple Assignment Theorem Atomic registers cannot implement multiple assignment Weird or what? –Single write/multiple read OK –Multi write/multiple read impossible (1)
89
© 2005 Herlihy and Shavit89 Proof Strategy If we can write to 2/3 array elements –We can solve 2-consensus –Impossible with atomic registers Therefore –Cannot implement multiple assignment with atomic registers (1)
90
© 2005 Herlihy and Shavit90 Proof Strategy Take a 3-element array –A writes atomically to slots 0 and 1 –B writes atomically to slots 1 and 2 –Any thread can scan any set of locations (1)
91
© 2005 Herlihy and Shavit91 Double Assignment Interface interface Assign2 { public void assign(int i 1, int v 1, int i 2, int v 2 ); public int read(int i); } (4)
92
© 2005 Herlihy and Shavit92 Double Assignment Interface interface Assign2 { public void assign(int i 1, int v 1, int i 2, int v 2 ); public int read(int i); } (4) Atomically assign value[i 1 ]= v 1 value[i 2 ]= v 2
93
© 2005 Herlihy and Shavit93 Double Assignment Interface interface Assign2 { public void assign(int i 1, int v 1, int i 2, int v 2 ); public int read(int i); } (4) Return i-th value
94
© 2005 Herlihy and Shavit94 Initially Writes to 0 and 1 Writes to 1 and 2 A B
95
© 2005 Herlihy and Shavit95 Thread A wins if A B (1) Thread B didn’t move
96
© 2005 Herlihy and Shavit96 Thread A wins if A B (1) Thread B moved later
97
© 2005 Herlihy and Shavit97 Thread A loses if A B (1) Thread B moved earlier
98
© 2005 Herlihy and Shavit98 Multi-Consensus Code class MultiConsensus extends Consensus{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4)
99
© 2005 Herlihy and Shavit99 Multi-Consensus Code class MultiConsensus extends Consensus{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) Extends ConsensusProtocol Decide sets j=i-1 and proposes value
100
© 2005 Herlihy and Shavit100 Multi-Consensus Code class MultiConsensus extends Consensus{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) Three slots initialized to EMPTY
101
© 2005 Herlihy and Shavit101 Multi-Consensus Code class MultiConsensus extends Consensus{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) Assign id 0 to entries 0,1 (or id 1 to entries 1,2)
102
© 2005 Herlihy and Shavit102 Multi-Consensus Code class MultiConsensus extends Consensus{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) Read the register my thread didn’t assign
103
© 2005 Herlihy and Shavit103 class MultiConsensus extends Consensus{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} Multi-Consensus Code (4) Other thread didn’t move, so I win
104
© 2005 Herlihy and Shavit104 class MultiConsensus extends Consensus{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} Multi-Consensus Code (4) Other thread moved later so I win
105
© 2005 Herlihy and Shavit105 Multi-Consensus Code class MultiConsensus extends Consensus{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) OK, I win.
106
© 2005 Herlihy and Shavit106 class MultiConsensus extends Consensus{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} Multi-Consensus Code (4) (1) Other thread moved first, so I lose
107
© 2005 Herlihy and Shavit107 Summary If a thread can assign atomically to 2 out of 3 array locations Then we can solve 2-consensus Therefore –No wait-free multi-assignment –From read/write registers
108
© 2005 Herlihy and Shavit108 Read-Modify-Write Objects Method call –Returns object’s prior value x –Replaces x with mumble(x)
109
© 2005 Herlihy and Shavit109 public abstract class RMWRegister { private int value; public void synchronized getAndMumble() { int prior = this.value; this.value = mumble(this.value); return prior; } Read-Modify-Write (1)
110
© 2005 Herlihy and Shavit110 public abstract class RMWRegister { private int value; public void synchronized getAndMumble() { int prior = this.value; this.value = mumble(this.value); return prior; } Read-Modify-Write (1)
111
© 2005 Herlihy and Shavit111 public abstract class RMWRegister { private int value; public void synchronized getAndMumble() { int prior = this.value; this.value = mumble(this.value); return prior; } Read-Modify-Write (1) Return prior value
112
© 2005 Herlihy and Shavit112 public abstract class RMWRegister { private int value; public void synchronized getAndMumble() { int prior = this.value; this.value = mumble(this.value); return prior; } Read-Modify-Write (1) Apply function to current value
113
© 2005 Herlihy and Shavit113 RMW Everywhere! Most synchronization instructions –are RMW methods The rest –Can be trivially transformed into RMW methods
114
© 2005 Herlihy and Shavit114 public abstract class RMWRegister { private int value; public void synchronized read() { int prior = this.value; this.value = this.value; return prior; } Example: Read (1)
115
© 2005 Herlihy and Shavit115 public abstract class RMW { private int value; public void synchronized read() { int prior = this.value; this.value = this.value; return prior; } Example: Read (1) Apply f(v)=v, the identity function
116
© 2005 Herlihy and Shavit116 public abstract class RMWRegister { private int value; public void synchronized getAndSet(int v) { int prior = this.value; this.value = v; return prior; } … } Example: getAndSet (1)
117
© 2005 Herlihy and Shavit117 public abstract class RMWRegister { private int value; public void synchronized getAndSet(int v) { int prior = this.value; this.value = v; return prior; } … } Example: getAndSet (swap) (1) F(x)=v is constant function
118
© 2005 Herlihy and Shavit118 public abstract class RMWRegister { private int value; public void synchronized getAndIncrement() { int prior = this.value; this.value = this.value + 1; return prior; } … } getAndIncrement (1)
119
© 2005 Herlihy and Shavit119 public abstract class RMWRegister { private int value; public void synchronized getAndIncrement() { int prior = this.value; this.value = this.value + 1; return prior; } … } getAndIncrement (1) F(x) = x+1
120
© 2005 Herlihy and Shavit120 public abstract class RMWRegister { private int value; public void synchronized getAndAdd(int a) { int prior = this.value; this.value = this.value + a; return prior; } … } getAndAdd (1)
121
© 2005 Herlihy and Shavit121 public abstract class RMWRegister { private int value; public void synchronized getAndIncrement(int a) { int prior = this.value; this.value = this.value + a; return prior; } … } Example: getAndAdd (1) F(x) = x+a
122
© 2005 Herlihy and Shavit122 public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { int prior = this.value; if (this.value==expected) { this.value = update; return true; } return false; } … } compareAndSet (1)
123
© 2005 Herlihy and Shavit123 public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { int prior = this.value; if (this.value==expected) { this.value = update; return true; } return false; } … } compareAndSet (1) If value is what was expected, …
124
© 2005 Herlihy and Shavit124 public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { int prior = this.value; if (this.value==expected) { this.value = update; return true; } return false; } … } compareAndSet (1) … replace it
125
© 2005 Herlihy and Shavit125 public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { int prior = this.value; if (this.value==expected) { this.value = update; return true; } return false; } … } compareAndSet (1) Report success
126
© 2005 Herlihy and Shavit126 public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { int prior = this.value; if (this.value==expected) { this.value = update; return true; } return false; } … } compareAndSet (1) Otherwise report failure
127
© 2005 Herlihy and Shavit127 public abstract class RMWRegister { private int value; public void synchronized getAndMumble() { int prior = this.value; this.value = mumble(this.value); return prior; } Read-Modify-Write (1) Lets characterize F(x)…
128
© 2005 Herlihy and Shavit128 Definition A RMW method –With function mumble(x) – is non-trivial if there exists a value v –Such that v ≠ mumble(v)
129
© 2005 Herlihy and Shavit129 Par Example Identity(x) = x – is trivial getAndIncrement(x) = x+1 –is non-trivial
130
© 2005 Herlihy and Shavit130 Theorem Any non-trivial RMW object has consensus number at least 2 No wait-free implementation of RMW registers from atomic registers Hardware RMW instructions not just a convenience
131
© 2005 Herlihy and Shavit131 Reminder Subclasses of consensus have –propose(x) method which just stores x into proposed[i] Built-in method –decide(object value) method which determines winning value Customized, class-specific method
132
© 2005 Herlihy and Shavit132 Proof public class RMWConsensus implements ConsensusProtocol { private RMWRegister r = v; public Object decide(object value) { propose(value); if (r.getAndMumble() == v) return proposed[i]; else return proposed[j]; }} (4)
133
© 2005 Herlihy and Shavit133 public class RMWConsensus implements ConsensusProtocol { private RMWRegister r = v; public Object decide(object value) { propose(value); if (r.getAndMumble() == v) return proposed[i]; else return proposed[j]; }} Proof (4) Initialized to v
134
© 2005 Herlihy and Shavit134 Proof public class RMWConsensus implements Consensus { private RMWRegister r = v; public Object decide(object value) { if (r.getAndMumble() == v) return proposed[i]; else return proposed[j]; }} (4) Am I first?
135
© 2005 Herlihy and Shavit135 public class RMWConsensus implements ConsensusProtocol { private RMWRegister r = v; public Object decide(object value) { propose(value); if (r.getAndMumble() == v) return proposed[i]; else return proposed[j]; }} Proof (4) Yes, return my input
136
© 2005 Herlihy and Shavit136 public class RMWConsensus implements ConsensusProtocol { private RMWRegister r = v; public Object decide(object value) { propose(value); if (r.getAndMumble() == v) return proposed[i]; else return proposed[j]; }} Proof (4) No, return other’s input
137
© 2005 Herlihy and Shavit137 Proof We have displayed –A two-thread consensus protocol –Using any non-trivial RMW object
138
© 2005 Herlihy and Shavit138 Interfering RMW Let F be a set of functions such that for all f i and f j, either –Commute: f i (f j (v))=f j (f i (v)) –Overwrite: f i (f j (v))=f i (v) Claim: Any set of RMW objects that commutes or overwrites has consensus number exactly 2
139
© 2005 Herlihy and Shavit139 Examples “test-and-set” getAndSet(1) f(v)=1 “swap” getAndSet(x) f(v,x)=x “fetch-and-inc” getAndIncrement() f(v)=v+1 Overwrite f i (f j (v))=f i (v) Commute f i (f j (v))= f j (f i (v))
140
© 2005 Herlihy and Shavit140 Meanwhile Back at the Critical State c 0-valent 1-valent A about to apply f A B about to apply f B
141
© 2005 Herlihy and Shavit141 Maybe the Functions Commute c 0-valent A applies f A B applies f B A applies f A B applies f B 01 C runs solo 1-valent
142
© 2005 Herlihy and Shavit142 Maybe the Functions Commute c 0-valent A applies f A B applies f B A applies f A B applies f B 01 C runs solo 1-valent These states look the same to C Contradiction
143
© 2005 Herlihy and Shavit143 Maybe the Functions Overwrite c 0-valent A applies f A B applies f B A applies f A 0 1 C runs solo 1-valent
144
© 2005 Herlihy and Shavit144 Maybe the Functions Overwrite c 0-valent A applies f A B applies f B A applies f A 0 1 C runs solo 1-valent These states look the same to C Contradiction
145
© 2005 Herlihy and Shavit145 Impact Many early machines provided these “weak” RMW instructions –Test-and-set (IBM 360) –Fetch-and-add (NYU Ultracomputer) –Swap (Original SPARCs) We now understand their limitations –But why do we want consensus anyway?
146
© 2005 Herlihy and Shavit146 public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { int prior = this.value; if (this.value==expected) { this.value = update; return true; } return false; } … } compareAndSet (1)
147
© 2005 Herlihy and Shavit147 public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { int prior = this.value; if (this.value==expected) { this.value = update; return true; } return false; } … } compareAndSet (1) replace value if its what we expected, …
148
© 2005 Herlihy and Shavit148 public class RMWConsensus implements ConsensusProtocol { private AtomicInteger r = new AtomicInteger(-1); public Object decide(object value) { propose(value); r.compareAndSet(-1,i); return proposed[r.get()]; } compareAndSet Has ∞ Consensus Number (4)
149
© 2005 Herlihy and Shavit149 public class RMWConsensus implements ConsensusProtocol { private AtomicInteger r = new AtomicInteger(-1); public Object decide(object value) { propose(value) r.compareAndSet(-1,i); return proposed[r.get()]; } compareAndSet Has ∞ Consensus Number (4) Initialized to -1
150
© 2005 Herlihy and Shavit150 public class RMWConsensus implements ConsensusProtocol { private AtomicInteger r = new AtomicInteger(-1); public Object decide(object value) { propose(value); r.compareAndSet(-1,i); return proposed[r.get()]; } compareAndSet Has ∞ Consensus Number (4) Try to swap in my id
151
© 2005 Herlihy and Shavit151 public class RMWConsensus implements ConsensusProtocol { private AtomicInteger r = new AtomicInteger(-1); public Object decide(object value) { propose(value); r.compareAndSet(-1,i); return proposed[r.get()]; } compareAndSet Has ∞ Consensus Number (4) Decide winner’s preference
152
© 2005 Herlihy and Shavit152 The Consensus Hierarchy 1 Read/Write Registers, Snapshots… 2 getAndSet, getAndIncrement, … ∞ compareAndSet,…......
153
© 2005 Herlihy and Shavit153 Multiple Assignment Atomic k-assignment Solves consensus for 2k-2 threads Every even consensus number has an object (can be extended to odd numbers)
154
© 2005 Herlihy and Shavit154 Lock-Freedom Lock-free: in an infinite execution infinitely often some method call finishes (obviously, in a finite number of steps) Pragmatic approach Implies no mutual exclusion
155
© 2005 Herlihy and Shavit155 Lock-Free vs. Wait-free Wait-Free: each method call takes a finite number of steps to finish Lock-free: in an infinite execution infinitely often some method call finishes
156
© 2005 Herlihy and Shavit156 Lock-Freedom Any wait-free implementation is lock-free. Lock-free is the same as wait- free if the execution is finite. Old saying: “Lock-free is to wait-free as deadlock-free is to lockout-free.”
157
© 2005 Herlihy and Shavit157 Lock-Free Implementations Lock-free consensus is just as impossible Lock-free = Wait-free for finite executions All the results we presented hold for lock-free algorithms also. (2)
158
© 2005 Herlihy and Shavit158 There is More: Universality Consensus is universal From n-thread consensus –Wait-free/Lock-free –Linearizable –n-threaded –Implementation –Of any sequentially specified object
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.