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

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Concurrent Queues and Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
1 Chapter 4 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization.
Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock- Free Objects” Presentation Robert T. Bauer.
Monitors & Blocking Synchronization 1. Producers & Consumers Problem Two threads that communicate through a shared FIFO queue. These two threads can’t.
Barrier Synchronization Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Introduction Companion slides for
Concurrent Queues and Stacks The Art of Multiprocessor Programming Spring 2007.
Linked Lists: Lazy and Non-Blocking Synchronization Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)
Spin Locks and Contention Based on slides by by Maurice Herlihy & Nir Shavit Tomer Gurevich.
Scalable Synchronous Queues By William N. Scherer III, Doug Lea, and Michael L. Scott Presented by Ran Isenberg.
Lecture 6-2 : Concurrent Queues and Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Art of Multiprocessor Programming1 Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified.
Concurrent Queues and Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Shared Counters and Parallelism Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Monitors and Blocking Synchronization By Tal Walter.
Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09.
CS510 Advanced OS Seminar Class 10 A Methodology for Implementing Highly Concurrent Data Objects by Maurice Herlihy.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Concurrent Queues and Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Stacks, Queues, and Deques
שירן חליבה Concurrent Queues. Outline: Some definitions 3 queue implementations : A Bounded Partial Queue An Unbounded Total Queue An Unbounded Lock-Free.
Multicore Programming
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
מרצה: יהודה אפק מגיש: ערן שרגיאן. OutlineOutline Quick reminder of the Stack structure. The Unbounded Lock-Free Stack. The Elimination Backoff Stack.
JAVA MEMORY MODEL AND ITS IMPLICATIONS Srikanth Seshadri
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Gal Milman Based on Chapter 10 (Concurrent Queues and the ABA Problem) in The Art of Multiprocessor Programming by Herlihy and Shavit Seminar 2 (236802)
Programming Language Basics Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Multiprocessor Architecture Basics Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
A Methodology for Implementing Highly Concurrent Data Objects by Maurice Herlihy Slides by Vincent Rayappa.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
SYNAR Systems Networking and Architecture Group CMPT 886: The Art of Scalable Synchronization Dr. Alexandra Fedorova School of Computing Science SFU.
Concurrent Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Shared Counters and Parallelism Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Scalable lock-free Stack Algorithm Wael Yehia York University February 8, 2010.
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.
Distributed Algorithms (22903) Lecturer: Danny Hendler Lock-free stack algorithms.
The Relative Power of Synchronization Operations Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
CSC Multiprocessor Programming, Spring, 2012 Chapter 10 – Avoiding Liveness Hazards Dr. Dale E. Parson, week 11.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Lecture 9 : Concurrent Data Structures Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Review Array Array Elements Accessing array elements
Monitors and Blocking Synchronization
Concurrent Objects Companion slides for
Distributed Algorithms (22903)
CSC Multiprocessor Programming, Spring, 2011
Distributed Algorithms (22903)
Distributed Algorithms (22903)
Concurrent Queues and Stacks
Concurrent Queues and Stacks
Producer-Consumer Problem
Barrier Synchronization
Shared Counters and Parallelism
Stacks, Queues, and Deques
Presentation transcript:

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

Art of Multiprocessor Programming© Herlihy-Shavit The Five-Fold Path Coarse-grained locking Fine-grained locking Optimistic synchronization Lazy synchronization Lock-free synchronization

Art of Multiprocessor Programming© Herlihy-Shavit Another Fundamental Problem We told you about –Sets implemented using linked lists Next: queues Next: stacks

Art of Multiprocessor Programming© Herlihy-Shavit Queues & Stacks Both: pool of items Queue –enq() & deq() –First-in-first-out (FIFO) order Stack –push() & pop() –Last-in-first-out (LIFO) order

Art of Multiprocessor Programming© Herlihy-Shavit Bounded vs Unbounded Bounded –Fixed capacity –Good when resources an issue Unbounded –Holds any number of objects

Art of Multiprocessor Programming© Herlihy-Shavit Blocking vs Non-Blocking Problem cases: –Removing from empty pool –Adding to full (bounded) pool Blocking –Caller waits until state changes Non-Blocking –Method throws exception

Art of Multiprocessor Programming© Herlihy-Shavit This Lecture Bounded, Blocking, Lock-based Queue Unbounded, Non-Blocking, Lock-free Queue Monitors ABA problem Unbounded Non-Blocking Lock-free Stack Elimination-Backoff Stack

Art of Multiprocessor Programming© Herlihy-Shavit Queue: Concurrency enq(x) y=deq() enq() and deq() work at different ends of the object tailhead

Art of Multiprocessor Programming© Herlihy-Shavit Concurrency enq(x) Challenge: what if the queue is empty or full? y=deq() tail head

Art of Multiprocessor Programming© Herlihy-Shavit Bounded Queue Sentinel head tail

Art of Multiprocessor Programming© Herlihy-Shavit Bounded Queue head tail First actual item

Art of Multiprocessor Programming© Herlihy-Shavit Bounded Queue head tail Lock out other deq() calls deqLock

Art of Multiprocessor Programming© Herlihy-Shavit Bounded Queue head tail Lock out other enq() calls deqLock enqLock

Art of Multiprocessor Programming© Herlihy-Shavit Not Done Yet head tail deqLock enqLock Need to tell whether queue is full or empty

Art of Multiprocessor Programming© Herlihy-Shavit Not Done Yet head tail deqLock enqLock Permission to enqueue 8 items permits 8

Art of Multiprocessor Programming© Herlihy-Shavit Not Done Yet head tail deqLock enqLock Incremented by deq() Decremented by enq() permits 8

Art of Multiprocessor Programming© Herlihy-Shavit Enqueuer head tail deqLock enqLock permits 8 Lock enqLock

Art of Multiprocessor Programming© Herlihy-Shavit Enqueuer head tail deqLock enqLock permits 8 Read permits OK

Art of Multiprocessor Programming© Herlihy-Shavit Enqueuer head tail deqLock enqLock permits 8 No need to lock tail

Art of Multiprocessor Programming© Herlihy-Shavit Enqueuer head tail deqLock enqLock permits 8 Enqueue Node

Art of Multiprocessor Programming© Herlihy-Shavit Enqueuer head tail deqLock enqLock permits 8 7 getAndDecrement()

Art of Multiprocessor Programming© Herlihy-Shavit Enqueuer head tail deqLock enqLock permits 8 Release lock 7

Art of Multiprocessor Programming© Herlihy-Shavit Enqueuer head tail deqLock enqLock permits 7 If queue was empty, notify/signal waiting dequeuers

Art of Multiprocessor Programming© Herlihy-Shavit Unsuccesful Enqueuer head tail deqLock enqLock permits 0 Uh-oh Read permits

Art of Multiprocessor Programming© Herlihy-Shavit Dequeuer head tail deqLock enqLock permits 8 Lock deqLock

Art of Multiprocessor Programming© Herlihy-Shavit Dequeuer head tail deqLock enqLock permits 7 Read sentinel’s next field OK

Art of Multiprocessor Programming© Herlihy-Shavit Dequeuer head tail deqLock enqLock permits 7 Read value

Art of Multiprocessor Programming© Herlihy-Shavit Dequeuer head tail deqLock enqLock permits 7 Make first Node new sentinel

Art of Multiprocessor Programming© Herlihy-Shavit Dequeuer head tail deqLock enqLock permits 8 Increment permits

Art of Multiprocessor Programming© Herlihy-Shavit Dequeuer head tail deqLock enqLock permits 7 Release deqLock

Art of Multiprocessor Programming© Herlihy-Shavit Unsuccesful Dequeuer head tail deqLock enqLock permits 8 Read sentinel’s next field uh-oh

Art of Multiprocessor Programming© Herlihy-Shavit Bounded Queue public class BoundedQueue { ReentrantLock enqLock, deqLock; Condition notEmptyCondition, notFullCondition; AtomicInteger permits; Node head; Node tail; int capacity; enqLock = new ReentrantLock(); notFullCondition = enqLock.newCondition(); deqLock = new ReentrantLock(); notEmptyCondition = deqLock.newCondition(); }

Art of Multiprocessor Programming© Herlihy-Shavit Bounded Queue public class BoundedQueue { ReentrantLock enqLock, deqLock; Condition notEmptyCondition, notFullCondition; AtomicInteger permits; Node head; Node tail; int capacity; enqLock = new ReentrantLock(); notFullCondition = enqLock.newCondition(); deqLock = new ReentrantLock(); notEmptyCondition = deqLock.newCondition(); } Enq & deq locks

Art of Multiprocessor Programming© Herlihy-Shavit Digression: Monitor Locks 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

Art of Multiprocessor Programming© Herlihy-Shavit public interface Lock { void lock(); void lockInterruptibly() throws InterruptedException; boolean tryLock(); boolean tryLock(long time, TimeUnit unit); Condition newCondition(); void unlock; } The Java Lock Interface Acquire lock

Art of Multiprocessor Programming© Herlihy-Shavit public interface Lock { void lock(); void lockInterruptibly() throws InterruptedException; boolean tryLock(); boolean tryLock(long time, TimeUnit unit); Condition newCondition(); void unlock; } The Java Lock Interface Release lock

Art of Multiprocessor Programming© Herlihy-Shavit public interface Lock { void lock(); void lockInterruptibly() throws InterruptedException; boolean tryLock(); boolean tryLock(long time, TimeUnit unit); Condition newCondition(); void unlock; } The Java Lock Interface Try for lock, but not too hard

Art of Multiprocessor Programming© Herlihy-Shavit public interface Lock { void lock(); void lockInterruptibly() throws InterruptedException; boolean tryLock(); boolean tryLock(long time, TimeUnit unit); Condition newCondition(); void unlock; } The Java Lock Interface Create condition to wait on

Art of Multiprocessor Programming© Herlihy-Shavit 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; } Guess what this method does?

Art of Multiprocessor Programming© Herlihy-Shavit Lock Conditions public interface Condition { void await(); boolean await(long time, TimeUnit unit); … void signal(); void signalAll(); }

Art of Multiprocessor Programming© Herlihy-Shavit public interface Condition { void await(); boolean await(long time, TimeUnit unit); … void signal(); void signalAll(); } Lock Conditions Release lock and wait on condition

Art of Multiprocessor Programming© Herlihy-Shavit public interface Condition { void await(); boolean await(long time, TimeUnit unit); … void signal(); void signalAll(); } Lock Conditions Wake up one waiting thread

Art of Multiprocessor Programming© Herlihy-Shavit public interface Condition { void await(); boolean await(long time, TimeUnit unit); … void signal(); void signalAll(); } Lock Conditions Wake up all waiting threads

Art of Multiprocessor Programming© Herlihy-Shavit Await Releases lock associated with q Sleeps (gives up processor) Awakens (resumes running) Reacquires lock & returns q.await()

Art of Multiprocessor Programming© Herlihy-Shavit Signal Awakens one waiting thread –Which will reacquire lock q.signal();

Art of Multiprocessor Programming© Herlihy-Shavit Signal All Awakens all waiting threads –Which will each reacquire lock q.signalAll();

Art of Multiprocessor Programming© Herlihy-Shavit A Monitor Lock Critical Section waiting room lock() unLock()

Art of Multiprocessor Programming© Herlihy-Shavit Unsuccessful Deq Critical Section waiting room lock() await() deq() Oh no, empty!

Art of Multiprocessor Programming© Herlihy-Shavit Another One Critical Section waiting room lock() await() deq() Oh no, empty!

Art of Multiprocessor Programming© Herlihy-Shavit Enqueuer to the Rescue Critical Section waiting room lock() signalAll() enq( ) unLock() Yawn!

Art of Multiprocessor Programming© Herlihy-Shavit Yawn! Monitor Signalling Critical Section waiting room Yawn! Awakened thread might still lose lock to outside contender…

Art of Multiprocessor Programming© Herlihy-Shavit Dequeurs Signalled Critical Section waiting room Found it Yawn!

Art of Multiprocessor Programming© Herlihy-Shavit Yawn! Dequeurs Signalled Critical Section waiting room Still empty!

Art of Multiprocessor Programming© Herlihy-Shavit Dollar Short + Day Late Critical Section waiting room

Art of Multiprocessor Programming© Herlihy-Shavit Lost Wake-Up Critical Section waiting room lock() signal () enq( ) unLock() Yawn!

Art of Multiprocessor Programming© Herlihy-Shavit Lost Wake-Up Critical Section waiting room lock() enq( ) unLock() Yawn!

Art of Multiprocessor Programming© Herlihy-Shavit Lost Wake-Up Critical Section waiting room Yawn!

Art of Multiprocessor Programming© Herlihy-Shavit Lost Wake-Up Critical Section waiting room Found it

Art of Multiprocessor Programming© Herlihy-Shavit What’s Wrong Here? Critical Section waiting room zzzz….!

Solution to Lost Wakeup Always use signalAll and notifyAll Not signal and notify Art of Multiprocessor Programming© Herlihy-Shavit

Art of Multiprocessor Programming© Herlihy-Shavit public class Queue { int head = 0, tail = 0; T[QSIZE] items; public synchronized T deq() { while (tail – head == 0) this.wait(); T result = items[head % QSIZE]; head++; this.notifyAll(); return result; } … }} Java Synchronized Methods

Art of Multiprocessor Programming© Herlihy-Shavit public class Queue { int head = 0, tail = 0; T[QSIZE] items; public synchronized T deq() { while (tail – head == 0) this.wait(); T result = items[head % QSIZE]; head++; this.notifyAll(); return result; } … }} Java Synchronized Methods Each object has an implicit lock with an implicit condition

Art of Multiprocessor Programming© Herlihy-Shavit public class Queue { int head = 0, tail = 0; T[QSIZE] items; public synchronized T deq() { while (tail – head == 0) this.wait(); T result = items[head % QSIZE]; head++; this.notifyAll(); return result; } … }} Java Synchronized Methods Lock on entry, unlock on return

Art of Multiprocessor Programming© Herlihy-Shavit public class Queue { int head = 0, tail = 0; T[QSIZE] items; public synchronized T deq() { while (tail – head == 0) this.wait(); T result = items[head % QSIZE]; head++; this.notifyAll(); return result; } … }} Java Synchronized Methods Wait on implicit condition

Art of Multiprocessor Programming© Herlihy-Shavit public class Queue { int head = 0, tail = 0; T[QSIZE] items; public synchronized T deq() { while (tail – head == 0) this.wait(); T result = items[head % QSIZE]; head++; this.notifyAll(); return result; } … }} Java Synchronized Methods Signal all threads waiting on condition

Art of Multiprocessor Programming© Herlihy-Shavit (Pop!) The Bounded Queue public class BoundedQueue { ReentrantLock enqLock, deqLock; Condition notEmptyCondition, notFullCondition; AtomicInteger permits; Node head; Node tail; int capacity; enqLock = new ReentrantLock(); notFullCondition = enqLock.newCondition(); deqLock = new ReentrantLock(); notEmptyCondition = deqLock.newCondition(); }

Art of Multiprocessor Programming© Herlihy-Shavit Bounded Queue Fields public class BoundedQueue { ReentrantLock enqLock, deqLock; Condition notEmptyCondition, notFullCondition; AtomicInteger permits; Node head; Node tail; int capacity; enqLock = new ReentrantLock(); notFullCondition = enqLock.newCondition(); deqLock = new ReentrantLock(); notEmptyCondition = deqLock.newCondition(); } Enq & deq locks

Art of Multiprocessor Programming© Herlihy-Shavit Bounded Queue Fields public class BoundedQueue { ReentrantLock enqLock, deqLock; Condition notEmptyCondition, notFullCondition; AtomicInteger permits; Node head; Node tail; int capacity; enqLock = new ReentrantLock(); notFullCondition = enqLock.newCondition(); deqLock = new ReentrantLock(); notEmptyCondition = deqLock.newCondition(); } Enq lock’s associated condition

Art of Multiprocessor Programming© Herlihy-Shavit Bounded Queue Fields public class BoundedQueue { ReentrantLock enqLock, deqLock; Condition notEmptyCondition, notFullCondition; AtomicInteger permits; Node head; Node tail; int capacity; enqLock = new ReentrantLock(); notFullCondition = enqLock.newCondition(); deqLock = new ReentrantLock(); notEmptyCondition = deqLock.newCondition(); } Num permits: 0 to capacity

Art of Multiprocessor Programming© Herlihy-Shavit Bounded Queue Fields public class BoundedQueue { ReentrantLock enqLock, deqLock; Condition notEmptyCondition, notFullCondition; AtomicInteger permits; Node head; Node tail; int capacity; enqLock = new ReentrantLock(); notFullCondition = enqLock.newCondition(); deqLock = new ReentrantLock(); notEmptyCondition = deqLock.newCondition(); } Head and Tail

Art of Multiprocessor Programming© Herlihy-Shavit Enq Method Part One public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (permits.get() == 0) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = e; if (permits.getAndDecrement() == capacity) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … }

Art of Multiprocessor Programming© Herlihy-Shavit public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (permits.get() == 0) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = e; if (permits.getAndDecrement() == capacity) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … } Enq Method Part One Lock and unlock enq lock

Art of Multiprocessor Programming© Herlihy-Shavit public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (permits.get() == 0) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = e; if (permits.getAndDecrement() == capacity) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … } Enq Method Part One If queue is full, patiently await further instructions …

Art of Multiprocessor Programming© Herlihy-Shavit public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (permits.get() == 0) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = e; if (permits.getAndDecrement() == capacity) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … } Be Afraid How do we know the permits field won’t change?

Art of Multiprocessor Programming© Herlihy-Shavit public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (permits.get() == 0) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = e; if (permits.getAndDecrement() == capacity) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … } Enq Method Part One Add new node

Art of Multiprocessor Programming© Herlihy-Shavit public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (permits.get() == 0) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = e; if (permits.getAndDecrement() == capacity) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … } Enq Method Part One If queue was empty, wake frustrated dequeuers

Art of Multiprocessor Programming© Herlihy-Shavit Enq Method Part Deux public void enq(T x) { … if (mustWakeDequeuers) { deqLock.lock(); try { notEmptyCondition.signalAll(); } finally { deqLock.unlock(); }

Art of Multiprocessor Programming© Herlihy-Shavit Enq Method Part Deux public void enq(T x) { … if (mustWakeDequeuers) { deqLock.lock(); try { notEmptyCondition.signalAll(); } finally { deqLock.unlock(); } Are there dequeuers to be signaled?

Art of Multiprocessor Programming© Herlihy-Shavit public void enq(T x) { … if (mustWakeDequeuers) { deqLock.lock(); try { notEmptyCondition.signalAll(); } finally { deqLock.unlock(); } Enq Method Part Deux Lock and unlock deq lock

Art of Multiprocessor Programming© Herlihy-Shavit public void enq(T x) { … if (mustWakeDequeuers) { deqLock.lock(); try { notEmptyCondition.signalAll(); } finally { deqLock.unlock(); } Enq Method Part Deux Signal dequeuers that queue no longer empty

Art of Multiprocessor Programming© Herlihy-Shavit The Enq() & Deq() Methods 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?

Art of Multiprocessor Programming© Herlihy-Shavit Split the Counter The enq() method –Decrements only –Cares only if value is zero The deq() method –Increments only –Cares only if value is capacity

Art of Multiprocessor Programming© Herlihy-Shavit Split Counter Enqueuer decrements enqSidePermits Dequeuer increments deqSidePermits When enqueuer runs out –Locks deqLock –Transfers permits Intermittent synchronization –Not with each method call –Need both locks! (careful …)

Art of Multiprocessor Programming© Herlihy-Shavit A Lock-Free Queue Sentinel head tail

Art of Multiprocessor Programming© Herlihy-Shavit Compare and Set CAS

Art of Multiprocessor Programming© Herlihy-Shavit Enqueue head tail Enq( )

Art of Multiprocessor Programming© Herlihy-Shavit Enqueue head tail

Art of Multiprocessor Programming© Herlihy-Shavit Logical Enqueue head tail CAS

Art of Multiprocessor Programming© Herlihy-Shavit Physical Enqueue head tail Enqueue Node CAS

Art of Multiprocessor Programming© Herlihy-Shavit Enqueue These two steps are not atomic The tail field refers to either –Actual last Node (good) –Penultimate Node (not so good) Be prepared!

Art of Multiprocessor Programming© Herlihy-Shavit Enqueue What do you do if you find –A trailing tail? Stop and help fix it –If tail node has non-null next field –CAS the queue’s tail field to tail.next As in the universal construction

Art of Multiprocessor Programming© Herlihy-Shavit When CASs Fail During logical enqueue –Abandon hope, restart –Still lock-free (why?) During physical enqueue –Ignore it (why?)

Art of Multiprocessor Programming© Herlihy-Shavit Dequeuer head tail Read value

Art of Multiprocessor Programming© Herlihy-Shavit Dequeuer head tail Make first Node new sentinel CAS

Art of Multiprocessor Programming© Herlihy-Shavit 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?

Art of Multiprocessor Programming© Herlihy-Shavit Dequeuer head tail CAS Can recycle

Art of Multiprocessor Programming© Herlihy-Shavit 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 …

Art of Multiprocessor Programming© Herlihy-Shavit Why Recycling is Hard Free pool headtail Want to redirect tail from grey to red zzz…

Art of Multiprocessor Programming© Herlihy-Shavit Both Nodes Reclaimed Free pool zzz headtail

Art of Multiprocessor Programming© Herlihy-Shavit One Node Recycled Free pool Yawn! headtail

Art of Multiprocessor Programming© Herlihy-Shavit Why Recycling is Hard Free pool CAS headtail OK, here I go!

Art of Multiprocessor Programming© Herlihy-Shavit Final State Free pool zOMG what went wrong? headtail Bad news

Art of Multiprocessor Programming© Herlihy-Shavit The Dreaded ABA Problem Head pointer has value A Thread reads value A headtail

Art of Multiprocessor Programming© Herlihy-Shavit Dreaded ABA continued zzz Head pointer has value B Node A freed headtail

Art of Multiprocessor Programming© Herlihy-Shavit Dreaded ABA continued Yawn! Head pointer has value A again Node A recycled & reinitialized headtail

Art of Multiprocessor Programming© Herlihy-Shavit Dreaded ABA continued CAS succeeds because pointer matches even though pointer’s meaning has changed CAS headtail

Art of Multiprocessor Programming© Herlihy-Shavit The Dreaded ABA Problem Is a result of CAS() semantics –I blame Sun, Intel, AMD, … Not with Load-Locked/Store- Conditional –Good for IBM?

Art of Multiprocessor Programming© Herlihy-Shavit Dreaded ABA – A Solution 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

Art of Multiprocessor Programming© Herlihy-Shavit Atomic Stamped Reference AtomicStampedReference class –Java.util.concurrent.atomic package address S Stamp Reference Can get reference and stamp atomically

Art of Multiprocessor Programming© Herlihy-Shavit Concurrent Stack Methods –push(x) –pop() Last-in, First-out (LIFO) order Lock-Free!

Art of Multiprocessor Programming© Herlihy-Shavit Empty Stack Top

Art of Multiprocessor Programming© Herlihy-Shavit Push Top

Art of Multiprocessor Programming© Herlihy-Shavit Push Top CAS

Art of Multiprocessor Programming© Herlihy-Shavit Push Top

Art of Multiprocessor Programming© Herlihy-Shavit Push Top

Art of Multiprocessor Programming© Herlihy-Shavit Push Top

Art of Multiprocessor Programming© Herlihy-Shavit Push Top CAS

Art of Multiprocessor Programming© Herlihy-Shavit Push Top

Art of Multiprocessor Programming© Herlihy-Shavit Pop Top

Art of Multiprocessor Programming© Herlihy-Shavit Pop Top CAS

Art of Multiprocessor Programming© Herlihy-Shavit Pop Top CAS mine!

Art of Multiprocessor Programming© Herlihy-Shavit Pop Top CAS

Art of Multiprocessor Programming© Herlihy-Shavit Pop Top

Art of Multiprocessor Programming© Herlihy-Shavit 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)) { return; } else backoff.backoff(); }} Lock-free Stack

Art of Multiprocessor Programming© Herlihy-Shavit 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)) { return; } else backoff.backoff() }} Lock-free Stack tryPush attempts to push a node

Art of Multiprocessor Programming© Herlihy-Shavit 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)) { return; } else backoff.backoff() }} Lock-free Stack Read top value

Art of Multiprocessor Programming© Herlihy-Shavit 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)) { return; } else backoff.backoff() }} Lock-free Stack current top will be new node’s successor

Art of Multiprocessor Programming© Herlihy-Shavit 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)) { return; } else backoff.backoff() }} Lock-free Stack Try to swing top, return success or failure

Art of Multiprocessor Programming© Herlihy-Shavit 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)) { return; } else backoff.backoff() }} Lock-free Stack Push calls tryPush

Art of Multiprocessor Programming© Herlihy-Shavit 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)) { return; } else backoff.backoff() }} Lock-free Stack Create new node

Art of Multiprocessor Programming© Herlihy-Shavit 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)) { return; } else backoff.backoff() }} Lock-free Stack If tryPush() fails, back off before retrying Makes scheduling benevolent so Method is effectively wait- free

Art of Multiprocessor Programming© Herlihy-Shavit Lock-free Stack Good –No locking Bad –Without GC, fear ABA –Without backoff, huge contention at top –In any case, no parallelism

Art of Multiprocessor Programming© Herlihy-Shavit Big Question Are stacks inherently sequential? Reasons why –Every pop() call fights for top item Reasons why not –Stay tuned …

Art of Multiprocessor Programming© Herlihy-Shavit Elimination-Backoff Stack How to –“turn contention into parallelism” Replace familiar –exponential backoff With alternative –elimination-backoff

Art of Multiprocessor Programming© Herlihy-Shavit Observation Push( ) Pop() linearizable stack After an equal number of pushes and pops, stack stays the same Yes!

Art of Multiprocessor Programming© Herlihy-Shavit Idea: Elimination Array Push( ) Pop() stack Pick at random Pick at random Elimination Array

Art of Multiprocessor Programming© Herlihy-Shavit Push Collides With Pop Push( ) Pop() stack continue No need to access stack Yes!

Art of Multiprocessor Programming© Herlihy-Shavit No Collision Push( ) Pop() stack If no collision, access stack If pushes collide or pops collide access stack

Art of Multiprocessor Programming© Herlihy-Shavit 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

Art of Multiprocessor Programming© Herlihy-Shavit Elimination-Backoff Stack Push( ) Pop() Top CAS If CAS fails, back off

Art of Multiprocessor Programming© Herlihy-Shavit Dynamic Range and Delay Push( ) Pick random range and max time to wait for collision based on level of contention encountered

Art of Multiprocessor Programming© Herlihy-Shavit Linearizability Un-eliminated calls –linearized as before Eliminated calls: –linearize pop() immediately after matching push() Combination is a linearizable stack

Art of Multiprocessor Programming© Herlihy-Shavit Un-Eliminated Linearizability push(v 1 ) time linearizable push(v 1 ) pop(v 1 )

Art of Multiprocessor Programming© Herlihy-Shavit Eliminated Linearizability pop(v 2 )push(v 1 ) push(v 2 ) time push(v 2 ) linearizable pop(v 2 ) push(v 1 ) pop(v 1 ) Collision Point Red calls are eliminated

Art of Multiprocessor Programming© Herlihy-Shavit 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

Art of Multiprocessor Programming© Herlihy-Shavit public class EliminationArray { private static final int duration =...; private static final int timeUnit =...; Exchanger [] exchanger; public EliminationArray(int capacity) { exchanger = new Exchanger[capacity]; for (int i = 0; i < capacity; i++) exchanger[i] = new Exchanger (); … } … } Elimination Array

Art of Multiprocessor Programming© Herlihy-Shavit public class EliminationArray { private static final int duration =...; private static final int timeUnit =...; Exchanger [] exchanger; public EliminationArray(int capacity) { exchanger = new Exchanger[capacity]; for (int i = 0; i < capacity; i++) exchanger[i] = new Exchanger (); … } … } Elimination Array An array of exchangers

Art of Multiprocessor Programming© Herlihy-Shavit Lock-free Exchanger EMPTY

Art of Multiprocessor Programming© Herlihy-Shavit EMPTY Lock-free Exchanger CAS

Art of Multiprocessor Programming© Herlihy-Shavit WAITING Lock-free Exchanger

Art of Multiprocessor Programming© Herlihy-Shavit Lock-free Exchanger In search of partner … WAITING

Art of Multiprocessor Programming© Herlihy-Shavit WAITING Lock-free Exchanger Slot Still waiting … Try to exchange item and set state to BUSY CAS

Art of Multiprocessor Programming© Herlihy-Shavit BUSY Lock-free Exchanger Slot Partner showed up, take item and reset to EMPTY item stamp/state

Art of Multiprocessor Programming© Herlihy-Shavit EMPTYBUSY Lock-free Exchanger Slot item stamp/state Partner showed up, take item and reset to EMPTY

Art of Multiprocessor Programming© Herlihy-Shavit public class Exchanger { AtomicStampedReference slot = new AtomicStampedReference (null, 0); A Lock-Free Exchanger

Art of Multiprocessor Programming© Herlihy-Shavit public class Exchanger { AtomicStampedReference slot = new AtomicStampedReference (null, 0); A Lock-Free Exchanger Atomically modifiable reference + time stamp

Art of Multiprocessor Programming© Herlihy-Shavit Atomic Stamped Reference AtomicStampedReference class –Java.util.concurrent.atomic package In C or C++: address S Stamp Reference

Art of Multiprocessor Programming© Herlihy-Shavit Extracting Reference & Stamp Public T get(int[] stampHolder);

Art of Multiprocessor Programming© Herlihy-Shavit Extracting Reference & Stamp Public T get(int[] stampHolder); Returns reference to object of type T Returns stamp at array index 0!

Art of Multiprocessor Programming© Herlihy-Shavit Exchanger Status enum Status {EMPTY, WAITING, BUSY};

Art of Multiprocessor Programming© Herlihy-Shavit Exchanger Status enum Status {EMPTY, WAITING, BUSY}; Nothing yet

Art of Multiprocessor Programming© Herlihy-Shavit Exchange Status enum Status {EMPTY, WAITING, BUSY}; Nothing yet One thread is waiting for rendez-vous

Art of Multiprocessor Programming© Herlihy-Shavit Exchange Status enum Status {EMPTY, WAITING, BUSY}; Nothing yet One thread is waiting for rendez-vous Other threads busy with rendez-vous

Art of Multiprocessor Programming© Herlihy-Shavit 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 herItem = slot.get(stampHolder); int stamp = stampHolder[0]; switch(stamp) { case EMPTY: … // slot is free case WAITING: … // someone waiting for me case BUSY: … // others exchanging } The Exchange

Art of Multiprocessor Programming© Herlihy-Shavit 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 herItem = slot.get(stampHolder); int stamp = stampHolder[0]; switch(stamp) { case EMPTY: … // slot is free case WAITING: … // someone waiting for me case BUSY: … // others exchanging } The Exchange Item & timeout

Art of Multiprocessor Programming© Herlihy-Shavit 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 herItem = slot.get(stampHolder); int stamp = stampHolder[0]; switch(stamp) { case EMPTY: … // slot is free case WAITING: … // someone waiting for me case BUSY: … // others exchanging } The Exchange Array to hold timestamp

Art of Multiprocessor Programming© Herlihy-Shavit 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 herItem = slot.get(stampHolder); int stamp = stampHolder[0]; switch(stamp) { case EMPTY: // slot is free case WAITING: // someone waiting for me case BUSY: // others exchanging } }} The Exchange Loop until timeout

Art of Multiprocessor Programming© Herlihy-Shavit 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 herItem = slot.get(stampHolder); int stamp = stampHolder[0]; switch(stamp) { case EMPTY: // slot is free case WAITING: // someone waiting for me case BUSY: // others exchanging } }} The Exchange Get other’s item and timestamp

Art of Multiprocessor Programming© Herlihy-Shavit 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 herItem = slot.get(stampHolder); int stamp = stampHolder[0]; switch(stamp) { case EMPTY: … // slot is free case WAITING: … // someone waiting for me case BUSY: … // others exchanging } }} The Exchange Exchanger slot has three states

Art of Multiprocessor Programming© Herlihy-Shavit case EMPTY: // slot is free if (slot.compareAndSet(herItem, myItem, EMPTY, WAITING)) { while (System.nanoTime() < timeBound){ herItem = slot.get(stampHolder); if (stampHolder[0] == BUSY) { slot.set(null, EMPTY); return herItem; }} if (slot.compareAndSet(myItem, null, WAITING, EMPTY)){throw new TimeoutException(); } else { herItem = slot.get(stampHolder); slot.set(null, EMPTY); return herItem; } } break; Exchanger State EMPTY

Art of Multiprocessor Programming© Herlihy-Shavit case EMPTY: // slot is free if (slot.compareAndSet(herItem, myItem, EMPTY, WAITING)) { while (System.nanoTime() < timeBound){ herItem = slot.get(stampHolder); if (stampHolder[0] == BUSY) { slot.set(null, EMPTY); return herItem; }} if (slot.compareAndSet(myItem, null, WAITING, EMPTY)){throw new TimeoutException(); } else { herItem = slot.get(stampHolder); slot.set(null, EMPTY); return herItem; } } break; Exchanger State EMPTY Slot is free, try to insert myItem and change state to WAITING

Art of Multiprocessor Programming© Herlihy-Shavit case EMPTY: // slot is free if (slot.compareAndSet(herItem, myItem, EMPTY, WAITING)) { while (System.nanoTime() < timeBound){ herItem = slot.get(stampHolder); if (stampHolder[0] == BUSY) { slot.set(null, EMPTY); return herItem; }} if (slot.compareAndSet(myItem, null, WAITING, EMPTY)){throw new TimeoutException(); } else { herItem = slot.get(stampHolder); slot.set(null, EMPTY); return herItem; } } break; Exchanger State EMPTY Loop while still time left to attempt exchange

Art of Multiprocessor Programming© Herlihy-Shavit case EMPTY: // slot is free if (slot.compareAndSet(herItem, myItem, WAITING, BUSY)) { while (System.nanoTime() < timeBound){ herItem = slot.get(stampHolder); if (stampHolder[0] == BUSY) { slot.set(null, EMPTY); return herItem; }} if (slot.compareAndSet(myItem, null, WAITING, EMPTY)){throw new TimeoutException(); } else { herItem = slot.get(stampHolder); slot.set(null, EMPTY); return herItem; } } break; Exchanger State EMPTY Get item and stamp in slot and check if state changed to BUSY

Art of Multiprocessor Programming© Herlihy-Shavit case EMPTY: // slot is free if (slot.compareAndSet(herItem, myItem, EMPTY, WAITING)) { while (System.nanoTime() < timeBound){ herItem = slot.get(stampHolder); if (stampHolder[0] == BUSY) { slot.set(null, EMPTY); return herItem; }} if (slot.compareAndSet(myItem, null, WAITING, EMPTY)){throw new TimeoutException(); } else { herItem = slot.get(stampHolder); slot.set(null, EMPTY); return herItem; } } break; Exchanger State EMPTY If successful reset slot state to EMPTY

Art of Multiprocessor Programming© Herlihy-Shavit case EMPTY: // slot is free if (slot.compareAndSet(herItem, myItem, WAITING, BUSY)) { while (System.nanoTime() < timeBound){ herItem = slot.get(stampHolder); if (stampHolder[0] == BUSY) { slot.set(null, EMPTY); return herItem; }} if (slot.compareAndSet(myItem, null, WAITING, EMPTY)){throw new TimeoutException(); } else { herItem = slot.get(stampHolder); slot.set(null, EMPTY); return herItem; } } break; Exchanger State EMPTY and return item found in slot

Art of Multiprocessor Programming© Herlihy-Shavit case EMPTY: // slot is free if (slot.compareAndSet(herItem, myItem, EMPTY, WAITING)) { while (System.nanoTime() < timeBound){ herItem = slot.get(stampHolder); if (stampHolder[0] == BUSY) { slot.set(null, EMPTY); return herItem; }} if (slot.compareAndSet(myItem, null, WAITING, EMPTY)){throw new TimeoutException(); } else { herItem = slot.get(stampHolder); slot.set(null, EMPTY); return herItem; } } break; Exchanger State EMPTY Otherwise we ran out of time, try to reset state to EMPTY, if successful time out

Art of Multiprocessor Programming© Herlihy-Shavit case EMPTY: // slot is free if (slot.compareAndSet(herItem, myItem, WAITING, BUSY)) { while (System.nanoTime() < timeBound){ herItem = slot.get(stampHolder); if (stampHolder[0] == BUSY) { slot.set(null, EMPTY); return herItem; }} if (slot.compareAndSet(myItem, null, WAITING, EMPTY)){throw new TimeoutException(); } else { herItem = slot.get(stampHolder); slot.set(null, EMPTY); return herItem; } } break; Exchanger State EMPTY If reset failed, someone showed up after all, take that item

Art of Multiprocessor Programming© Herlihy-Shavit case EMPTY: // slot is free if (slot.compareAndSet(herItem, myItem, EMPTY, WAITING)) { while (System.nanoTime() < timeBound){ herItem = slot.get(stampHolder); if (stampHolder[0] == BUSY) { slot.set(null, EMPTY); return herItem; }} if (slot.compareAndSet(myItem, null, WAITING, EMPTY)){throw new TimeoutException(); } else { herItem = slot.get(stampHolder); slot.set(null, EMPTY); return herItem; } } break; Exchanger State EMPTY Set slot to EMPTY with new timestamp and return item found

Art of Multiprocessor Programming© Herlihy-Shavit case EMPTY: // slot is free if (slot.compareAndSet(herItem, myItem, EMPTY, WAITING)) { while (System.nanoTime() < timeBound){ herItem = slot.get(stampHolder); if (stampHolder[0] == BUSY) { slot.set(null, EMPTY); return herItem; }} if (slot.compareAndSet(myItem, null, WAITING, EMPTY)){throw new TimeoutException(); } else { herItem = slot.get(stampHolder); slot.set(null, EMPTY); return herItem; } } break; Exchanger State EMPTY If initial CAS failed then someone else changed state from EMPTY to WAITING so retry from start

Art of Multiprocessor Programming© Herlihy-Shavit case WAITING: // someone waiting for me if (slot.CAS(herItem, myItem, WAITING, BUSY)) return herItem; break; case BUSY: // others in middle of exchanging break; default: // impossible break; } States WAITING and BUSY

Art of Multiprocessor Programming© Herlihy-Shavit case WAITING: // someone waiting for me if (slot.CAS(herItem, myItem, WAITING, BUSY)) return herItem; break; case BUSY: // others in middle of exchanging break; default: // impossible break; } States WAITING and BUSY Someone is waiting for an exchange, so try to CAS in my item in & change state to BUSY

Art of Multiprocessor Programming© Herlihy-Shavit case WAITING: // someone waiting for me if (slot.CAS(herItem, myItem, WAITING, BUSY)) return herItem; break; case BUSY: // others in middle of exchanging break; default: // impossible break; } States WAITING and BUSY If successful, return that item. Otherwise another thread got it.

Art of Multiprocessor Programming© Herlihy-Shavit case WAITING: // someone waiting for me if (slot.CAS(herItem, myItem, WAITING, BUSY)) return herItem; break; case BUSY: // others in middle of exchanging break; default: // impossible break; } States WAITING and BUSY Other threads using slot, so start over.

Art of Multiprocessor Programming© Herlihy-Shavit 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

Art of Multiprocessor Programming© Herlihy-Shavit 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) }} Elimination Array

Art of Multiprocessor Programming© Herlihy-Shavit 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) }} Elimination Array Visit elimination array with value and range

Art of Multiprocessor Programming© Herlihy-Shavit 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) }} Elimination Array Pick a random array entry

Art of Multiprocessor Programming© Herlihy-Shavit 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) }} Elimination Array Exchange value or time out

Art of Multiprocessor Programming© Herlihy-Shavit public void push(T value) {... while (true) { if (tryPush(node)) { return; } else try { T otherValue = eliminationArray.visit(value,policy.Range); if (otherValue == null) { return; } Elimination Stack Push

Art of Multiprocessor Programming© Herlihy-Shavit public void push(T value) {... while (true) { if (tryPush(node)) { return; } else try { T otherValue = eliminationArray.visit(value,policy.Range); if (otherValue == null) { return; } Elimination Stack Push First try to push

Art of Multiprocessor Programming© Herlihy-Shavit public void push(T value) {... while (true) { if (tryPush(node)) { return; } else try { T otherValue = eliminationArray.visit(value,policy.Range); if (otherValue == null) { return; } Elimination Stack Push If failed back-off to try to eliminate

Art of Multiprocessor Programming© Herlihy-Shavit public void push(T value) {... while (true) { if (tryPush(node)) { return; } else try { T otherValue = eliminationArray.visit(value,policy.Range); if (otherValue == null) { return; } Elimination Stack Push Value being pushed and range to try

Art of Multiprocessor Programming© Herlihy-Shavit public void push(T value) {... while (true) { if (tryPush(node)) { return; } else try { T otherValue = eliminationArray.visit(value,policy.Range); if (otherValue == null) { return; } Elimination Stack Push Only a pop inserts null value so elimination was successful

Art of Multiprocessor Programming© Herlihy-Shavit public void push(T value) {... while (true) { if (tryPush(node)) { return; } else try { T otherValue = eliminationArray.visit(value,policy.Range); if (otherValue == null) { return; } Elimination Stack Push Else retry push on lock-free stack

Art of Multiprocessor Programming© Herlihy-Shavit public T pop() {... while (true) { if (tryPop()) { return returnNode.value; } else try { T otherValue = eliminationArray.visit(null,policy.Range; if otherValue != null) { return otherValue; } }} Elimination Stack Pop

Art of Multiprocessor Programming© Herlihy-Shavit public T pop() {... while (true) { if (tryPop()) { return returnNode.value; } else try { T otherValue = eliminationArray.visit(null,policy.Range; if ( otherValue != null) { return otherValue; } }} Elimination Stack Pop Like push, non-null value pushed by other thread, so elimination succeeds

Art of Multiprocessor Programming© Herlihy-Shavit Summary We saw both lock-based and lock- free implementations of queues and stacks Not every data structure that looks sequential is sequential. –Linearizable stack not inherently sequential ABA is a real problem, fear it.

Art of Multiprocessor Programming© Herlihy-Shavit This work is licensed under a Creative Commons Attribution- ShareAlike 2.5 License.Creative Commons Attribution- ShareAlike 2.5 License You are free: –to Share — to copy, distribute and transmit the work –to Remix — to adapt the work Under the following conditions: –Attribution. You must attribute the work to “The Art of Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work). –Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to – 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.