143#ifndef __ADS_LockFreePriorityQueue
144 #define __ADS_LockFreePriorityQueue
159#include <type_traits>
175template<
class K ,
class T ,
class Less = std::less< K > >
179 static_assert( std::is_nothrow_copy_constructible_v< K > ,
180 "the keys are copied without exceptions" );
181 static_assert( std::is_nothrow_move_constructible_v< T > ,
182 "the items are moved without exceptions" );
221 std::uint64_t seed = 1 ,
222 Less less = Less() );
238 [[nodiscard]]
unsigned offset(
void )
const {
return( f_b ); }
242 [[nodiscard]] std::size_t
size(
void )
const;
245 [[nodiscard]]
bool empty(
void )
const;
249 [[nodiscard]] std::size_t
prefix(
void )
const;
259 using Word = std::uintptr_t;
261 static constexpr Word kMark = 1;
262 static constexpr Word kCut = 2;
269 static constexpr std::memory_order kWalk = std::memory_order_seq_cst;
275 std::atomic< Word > * next;
278 std::atomic< bool > inserting;
292 struct alignas( 64 ) Record
295 std::atomic< std::uint64_t > local{ 0 };
297 std::atomic< bool > used{
true };
299 Record * next =
nullptr;
301 std::uint64_t
id = 0;
303 std::uint64_t count = 0;
306 Link * limbo[ 3 ] = { nullptr , nullptr ,
nullptr };
308 std::uint64_t epoch[ 3 ] = { 0 , 0 , 0 };
312 [[nodiscard]]
static Link * ptr( Word w ) {
313 return(
reinterpret_cast< Link *
>( w & ~( kMark | kCut ) ) );
317 [[nodiscard]]
static Word word(
const Link * p ) {
318 return(
reinterpret_cast< Word
>( p ) );
322 [[nodiscard]]
static bool marked(
const Link * p ) {
323 return( p->next[ 0 ].load( kWalk ) & kMark );
328 static void * allocate( std::size_t n ,
int h , std::atomic< Word > *& next );
331 static Link * makeLink(
int h );
334 static Node * make(
int h ,
const K & k , std::uint64_t s , T && x );
337 static void destroy( Node * p );
340 static void destroyLink( Link * p );
343 static void destroyAll( Link * p );
346 [[nodiscard]]
bool before(
const Link * p ,
const K & k ,
347 std::uint64_t s )
const;
351 void locate(
const K & k , std::uint64_t s , Link ** preds ,
352 Link ** succs )
const;
357 [[nodiscard]]
bool behind(
const Link * c ,
const Link * n )
const;
361 void restructure(
Counts & c );
364 void release(
void );
370 alignas( 64 ) std::atomic< std::uint64_t > f_epoch{ 2 };
372 alignas( 64 ) std::atomic< Record * > f_records{
nullptr };
374 std::atomic< std::uint64_t > f_ids{ 0 };
375 std::atomic< std::uint64_t > f_seed;
385template<
class K ,
class T ,
class Less >
521 : f_q( &q ) , f_r( r ) , f_x( seed ) { }
524 std::uint64_t next(
void );
538 void retire( Node * p );
541 void advance(
void );
558#include "ADS/concurrent/impl/LockFreePriorityQueue.tpp"
The capabilities of the structures, beyond the book: what an algorithm may ask of its argument to cho...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
the access of one thread to a LockFreePriorityQueue: its record of the epochs, the generator of its h...
Definition LockFreePriorityQueue.h:387
std::optional< K > tryMinKey(void)
the least key, linearizable at the read of the first pointer of level 0 not marked; nothing if the qu...
const Counts & counts(void) const
what the handle counted so far
Definition LockFreePriorityQueue.h:513
bool empty(void)
whether the queue was empty at the linearization point of tryMinKey()
Definition LockFreePriorityQueue.h:510
std::optional< Entry > tryDeleteMin(void)
the pair of least key, removed; nothing if the queue was empty at the linearization point
Handle & operator=(Handle &&)=delete
no assignment, since a record has one owner
friend class LockFreePriorityQueue
Definition LockFreePriorityQueue.h:517
void insert(K k, T x)
inserts the pair , linearizable at the compare-and-swap that links its node at level 0
~Handle()
the record given back to the queue, for another handle
Handle(const Handle &)=delete
no copy, since a record has one owner
Handle(Handle &&o) noexcept
the record moved, the other handle left without one
Handle & operator=(const Handle &)=delete
no assignment, since a record has one owner
std::size_t size(void) const
the number of the pairs, i.e., of the nodes not deleted of level 0; with no operation running
LockFreePriorityQueue & operator=(const LockFreePriorityQueue &)=delete
no copy, since the threads share it by its address
static constexpr decrease_key_kind decrease_key
a queue with no handles on the pairs, on which a search lowers a key by a new insertion [see decrease...
Definition LockFreePriorityQueue.h:191
K Key
the type of the keys
Definition LockFreePriorityQueue.h:186
~LockFreePriorityQueue()
the frees of the nodes, of those waiting for their epoch and of the records
bool consistent(void) const
the invariants of the lists: the deleted nodes a prefix of level 0, the rest ordered by (key,...
LockFreePriorityQueue(const LockFreePriorityQueue &)=delete
no copy, since the threads share it by its address
unsigned offset(void) const
the length of the prefix past which a DeleteMin cuts it
Definition LockFreePriorityQueue.h:238
static constexpr int kLevels
the levels of the skiplist
Definition LockFreePriorityQueue.h:194
T Item
the type of the items
Definition LockFreePriorityQueue.h:187
Handle handle(void)
a handle for one thread, on a record of the epochs of its own
LockFreePriorityQueue(unsigned offset=32, std::uint64_t seed=1, Less less=Less())
the empty queue, whose DeleteMin cut the prefix once they walked more than = offset nodes,...
std::size_t prefix(void) const
the number of the deleted nodes still in the list of level 0; with no operation running
bool empty(void) const
whether size() is 0; with no operation running
decrease_key_kind
how a priority queue lowers the priority of an item: it cannot (none), by a new insertion and the sta...
Definition Capabilities.h:553
@ lazy
Definition Capabilities.h:553
what a handle counted of its operations, for comparisons that do not time: the read-modify-writes,...
Definition LockFreePriorityQueue.h:207
std::uint64_t rmw
compare-and-swaps and fetch-and-ors
Definition LockFreePriorityQueue.h:208
std::uint64_t failed
those that changed nothing
Definition LockFreePriorityQueue.h:209
std::uint64_t cuts
the prefixes cut off
Definition LockFreePriorityQueue.h:211
std::uint64_t walked
the nodes walked by the DeleteMin
Definition LockFreePriorityQueue.h:210
a pair of the queue, its key and its item
Definition LockFreePriorityQueue.h:198
K key
the key, the least deleted first
Definition LockFreePriorityQueue.h:199
T item
the item
Definition LockFreePriorityQueue.h:200