Chapter 10 Concurrent Queues and the ABA Problem
Chapter 10: Concurrent Queues and the ABA Problem Presentation by Aviv Nachman
Agenda • Definitions. • Queues. • A Bounded Partial Queue. • An Unbounded Total Queue. • An Unbounded Lock-Free Queue. • Memory Reclamation and the ABA Problem.
Agenda • Definitions. • Queues. • A Bounded Partial Queue. • An Unbounded Total Queue. • An Unbounded Lock-Free Queue. • Memory Reclamation and the ABA Problem.
Definitions • A bounded queue holds a limited number of items. • A method is total if calls do not wait for certain condition to become true. • A method is partial if calls may wait for conditions to hold.
Agenda • Definitions. • Queues. • A Bounded Partial Queue. • An Unbounded Total Queue. • An Unbounded Lock-Free Queue. • Memory Reclamation and the ABA Problem.
Queues • FIFO order. • Enqueue inserts an item to the tail of the queue. • Dequeue removes an item from the head of the queue. • The head always points to a sentinel item (used for convenience).
Queues Dequeue Enqueue val 2 T val 2 H Sentinel val 1 Sentinel
Agenda • Definitions. • Queues. • A Bounded Partial Queue. • An Unbounded Total Queue. • An Unbounded Lock-Free Queue. • Memory Reclamation and the ABA Problem.
A Bounded Partial Queue • Only one enqueuer/dequeuer can enqueue/dequeue at a certain time. • We hold two locks, one for the head and one for the tail.
A Bounded Partial Queue But what if we enqueue and dequeue at the same time? We need to atomically modify size variable. • The queue cannot hold more than #capacity items (Bounded principle). • We hold size variable to make sure we don’t exceed from #capacity items. • Increase/decrease size every time we enqueue/dequeue.
A Bounded Partial Queue • We will hold two condition variables: • To be able to wake up enqueuers who wished to insert to a full queue. • To be able to wake up dequeuer who wished to dequeue from an empty queue. • In both case we will wake up using signal-all in order to avoid lost wakeup effect.
A Bounded Partial Queue Dequeue Enqueue val 1 T H Sleep on not. Empty. Condition val 1 Sentinel val 1 Signal. All not. Empty. Condition Size: 01
A Bounded Partial Queue •
Agenda • Definitions. • Queues. • A Bounded Partial Queue. • An Unbounded Total Queue. • An Unbounded Lock-Free Queue. • Memory Reclamation and the ABA Problem.
An Unbounded Total Queue • Similar to the Bounded Partial Queue with slight changes: • The Queue is unbounded: we don’t restrict capacity. • Note: Enqueue method always succeed. • The dequeue method is total: dequeue throws Empty. Exception if there is no item to dequeue.
A Bounded Partial Queue Enqueue val 1 T Dequeue H Dequeue val 1 Sentinel val 1 Empty. Exception
Agenda • Definitions. • Queues. • A Bounded Partial Queue. • An Unbounded Total Queue. • An Unbounded Lock-Free Queue. • Memory Reclamation and the ABA Problem.
An Unbounded Lock-Free Queue • Every node’s successor field, head and tail will be an atomic reference. • The lock-free queue uses CAS functionality to atomically change the references.
An Unbounded Lock-Free Queue • Enqueue method: 1. Create a node with the desired value. 2. Check that the successor of the item pointed by the tail is null: 3. If 2 is true add the new node to the end of the chain with CAS (if CAS fails, go back to 2). 4. Adjust tail reference to point to the new node with CAS and finish (regardless of the success of this CAS). 5. If 2 fails there is an unfinished enqueue, finish it (update tail with CAS) and go back to 2.
An Unbounded Lock-Free Queue
An Unbounded Lock-Free Queue • Dequeue method: 1. 2. Check if head and tail points to the same node: 3. If 1 is false fetch the value of the first item in the queue and try to CAS head to point to this item (make it the new sentinel) 4. If 3 succeed return the fetched value, otherwise go back to 1. if 1 is true and the list contains only the sentinel (queue is empty) throw an exception, otherwise there is unfinished queue, finish it with CAS of the tail and go back to 1.
An Unbounded Lock-Free Queue
An Unbounded Lock-Free Queue Enqueue val 1 T H Dequeue ü first == last ü next != null CAS val 1 Sentinel val 1 2 nd CAS fails (tail doesn’t point to sentinel anymore)
Agenda • Definitions. • Queues. • A Bounded Partial Queue. • An Unbounded Total Queue. • An Unbounded Lock-Free Queue. • Memory Reclamation and the ABA Problem.
Memory Reclamation and the ABA Problem. • So far we relied on java GC to recycle dequeued nodes. • What if we want to do our own memory management?
Memory Reclamation and the ABA Problem. • A natural way to recycle nodes in a lock-free manner is to have each thread maintain its own private free list of unused queue entries. • On enqueue we take a node from the free list, if its empty we allocate a new one. • On dequeue we put the node back into the free list.
Memory Reclamation and the ABA Problem. T H This is the ABA problem. val 2 val 1 sentinel Thread #1 is about to CAS H from sentinel to val 1. Thread #2 dequeues twice Thread #2 enqueues the sentinel. Thread #3 dequeues. #2 local pool
Memory Reclamation and the ABA Problem. • One straightforward way to fix this problem is to tag each atomic reference with a unique stamp. • The stamp will help us to avoid the ABA problem, incrementing the value of the stamp each time we modify the object.
Memory Reclamation and the ABA Problem. •
Memory Reclamation and the ABA Problem.
summary üWe have seen three ways to implement concurrent queues, each one with its pros and cons: i. A Bounded Partial Queue. ii. An Unbounded Total Queue. iii. An Unbounded Lock-Free Queue. üWe understood the ABA problem, its effects and a certain way to solve it.
- Slides: 32