time

advertisement
Concurrent Computation:
Correctness and Progress
Erez Timnat
- The Art Of Multiprocessor Programming,
Herlihy & Shavit, Ch. 3
Correctness Properties
• Quiescent Consistency
• Sequential Consistency
• Linearizability
Quiescent Consistency
Principle 1: Method calls should appear to
happen in a one-at-a-time, sequential order.
Quiescent Consistency
• Principle 1: Method calls should appear to happen
in a one-at-a-time, sequential order.
Thread A
Thread B
r.write(7)
r.write(-3)
r.read(-7)
time
Quiescent Consistency
• Principle 1: Method calls should appear to happen
in a one-at-a-time, sequential order.
Thread A
Thread B
r.write(7)
r.write(-3)
r.read(-7)
time
Quiescent Consistency
Principle 1: Method calls should appear to
happen in a one-at-a-time, sequential order.
Principle 2: Method calls separated by a period
of quiescence should appear to take effect in
their real-time order.
Quiescent Consistency – Example 1
r quiescent
Thread A
Thread B
r.read(-3)
r.write(7)
r.write(-3)
time
Quiescent Consistency – Example 2
Thread A
Thread B
r.read(7)
r.write(7)
r.read(7)
r.write(-3)
r.read(7)
time
Quiescently Consistent?
Quiescent Consistency – Example 2
Thread A
Thread B
r.read(7)
r.write(7)
r.read(7)
r.write(-3)
r.read(7)
time
Quiescently Consistent? Yes!
Sequential Consistency
Principle 1: Method calls should appear to
happen in a one-at-a-time, sequential order.
• Principle 2: Method calls should appear to
take effect in program order.
Quiescent Consistency – Problem
Thread A
Thread B
r.read(7)
r.write(7)
r.read(7)
r.write(-3)
r.read(7)
time
Quiescently Consistent, Not Sequentially Consistent
Sequential Consistency
Thread A
Thread B
r.read(7)
r.write(7)
r.read(7)
r.write(-3)
r.read(7)
time
Sequential Consistency
Thread A
Thread B
r.read(7)
r.write(7)
r.read(7)
r.write(-3)
r.read(-3)
time
Sequential Consistency : Example 2
Thread A
Thread B
q.enq(7)
q.deq(-3)
q.enq(-3)
q.deq(7)
time
Sequentially Consistent?
Sequential Consistency : Example 2
Thread A
Thread B
q.enq(7)
q.deq(-3)
q.enq(-3)
q.deq(7)
time
Sequentially Consistent? No!
Sequential Consistency : Example 3
Thread A q.enq(7)
Thread B
q.deq(-3)
q.enq(-3)
q.deq(7)
time
Sequentially Consistent?
Sequential Consistency : Example 3
Thread A q.enq(7)
Thread B
q.deq(-3)
q.enq(-3)
q.deq(7)
time
Sequentially Consistent? Yes!
Sequential Consistency : Example 3
Thread A q.enq(7)
Thread B
q.deq(-3)
q.enq(-3)
q.deq(7)
time
Quiescently Consistent?
Sequential Consistency : Example 3
Thread A q.enq(7)
Thread B
q.deq(-3)
q.enq(-3)
q.deq(7)
time
Quiescently Consistent? No!
Sequential Consistency : Example 3
Thread A q.enq(7)
Thread B
q.deq(-3)
q.enq(-3)
q.deq(7)
time
Sequentially Consistent, Not Quiescently Consistent
Linearizability
Principle 1: Method calls should appear to
happen in a one-at-a-time, sequential order.
• Principle 2: Each method call should appear
to take effect instantaneously at some
moment between its invocation and
response.
Sequential Consistency : Example 3
Thread A q.enq(7)
Thread B
q.deq(-3)
q.enq(-3)
q.deq(7)
time
Sequentially Consistent, Not Linearizable
Linearizability: Example 2
Thread A
Thread B
q.enq(7)
q.deq(-3)
q.enq(-3)
q.deq(7)
time
Linearizable?
Linearizability: Example 2
Thread A
Thread B
q.enq(7)
q.enq(-3)
q.deq(-3)
q.deq(7)
time
Linearizable? Yes!
Read/Write Register Example
write(0)
read(1)
write(2)
write(1)
read(0)
time
(4)
Art of Multiprocessor Programming
25
Read/Write Register Example
write(0)
read(1)
write(2)
write(1)
read(0)
write(1) already
happened time
(4)
Art of Multiprocessor Programming
26
Read/Write Register Example
write(0)
read(1)
write(2)
write(1)
read(0)
write(1) already
happened time
(4)
Art of Multiprocessor Programming
27
Read/Write Register Example
write(0)
read(1)
write(2)
write(1)
read(0)
write(1) already
happened time
(4)
Art of Multiprocessor Programming
28
Read/Write Register Example
write(0)
read(1)
write(2)
write(1)
read(1)
write(1) already
happened time
(4)
Art of Multiprocessor Programming
29
Read/Write Register Example
write(0)
read(1)
write(2)
write(1)
read(1)
write(1) already
happened time
(4)
Art of Multiprocessor Programming
30
Read/Write Register Example
write(0)
read(1)
write(2)
write(1)
read(1)
write(1) already
happened time
(4)
Art of Multiprocessor Programming
31
Read/Write Register Example
write(0)
write(2)
write(1)
read(1)
time
(4)
Art of Multiprocessor Programming
32
Read/Write Register Example
write(0)
write(2)
write(1)
read(1)
time
(4)
Art of Multiprocessor Programming
33
Read/Write Register Example
write(0)
write(2)
write(1)
read(1)
time
(4)
Art of Multiprocessor Programming
34
Correctness Properties - Summary
• Quiescent Consistency – Separating quiescent points.
• Sequential Consistency – Same thread calls by order.
• Linearizability – Linearization points.
Progress Conditions
Progress Conditions
• Lock-free: some thread calling a method eventually returns.
• Wait-free: every thread calling a method eventually returns.
49
Example - Counter
• Increment()
• Get()
50
A Lock-Based Counter
public class LockBasedCounter {
int counter;
Lock lock;
public LockBasedCounter() {
Counter = 0;
lock = new ReentrantLock();
}
51
Implementation: Lock-Based Counter
public void Increment() {
lock.lock();
counter++;
lock.unlock();
}
public void Get() {
return counter;
}
52
Now consider the following implementation
• The same thing without mutual exclusion.
• Assume 4 threads, each with a unique ID between 0 and 3.
53
Wait-Free Counter
public class WaitFreeCounter {
int []counters = new int[4];
public void Increment() {
counters[Thread.currentThread().getId()]++;
}
public int Get() {
int sum=0;
for (int i=0; i<4; i++) {
sum+=counters[i];
}
return sum;
}}
54
Lock-Free Counter
public class LockFreeCounter {
int counter = 0;
public void Increment() {
int temp;
while (true) {
temp=counter;
if (CAS(&counter,temp,temp+1)) {
break;
}
}
}
public int Get() {
return counter;
}}
55
Progress Conditions - Summary
• Lock-free: some thread calling a method eventually returns.
• Wait-free: every thread calling a method eventually returns.
56
Questions?
Download