|
ADS
Algorithms and Data Structures
|
A priority queue shared by threads that is exact, linearizable and lock-free, beyond the book: the skiplist of Lindén and Jonsson, whose DeleteMin pays one read-modify-write in the common case and leaves the nodes it deletes in the list, to be cut off in batches. More...
#include "ADS/Capabilities.h"#include "ADS/Contract.h"#include <atomic>#include <cstddef>#include <cstdint>#include <functional>#include <optional>#include <type_traits>#include <utility>#include "ADS/concurrent/impl/LockFreePriorityQueue.tpp"Go to the source code of this file.
Classes | |
| class | ADS::LockFreePriorityQueue< K, T, Less > |
| an exact PRIORITY QUEUE shared by threads, linearizable and lock-free: the skiplist of Lindén and Jonsson, its deleted prefix cut in batches More... | |
| struct | ADS::LockFreePriorityQueue< K, T, Less >::Entry |
| a pair of the queue, its key and its item More... | |
| struct | ADS::LockFreePriorityQueue< K, T, Less >::Counts |
| what a handle counted of its operations, for comparisons that do not time: the read-modify-writes, those that failed, the nodes walked by the DeleteMin, and the cuts of the prefix More... | |
| class | ADS::LockFreePriorityQueue< K, T, Less >::Handle |
| the access of one thread to a LockFreePriorityQueue: its record of the epochs, the generator of its heights and the counter of its stamps More... | |
Namespaces | |
| namespace | ADS |
A priority queue shared by threads that is exact, linearizable and lock-free, beyond the book: the skiplist of Lindén and Jonsson, whose DeleteMin pays one read-modify-write in the common case and leaves the nodes it deletes in the list, to be cut off in batches.
It holds pairs \( ( k , x ) \) of a key and an item, the least key first by less, and it is meant as the shared OPEN of a parallel best-first search that wants the exact order (e.g., SPA* of Burns et al.), where the MultiQueue [see MultiQueue.h] relaxes it and the queues that are stolen [see StealablePriorityQueue.h] keep it only per worker.
The pairs are the nodes of a skiplist of \( L = 32 \) levels, between a head and a tail that hold no pair; the list of level 0 holds all the nodes in the order of their keys, and the list of level \( i \) a subsequence of it, a node of height \( h \) being in the levels \( 0 , \ldots , h - 1 \), with \( h \) drawn with probability \( 2^{-h} \). The pointer of level 0 of a node carries in its lowest bit the delete flag \( d \) of the node that follows: a node is deleted when the pointer that reaches it at level 0 is marked, so that the mark of a deletion sits in the predecessor and an insertion, which swings that pointer by a compare-and-swap from its value unmarked, never puts a node in front of a deleted one. The deleted nodes therefore form a prefix of the list of level 0 (Lindén and Jonsson), and a DeleteMin walks the prefix and deletes the first node not deleted by a fetch-and-or of the mark, its only read-modify-write when nobody deletes the same node. The prefix is not unlinked node by node: a DeleteMin that walked more than \( b \) nodes (the parameter offset) moves the pointer of level 0 of the head past the prefix by one compare-and-swap, the pointers of the upper levels of the head after it (the restructure), and hands the nodes cut off to the reclamation below. The keys equal under less are told apart by a stamp of the insertion, a counter of the handle that inserts and its number, so that the keys of the list are distinct, as the algorithm wants them (the device of Sundell and Tsigas); two pairs of equal keys come out in the order of their stamps.
The guarantees, which An exact priority queue without locks proves, are these:
We depart from the paper in two points. First, an insertion that links its node \( n \) at an upper level checks that the successor \( c \) found there is not before \( n \) at level 0 by reading the successor \( f \) of \( c \): \( c \) is before \( n \) only if it is deleted, and then either its delete flag is set or \( f \) is \( n \) or a node of a key less than that of \( n \). The paper checks instead whether \( c \) is the last deleted node met by the walk of level 0, which misses \( c \) when the walk starts past it, as it does when the upper lists have branches left by insertions after nodes cut off; the node then points back to \( c \), and a later walk may reach through it the nodes freed. Second, a DeleteMin that cuts the prefix also sets the bit 1 of the pointer of level 0 of the head, and clears it once its restructure is over, while a DeleteMin that finds the bit set does not try a cut: the restructures run one at a time, which makes the proof of the reclamation a matter of one thread, and costs nothing to the progress, since no operation ever waits for the bit.
The memory of the nodes cut off is reclaimed by epochs (Fraser), owned by the queue, since the area has no other reclamation that fits: each thread uses the queue through a Handle, whose record announces the epoch it runs in; a node cut off waits in the record of the thread that cut it, stamped with the epoch of the cut, and is freed once the epoch has grown by 2, which happens only after every thread running an operation has announced the newer epoch. No node is leaked: the nodes not yet freed are freed by the destructor. A thread stopped inside an operation stops the epoch and keeps the nodes cut off afterwards from being freed (the reclamation is blocking, as in the paper); the operations themselves never wait for it.
The keys must be copyable without exceptions, the items movable without exceptions; Less is a strict weak order. A key is lowered by a new insertion, the stale pair being skipped when it comes out (decrease_key_kind::lazy). The contracts [see Contract.h]: no handle is alive when the queue is destroyed; size(), empty() and consistent() of the queue are called with no operation running, while the operations of the handles may run at once on any threads, each handle used by one thread at a time; consistent() checks the invariants of the lists (the prefix, the order, the upper levels as subsequences of level 0, the head not behind a node being inserted). The operations are defined in LockFreePriorityQueue.tpp.