This repository was archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathconcurrent_skip_list_impl.hpp
More file actions
3369 lines (3055 loc) · 95.8 KB
/
concurrent_skip_list_impl.hpp
File metadata and controls
3369 lines (3055 loc) · 95.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020-2021, Intel Corporation */
/**
* @file
* Persistent memory aware implementation of the concurrent skip list.
*/
#ifndef PMEMOBJ_CONCURRENT_SKIP_LIST_IMPL_HPP
#define PMEMOBJ_CONCURRENT_SKIP_LIST_IMPL_HPP
#include <algorithm>
#include <array>
#include <atomic>
#include <cstdlib>
#include <limits>
#include <mutex> /* for std::unique_lock */
#include <random>
#include <type_traits>
#include <libpmemobj++/detail/common.hpp>
#include <libpmemobj++/detail/enumerable_thread_specific.hpp>
#include <libpmemobj++/detail/life.hpp>
#include <libpmemobj++/detail/pair.hpp>
#include <libpmemobj++/detail/template_helpers.hpp>
#include <libpmemobj++/mutex.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>
#include <libpmemobj++/experimental/atomic_self_relative_ptr.hpp>
#include <libpmemobj++/experimental/self_relative_ptr.hpp>
/* Windows has a max and a min macros which collides with min() and max()
* methods of default_random_generator */
#if defined(_WIN32)
#if defined(max)
#undef max
#endif
#if defined(min)
#undef min
#endif
#endif
namespace pmem
{
namespace detail
{
#ifndef NDEBUG
inline void
try_insert_node_finish_marker()
{
}
#endif
/**
* Copy assignment implementation for allocator if
* propagate_on_container_copy_assignment == true_type
*/
template <typename MyAlloc, typename OtherAlloc>
inline void
allocator_copy_assignment(MyAlloc &my_allocator, OtherAlloc &other_allocator,
std::true_type)
{
my_allocator = other_allocator;
}
/**
* Copy assignment implementation for allocator if
* propagate_on_container_copy_assignment == false_type
*/
template <typename MyAlloc, typename OtherAlloc>
inline void
allocator_copy_assignment(MyAlloc &, OtherAlloc &, std::false_type)
{ /* NO COPY */
}
/**
* Move assignment implementation for allocator if
* propagate_on_container_move_assignment == true_type.
*/
template <typename MyAlloc, typename OtherAlloc>
inline void
allocator_move_assignment(MyAlloc &my_allocator, OtherAlloc &other_allocator,
std::true_type)
{
my_allocator = std::move(other_allocator);
}
/**
* Move assignment implementation for allocator if
* propagate_on_container_move_assignment == false_type.
*/
template <typename MyAlloc, typename OtherAlloc>
inline void
allocator_move_assignment(MyAlloc &, OtherAlloc &, std::false_type)
{ /* NO MOVE */
}
/**
* Swap implementation for allocators if propagate_on_container_swap ==
* true_type.
*/
template <typename MyAlloc, typename OtherAlloc>
inline void
allocator_swap(MyAlloc &my_allocator, OtherAlloc &other_allocator,
std::true_type)
{
std::swap(my_allocator, other_allocator);
}
/**
* Swap implementation for allocators if propagate_on_container_swap ==
* false_type.
*/
template <typename MyAlloc, typename OtherAlloc>
inline void
allocator_swap(MyAlloc &, OtherAlloc &, std::false_type)
{ /* NO SWAP */
}
template <typename Value, typename UsePersistentAwarePtr,
typename Mutex = pmem::obj::mutex,
typename LockType = std::unique_lock<Mutex>>
class skip_list_node {
public:
using value_type = Value;
using size_type = std::size_t;
using reference = value_type &;
using const_reference = const value_type &;
using pointer = value_type *;
using const_pointer = const value_type *;
using node_pointer =
obj::experimental::self_relative_ptr<skip_list_node,
UsePersistentAwarePtr>;
using atomic_node_pointer = std::atomic<node_pointer>;
using mutex_type = Mutex;
using lock_type = LockType;
skip_list_node(size_type levels) : height_(levels)
{
for (size_type lev = 0; lev < height_; ++lev)
detail::create<atomic_node_pointer>(&get_next(lev),
nullptr);
assert(height() == levels);
#if LIBPMEMOBJ_CPP_VG_HELGRIND_ENABLED
/*
* Valgrind does not understand atomic semantic and reports
* false-postives in drd and helgrind tools.
*/
for (size_type lev = 0; lev < height_; ++lev) {
VALGRIND_HG_DISABLE_CHECKING(&get_next(lev),
sizeof(get_next(lev)));
}
#endif
}
skip_list_node(size_type levels, const node_pointer *new_nexts)
: height_(levels)
{
for (size_type lev = 0; lev < height_; ++lev)
detail::create<atomic_node_pointer>(&get_next(lev),
new_nexts[lev]);
assert(height() == levels);
#if LIBPMEMOBJ_CPP_VG_HELGRIND_ENABLED
/*
* Valgrind does not understand atomic semantic and reports
* false-postives in drd and helgrind tools.
*/
for (size_type lev = 0; lev < height_; ++lev) {
VALGRIND_HG_DISABLE_CHECKING(&get_next(lev),
sizeof(get_next(lev)));
}
#endif
}
~skip_list_node()
{
for (size_type lev = 0; lev < height_; ++lev)
detail::destroy<atomic_node_pointer>(get_next(lev));
}
skip_list_node(const skip_list_node &) = delete;
skip_list_node &operator=(const skip_list_node &) = delete;
pointer
get() noexcept
{
return &val;
}
const_pointer
get() const noexcept
{
return &val;
}
reference
value()
{
return *get();
}
node_pointer
next(size_type level) const
{
assert(level < height());
return get_next(level).load(std::memory_order_acquire);
}
template <typename U = void,
typename = typename std::enable_if<
std::is_same<UsePersistentAwarePtr,
std::true_type>::value,
U>::type>
node_pointer
next(size_type level)
{
assert(level < height());
return get_next(level).persist_load(std::memory_order_acquire);
}
/**
* Can`t be called concurrently
* Should be called inside a transaction
*/
void
set_next_tx(size_type level, node_pointer next)
{
assert(level < height());
assert(pmemobj_tx_stage() == TX_STAGE_WORK);
auto &node = get_next(level);
obj::flat_transaction::snapshot<atomic_node_pointer>(&node);
node.store(next, std::memory_order_release);
}
void
set_next(obj::pool_base pop, size_type level, node_pointer next)
{
assert(level < height());
auto &node = get_next(level);
node.store(next, std::memory_order_release);
pop.persist(&node, sizeof(node));
}
void
set_next(size_type level, node_pointer next)
{
assert(level < height());
auto &node = get_next(level);
node.store(node_pointer{next.get(), true},
std::memory_order_release);
/* instead of persist it immediately, mark it dirty,
* and rely on consequent get_next operation to flush.
*/
}
void
set_nexts(const node_pointer *new_nexts, size_type h)
{
assert(h == height());
auto *nexts = get_nexts();
for (size_type i = 0; i < h; i++) {
nexts[i].store(node_pointer{new_nexts[i].get(), true},
std::memory_order_relaxed);
}
}
void
set_nexts(obj::pool_base pop, const node_pointer *new_nexts,
size_type h)
{
set_nexts(new_nexts, h);
auto *nexts = get_nexts();
pop.persist(nexts, sizeof(nexts[0]) * h);
}
/** @return number of layers */
size_type
height() const
{
return height_;
}
lock_type
acquire()
{
return lock_type(mutex);
}
private:
atomic_node_pointer *
get_nexts()
{
return reinterpret_cast<atomic_node_pointer *>(this + 1);
}
atomic_node_pointer &
get_next(size_type level)
{
auto *arr = get_nexts();
return arr[level];
}
const atomic_node_pointer &
get_next(size_type level) const
{
auto *arr =
reinterpret_cast<const atomic_node_pointer *>(this + 1);
return arr[level];
}
mutex_type mutex;
union {
value_type val;
};
size_type height_;
};
template <typename NodeType, bool is_const>
class skip_list_iterator {
using node_type = NodeType;
using node_ptr = typename std::conditional<is_const, const node_type *,
node_type *>::type;
friend class skip_list_iterator<node_type, true>;
public:
using value_type = typename node_type::value_type;
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using reference =
typename std::conditional<is_const,
typename node_type::const_reference,
typename node_type::reference>::type;
using pointer = typename std::conditional<is_const, const value_type *,
value_type *>::type;
skip_list_iterator() : node(nullptr)
{
}
/** Copy constructor. */
skip_list_iterator(const skip_list_iterator &other) : node(other.node)
{
}
/** Copy constructor for const iterator from non-const iterator */
template <typename U = void,
typename = typename std::enable_if<is_const, U>::type>
skip_list_iterator(const skip_list_iterator<node_type, false> &other)
: node(other.node)
{
}
reference operator*() const
{
return *(node->get());
}
pointer operator->() const
{
return node->get();
}
skip_list_iterator &
operator++()
{
assert(node != nullptr);
node = node->next(0).get();
return *this;
}
skip_list_iterator
operator++(int)
{
skip_list_iterator tmp = *this;
++*this;
return tmp;
}
skip_list_iterator &
operator=(const skip_list_iterator &other)
{
node = other.node;
return *this;
}
private:
explicit skip_list_iterator(node_type *n) : node(n)
{
}
template <typename T = void,
typename = typename std::enable_if<is_const, T>::type>
explicit skip_list_iterator(const node_type *n) : node(n)
{
}
node_ptr node;
template <typename Traits>
friend class concurrent_skip_list;
template <typename T, bool M, bool U>
friend bool operator==(const skip_list_iterator<T, M> &lhs,
const skip_list_iterator<T, U> &rhs);
template <typename T, bool M, bool U>
friend bool operator!=(const skip_list_iterator<T, M> &lhs,
const skip_list_iterator<T, U> &rhs);
};
template <typename T, bool M, bool U>
bool
operator==(const skip_list_iterator<T, M> &lhs,
const skip_list_iterator<T, U> &rhs)
{
return lhs.node == rhs.node;
}
template <typename T, bool M, bool U>
bool
operator!=(const skip_list_iterator<T, M> &lhs,
const skip_list_iterator<T, U> &rhs)
{
return lhs.node != rhs.node;
}
struct default_random_generator {
using gen_type = std::mt19937_64;
using result_type = typename gen_type::result_type;
size_t
operator()()
{
static thread_local gen_type engine(
static_cast<size_t>(time(0)));
return engine();
}
static constexpr result_type
min()
{
return gen_type::min();
}
static constexpr result_type
max()
{
return gen_type::max();
}
};
template <typename RndGenerator, size_t MAX_LEVEL>
class geometric_level_generator {
public:
using rnd_generator_type = RndGenerator;
static constexpr size_t max_level = MAX_LEVEL;
size_t
operator()()
{
/* rnd_generator_type should be thread-safe random number
* generator. */
static rnd_generator_type gen;
/* std::geometric_distribution is not thread-safe. We mark it as
* a thread_local to avoid data races. */
static thread_local std::geometric_distribution<size_t> d;
return (d(gen) % MAX_LEVEL) + 1;
}
};
/**
* Persistent memory aware implementation of the concurrent skip list.
*
* The implementation is based on the lock-based algorithm described in
* https://www.cs.tau.ac.il/~shanir/nir-pubs-web/Papers/OPODIS2006-BA.pdf.
*
* Our concurrent skip list implementation supports concurrent insertion and
* traversal, but not concurrent erasure. The erase method is prefixed with
* unsafe_, to indicate that there is no concurrency safety.
*
* Each time, the pool with concurrent_skip_list is being opened, the
* concurrent_skip_list requires runtime_initialize() to be called in order to
* restore the state after process restart.
*
* Traits template parameter allows to specify properties of the
* concurrent_ski_list. The Traits type should has the following member types:
* * key_type - type of the key
* * mapped_type - type of the mapped_value
* * value_type - type of the value stored inside the skip list node (e.g.
* pair<const key_type, mapped_type>).
* * compare_type - The comparison functor used to sort elements in the skip
* list.
* * allocator_type - The type of allocator used by the skip list.
* * max_level - The constant value which specify the number of layers in the
* skip list.
* * random_generator_type - The type of random generator used by the skip list.
* It should be thread-safe.
*/
template <typename Traits>
class concurrent_skip_list {
protected:
using traits_type = Traits;
using key_type = typename traits_type::key_type;
using mapped_type = typename traits_type::mapped_type;
using value_type = typename traits_type::value_type;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using key_compare = typename traits_type::compare_type;
using allocator_type = typename traits_type::allocator_type;
using allocator_traits_type = std::allocator_traits<allocator_type>;
using reference = value_type &;
using const_reference = const value_type &;
using pointer = typename allocator_traits_type::pointer;
using const_pointer = typename allocator_traits_type::const_pointer;
using use_persistent_aware_ptr =
typename traits_type::use_persistent_aware_ptr;
using list_node_type =
skip_list_node<value_type, use_persistent_aware_ptr>;
using iterator = skip_list_iterator<list_node_type, false>;
using const_iterator = skip_list_iterator<list_node_type, true>;
static constexpr size_type MAX_LEVEL = traits_type::max_level;
using random_level_generator_type = geometric_level_generator<
typename traits_type::random_generator_type, MAX_LEVEL>;
using node_allocator_type = typename std::allocator_traits<
allocator_type>::template rebind_alloc<uint8_t>;
using node_allocator_traits = typename std::allocator_traits<
allocator_type>::template rebind_traits<uint8_t>;
using node_ptr = list_node_type *;
using const_node_ptr = const list_node_type *;
using persistent_node_ptr =
obj::experimental::self_relative_ptr<list_node_type,
use_persistent_aware_ptr>;
using prev_array_type = std::array<node_ptr, MAX_LEVEL>;
using next_array_type = std::array<persistent_node_ptr, MAX_LEVEL>;
using node_lock_type = typename list_node_type::lock_type;
using lock_array = std::array<node_lock_type, MAX_LEVEL>;
public:
static constexpr bool allow_multimapping =
traits_type::allow_multimapping;
/**
* Default constructor. Construct empty skip list.
*
* @pre must be called in transaction scope.
* @throw pmem::pool_error if an object is not in persistent memory.
* @throw pmem::transaction_scope_error if constructor wasn't called in
* transaction.
*/
concurrent_skip_list()
{
check_tx_stage_work();
init();
}
/**
* Constructs an empty container.
*
* @param[in] comp comparison function object to use for all comparisons
* of keys.
* @param[in] alloc allocator to use for all memory allocations of this
* container.
*
* @pre must be called in transaction scope.
*
* @throw pmem::pool_error if an object is not in persistent memory.
* @throw pmem::transaction_scope_error if constructor wasn't called in
* transaction.
* @throw pmem::transaction_alloc_error when allocating memory for
* inserted elements in transaction failed.
*/
explicit concurrent_skip_list(
const key_compare &comp,
const allocator_type &alloc = allocator_type())
: _node_allocator(alloc), _compare(comp)
{
check_tx_stage_work();
init();
}
/**
* Constructs the container with the contents of the range [first,
* last). If multiple elements in the range have keys that compare
* equivalent, the first element is inserted.
*
* @param[in] first first iterator of inserted range.
* @param[in] last last iterator of inserted range.
* @param[in] comp comparison function object to use for all comparisons
* of keys.
* @param[in] alloc allocator to use for all memory allocations of this
* container.
*
* InputIt must meet the requirements of LegacyInputIterator.
*
* @pre must be called in transaction scope.
*
* @throw pmem::pool_error if an object is not in persistent memory.
* @throw pmem::transaction_scope_error if constructor wasn't called in
* transaction.
* @throw pmem::transaction_alloc_error when allocating memory for
* inserted elements in transaction failed.
* @throw rethrows element constructor exception.
*/
template <class InputIt>
concurrent_skip_list(InputIt first, InputIt last,
const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: _node_allocator(alloc), _compare(comp)
{
check_tx_stage_work();
init();
while (first != last)
internal_unsafe_emplace(*first++);
}
/**
* Copy constructor. Constructs the container with the copy of the
* contents of other.
*
* @param[in] other reference to the concurrent_skip_list to be copied.
*
* @pre must be called in transaction scope.
*
* @post size() == other.size()
*
* @throw pmem::pool_error if an object is not in persistent memory.
* @throw pmem::transaction_alloc_error when allocating memory for
* copied elements in transaction failed.
* @throw pmem::transaction_scope_error if constructor wasn't called in
* transaction.
* @throw rethrows element constructor exception.
*/
concurrent_skip_list(const concurrent_skip_list &other)
: _node_allocator(node_allocator_traits::
select_on_container_copy_construction(
other._node_allocator)),
_compare(other._compare),
_rnd_generator(other._rnd_generator)
{
check_tx_stage_work();
init();
internal_copy(other);
assert(_size == other._size);
}
/**
* Copy constructor. Constructs the container with the copy of the
* contents of other.
*
* @param[in] other reference to the concurrent_skip_list to be copied.
* @param[in] alloc allocator to use for all memory allocations of this
* container.
*
* @pre must be called in transaction scope.
*
* @post size() == other.size()
*
* @throw pmem::pool_error if an object is not in persistent memory.
* @throw pmem::transaction_alloc_error when allocating memory for
* copied elements in transaction failed.
* @throw pmem::transaction_scope_error if constructor wasn't called in
* transaction.
* @throw rethrows element constructor exception.
*/
concurrent_skip_list(const concurrent_skip_list &other,
const allocator_type &alloc)
: _node_allocator(alloc),
_compare(other._compare),
_rnd_generator(other._rnd_generator)
{
check_tx_stage_work();
init();
internal_copy(other);
assert(_size == other._size);
}
/**
* Move constructor. Constructs the container with the contents of other
* using move semantics. Allocator is obtained by move-construction from
* the allocator belonging to other
*
* @param[in] other reference to the concurrent_skip_list to be copied.
*
* @pre must be called in transaction scope.
*
* @post size() == other.size()
*
* @throw pmem::pool_error if an object is not in persistent memory.
* @throw pmem::transaction_alloc_error when allocating memory for
* copied elements in transaction failed.
* @throw pmem::transaction_scope_error if constructor wasn't called in
* transaction.
* @throw rethrows element constructor exception.
*/
concurrent_skip_list(concurrent_skip_list &&other)
: _node_allocator(std::move(other._node_allocator)),
_compare(other._compare),
_rnd_generator(other._rnd_generator)
{
check_tx_stage_work();
init();
internal_move(std::move(other));
}
/**
* Move constructor. Constructs the container with the contents of other
* using move semantics.
*
* @param[in] other reference to the concurrent_skip_list to be copied.
* @param[in] alloc allocator to use for all memory allocations of this
* container.
*
* @pre must be called in transaction scope.
*
* @post size() == other.size()
*
* @throw pmem::pool_error if an object is not in persistent memory.
* @throw pmem::transaction_alloc_error when allocating memory for
* copied elements in transaction failed.
* @throw pmem::transaction_scope_error if constructor wasn't called in
* transaction.
* @throw rethrows element constructor exception.
*/
concurrent_skip_list(concurrent_skip_list &&other,
const allocator_type &alloc)
: _node_allocator(alloc),
_compare(other._compare),
_rnd_generator(other._rnd_generator)
{
check_tx_stage_work();
init();
if (alloc == other.get_allocator()) {
internal_move(std::move(other));
} else {
init();
internal_copy(std::make_move_iterator(other.begin()),
std::make_move_iterator(other.end()));
}
}
/**
* Initialize concurrent_skip_list after process restart.
* MUST be called every time after process restart.
* Not thread safe.
*
*/
void
runtime_initialize()
{
tls_restore();
assert(this->size() ==
size_type(std::distance(this->begin(), this->end())));
}
/**
* Should be called before concurrent_skip_list destructor is called.
* Otherwise, program can terminate if an exception occurs while freeing
* memory inside dtor.
*
* The skip list map can NOT be used after free_data() was called
* (unless it was called in a transaction and that transaction aborted).
*
* @throw std::transaction_error in case of PMDK transaction failure
* @throw pmem::transaction_free_error when freeing underlying memory
* failed.
*/
void
free_data()
{
if (dummy_head == nullptr)
return;
auto pop = get_pool_base();
obj::flat_transaction::run(pop, [&] {
clear();
delete_dummy_head();
});
}
/**
* Destructor.
* free_data should be called before concurrent_skip_list
* destructor is called. Otherwise, program can terminate if
* an exception occurs while freeing memory inside dtor.
*
* The skip list map can NOT be used after free_data() was called
* (unless it was called in a transaction and that transaction aborted).
*/
~concurrent_skip_list()
{
try {
free_data();
} catch (...) {
std::terminate();
}
}
/**
* Copy assignment operator. Replaces the contents with a copy of the
* contents of other transactionally. If
* std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value
* is true, the target allocator is replaced by a copy of the source
* allocator.
*
* @post size() == other.size()
*
* @throw pmem::transaction_alloc_error when allocating new memory
* failed.
* @throw pmem::transaction_free_error when freeing old existing
* elements failed.
* @throw rethrows constructor exception.
*/
concurrent_skip_list &
operator=(const concurrent_skip_list &other)
{
if (this == &other)
return *this;
obj::pool_base pop = get_pool_base();
obj::flat_transaction::run(pop, [&] {
using pocca_t = typename node_allocator_traits::
propagate_on_container_copy_assignment;
clear();
allocator_copy_assignment(_node_allocator,
other._node_allocator,
pocca_t());
_compare = other._compare;
_rnd_generator = other._rnd_generator;
internal_copy(other);
});
return *this;
}
/**
* Move assignment operator. Replaces the contents with those of other
* using move semantics (i.e. the data in other is moved from other into
* this container). other is in a valid but unspecified state
* afterwards. If
* std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value
* is true, the target allocator is replaced by a copy of the source
* allocator. If it is false and the source and the target allocators do
* not compare equal, the target cannot take ownership of the source
* memory and must move-assign each element individually, allocating
* additional memory using its own allocator as needed. In any case, all
* elements originally present in *this are either destroyed or replaced
* by elementwise move-assignment.
*
* @throw pmem::transaction_alloc_error when allocating new memory
* failed.
* @throw pmem::transaction_free_error when freeing old existing
* elements failed.
* @throw rethrows constructor exception.
*/
concurrent_skip_list &
operator=(concurrent_skip_list &&other)
{
if (this == &other)
return *this;
obj::pool_base pop = get_pool_base();
obj::flat_transaction::run(pop, [&] {
using pocma_t = typename node_allocator_traits::
propagate_on_container_move_assignment;
clear();
if (pocma_t::value ||
_node_allocator == other._node_allocator) {
delete_dummy_head();
allocator_move_assignment(_node_allocator,
other._node_allocator,
pocma_t());
_compare = other._compare;
_rnd_generator = other._rnd_generator;
internal_move(std::move(other));
} else {
internal_copy(
std::make_move_iterator(other.begin()),
std::make_move_iterator(other.end()));
}
});
return *this;
}
/**
* Replaces the contents with those identified by initializer list il.
*
* @param[in] il initializer list to use as data source
*
* @throw pmem::transaction_alloc_error when allocating new memory
* failed.
* @throw pmem::transaction_free_error when freeing old existing
* elements failed.
* @throw rethrows constructor exception.
*/
concurrent_skip_list &
operator=(std::initializer_list<value_type> il)
{
obj::pool_base pop = get_pool_base();
obj::flat_transaction::run(pop, [&] {
clear();
for (auto it = il.begin(); it != il.end(); ++it)
internal_unsafe_emplace(*it);
});
return *this;
}
/**
* Inserts value in a thread-safe way. No iterators or references are
* invalidated.
*
* @param[in] value element value to insert.
*
* @return a pair consisting of an iterator to the inserted element (or
* to the element that prevented the insertion) and a bool denoting
* whether the insertion took place.
*
* @throw pmem::transaction_error when snapshotting failed.
* @throw pmem::transaction_alloc_error when allocating new memory
* failed.
* @throw pmem::transaction_scope_error if called inside transaction.
* @throw rethrows constructor exception.
*/
std::pair<iterator, bool>
insert(const value_type &value)
{
return internal_insert(value.first, value);
}
/**
* Inserts value. No iterators or references are invalidated.
* This overload is equivalent to emplace(std::forward<P>(value)) and
* only participates in overload resolution if
* std::is_constructible<value_type, P&&>::value == true.
*
* @param[in] value element value to insert.
*
* @return a pair consisting of an iterator to the inserted element (or
* to the element that prevented the insertion) and a bool denoting
* whether the insertion took place.
*
* @throw pmem::transaction_error when snapshotting failed.
* @throw pmem::transaction_alloc_error when allocating new memory
* failed.
* @throw pmem::transaction_scope_error if called inside transaction.
* @throw rethrows constructor exception.
*/
template <typename P,
typename std::enable_if<
std::is_constructible<value_type, P &&>::value>::type>
std::pair<iterator, bool>
insert(P &&value)
{
return emplace(std::forward<P>(value));
}
/**
* Inserts value using move semantic. No iterators or references are
* invalidated.
*
* @param[in] value element value to insert.
*
* @return a pair consisting of an iterator to the inserted element (or
* to the element that prevented the insertion) and a bool denoting
* whether the insertion took place.
*
* @throw pmem::transaction_error when snapshotting failed.
* @throw pmem::transaction_alloc_error when allocating new memory
* failed.
* @throw pmem::transaction_scope_error if called inside transaction.
* @throw rethrows constructor exception.
*/
std::pair<iterator, bool>
insert(value_type &&value)
{
return internal_insert(value.first, std::move(value));
}
/**
* Inserts value in the position as close as possible, just prior to
* hint. No iterators or references are invalidated.
*
* @param[in] hint iterator to the position before which the new element