Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit."— Presentation transcript:

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

2 Art of Multiprocessor Programming22 Another Fundamental Problem We told you about –Sets implemented by linked lists Next: queues Next: stacks

3 Art of Multiprocessor Programming33 Queue: Concurrency enq(x) y=deq() enq() and deq() work at different ends of the object tailhead

4 Art of Multiprocessor Programming44 Concurrency enq(x) Challenge: what if the queue is empty or full? y=deq() tail head

5 Art of Multiprocessor Programming55 Bounded Queue Sentinel head tail

6 Art of Multiprocessor Programming66 Bounded Queue head tail First actual item

7 Art of Multiprocessor Programming77 Bounded Queue head tail Lock out other deq() calls deqLock

8 Art of Multiprocessor Programming88 Bounded Queue head tail Lock out other enq() calls deqLock enqLock

9 Art of Multiprocessor Programming 99 Not Done Yet head tail deqLock enqLock Need to tell whether queue is full or empty

10 Art of Multiprocessor Programming10 Not Done Yet head tail deqLock enqLock Max size is 8 items size 1

11 Art of Multiprocessor Programming11 Not Done Yet head tail deqLock enqLock Incremented by enq() Decremented by deq() size 1

12 Art of Multiprocessor Programming12 Enqueuer head tail deqLock enqLock size 1 Lock enqLock

13 Art of Multiprocessor Programming13 Enqueuer head tail deqLock enqLock size 1 Read size OK

14 Art of Multiprocessor Programming14 Enqueuer head tail deqLock enqLock size 1 No need to lock tail

15 Art of Multiprocessor Programming15 Enqueuer head tail deqLock enqLock size 1 Enqueue Node

16 Art of Multiprocessor Programming16 Enqueuer head tail deqLock enqLock size 1 2 getAndincrement()

17 Art of Multiprocessor Programming17 Enqueuer head tail deqLock enqLock size 8 Release lock 2

18 Art of Multiprocessor Programming18 Enqueuer head tail deqLock enqLock size 2 If queue was empty, notify waiting dequeuers

19 Art of Multiprocessor Programming19 Unsuccesful Enqueuer head tail deqLock enqLock size 8 Uh-oh Read size …

20 Art of Multiprocessor Programming20 Dequeuer head tail deqLock enqLock size 2 Lock deqLock

21 Art of Multiprocessor Programming21 Dequeuer head tail deqLock enqLock size 2 Read sentinel’s next field OK

22 Art of Multiprocessor Programming22 Dequeuer head tail deqLock enqLock size 2 Read value

23 Art of Multiprocessor Programming23 Dequeuer head tail deqLock enqLock size 2 Make first Node new sentinel

24 Art of Multiprocessor Programming24 Dequeuer head tail deqLock enqLock size 1 Decrement size

25 Art of Multiprocessor Programming25 Dequeuer head tail deqLock enqLock size 1 Release deqLock

26 Art of Multiprocessor Programming26 Unsuccesful Dequeuer head tail deqLock enqLock size 0 Read sentinel’s next field uh-oh

27 Art of Multiprocessor Programming27 Bounded Queue public class BoundedQueue { ReentrantLock enqLock, deqLock; Condition notEmptyCondition, notFullCondition; AtomicInteger size; Node head; Node tail; int capacity; enqLock = new ReentrantLock(); notFullCondition = enqLock.newCondition(); deqLock = new ReentrantLock(); notEmptyCondition = deqLock.newCondition(); }

28 Art of Multiprocessor Programming28 Bounded Queue Fields public class BoundedQueue { ReentrantLock enqLock, deqLock; Condition notEmptyCondition, notFullCondition; AtomicInteger size; Node head; Node tail; int capacity; enqLock = new ReentrantLock(); notFullCondition = enqLock.newCondition(); deqLock = new ReentrantLock(); notEmptyCondition = deqLock.newCondition(); } Enq & deq locks

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

30 Art of Multiprocessor Programming30 Bounded Queue Fields public class BoundedQueue { ReentrantLock enqLock, deqLock; Condition notEmptyCondition, notFullCondition; AtomicInteger size; Node head; Node tail; int capacity; enqLock = new ReentrantLock(); notFullCondition = enqLock.newCondition(); deqLock = new ReentrantLock(); notEmptyCondition = deqLock.newCondition(); } size: 0 to capacity

31 Art of Multiprocessor Programming31 Bounded Queue Fields public class BoundedQueue { ReentrantLock enqLock, deqLock; Condition notEmptyCondition, notFullCondition; AtomicInteger size; Node head; Node tail; int capacity; enqLock = new ReentrantLock(); notFullCondition = enqLock.newCondition(); deqLock = new ReentrantLock(); notEmptyCondition = deqLock.newCondition(); } Head and Tail

32 Art of Multiprocessor Programming32 Enq Method Part One public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (size.get() == Capacity) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = tail.next; if (size.getAndIncrement() == 0) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … }

33 Art of Multiprocessor Programming33 public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (size.get() == capacity) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = tail.next; if (size.getAndIncrement() == 0) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … } Enq Method Part One Lock and unlock enq lock

34 Art of Multiprocessor Programming34 public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (size.get() == capacity) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = tail.next; if (size.getAndIncrement() == 0) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … } Enq Method Part One Wait while queue is full …

35 Art of Multiprocessor Programming35 public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (size.get() == capacity) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = tail.next; if (size.getAndIncrement() == 0) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … } Enq Method Part One when await() returns, you might still fail the test !

36 Art of Multiprocessor Programming36 public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (size.get() == capacity) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = tail.next; if (size.getAndIncrement() == 0) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … } Be Afraid After the loop: how do we know the queue won’t become full again?

37 Art of Multiprocessor Programming37 public void enq(T x) { boolean mustWakeDequeuers = false; enqLock.lock(); try { while (size.get() == capacity) notFullCondition.await(); Node e = new Node(x); tail.next = e; tail = tail.next; if (size.getAndIncrement() == 0) mustWakeDequeuers = true; } finally { enqLock.unlock(); } … } Enq Method Part One Add new node

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

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

40 Art of Multiprocessor Programming40 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?

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

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

43 Art of Multiprocessor Programming43 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?

44 Art of Multiprocessor Programming44 Split the Counter The enq() method –Increments only –Cares only if value is capacity The deq() method –Decrements only –Cares only if value is zero

45 Art of Multiprocessor Programming45 Split Counter Enqueuer increments enqSize Dequeuer decrements deqSize When enqueuer runs out –Locks deqLock –computes size = enqSize - DeqSize Intermittent synchronization –Not with each method call –Need both locks! (careful …)

46 Art of Multiprocessor Programming46 A Lock-Free Queue Sentinel head tail

47 Art of Multiprocessor Programming47 Compare and Set CAS

48 Art of Multiprocessor Programming48 Enqueue head tail enq( )

49 Art of Multiprocessor Programming49 Logical Enqueue head tail CAS

50 Art of Multiprocessor Programming50 Physical Enqueue head tail CAS

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

52 Art of Multiprocessor Programming52 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

53 Art of Multiprocessor Programming53 When CASs Fail During logical enqueue –Abandon hope, restart –Still lock-free During physical enqueue –Ignore it

54 Art of Multiprocessor Programming 54

55 Art of Multiprocessor Programming55 Dequeuer head tail Read value

56 Art of Multiprocessor Programming56 Dequeuer head tail Make first Node new sentinel CAS

57 Art of Multiprocessor Programming 57

58 Art of Multiprocessor Programming58 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?

59 Art of Multiprocessor Programming59 Dequeuer head tail CAS Can recycle

60 Art of Multiprocessor Programming60 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 …

61 Art of Multiprocessor Programming61 Why Recycling is Hard Free pool headtail Want to redirect head from gray to red zzz… Head reference has value A Thread reads value A Head reference has value A Thread reads value A

62 Art of Multiprocessor Programming62 Both Nodes Reclaimed Free pool zzz headtail Head reference has value B Node A freed Head reference has value B Node A freed

63 Art of Multiprocessor Programming63 One Node Recycled Free pool Yawn! headtail Head reference has value A again Node A recycled and reinitialized Head reference has value A again Node A recycled and reinitialized

64 Art of Multiprocessor Programming64 Why Recycling is Hard Free pool CAS headtail OK, here I go! CAS succeeds because references match, even though reference’s meaning has changed CAS succeeds because references match, even though reference’s meaning has changed

65 Art of Multiprocessor Programming65 Recycle FAIL Free pool zOMG what went wrong? headtail

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

67 Art of Multiprocessor Programming67 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

68 Art of Multiprocessor Programming68 Atomic Stamped Reference AtomicStampedReference class –Java.util.concurrent.atomic package address S Stamp Reference Can get reference & stamp atomically

69 Art of Multiprocessor Programming 69

70 Art of Multiprocessor Programming70 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 –http://creativecommons.org/licenses/by-sa/3.0/. 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.


Download ppt "Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit."

Similar presentations


Ads by Google