CS 636: Shared Memory Synchronization Swarnendu Biswas
Total Page:16
File Type:pdf, Size:1020Kb
CS 636: Shared Memory Synchronization Swarnendu Biswas Semester 2020-2021-II CSE, IIT Kanpur Content influenced by many excellent references, see References slide for acknowledgements. What is the desired property? class Set { final Vector elems = new Vector(); void add(Object x) { if (!elems.contains(x)) { elems.add(x); } } } class Vector { synchronized void add(Object o) { ... } synchronized boolean contains(Object o) { ... } } CS 636 Swarnendu Biswas What is the desired property? Q.insert(elem): Q.remove(): atomic { atomic { while (Q.full()) {} while (Q.empty()) {} // Add elem to the Q // Return data from Q } } CS 636 Swarnendu Biswas Synchronization Patterns • Mutual exclusion lock:bool := false Lock.acquire(): Lock.release(): while TAS(&lock) lock := false // spin • Condition synchronization while ¬ condition // do nothing (spin) • Global synchronization CS 636 Swarnendu Biswas Locks (Mutual Exclusion) public interface Lock { Lock mtx = new LockImpl(…); public void lock(); … public void unlock(); } mtx.lock(); … try { public class LockImpl … // body implements Lock { } finally { … mtx.unlock(); … } } CS 636 Swarnendu Biswas Desired Synchronization Properties • Mutual exclusion • Critical sections on the same lock from different threads do not overlap • Safety property • Livelock freedom If a lock is available, then some thread should be able to acquire it within bounded steps CS 636 Swarnendu Biswas Deadlock-Free • If some thread calls lock() – And never returns – Then other threads must complete lock() and unlock() calls infinitely often • System as a whole makes progress – Even if individuals starve Art of Multiprocessor Programming 7 Starvation-Free • If some thread calls lock() – It will eventually return • Individual threads make progress Art of Multiprocessor Programming 8 Desired Synchronization Properties • Deadlock freedom • If a thread attempts to acquire the lock, then some thread should be able to acquire the lock • Individual threads may starve • Liveness property • Starvation freedom • Every thread that acquires a lock eventually releases it • A lock acquire request must eventually succeed within bounded steps • Implies deadlock freedom CS 636 Swarnendu Biswas Classic Mutual Exclusion Algorithms CS 636 Swarnendu Biswas LockOne: What could go wrong? class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; j = 1-i; while (flag[j]) {} } } Art of Multiprocessor Programming 12 Deadlock Freedom • LockOne Fails deadlock-freedom – Concurrent execution can deadlock flag[i] = true; flag[j] = true; while (flag[j]){} while (flag[i]){} – Sequential executions OK Art of Multiprocessor Programming 13 LockOne Satisfies Mutual Exclusion j k • Assume CSA overlaps CSB • Consider each thread's last (j-th and k-th) read and write in the lock() method before entering • Derive a contradiction Art of Multiprocessor Programming 14 From the Code • writeA(flag[A]=true) → readA(flag[B]==false) →CSA • writeB(flag[B]=true) → readB(flag[A]==false) → CSB class LockOne implements Lock { … public void lock() { flag[i] = true; j = 1 – i; while (flag[j]) {} } } Art of Multiprocessor Programming 15 From the Assumption • readA(flag[B]==false) → writeB(flag[B]=true) • readB(flag[A]==false) → writeA(flag[B]=true) Art of Multiprocessor Programming 16 Combining • Assumptions: – readA(flag[B]==false) → writeB(flag[B]=true) – readB(flag[A]==false) → writeA(flag[A]=true) • From the code – writeA(flag[A]=true) → readA(flag[B]==false) – writeB(flag[B]=true) → readB(flag[A]==false) Art of Multiprocessor Programming 17 Combining • Assumptions: – readA(flag[B]==false) → writeB(flag[B]=true) – readB(flag[A]==false) → writeA(flag[A]=true) • From the code – writeA(flag[A]=true) → readA(flag[B]==false) – writeB(flag[B]=true) → readB(flag[A]==false) Art of Multiprocessor Programming 18 Combining • Assumptions: – readA(flag[B]==false) → writeB(flag[B]=true) – readB(flag[A]==false) → writeA(flag[A]=true) • From the code – writeA(flag[A]=true) → readA(flag[B]==false) – writeB(flag[B]=true) → readB(flag[A]==false) Art of Multiprocessor Programming 19 Combining • Assumptions: – readA(flag[B]==false) → writeB(flag[B]=true) – readB(flag[A]==false) → writeA(flag[A]=true) • From the code – writeA(flag[A]=true) → readA(flag[B]==false) – writeB(flag[B]=true) → readB(flag[A]==false) Art of Multiprocessor Programming 20 Combining • Assumptions: – readA(flag[B]==false) → writeB(flag[B]=true) – readB(flag[A]==false) → writeA(flag[A]=true) • From the code – writeA(flag[A]=true) → readA(flag[B]==false) – writeB(flag[B]=true) → readB(flag[A]==false) Art of Multiprocessor Programming 21 Combining • Assumptions: – readA(flag[B]==false) → writeB(flag[B]=true) – readB(flag[A]==false) → writeA(flag[A]=true) • From the code – writeA(flag[A]=true) → readA(flag[B]==false) – writeB(flag[B]=true) → readB(flag[A]==false) Art of Multiprocessor Programming 22 Cycle! Art of Multiprocessor Programming 23 LockTwo: What could go wrong? public class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; } public void unlock() {} } Art of Multiprocessor Programming 25 LockTwo Claims • Satisfies mutual exclusion – If thread i in CS public void LockTwo() { – Then victim == j victim = i; – Cannot be both 0 and 1 while (victim == i) {}; } • Not deadlock free – Sequential execution deadlocks – Concurrent execution does not Art of Multiprocessor Programming 26 Peterson’s Algorithm class PetersonLock { public void lock() { int i = ThreadID.get(); static volatile boolean[] flag = int j = 1-i; new boolean[2]; flag[i] = true; static volatile int victim; victim = i; while (flag[j] && victim == i) {} public void unlock() { } int i = ThreadID.get(); flag[i] = false; } } CS 636 Swarnendu Biswas Mutual Exclusion public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; • If thread 0 in • If thread 1 in critical section, critical section, – flag[0]=true, – flag[1]=true, – victim = 1 – victim = 0 Cannot both be true Art of Multiprocessor Programming 28 Starvation Free • Thread i blocked only if j repeatedly public void lock() { flag[i] = true; re-enters so that victim = i; while (flag[j] && victim == i) {}; flag[j] == true and } victim == i public void unlock() { flag[i] = false; • When j re-enters } – it sets victim to j. – So i gets in Art of Multiprocessor Programming 29 Deadlock Free public void lock() { … while (flag[j] && victim == i) {}; • Thread blocked – only at while loop – only if it is the victim • One or the other must not be the victim Art of Multiprocessor Programming 30 Peterson’s Algorithm class PetersonLock { public void lock() { int i = ThreadID.get(); static volatile boolean[] flag = int j = 1-i; new boolean[2]; flag[i] = true; static volatile int victim; victim = i; while (flag[j] && victim == i) {} public void unlock() { } int i = ThreadID.get(); flag[i] = false; •} Is this algorithm correct under } sequential consistency? • What if we do not have sequential consistency? CS 636 Swarnendu Biswas Filter Lock for n Threads non-CS with n threads level=0 • There are n-1 waiting rooms n-1 threads level=1 called “levels” • At least one thread trying to enter a level succeeds • One thread gets blocked at each level if many threads try to enter 2 threads level=n-2 CS level=n-1 CS 636 Swarnendu Biswas Filter Lock class FilterLock { public void unlock() { int me = ThreadID.get(); volatile int[] level; level[me]= 0; volatile int[] victim; } public FilterLock() { level = new int[n]; victim = new int[n]; for (int i = 0; i < n; i++) { level[i] = 0; } } CS 636 Swarnendu Biswas Filter Lock … public void lock() { int me = ThreadID.get(); for (int i = 1; i < n; i++) { // Attempt to enter level i level[me] = i; // visit level i victim[i] = me; // Thread “me” is a good guy! // spin while conflict exits while ((∃k != me) level[k] >= i && victim[i] == me) { } } } } CS 636 Swarnendu Biswas Claim • Start at level L=0 • At most n-L threads enter level L • Mutual exclusion at level L=n-1 ncs L=0 L=1 L=n-2 cs L=n-1 Art of Multiprocessor Programming 35 Induction Hypothesis • No more than n-L+1 at level L-1 • Induction step: by contradiction • Assume all at level L-1 enter level L ncs assume • A last to write victim[L] L-1 has n-L+1 L has n-L • B is any other thread at level L cs prove Art of Multiprocessor Programming 36 Proof Structure ncs Assumed to enter L-1 A B n-L+1 = 4 n-L+1 = 4 Last to write cs By way of contradiction victim[L] all enter L Show that A must have seen B in level[L] and since victim[L] == A could not have entered Art of Multiprocessor Programming 37 From the Code (1) writeB(level[B]=L)➔writeB(victim[L]=B) public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i; while (($ k != i) level[k] >= L) && victim[L] == i) {}; }} Art of Multiprocessor Programming 38 From the Code (2) writeA(victim[L]=A)➔readA(level[B]) public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i; while (($ k != i) level[k] >= L) && victim[L] == i) {}; }} Art of Multiprocessor Programming 39 By Assumption (3) writeB(victim[L]=B)➔writeA(victim[L]=A) By assumption, A is the last thread to write victim[L] Art of Multiprocessor Programming 40 Combining Observations (1) writeB(level[B]=L)➔writeB(victim[L]=B) (3) writeB(victim[L]=B)➔writeA(victim[L]=A) (2) writeA(victim[L]=A)➔readA(level[B]) Art of Multiprocessor Programming 41 Combining Observations (1) writeB(level[B]=L)➔writeB(victim[L]=B) (3) writeB(victim[L]=B)➔writeA(victim[L]=A) (2) writeA(victim[L]=A)➔readA(level[B])