ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LockFreePriorityQueue.h File Reference

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

Detailed description

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:

  • insert() and tryDeleteMin() are linearizable, insert() at the compare-and-swap that links its node at level 0, tryDeleteMin() at the fetch-and-or that marks the pointer to the node it returns, or, when it returns nothing, at the read of a pointer to the tail from the head or from a deleted node; the abstract queue is the sequence of the nodes not deleted of level 0, ordered by (key, stamp). tryMinKey() is linearizable at the read of the first unmarked pointer of level 0.
  • The operations are lock-free but for the allocation of the nodes: a compare-and-swap or a fetch-and-or fails, or a walk goes on, only because another operation changed the list, and each operation completes after a bounded number of its own successful read-modify-writes, \( L + 2 \) at most.
  • The prefix that a DeleteMin walks: in a run of one thread a DeleteMin walks at most \( b + 1 \) nodes, the list keeps at most \( b \) deleted nodes between two operations, and a cut comes once per \( b \) DeleteMin that delete a node; with many threads, a DeleteMin that walked more than \( b \) nodes cuts the prefix up to the node it deleted, or up to the first node being inserted that it walked, unless another cut started since it read the head or is in progress. A thread stopped inside an insertion or inside a cut lets the prefix grow without bound (Lindén and Jonsson remark it of the first case), and the DeleteMin then walk it all.

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.

See also
J. Lindén, B. Jonsson, A skiplist-based concurrent priority queue with minimal memory contention, International Conference on Principles of Distributed Systems (OPODIS), LNCS 8304, 206-220, 2013, doi:10.1007/978-3-319-03850-6_15
H. Sundell, P. Tsigas, Fast and lock-free concurrent priority queues for multi-thread systems, Journal of Parallel and Distributed Computing 65(5), 609-627, 2005, doi:10.1016/j.jpdc.2004.12.005 (first at IPDPS 2003, doi:10.1109/IPDPS.2003.1213189)
E. Burns, S. Lemons, W. Ruml, R. Zhou, Best-first heuristic search for multicore machines, Journal of Artificial Intelligence Research 39, 689-743, 2010, doi:10.1613/jair.3094
I. Lotan, N. Shavit, Skiplist-based concurrent priority queues, International Parallel and Distributed Processing Symposium (IPDPS), 263-268, 2000, doi:10.1109/IPDPS.2000.845994
W. Pugh, Skip lists: a probabilistic alternative to balanced trees, Communications of the ACM 33(6), 668-676, 1990, doi:10.1145/78973.78977
T. L. Harris, A pragmatic implementation of non-blocking linked-lists, International Symposium on Distributed Computing (DISC), LNCS 2180, 300-314, 2001, doi:10.1007/3-540-45414-4_21
K. Fraser, Practical lock-freedom, PhD thesis, technical report UCAM-CL-TR-579, University of Cambridge, 2004, doi:10.48456/tr-579
T. E. Hart, P. E. McKenney, A. D. Brown, J. Walpole, Performance of memory reclamation for lockless synchronization, Journal of Parallel and Distributed Computing 67(12), 1270-1285, 2007, doi:10.1016/j.jpdc.2007.04.010
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa