The Five-Fold Path. Queues & Stacks. Bounded vs Unbounded. Blocking vs Non-Blocking. Concurrent Queues and Stacks. Another Fundamental Problem

Size: px
Start display at page:

Download "The Five-Fold Path. Queues & Stacks. Bounded vs Unbounded. Blocking vs Non-Blocking. Concurrent Queues and Stacks. Another Fundamental Problem"

Transcription

1 Concurrent Queues and Stacks The Five-Fold Path Coarse-grained locking Fine-grained locking Optimistic synchronization Lazy synchronization Lock-free synchronization 2 Another Fundamental Problem Queues & Stacks We told you about Sets implemented using linked lists Next: queues Next: stacks Both: pool of items Queue enq() & deq() First-in-first-out (FIFO) order Stack push() & pop() Last-in-first-out (LIFO) order 3 4 Bounded vs Unbounded Blocking vs Non-Blocking Bounded Fixed capacity Good when resources an issue Unbounded Holds any number of objects Problem cases: Removing from empty pool Adding to full (bounded) pool Blocking Caller waits until state changes Non-Blocking Method throws exception 5 6 1

2 This Lecture Queue: Concurrency Bounded, Blocking, Lock-based Queue Unbounded, Non-Blocking, Lock-free Queue ABA problem Unbounded Non-Blocking Lock-free Stack Elimination-Backoff Stack enq(x) enq() and deq() work at different ends of the object y=deq() 7 8 Concurrency Bounded Queue enq(x) y=deq() Sentinel Challenge: what if the queue is empty or full? 9 10 Bounded Queue Bounded Queue First actual item Lock out other deq() calls

3 Bounded Queue Not Done Yet Lock out other enq() calls Need to tell whether queue is full or empty Not Done Yet Not Done Yet 8 Permission to enqueue 8 items 8 Incremented by deq() Decremented by enq() Enqueuer Enqueuer Lock Read 8 8 OK

4 Enqueuer Enqueuer No need to lock Enqueue Node Enqueuer Enqueuer Release lock getanddecrement() Enqueuer Unsuccesful Enqueuer 7 If queue was empty, notify/signal waiting dequeuers 0 Read Uh-oh

5 Dequeuer Dequeuer 8 Lock 7 Read sentinel s next field OK Dequeuer Dequeuer Make first Node new sentinel 7 Read value Dequeuer Dequeuer 8 Increment 7 Release

6 Unsuccesful Dequeuer Bounded Queue 8 Read sentinel s next field uh-oh public class BoundedQueue<T> { ReentrantLock, ; Condition notemptycondition, notfullcondition; AtomicInteger ; Node ; Node ; int capacity; = new ReentrantLock(); notfullcondition =.newcondition(); = new ReentrantLock(); notemptycondition =.newcondition(); Bounded Queue Digression: Monitor Locks public class BoundedQueue<T> { ReentrantLock, ; Condition notemptycondition, notfullcondition; AtomicInteger ; Node ; Node ; int capacity; Enq & deq locks = new ReentrantLock(); notfullcondition =.newcondition(); = new ReentrantLock(); notemptycondition =.newcondition(); Java Synchronized objects and Java ReentrantLocks are monitors Allow blocking on a condition rather than spinning Threads: acquire and release lock wait on a condition The Java Lock Interface The Java Lock Interface public interface Lock { void lock(); void lockinterruptibly() throws InterruptedException; boolean trylock(); boolean trylock(long time, TimeUnit unit); Condition newcondition(); void unlock; Acquire lock public interface Lock { void lock(); void lockinterruptibly() throws InterruptedException; boolean trylock(); boolean trylock(long time, TimeUnit unit); Condition newcondition(); void unlock; Release lock

7 The Java Lock Interface The Java Lock Interface public interface Lock { void lock(); void lockinterruptibly() throws InterruptedException; boolean trylock(); boolean trylock(long time, TimeUnit unit); Condition newcondition(); void unlock; Try for lock, but not too hard public interface Lock { void lock(); void lockinterruptibly() throws InterruptedException; boolean trylock(); boolean trylock(long time, TimeUnit unit); Condition newcondition(); void unlock; Create condition to wait on The Java Lock Interface Lock Conditions public interface Lock { void lock(); void lockinterruptibly() throws InterruptedException; boolean trylock(); boolean trylock(long time, TimeUnit unit); Condition newcondition(); void unlock; Guess what this method does? public interface Condition { void await(); boolean await(long time, TimeUnit unit); void signal(); void signalall(); Lock Conditions Lock Conditions public interface Condition { void await(); boolean await(long time, TimeUnit unit); void signal(); void signalall(); Release lock and wait on condition public interface Condition { void await(); boolean await(long time, TimeUnit unit); void signal(); void signalall(); Wake up one waiting thread

8 Lock Conditions Await public interface Condition { void await(); boolean await(long time, TimeUnit unit); void signal(); void signalall(); Wake up all waiting threads q.await() Releases lock associated with q Sleeps (gives up processor) Awakens (resumes running) Reacquires lock & returns Signal Signal All q.signal(); q.signalall(); Awakens one waiting thread Which will reacquire lock Awakens all waiting threads Which will each reacquire lock A Monitor Lock Unsuccessful Deq Lock() waiting room Lock() waiting room Critical Section unlock() Deq() Critical Section await() Oh no, Empty!

9 Lock() Another One waiting room Enqueur to the Rescue Lock() waiting room Yawn! Yawn! Deq() Critical Section await() Oh no, Empty! Enq( ) Critical Section signalall() unlock() Monitor Signalling waiting room Yawn! Yawn! Dequeurs Signalled waiting room Yawn! Critical Section Awakend thread might still lose lock to outside contender Critical Section Found it Dequeurs Signalled Dollar Short + Day Late waiting room Yawn! waiting room Critical Section Still empty! Critical Section

10 Lock() Lost Wake-Up Yawn! waiting room Lock() Lost Wake-Up Yawn! waiting room Enq( ) Critical Section signal () unlock() Enq( ) Critical Section unlock() Lost Wake-Up Yawn! Lost Wake-Up waiting room waiting room Critical Section Critical Section Found it What s Wrong Here? waiting room zzzz.! Solution to Lost Wakeup Always use signalall and notifyall Not signal and notify Critical Section

11 Java Synchronized Methods public class Queue<T> { int = 0,, = 0; T[QSIZE] items; public synchronized T deq() { while ( == 0) this.wait(); T result = items[ % QSIZE]; ++; this.notifyall(); return result; Java Synchronized Methods public class Queue<T> { int = 0,, = 0; T[QSIZE] items; public synchronized T deq() { while ( == 0) this.wait(); T result = items[ % QSIZE]; ++; this.notifyall(); return result; Each object has an implicit lock with an implicit condition Java Synchronized Methods public class Queue<T> { int = 0,, = 0; T[QSIZE] items; Lock on entry, unlock on return public synchronized T deq() { while ( == 0) this.wait(); T result = items[ % QSIZE]; ++; this.notifyall(); return result; Java Synchronized Methods public class Queue<T> { int = 0,, = 0; T[QSIZE] items; Wait on implicit condition public synchronized T deq() { while ( == 0) this.wait(); T result = items[ % QSIZE]; ++; this.notifyall(); return result; Java Synchronized Methods public class Queue<T> { int = 0,, = 0; T[QSIZE] items; Signal all threads waiting on condition public synchronized T deq() { while ( == 0) this.wait(); T result = items[ % QSIZE]; ++; this.notifyall(); return result; (Pop!) The Bounded Queue public class BoundedQueue<T> { ReentrantLock, ; Condition notemptycondition, notfullcondition; AtomicInteger ; Node ; Node ; int capacity; = new ReentrantLock(); notfullcondition =.newcondition(); = new ReentrantLock(); notemptycondition =.newcondition();

12 Bounded Queue Fields Bounded Queue Fields public class BoundedQueue<T> { ReentrantLock, ; Condition notemptycondition, notfullcondition; AtomicInteger ; Node ; Node ; int capacity; Enq & deq locks = new ReentrantLock(); notfullcondition =.newcondition(); = new ReentrantLock(); notemptycondition =.newcondition(); public class BoundedQueue<T> { ReentrantLock, ; Condition notemptycondition, notfullcondition; AtomicInteger ; Node ; Enq lock s associated Node ; condition int capacity; = new ReentrantLock(); notfullcondition =.newcondition(); = new ReentrantLock(); notemptycondition =.newcondition(); Bounded Queue Fields Bounded Queue Fields public class BoundedQueue<T> { ReentrantLock, ; Condition notemptycondition, notfullcondition; AtomicInteger ; Node ; Node ; int capacity; = new ReentrantLock(); notfullcondition =.newcondition(); = new ReentrantLock(); notemptycondition =.newcondition(); Num : 0 to capacity public class BoundedQueue<T> { ReentrantLock, ; Condition notemptycondition, notfullcondition; AtomicInteger ; Head and Tail Node ; Node ; int capacity; = new ReentrantLock(); notfullcondition =.newcondition(); = new ReentrantLock(); notemptycondition =.newcondition(); Enq Method Part One Enq Method Part One public void enq(t x) { boolean mustwakedequeuers = false;.lock(); try { while (.get() == 0) notfullcondition.await(); Node e = new Node(x);.next = e; = e; if (.getanddecrement() == capacity) mustwakedequeuers = true; finally {.unlock(); 71 public void enq(t x) { boolean mustwakedequeuers = false;.lock(); try { while (.get() == 0) notfullcondition.await(); Node e = new Node(x);.next = e; = e; if (.getanddecrement() == capacity) mustwakedequeuers = true; finally {.unlock(); Lock and unlock enq lock 72 12

13 Enq Method Part One public void enq(t x) { boolean mustwakedequeuers = false;.lock(); try { while (.get() == 0) notfullcondition.await(); Node e = new Node(x);.next = e; = e; if (.getanddecrement() == capacity) mustwakedequeuers = true; finally {.unlock(); If queue is full, patiently await further instructions 73 Be Afraid public void enq(t x) { boolean mustwakedequeuers = false;.lock(); try { while (.get() == 0) notfullcondition.await(); Node e = new Node(x);.next = e; = e; if (.getanddecrement() == capacity) mustwakedequeuers = true; finally {.unlock(); How do we know the field won t change? 74 Enq Method Part One Enq Method Part One public void enq(t x) { boolean mustwakedequeuers = false;.lock(); try { while (.get() == 0) notfullcondition.await(); Node e = new Node(x);.next = e; = e; if (.getanddecrement() == capacity) mustwakedequeuers = true; finally {.unlock(); Add new node 75 public void enq(t x) { boolean mustwakedequeuers = false;.lock(); try { while (.get() == 0) notfullcondition.await(); Node e = new Node(x);.next = e; = e; if (.getanddecrement() == capacity) mustwakedequeuers = true; finally {.unlock(); If queue was empty, wake frustrated dequeuers 76 Enq Method Part Deux Enq Method Part Deux public void enq(t x) { if (mustwakedequeuers) {.lock(); try { notemptycondition.signalall(); finally {.unlock(); public void enq(t x) { if (mustwakedequeuers) {.lock(); try { notemptycondition.signalall(); finally {.unlock(); Are there dequeuers to be signaled?

14 Enq Method Part Deux Enq Method Part Deux public void enq(t x) { if (mustwakedequeuers) {.lock(); try { notemptycondition.signalall(); finally {.unlock(); Lock and unlock deq lock public void enq(t x) { Signal dequeuers that queue if (mustwakedequeuers) no longer empty {.lock(); try { notemptycondition.signalall(); finally {.unlock(); The Enq() & Deq() Methods Split the Counter Share no locks That s good But do share an atomic counter Accessed on every method call That s not so good Can we alleviate this bottleneck? The enq() method Decrements only Cares only if value is zero The deq() method Increments only Cares only if value is capacity Split Counter A Lock-Free Queue Enqueuer decrements enqsidepermits Dequeuer increments deqsidepermits When enqueuer runs out Locks Transfers Intermittent synchronization Not with each method call Need both locks! (careful ) Sentinel

15 Compare and Set Enqueue CAS Enq( ) Enqueue Logical Enqueue CAS Physical Enqueue Enqueue CAS Enqueue Node These two steps are not atomic The field refers to either Actual last Node (good) Penultimate Node (not so good) Be prepared!

16 Enqueue When CASs Fail What do you do if you find A trailing? Stop and help fix it If node has non-null next field CAS the queue s field to.next As in the universal construction During logical enqueue Abandon hope, restart Still lock-free (why?) During physical enqueue Ignore it (why?) Dequeuer Dequeuer Make first Node new sentinel CAS Read value Memory Reuse? What do we do with nodes after we dequeue them? Java: let garbage collector deal? Suppose there is no GC, or we prefer not to use it? CAS Dequeuer Can recycle

17 Simple Solution Each thread has a free list of unused queue nodes Allocate node: pop from list Free node: push onto list Deal with underflow somehow Why Recycling is Hard Want to redirect from zzz grey to red Free pool Both Nodes Reclaimed One Node Recycled zzz Yawn! Free pool Free pool Why Recycling is Hard CAS Final State Bad news OK, here I go! zomg what went wrong? Free pool Free pool

18 The Dreaded ABA Problem Dreaded ABA continued Head pointer has value A Thread reads value A zzz Head pointer has value B Node A freed Dreaded ABA continued Dreaded ABA continued CAS Yawn! Head pointer has value A again Node A recycled & reinitialized CAS succeeds because pointer matches even though pointer s meaning has changed The Dreaded ABA Problem Dreaded ABA A Solution Is a result of CAS() semantics I blame Sun, Intel, AMD, Not with Load-Locked/Store- Conditional Good for IBM? Tag each pointer with a counter Unique over lifetime of node Pointer size vs word size issues Overflow? Don t worry be happy? Bounded tags? AtomicStampedReference class

19 Atomic Stamped Reference AtomicStampedReference class Java.util.concurrent.atomic package Reference Can get reference and stamp atomically address S Concurrent Stack Methods push(x) pop() Last-in, First-out (LIFO) order Lock-Free! Stamp Empty Stack Push Top Top Push Push Top CAS Top

20 Push Push Top Top Push Push Top CAS Top Pop Pop Top Top CAS

21 Pop Pop Top CAS Top CAS mine! Pop Lock-free Stack Top public class LockFreeStack { private AtomicReference top = new AtomicReference(null null); public boolean trypush(node node){ Node oldtop = top.get(); node.next = oldtop; return(top.compareandset(oldtop, node)) public void push(t value) { Node node = new Node(value); while (true true) ) { if (trypush(node)) { else backoff.backoff(); Lock-free Stack Lock-free Stack public class LockFreeStack { private AtomicReference top = new AtomicReference(null); public Boolean trypush(node node){ Node oldtop = top.get(); node.next = oldtop; return(top.compareandset(oldtop, node)) public void push(t value) { Node node = new Node(value); while (true) { if (trypush(node)) { trypush else backoff.backoff() attempts to push a node public class LockFreeStack { private AtomicReference top = new AtomicReference(null); public boolean trypush(node node){ Node oldtop = top.get(); node.next = oldtop; return(top.compareandset(oldtop, node)) public void push(t value) { Node node = new Node(value); while (true) { if (trypush(node)) { Read top value else backoff.backoff()

22 Lock-free Stack Lock-free Stack public class LockFreeStack { private AtomicReference top = new AtomicReference(null); public boolean trypush(node node){ Node oldtop = top.get(); node.next = oldtop; return(top.compareandset(oldtop, node)) public void push(t value) { Node node = new Node(value); while (true) { if (trypush(node)) { else backoff.backoff() current top will be new node s successor 127 public class LockFreeStack { private AtomicReference top = new AtomicReference(null); public boolean trypush(node node){ Node oldtop = top.get(); node.next = oldtop; return(top.compareandset(oldtop, node)) public void push(t value) { Node node = new Node(value); while (true) { if (trypush(node)) { else backoff.backoff() Try to swing top, return success or failure 128 Lock-free Stack Lock-free Stack public class LockFreeStack { private AtomicReference top = new AtomicReference(null); public boolean trypush(node node){ Node oldtop = top.get(); node.next = oldtop; return(top.compareandset(oldtop, node)) public void push(t value) { Node node = new Node(value); while (true) { if (trypush(node)) { else backoff.backoff() Push calls trypush public class LockFreeStack { private AtomicReference top = new AtomicReference(null); public boolean trypush(node node){ Node oldtop = top.get(); node.next = oldtop; return(top.compareandset(oldtop, node)) public void push(t value) { Node node = new Node(value); while (true) { if (trypush(node)) { else backoff.backoff() Create new node Lock-free Stack Makes scheduling benevolent so public class LockFreeStack { Method is effectively wait- private AtomicReference top = new AtomicReference(null); free public boolean trypush(node node){ Node oldtop If = trypush() top.get(); fails, node.next = oldtop; return(top.compareandset(oldtop, back off before retrying node)) public void push(t value) { Node node = new Node(value); while (true) { if (trypush(node)) { else backoff.backoff() 131 Lock-free Stack Good No locking Bad Without GC, fear ABA Without backoff, huge contention at top In any case, no parallelism

23 Big Question Elimination-Backoff Stack Are stacks inherently sequential? Reasons why Every pop() call fights for top item Reasons why not Stay tuned How to turn contention into parallelism Replace familiar exponential backoff With alternative elimination-backoff Observation Idea: Elimination Array Push( ) linearizable stack Pick at random Push( ) stack Pop() Yes! After an equal number of pushes and pops, stack stays the same Pop() Pick at random Elimination Array Push Collides With Pop continue No Collision Push( ) stack Push( ) stack Pop() continue Yes! No need to access stack Pop() If pushes collide or If pops no collision, collide access access stack stack

24 Elimination-Backoff Stack Elimination-Backoff Stack Lock-free stack + elimination array Access Lock-free stack, If uncontended, apply operation if contended, back off to elimination array and attempt elimination Push( ) Pop() CAS Top If CAS fails, back off Dynamic Range and Delay Linearizability Push( ) Pick random range and max time to wait for collision based on level of contention encountered Un-eliminated calls linearized as before Eliminated calls: linearize pop() immediately after matching push() Combination is a linearizable stack Un-Eliminated Linearizability Eliminated Linearizability Collision Point pop(v 1 ) pop(v 1 ) push(v 1 ) push(v 1 ) pop(v 2 ) push(v 2 ) Red calls are eliminated time time

25 Backoff Has Dual Effect Elimination introduces parallelism Backoff onto array cuts contention on lock-free stack Elimination in array cuts down total number of threads ever accessing lock-free stack Elimination Array public class EliminationArray { private static final int duration =...; private static final int timeunit =...; Exchanger<T>[] exchanger; public EliminationArray(int int capacity) { exchanger = new Exchanger[capacity]; for (int i = 0; ; i < capacity; i++) exchanger[i] = new Exchanger<T>(); Elimination Array public class EliminationArray { private static final int duration =...; private static final int timeunit =...; Exchanger<T>[] exchanger; public EliminationArray(int capacity) { exchanger = new Exchanger[capacity]; for (int i = 0; ; i < capacity; i++) exchanger[i] = new Exchanger<T>(); An array of exchangers A Lock-Free Exchanger public class Exchanger<T> { AtomicStampedReference<T> slot = new AtomicStampedReference<T>(null null, 0); A Lock-Free Exchanger public class Exchanger<T> { AtomicStampedReference<T> slot = new AtomicStampedReference<T>(null, 0); Atomically modifiable reference + time stamp Atomic Stamped Reference AtomicStampedReference class Java.util.concurrent.atomic package In C or C++: Reference address S Stamp

26 Extracting Reference & Stamp Extracting Reference & Stamp Public T get(int[] stampholder); Public T get(int[] stampholder); Returns reference to object of type T Returns stamp at array index 0! Exchanger Status enum Status {EMPTY, WAITING, BUSY; Exchanger Status enum Status {EMPTY { EMPTY, WAITING, BUSY; Nothing yet Exchange Status Exchange Status enum Status {EMPTY, { WAITING, BUSY; enum Status {EMPTY, { WAITING, BUSY; Nothing yet Nothing yet One thread is waiting for rendez-vous One thread is waiting for rendez-vous Other threads busy with rendez-vous

27 The Exchange The Exchange public T Exchange(T myitem, long nanos) throws TimeoutException { long timebound = System.nanoTime() + nanos; int[] stampholder = {EMPTY; while (true true) ) { if (System.nanoTime() > timebound) throw new TimeoutException(); T int stamp = stampholder[0]; switch(stamp) { case EMPTY: // slot is free case WAITING: // someone waiting for me case BUSY: // others exchanging public T Exchange(T myitem, long nanos) throws TimeoutException { long timebound = System.nanoTime() + nanos; int[] stampholder = {EMPTY; while (true) { if (System.nanoTime() > timebound) throw new TimeoutException(); T int stamp = stampholder[0]; switch(stamp) { Item & timeout case EMPTY: // slot is free case WAITING: // someone waiting for me case BUSY: // others exchanging The Exchange The Exchange public T Exchange(T myitem, long nanos) throws TimeoutException { long timebound = System.nanoTime() + nanos; int[] stampholder = {EMPTY; while (true) { if (System.nanoTime() > timebound) throw new TimeoutException(); T int stamp = stampholder[0]; switch(stamp) { case EMPTY: // slot is free case WAITING: // someone waiting for me case BUSY: // others exchanging Array to hold timestamp public T Exchange(T myitem, long nanos) throws TimeoutException { long timebound = System.nanoTime() + nanos; int[] stampholder = {0; { while (true true) ) { if (System.nanoTime() > timebound) throw new TimeoutException(); T int stamp = stampholder[0]; switch(stamp) { case EMPTY: // slot is free case WAITING: // someone waiting for me case BUSY: // others exchanging Loop until timeout The Exchange The Exchange public T Exchange(T myitem, long nanos) throws TimeoutException { long timebound = System.nanoTime() + nanos; int[] stampholder = {0; { while (true) { if (System.nanoTime() > timebound) throw new TimeoutException(); T int stamp = stampholder[0]; switch(stamp) { case EMPTY: // slot is free case WAITING: // someone waiting for me case BUSY: // others exchanging Get other s item and timestamp public T Exchange(T myitem, long nanos) throws TimeoutException { long timebound = System.nanoTime() + nanos; Exchanger int[] stampholder slot has = three {0; { states while (true) { if (System.nanoTime() > timebound) throw new TimeoutException(); T int stamp = stampholder[0]; switch(stamp) { case EMPTY: // slot is free case WAITING: // someone waiting for me case BUSY: // others exchanging

28 Lock-free Exchanger Lock-free Exchanger EMPTY CAS EMPTY Lock-free Exchanger Lock-free Exchanger In search of partner WAITING WAITING Try to Lock-free Exchanger Still waiting exchange item and set state to BUSY Lock-free Exchanger Partner showed up, take item and reset to EMPTY Slot Slot CAS WAITING BUSY item stamp/state

29 Lock-free Exchanger Partner showed up, take item and reset to EMPTY item Slot EMPTY BUSY stamp/state 169 Exchanger State EMPTY case EMPTY: // slot is free if (slot.compareandset(heritem, myitem, EMPTY, WAITING)) { while (System.nanoTime() < timebound){ if (stampholder[0] ] == BUSY) { slot.set(null, EMPTY); if (slot.compareandset(myitem, null, WAITING, EMPTY)){throw new TimeoutException(); else { slot.set(null, EMPTY); break; 170 Exchanger State EMPTY Exchanger State EMPTY case EMPTY: // slot is free if (slot.compareandset(heritem, myitem, EMPTY, WAITING)) { while (System.nanoTime() < timebound){ if (stampholder[0] ] == BUSY) { slot.set(null, EMPTY); if (slot.compareandset(myitem, null, WAITING, EMPTY)){throw new TimeoutException(); else { Slot is free, try to insert slot.set(null, myitem and EMPTY); change state return to WAITING heritem; break; 171 case EMPTY: // slot is free if (slot.compareandset(heritem, myitem, EMPTY, WAITING)) { while (System.nanoTime() < timebound){ if (stampholder[0] ] == BUSY) { slot.set(null, EMPTY); if (slot.compareandset(myitem, null, WAITING, EMPTY)){throw new TimeoutException(); else { Loop while still time left to slot.set(null, attempt exchange EMPTY); break; 172 Exchanger State EMPTY case EMPTY: // slot is free if (slot.compareandset(heritem, myitem, WAITING, BUSY)) { while (System.nanoTime() < timebound){ if (stampholder[0] ] == BUSY) { slot.set(null, EMPTY); if (slot.compareandset(myitem, null, WAITING, EMPTY)){throw new TimeoutException(); else { Get item and stamp in slot slot.set(null, and check EMPTY); if state changed return to BUSY heritem; break; 173 Exchanger State EMPTY case EMPTY: // slot is free if (slot.compareandset(heritem, myitem, EMPTY, WAITING)) { while (System.nanoTime() < timebound){ if (stampholder[0] ] == BUSY) { slot.set(null, EMPTY); if (slot.compareandset(myitem, null, WAITING, EMPTY)){throw new TimeoutException(); else { If successful reset slot slot.set(null, state to EMPTY); break;

30 Exchanger State EMPTY case EMPTY: // slot is free if (slot.compareandset(heritem, myitem, WAITING, BUSY)) { while (System.nanoTime() < timebound){ if (stampholder[0] ] == BUSY) { slot.set(null, EMPTY); if (slot.compareandset(myitem, null, WAITING, EMPTY)){throw new TimeoutException(); else { slot.set(null, EMPTY); and return item found in slot break; 175 Exchanger State EMPTY case EMPTY: // slot is free Otherwise if (slot.compareandset(heritem, we ran out of myitem, EMPTY, WAITING)) time, try { to reset state to while (System.nanoTime() < timebound){ EMPTY, if successful time out if (stampholder[0] ] == BUSY) { slot.set(null, EMPTY); if (slot.compareandset(myitem, null, WAITING, EMPTY)){throw new TimeoutException(); else { slot.set(null, EMPTY); break; 176 Exchanger State EMPTY case EMPTY: // slot is free If reset if (slot.compareandset(heritem, failed, someone showed myitem, up WAITING, BUSY)) after { all, take that item while (System.nanoTime() < timebound){ if (stampholder[0] ] == BUSY) { slot.set(null, EMPTY); if (slot.compareandset(myitem, null, WAITING, EMPTY)){throw new TimeoutException(); else { slot.set(null, EMPTY); break; 177 Exchanger State EMPTY case EMPTY: // slot is free Set if slot (slot.compareandset(heritem, to EMPTY with new myitem, EMPTY, WAITING)) timestamp { and return item found while (System.nanoTime() < timebound){ if (stampholder[0] ] == BUSY) { slot.set(null, EMPTY); if (slot.compareandset(myitem, null, WAITING, EMPTY)){throw new TimeoutException(); else { slot.set(null, EMPTY); break; 178 Exchanger State EMPTY States WAITING and BUSY case EMPTY: // slot is free if (slot.compareandset(heritem, myitem, EMPTY, WAITING)) { while (System.nanoTime() < timebound){ if (stampholder[0] ] == BUSY) { slot.set(null, If EMPTY); initial CAS failed then if (slot.compareandset(myitem, null, WAITING, EMPTY)){throw new TimeoutException(); so retry from start else { slot.set(null, EMPTY); break; 179 someone else changed state from EMPTY to WAITING case WAITING: // someone waiting for me if (slot.compareandset(heritem, myitem, WAITING, BUSY)) break; case BUSY: // others in middle of exchanging break; default: : // impossible break;

31 States WAITING and BUSY case WAITING: // someone waiting for me if (slot.compareandset(heritem, myitem, WAITING, BUSY)) break; case BUSY: // others in middle of exchanging break; default: state // impossible WAITING means break; someone is waiting for an exchange, so attempt to CAS my item in and change state to BUSY States WAITING and BUSY case WAITING: // someone waiting for me if (slot.compareandset(heritem, myitem, WAITING, BUSY)) break; case BUSY: // others in middle of exchanging break; default: // impossible break; If successful return her item, state is now BUSY, otherwise someone else took her item so try again from start States WAITING and BUSY case WAITING: // someone waiting for me if (slot.compareandset(heritem, myitem, WAITING, BUSY)) break; case BUSY: // others in middle of exchanging break; default: // impossible If state is BUSY then break; some other threads are using slot to exchange so start again The Exchanger Slot Exchanger is lock-free Because the only way an exchange can fail is if others repeatedly succeeded or no-one showed up The slot we need does not require symmetric exchange Elimination Array Elimination Array public class EliminationArray { public T visit(t value, int Range) throws TimeoutException { int slot = random.nextint(range); int nanodur = converttonanos(duration, timeunit)); return (exchanger[slot].exchange(value, nanodur) public class EliminationArray { public T visit(t value, int Range) throws TimeoutException { int slot = random.nextint(range); int nanodur = converttonanos(duration, timeunit)); return (exchanger[slot].exchange(value, nanodur) visit the elimination array with a value and a range (duration to wait is not dynamic)

32 Elimination Array Elimination Array public class EliminationArray { Pick a random array entry public T visit(t value, int Range) throws TimeoutException { int slot = random.nextint(range); int nanodur = converttonanos(duration, timeunit)); return (exchanger[slot].exchange(value, nanodur) public class EliminationArray { public T visit(t Exchange value, value int or Range) time throws out TimeoutException { int slot = random.nextint(range); int nanodur = converttonanos(duration, timeunit)); return (exchanger[slot].exchange(value, nanodur) Elimination Stack Push Elimination Stack Push public void push(t value) {... while (true true) ) { if (trypush(node)) { else try { T othervalue = eliminationarray.visit(value,policy.range); if (othervalue == null) ) { public void push(t value) {... while (true) { if (trypush(node)) { else try { T othervalue = eliminationarray.visit(value,policy.range); if (othervalue == null) { First try to push Elimination Stack Push Elimination Stack Push public void push(t value) {... while If (true) failed { back-off to try to eliminate if (trypush(node)) { else try { T othervalue = eliminationarray.visit(value,policy.range); if (othervalue == null) { public void push(t value) {... while Value (true) being { pushed and range to try if (trypush(node)) { else try { T othervalue = eliminationarray.visit(value,policy.range); if (othervalue == null) {

33 Elimination Stack Push public void push(t value) {... Only a pop has null value while (true) { so elimination was successful if (trypush(node)) { else try { T othervalue = eliminationarray.visit(value,policy.range); if (othervalue == null) { Elimination Stack Push public void push(t value) {... Else retry push on lock-free stack while (true) { if (trypush(node)) { else try { T othervalue = eliminationarray.visit(value,policy.range); if (othervalue == null) { Elimination Stack Pop Elimination Stack Pop public T pop() {... while (true true) ) { if (trypop()) { return returnnode.value; else try { T othervalue = eliminationarray.visit(null,policy.range; if othervalue!= null) ) { return othervalue; public T pop() {... Same as push, if non-null other while (true) { if (trypop()) thread must { have pushed return so elimnation returnnode.value; succeeds else try { T othervalue = eliminationarray.visit(null,policy.range; if ( othervalue!= null) ) { return othervalue; Summary We saw both lock-based and lockfree implementations of queues and stacks Don t be quick to declare a data structure inherently sequential Linearizable stack is not inherently sequential ABA is a real problem, pay attention 197 This work is licensed under a 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 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. 33

Concurrent Queues and Stacks. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrent Queues and Stacks. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Queues and Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit The Five-Fold Path Coarse-grained locking Fine-grained locking Optimistic synchronization

More information

Concurrent Queues, Monitors, and the ABA problem. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrent Queues, Monitors, and the ABA problem. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Queues, Monitors, and the ABA problem Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Queues Often used as buffers between producers and consumers

More information

CSE 613: Parallel Programming. Lecture 17 ( Concurrent Data Structures: Queues and Stacks )

CSE 613: Parallel Programming. Lecture 17 ( Concurrent Data Structures: Queues and Stacks ) CSE 613: Parallel Programming Lecture 17 ( Concurrent Data Structures: Queues and Stacks ) Rezaul A. Chowdhury Department of Computer Science SUNY Stony Brook Spring 2012 Desirable Properties of Concurrent

More information

Programming Paradigms for Concurrency Lecture 3 Concurrent Objects

Programming Paradigms for Concurrency Lecture 3 Concurrent Objects Programming Paradigms for Concurrency Lecture 3 Concurrent Objects Based on companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by Thomas Wies New York University

More information

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems : Organization of Programming Languages Threads Classic Concurrency Problems The Dining Philosophers Problem Philosophers either eat or think They must have two forks to eat Can only use forks on either

More information

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages The Dining Philosophers Problem CMSC 0: Organization of Programming Languages Threads Classic Concurrency Problems Philosophers either eat or think They must have two forks to eat Can only use forks on

More information

Linked Lists: Locking, Lock-Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Linked Lists: Locking, Lock-Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Linked Lists: Locking, Lock-Free, and Beyond Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Objects Adding threads should not lower throughput Contention

More information

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems The Dining Philosophers Problem Philosophers either eat or think They must have two forks to eat Can only use forks

More information

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

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Spin Locks and Contention Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Real world concurrency Understanding hardware architecture What is contention How to

More information

Whatever can go wrong will go wrong. attributed to Edward A. Murphy. Murphy was an optimist. authors of lock-free programs LOCK FREE KERNEL

Whatever can go wrong will go wrong. attributed to Edward A. Murphy. Murphy was an optimist. authors of lock-free programs LOCK FREE KERNEL Whatever can go wrong will go wrong. attributed to Edward A. Murphy Murphy was an optimist. authors of lock-free programs LOCK FREE KERNEL 251 Literature Maurice Herlihy and Nir Shavit. The Art of Multiprocessor

More information

Linked Lists: Locking, Lock- Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Linked Lists: Locking, Lock- Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Linked Lists: Locking, Lock- Free, and Beyond Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Coarse-Grained Synchronization Each method locks the object Avoid

More information

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

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Spin Locks and Contention Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Focus so far: Correctness and Progress Models Accurate (we never lied to you) But idealized

More information

Spin Locks and Contention

Spin Locks and Contention Spin Locks and Contention Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified for Software1 students by Lior Wolf and Mati Shomrat Kinds of Architectures

More information

Whatever can go wrong will go wrong. attributed to Edward A. Murphy. Murphy was an optimist. authors of lock-free programs 3.

Whatever can go wrong will go wrong. attributed to Edward A. Murphy. Murphy was an optimist. authors of lock-free programs 3. Whatever can go wrong will go wrong. attributed to Edward A. Murphy Murphy was an optimist. authors of lock-free programs 3. LOCK FREE KERNEL 309 Literature Maurice Herlihy and Nir Shavit. The Art of Multiprocessor

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Threads Synchronization Refers to mechanisms allowing a programmer to control the execution order of some operations across different threads in a concurrent

More information

Blocking Non-blocking Caveat:

Blocking Non-blocking Caveat: Overview of Lecture 5 1 Progress Properties 2 Blocking The Art of Multiprocessor Programming. Maurice Herlihy and Nir Shavit. Morgan Kaufmann, 2008. Deadlock-free: some thread trying to get the lock eventually

More information

Concurrent Objects and Linearizability

Concurrent Objects and Linearizability Chapter 3 Concurrent Objects and Linearizability 3.1 Specifying Objects An object in languages such as Java and C++ is a container for data. Each object provides a set of methods that are the only way

More information

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Threads block when they can t get that lock Wanna have your threads stall? Go ahead, synchronize it all The antidote to this liveness pitfall? Keeping

More information

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Introduction Companion slides for The by Maurice Herlihy & Nir Shavit Moore s Law Transistor count still rising Clock speed flattening sharply 2 Moore s Law (in practice) 3 Nearly Extinct: the Uniprocesor

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Multithreading Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to large compute clusters Can perform multiple

More information

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014 Threads and Locks, Part 2 CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014 1 Goals Cover the material presented in Chapter 2 of our concurrency textbook In particular, selected material

More information

Universality of Consensus. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Universality of Consensus. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Universality of Consensus Companion slides for The by Maurice Herlihy & Nir Shavit Turing Computability 1 0 1 1 0 1 0 A mathematical model of computation Computable = Computable on a T-Machine 2 Shared-Memory

More information

Last Lecture: Spin-Locks

Last Lecture: Spin-Locks Linked Lists: Locking, Lock- Free, and Beyond Last Lecture: Spin-Locks. spin lock CS critical section Resets lock upon exit Today: Concurrent Objects Adding threads should not lower throughput Contention

More information

Concurrent Skip Lists. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrent Skip Lists. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists Companion slides for The by Maurice Herlihy & Nir Shavit Set Object Interface Collection of elements No duplicates Methods add() a new element remove() an element contains() if element

More information

Unit 6: Indeterminate Computation

Unit 6: Indeterminate Computation Unit 6: Indeterminate Computation Martha A. Kim October 6, 2013 Introduction Until now, we have considered parallelizations of sequential programs. The parallelizations were deemed safe if the parallel

More information

Coarse-grained and fine-grained locking Niklas Fors

Coarse-grained and fine-grained locking Niklas Fors Coarse-grained and fine-grained locking Niklas Fors 2013-12-05 Slides borrowed from: http://cs.brown.edu/courses/cs176course_information.shtml Art of Multiprocessor Programming 1 Topics discussed Coarse-grained

More information

Modern High-Performance Locking

Modern High-Performance Locking Modern High-Performance Locking Nir Shavit Slides based in part on The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Locks (Mutual Exclusion) public interface Lock { public void lock();

More information

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course Advanced Java Programming Course MultiThreading By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City Session objectives Introduction Creating thread Thread class

More information

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads? Objectives MultiThreading What are Threads? Interrupting threads Thread properties By Võ Văn Hải Faculty of Information Technologies Summer 2012 Threads priorities Synchronization Callables and Futures

More information

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City Advanced Java Programming Course MultiThreading By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City Session objectives Introduction Creating thread Thread class

More information

Programming Paradigms for Concurrency Lecture 6 Synchronization of Concurrent Objects

Programming Paradigms for Concurrency Lecture 6 Synchronization of Concurrent Objects Programming Paradigms for Concurrency Lecture 6 Synchronization of Concurrent Objects Based on companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by Thomas

More information

Models of concurrency & synchronization algorithms

Models of concurrency & synchronization algorithms Models of concurrency & synchronization algorithms Lecture 3 of TDA383/DIT390 (Concurrent Programming) Carlo A. Furia Chalmers University of Technology University of Gothenburg SP3 2016/2017 Today s menu

More information

Linked Lists: Locking, Lock- Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Linked Lists: Locking, Lock- Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Linked Lists: Locking, Lock- Free, and Beyond Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Last Lecture: Spin-Locks CS. spin lock critical section Resets lock

More information

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013 1 CMSC 433 Programming Language Technologies and Paradigms Spring 2013 Wait / Notify / NotifyAll Optimistic Retries Composition Follow-up (the risk I mentioned) ReentrantLock, Wait, Notify, NotifyAll Some

More information

Lecture 7: Mutual Exclusion 2/16/12. slides adapted from The Art of Multiprocessor Programming, Herlihy and Shavit

Lecture 7: Mutual Exclusion 2/16/12. slides adapted from The Art of Multiprocessor Programming, Herlihy and Shavit Principles of Concurrency and Parallelism Lecture 7: Mutual Exclusion 2/16/12 slides adapted from The Art of Multiprocessor Programming, Herlihy and Shavit Time Absolute, true and mathematical time, of

More information

Concurrent Programming using Threads

Concurrent Programming using Threads Concurrent Programming using Threads Threads are a control mechanism that enable you to write concurrent programs. You can think of a thread in an object-oriented language as a special kind of system object

More information

CS 241 Honors Concurrent Data Structures

CS 241 Honors Concurrent Data Structures CS 241 Honors Concurrent Data Structures Bhuvan Venkatesh University of Illinois Urbana Champaign March 27, 2018 CS 241 Course Staff (UIUC) Lock Free Data Structures March 27, 2018 1 / 43 What to go over

More information

Shared-Memory Computability

Shared-Memory Computability Shared-Memory Computability 10011 Universal Object Wait-free/Lock-free computable = Threads with methods that solve n- consensus Art of Multiprocessor Programming Copyright Herlihy- Shavit 2007 93 GetAndSet

More information

Non-blocking Array-based Algorithms for Stacks and Queues!

Non-blocking Array-based Algorithms for Stacks and Queues! Non-blocking Array-based Algorithms for Stacks and Queues! Niloufar Shafiei! Department of Computer Science and Engineering York University ICDCN 09 Outline! Introduction! Stack algorithm! Queue algorithm!

More information

Lecture 10: Avoiding Locks

Lecture 10: Avoiding Locks Lecture 10: Avoiding Locks CSC 469H1F Fall 2006 Angela Demke Brown (with thanks to Paul McKenney) Locking: A necessary evil? Locks are an easy to understand solution to critical section problem Protect

More information

Java theory and practice: Going atomic The new atomic classes are the hidden gems of java.util.concurrent

Java theory and practice: Going atomic The new atomic classes are the hidden gems of java.util.concurrent 1 of 8 Java theory and practice: Going atomic The new atomic classes are the hidden gems of java.util.concurrent Level: Intermediate Brian Goetz (brian@quiotix.com), Principal Consultant, Quiotix 23 Nov

More information

Non-blocking Array-based Algorithms for Stacks and Queues. Niloufar Shafiei

Non-blocking Array-based Algorithms for Stacks and Queues. Niloufar Shafiei Non-blocking Array-based Algorithms for Stacks and Queues Niloufar Shafiei Outline Introduction Concurrent stacks and queues Contributions New algorithms New algorithms using bounded counter values Correctness

More information

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430 Implementing Mutual Exclusion Sarah Diesburg Operating Systems CS 3430 From the Previous Lecture The too much milk example shows that writing concurrent programs directly with load and store instructions

More information

Java ReentrantLock (Part 1)

Java ReentrantLock (Part 1) Java ReentrantLock (Part 1) Douglas C. Schmidt d.schmidt@vanderbilt.edu www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA Learning

More information

Hashing and Natural Parallism. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Hashing and Natural Parallism. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Hashing and Natural Parallism Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Sequential Closed Hash Map 0 1 2 3 16 9 buckets 2 Items h(k) = k mod 4 Art of Multiprocessor

More information

COMP 322: Fundamentals of Parallel Programming

COMP 322: Fundamentals of Parallel Programming COMP 322: Fundamentals of Parallel Programming https://wiki.rice.edu/confluence/display/parprog/comp322 Lecture 30: Advanced locking in Java Vivek Sarkar Department of Computer Science Rice University

More information

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming. Lecture 07: Advanced Thread programming Software System Components 2 Behzad Bordbar School of Computer Science, University of Birmingham, UK Recap How to deal with race condition in Java Using synchronised

More information

Fine-grained synchronization & lock-free programming

Fine-grained synchronization & lock-free programming Lecture 17: Fine-grained synchronization & lock-free programming Parallel Computer Architecture and Programming CMU 15-418/15-618, Spring 2016 Tunes Minnie the Moocher Robbie Williams (Swings Both Ways)

More information

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Operating Systems Lecture 4 - Concurrency and Synchronization Adrien Krähenbühl Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Mutual exclusion Hardware solutions Semaphores IPC: Message passing

More information

Concurrency in Java Prof. Stephen A. Edwards

Concurrency in Java Prof. Stephen A. Edwards Concurrency in Java Prof. Stephen A. Edwards The Java Language Developed by James Gosling et al. at Sun Microsystems in the early 1990s Originally called Oak, first intended application was as an OS for

More information

Stacks and Queues

Stacks and Queues Stacks and Queues 2-25-2009 1 Opening Discussion Let's look at solutions to the interclass problem. Do you have any questions about the reading? Do you have any questions about the assignment? Minute Essays

More information

Hazard Pointers. Number of threads unbounded time to check hazard pointers also unbounded! difficult dynamic bookkeeping! thread B - hp1 - hp2

Hazard Pointers. Number of threads unbounded time to check hazard pointers also unbounded! difficult dynamic bookkeeping! thread B - hp1 - hp2 Hazard Pointers Store pointers of memory references about to be accessed by a thread Memory allocation checks all hazard pointers to avoid the ABA problem thread A - hp1 - hp2 thread B - hp1 - hp2 thread

More information

CS510 Concurrent Systems. Jonathan Walpole

CS510 Concurrent Systems. Jonathan Walpole CS510 Concurrent Systems Jonathan Walpole Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms utline Background Non-Blocking Queue Algorithm Two Lock Concurrent Queue Algorithm

More information

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004 Lecture 8: Semaphores, Monitors, & Condition Variables 8.0 Main Points: Definition of semaphores Example of use

More information

JFokus Finding And Solving Java Deadlocks

JFokus Finding And Solving Java Deadlocks JFokus 03 - Finding And Solving Java Deadlocks Dr Heinz Kabutz JFokus 03 - Finding and Solving Java Deadlocks Heinz Kabutz German from Cape Town, now lives in Chania PhD Computer Science from University

More information

The Relative Power of Synchronization Methods

The Relative Power of Synchronization Methods Chapter 5 The Relative Power of Synchronization Methods So far, we have been addressing questions of the form: Given objects X and Y, is there a wait-free implementation of X from one or more instances

More information

Locks Chapter 4. Overview. Introduction Spin Locks. Queue locks. Roger Wattenhofer. Test-and-Set & Test-and-Test-and-Set Backoff lock

Locks Chapter 4. Overview. Introduction Spin Locks. Queue locks. Roger Wattenhofer. Test-and-Set & Test-and-Test-and-Set Backoff lock Locks Chapter 4 Roger Wattenhofer ETH Zurich Distributed Computing www.disco.ethz.ch Overview Introduction Spin Locks Test-and-Set & Test-and-Test-and-Set Backoff lock Queue locks 4/2 1 Introduction: From

More information

Concurrent Objects. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrent Objects. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Objects Companion slides for The by Maurice Herlihy & Nir Shavit Concurrent Computation memory object object 2 Objectivism What is a concurrent object? How do we describe one? How do we implement

More information

Peterson s Algorithm

Peterson s Algorithm Peterson s Algorithm public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; } 24/03/10 Art of Multiprocessor Programming 1 Mutual

More information

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems More Synchronization; Concurrency in Java CS 475, Spring 2018 Concurrent & Distributed Systems Review: Semaphores Synchronization tool that provides more sophisticated ways (than Mutex locks) for process

More information

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions CMSC 330: Organization of Programming Languages Multithreaded Programming Patterns in Java CMSC 330 2 Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to

More information

CMSC 433 Programming Language Technologies and Paradigms Fall Locks & Conditions

CMSC 433 Programming Language Technologies and Paradigms Fall Locks & Conditions CMSC 433 Programming Language Technologies and Paradigms Fall 2011 Locks & Conditions Intrinsic Lock Example public class RWDictionaryIntrinsicLock { Map m = new TreeMap();

More information

Locking Granularity. CS 475, Spring 2019 Concurrent & Distributed Systems. With material from Herlihy & Shavit, Art of Multiprocessor Programming

Locking Granularity. CS 475, Spring 2019 Concurrent & Distributed Systems. With material from Herlihy & Shavit, Art of Multiprocessor Programming Locking Granularity CS 475, Spring 2019 Concurrent & Distributed Systems With material from Herlihy & Shavit, Art of Multiprocessor Programming Discussion: HW1 Part 4 addtolist(key1, newvalue) Thread 1

More information

CPS 310 first midterm exam, 10/6/2014

CPS 310 first midterm exam, 10/6/2014 CPS 310 first midterm exam, 10/6/2014 Your name please: Part 1. More fun with fork and exec* What is the output generated by this program? Please assume that each executed print statement completes, e.g.,

More information

CS11 Java. Fall Lecture 7

CS11 Java. Fall Lecture 7 CS11 Java Fall 2006-2007 Lecture 7 Today s Topics All about Java Threads Some Lab 7 tips Java Threading Recap A program can use multiple threads to do several things at once A thread can have local (non-shared)

More information

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable) CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable) Past & Present Have looked at two constraints: Mutual exclusion constraint between two events is a requirement that

More information

CMSC421: Principles of Operating Systems

CMSC421: Principles of Operating Systems CMSC421: Principles of Operating Systems Nilanjan Banerjee Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/ Principles of Operating

More information

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

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Spin Locks and Contention Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit MIMD Architectures memory Shared Bus Memory Contention Communication Contention Communication

More information

Chapter 32 Multithreading and Parallel Programming

Chapter 32 Multithreading and Parallel Programming Chapter 32 Multithreading and Parallel Programming 1 Objectives To get an overview of multithreading ( 32.2). To develop task classes by implementing the Runnable interface ( 32.3). To create threads to

More information

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Page 1. Goals for Today Atomic Read-Modify-Write instructions Examples of Read-Modify-Write Goals for Today" CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables" Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors

More information

Lecture 21: Transactional Memory. Topics: Hardware TM basics, different implementations

Lecture 21: Transactional Memory. Topics: Hardware TM basics, different implementations Lecture 21: Transactional Memory Topics: Hardware TM basics, different implementations 1 Transactions New paradigm to simplify programming instead of lock-unlock, use transaction begin-end locks are blocking,

More information

Concurrent Preliminaries

Concurrent Preliminaries Concurrent Preliminaries Sagi Katorza Tel Aviv University 09/12/2014 1 Outline Hardware infrastructure Hardware primitives Mutual exclusion Work sharing and termination detection Concurrent data structures

More information

What's wrong with Semaphores?

What's wrong with Semaphores? Next: Monitors and Condition Variables What is wrong with semaphores? Monitors What are they? How do we implement monitors? Two types of monitors: Mesa and Hoare Compare semaphore and monitors Lecture

More information

Linked Lists: The Role of Locking. Erez Petrank Technion

Linked Lists: The Role of Locking. Erez Petrank Technion Linked Lists: The Role of Locking Erez Petrank Technion Why Data Structures? Concurrent Data Structures are building blocks Used as libraries Construction principles apply broadly This Lecture Designing

More information

PROCESS SYNCHRONIZATION

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization

More information

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer. CMSC 330: Organization of Programming Languages Threads Computation Abstractions t1 t2 t1 t3 t2 t1 p1 p2 p3 p4 CPU 1 CPU 2 A computer t4 t5 Processes (e.g., JVM s) Threads CMSC 330 2 Processes vs. Threads

More information

Locking: A necessary evil? Lecture 10: Avoiding Locks. Priority Inversion. Deadlock

Locking: A necessary evil? Lecture 10: Avoiding Locks. Priority Inversion. Deadlock Locking: necessary evil? Lecture 10: voiding Locks CSC 469H1F Fall 2006 ngela Demke Brown (with thanks to Paul McKenney) Locks are an easy to understand solution to critical section problem Protect shared

More information

1 P age DS & OOPS / UNIT II

1 P age DS & OOPS / UNIT II UNIT II Stacks: Definition operations - applications of stack. Queues: Definition - operations Priority queues - De que Applications of queue. Linked List: Singly Linked List, Doubly Linked List, Circular

More information

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: concurrency Outline Java threads thread implementation sleep, interrupt, and join threads that return values Thread synchronization

More information

Week 6. Data structures

Week 6. Data structures 1 2 3 4 5 n Week 6 Data structures 6 7 n 8 General remarks We start considering Part III Data Structures from CLRS. As a first example we consider two special of buffers, namely stacks and queues. Reading

More information

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

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Spin Locks and Contention Companion slides for The Programming by Maurice Herlihy & Nir Shavit Focus so far: Correctness Models Accurate (we never lied to you) But idealized (so we forgot to mention a

More information

Dr.-Ing. Michael Eichberg

Dr.-Ing. Michael Eichberg Dr.-Ing. Michael Eichberg Concurrent Programming (Overview of the Java concurrency model and its relationship to other models...) (some slides are based on slides by Andy Wellings) 2 Processes vs. Threads

More information

Fast and Lock-Free Concurrent Priority Queues for Multi-Thread Systems

Fast and Lock-Free Concurrent Priority Queues for Multi-Thread Systems Fast and Lock-Free Concurrent Priority Queues for Multi-Thread Systems Håkan Sundell Philippas Tsigas Outline Synchronization Methods Priority Queues Concurrent Priority Queues Lock-Free Algorithm: Problems

More information

G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency G52CON: Concepts of Concurrency Lecture 11 Synchronisation in Java II Natasha Alechina School of Computer Science nza@cs.nott.ac.uk Outline of this lecture Mutual exclusion in Java continued (next lecture:

More information

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019 CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion Ruth Anderson Winter 2019 Toward sharing resources (memory) So far, we have been studying parallel algorithms

More information

Lecture 9: Introduction to Monitors

Lecture 9: Introduction to Monitors COMP 150-CCP Concurrent Programming Lecture 9: Introduction to Monitors Dr. Richard S. Hall rickhall@cs.tufts.edu Concurrent programming February 14, 2008 Abstracting Locking Details Recall our discussion

More information

Lindsay Groves, Simon Doherty. Mark Moir, Victor Luchangco

Lindsay Groves, Simon Doherty. Mark Moir, Victor Luchangco Lindsay Groves, Simon Doherty Victoria University of Wellington Mark Moir, Victor Luchangco Sun Microsystems, Boston (FORTE, Madrid, September, 2004) Lock-based concurrency doesn t scale Lock-free/non-blocking

More information

Advance Operating Systems (CS202) Locks Discussion

Advance Operating Systems (CS202) Locks Discussion Advance Operating Systems (CS202) Locks Discussion Threads Locks Spin Locks Array-based Locks MCS Locks Sequential Locks Road Map Threads Global variables and static objects are shared Stored in the static

More information

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Page 1. Goals for Today Atomic Read-Modify-Write instructions Examples of Read-Modify-Write Goals for Today" CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables" Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors

More information

Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory

Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory Controlling access by concurrent processes to shared writeable data has been studied as

More information

Synchronization in Concurrent Programming. Amit Gupta

Synchronization in Concurrent Programming. Amit Gupta Synchronization in Concurrent Programming Amit Gupta Announcements Project 1 grades are out on blackboard. Detailed Grade sheets to be distributed after class. Project 2 grades should be out by next Thursday.

More information

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007 CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Threads and Synchronization May 8, 2007 Computation Abstractions t1 t1 t4 t2 t1 t2 t5 t3 p1 p2 p3 p4 CPU 1 CPU 2 A computer Processes

More information

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list. A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list. the first element of the list a new element to (the Top of)

More information

Lecture 21: Transactional Memory. Topics: consistency model recap, introduction to transactional memory

Lecture 21: Transactional Memory. Topics: consistency model recap, introduction to transactional memory Lecture 21: Transactional Memory Topics: consistency model recap, introduction to transactional memory 1 Example Programs Initially, A = B = 0 P1 P2 A = 1 B = 1 if (B == 0) if (A == 0) critical section

More information

Outline of lecture. i219 Software Design Methodology 10. Multithreaded programming. Kazuhiro Ogata (JAIST)

Outline of lecture. i219 Software Design Methodology 10. Multithreaded programming. Kazuhiro Ogata (JAIST) i219 Software Design Methodology 10. Multithreaded programming Kazuhiro Ogata (JAIST) Outline of lecture 2 Thread Race condition Synchronization Deadlock Bounded buffer problem Thread (1) 3 Units of execution.

More information

Lecture 9: Multiprocessor OSs & Synchronization. CSC 469H1F Fall 2006 Angela Demke Brown

Lecture 9: Multiprocessor OSs & Synchronization. CSC 469H1F Fall 2006 Angela Demke Brown Lecture 9: Multiprocessor OSs & Synchronization CSC 469H1F Fall 2006 Angela Demke Brown The Problem Coordinated management of shared resources Resources may be accessed by multiple threads Need to control

More information

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 13 Robert Grimm, New York University 1 Review Last week Exceptions 2 Outline Concurrency Discussion of Final Sources for today s lecture: PLP, 12

More information

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write Goals for Today CS162 Operating Systems and Systems Programming Lecture 5 Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors and condition variables Semaphores,

More information

Fine-grained synchronization & lock-free data structures

Fine-grained synchronization & lock-free data structures Lecture 19: Fine-grained synchronization & lock-free data structures Parallel Computer Architecture and Programming Redo Exam statistics Example: a sorted linked list struct Node { int value; Node* next;

More information

Parallel GC. (Chapter 14) Eleanor Ainy December 16 th 2014

Parallel GC. (Chapter 14) Eleanor Ainy December 16 th 2014 GC (Chapter 14) Eleanor Ainy December 16 th 2014 1 Outline of Today s Talk How to use parallelism in each of the 4 components of tracing GC: Marking Copying Sweeping Compaction 2 Introduction Till now

More information