ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LockFreePriorityQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------- File LockFreePriorityQueue.h ----------------------*/
3/*--------------------------------------------------------------------------*/
139/*--------------------------------------------------------------------------*/
140/*----------------------------- DEFINITIONS --------------------------------*/
141/*--------------------------------------------------------------------------*/
142
143#ifndef __ADS_LockFreePriorityQueue
144 #define __ADS_LockFreePriorityQueue
145/* self-identification: #endif constant */
146
147/*--------------------------------------------------------------------------*/
148/*------------------------------ INCLUDES ----------------------------------*/
149/*--------------------------------------------------------------------------*/
150
151#include "ADS/Capabilities.h"
152#include "ADS/Contract.h"
153
154#include <atomic>
155#include <cstddef>
156#include <cstdint>
157#include <functional>
158#include <optional>
159#include <type_traits>
160#include <utility>
161
162/*--------------------------------------------------------------------------*/
163/*--------------------------- NAMESPACE ------------------------------------*/
164/*--------------------------------------------------------------------------*/
165
166namespace ADS
167{
168
169/*--------------------------------------------------------------------------*/
170/*---------------------- CLASS LockFreePriorityQueue -----------------------*/
171/*--------------------------------------------------------------------------*/
172
175template< class K , class T , class Less = std::less< K > >
177{
178
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" );
183
184/*--------------------------------------------------------------------------*/
185 public:
186 using Key = K;
187 using Item = T;
188
192
194 static constexpr int kLevels = 32;
195
197 struct Entry
198 {
199 K key;
201 };
202
206 struct Counts
207 {
208 std::uint64_t rmw = 0;
209 std::uint64_t failed = 0;
210 std::uint64_t walked = 0;
211 std::uint64_t cuts = 0;
212 };
213
214 class Handle;
215
220 explicit LockFreePriorityQueue( unsigned offset = 32 ,
221 std::uint64_t seed = 1 ,
222 Less less = Less() );
223
228
233
235 [[nodiscard]] Handle handle( void );
236
238 [[nodiscard]] unsigned offset( void ) const { return( f_b ); }
239
242 [[nodiscard]] std::size_t size( void ) const;
243
245 [[nodiscard]] bool empty( void ) const;
246
249 [[nodiscard]] std::size_t prefix( void ) const;
250
255 [[nodiscard]] bool consistent( void ) const;
256
257/*--------------------------------------------------------------------------*/
258 private:
259 using Word = std::uintptr_t;
260
261 static constexpr Word kMark = 1;
262 static constexpr Word kCut = 2;
263
269 static constexpr std::memory_order kWalk = std::memory_order_seq_cst;
270
273 struct Link
274 {
275 std::atomic< Word > * next;
276 Link * later;
277 int height;
278 std::atomic< bool > inserting;
279 };
280
282 struct Node : Link
283 {
284 K key;
285 std::uint64_t stamp;
286 T item;
287 };
288
292 struct alignas( 64 ) Record
293 {
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 };
309 };
310
312 [[nodiscard]] static Link * ptr( Word w ) {
313 return( reinterpret_cast< Link * >( w & ~( kMark | kCut ) ) );
314 }
315
317 [[nodiscard]] static Word word( const Link * p ) {
318 return( reinterpret_cast< Word >( p ) );
319 }
320
322 [[nodiscard]] static bool marked( const Link * p ) {
323 return( p->next[ 0 ].load( kWalk ) & kMark );
324 }
325
328 static void * allocate( std::size_t n , int h , std::atomic< Word > *& next );
329
331 static Link * makeLink( int h );
332
334 static Node * make( int h , const K & k , std::uint64_t s , T && x );
335
337 static void destroy( Node * p );
338
340 static void destroyLink( Link * p );
341
343 static void destroyAll( Link * p );
344
346 [[nodiscard]] bool before( const Link * p , const K & k ,
347 std::uint64_t s ) const;
348
351 void locate( const K & k , std::uint64_t s , Link ** preds ,
352 Link ** succs ) const;
353
357 [[nodiscard]] bool behind( const Link * c , const Link * n ) const;
358
361 void restructure( Counts & c );
362
364 void release( void );
365
366 unsigned f_b;
367 Less f_less;
368 Link * f_head;
369 Link * f_tail;
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;
376
377 }; // end( class LockFreePriorityQueue )
378
379/*--------------------------------------------------------------------------*/
380/*-------------------- CLASS LockFreePriorityQueue::Handle -----------------*/
381/*--------------------------------------------------------------------------*/
382
385template< class K , class T , class Less >
386class LockFreePriorityQueue< K , T , Less >::Handle
387{
388
389/*--------------------------------------------------------------------------*/
390 public:
392 Handle( Handle && o ) noexcept;
393
395 Handle( const Handle & ) = delete;
397 Handle & operator=( const Handle & ) = delete;
399 Handle & operator=( Handle && ) = delete;
400
403
453 void insert( K k , T x );
454
502 std::optional< Entry > tryDeleteMin( void );
503
506 std::optional< K > tryMinKey( void );
507
510 [[nodiscard]] bool empty( void ) { return( ! tryMinKey() ); }
511
513 [[nodiscard]] const Counts & counts( void ) const { return( f_c ); }
514
515/*--------------------------------------------------------------------------*/
516 private:
518
520 Handle( LockFreePriorityQueue & q , Record * r , std::uint64_t seed )
521 : f_q( &q ) , f_r( r ) , f_x( seed ) { }
522
524 std::uint64_t next( void );
525
528 int height( void );
529
532 void enter( void );
533
535 void leave( void );
536
538 void retire( Node * p );
539
541 void advance( void );
542
544 Record * f_r;
545 std::uint64_t f_x;
546 Counts f_c;
547
548 }; // end( class LockFreePriorityQueue::Handle )
549
550/*--------------------------------------------------------------------------*/
551
552} // end( namespace ADS )
553
554/*--------------------------------------------------------------------------*/
555/*-------------------------- IMPLEMENTATION --------------------------------*/
556/*--------------------------------------------------------------------------*/
557
558#include "ADS/concurrent/impl/LockFreePriorityQueue.tpp"
559
560/*--------------------------------------------------------------------------*/
561
562#endif /* LockFreePriorityQueue.h included */
563
564/*--------------------------------------------------------------------------*/
565/*-------------------- End File LockFreePriorityQueue.h --------------------*/
566/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96
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