forked from greensky00/skiplist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskiplist.cc
1010 lines (873 loc) · 32.4 KB
/
skiplist.cc
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
/**
* Copyright (C) 2017-present Jung-Sang Ahn <[email protected]>
* All rights reserved.
*
* https://github.com/greensky00
*
* Skiplist
* Version: 0.2.9
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "skiplist.h"
#include <stdlib.h>
#define __SLD_RT_INS(e, n, t, c)
#define __SLD_NC_INS(n, nn, t, c)
#define __SLD_RT_RMV(e, n, t, c)
#define __SLD_NC_RMV(n, nn, t, c)
#define __SLD_BM(n)
#define __SLD_ASSERT(cond)
#define __SLD_P(args...)
#define __SLD_(b)
//#define __SL_DEBUG (1)
#ifdef __SL_DEBUG
#ifndef __cplusplus
#error "Debug mode is available with C++ compiler only."
#endif
#include "skiplist_debug.h"
#endif
#define __SL_YIELD (1)
#ifdef __SL_YIELD
#ifdef __cplusplus
#include <thread>
#define YIELD() std::this_thread::yield()
#else
#include <sched.h>
#define YIELD() sched_yield()
#endif
#else
#define YIELD()
#endif
#if defined(_STL_ATOMIC) && defined(__cplusplus)
// C++ (STL) atomic operations
#define MOR std::memory_order_relaxed
#define ATM_GET(var) (var).load(MOR)
#define ATM_LOAD(var, val) (val) = (var).load(MOR)
#define ATM_STORE(var, val) (var).store((val), MOR)
#define ATM_CAS(var, exp, val) (var).compare_exchange_weak((exp), (val))
#define ATM_FETCH_ADD(var, val) (var).fetch_add(val, MOR)
#define ATM_FETCH_SUB(var, val) (var).fetch_sub(val, MOR)
#define ALLOC_(type, var, count) (var) = new type[count]
#define FREE_(var) delete[] (var)
#else
// C-style atomic operations
#ifndef __cplusplus
typedef uint8_t bool;
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
#endif
#ifndef __cplusplus
#define thread_local /*_Thread_local*/
#endif
#define MOR __ATOMIC_RELAXED
#define ATM_GET(var) (var)
#define ATM_LOAD(var, val) __atomic_load(&(var), &(val), MOR)
#define ATM_STORE(var, val) __atomic_store(&(var), &(val), MOR)
#define ATM_CAS(var, exp, val) \
__atomic_compare_exchange(&(var), &(exp), &(val), 1, MOR, MOR)
#define ATM_FETCH_ADD(var, val) __atomic_fetch_add(&(var), (val), MOR)
#define ATM_FETCH_SUB(var, val) __atomic_fetch_sub(&(var), (val), MOR)
#define ALLOC_(type, var, count) \
(var) = (type*)calloc(count, sizeof(type))
#define FREE_(var) free(var)
#endif
static inline void _sl_node_init(skiplist_node *node,
size_t top_layer)
{
if (top_layer > UINT8_MAX) top_layer = UINT8_MAX;
__SLD_ASSERT(node->is_fully_linked == false);
__SLD_ASSERT(node->being_modified == false);
bool bool_val = false;
ATM_STORE(node->is_fully_linked, bool_val);
ATM_STORE(node->being_modified, bool_val);
ATM_STORE(node->removed, bool_val);
if (node->top_layer != top_layer ||
node->next == NULL) {
node->top_layer = top_layer;
if (node->next) FREE_(node->next);
ALLOC_(atm_node_ptr, node->next, top_layer+1);
}
}
void skiplist_init(skiplist_raw *slist,
skiplist_cmp_t *cmp_func) {
slist->cmp_func = NULL;
slist->aux = NULL;
// fanout 4 + layer 12: 4^12 ~= upto 17M items under O(lg n) complexity.
// for +17M items, complexity will grow linearly: O(k lg n).
slist->fanout = 4;
slist->max_layer = 12;
slist->num_entries = 0;
ALLOC_(atm_uint32_t, slist->layer_entries, slist->max_layer);
slist->top_layer = 0;
skiplist_init_node(&slist->head);
skiplist_init_node(&slist->tail);
_sl_node_init(&slist->head, slist->max_layer);
_sl_node_init(&slist->tail, slist->max_layer);
size_t layer;
for (layer = 0; layer < slist->max_layer; ++layer) {
slist->head.next[layer] = &slist->tail;
slist->tail.next[layer] = NULL;
}
bool bool_val = true;
ATM_STORE(slist->head.is_fully_linked, bool_val);
ATM_STORE(slist->tail.is_fully_linked, bool_val);
slist->cmp_func = cmp_func;
}
void skiplist_free(skiplist_raw *slist)
{
skiplist_free_node(&slist->head);
skiplist_free_node(&slist->tail);
FREE_(slist->layer_entries);
slist->layer_entries = NULL;
slist->aux = NULL;
slist->cmp_func = NULL;
}
void skiplist_init_node(skiplist_node *node)
{
node->next = NULL;
bool bool_false = false;
ATM_STORE(node->is_fully_linked, bool_false);
ATM_STORE(node->being_modified, bool_false);
ATM_STORE(node->removed, bool_false);
node->accessing_next = 0;
node->top_layer = 0;
node->ref_count = 0;
}
void skiplist_free_node(skiplist_node *node)
{
FREE_(node->next);
node->next = NULL;
}
size_t skiplist_get_size(skiplist_raw* slist) {
uint32_t val;
ATM_LOAD(slist->num_entries, val);
return val;
}
skiplist_raw_config skiplist_get_default_config()
{
skiplist_raw_config ret;
ret.fanout = 4;
ret.maxLayer = 12;
ret.aux = NULL;
return ret;
}
skiplist_raw_config skiplist_get_config(skiplist_raw *slist)
{
skiplist_raw_config ret;
ret.fanout = slist->fanout;
ret.maxLayer = slist->max_layer;
ret.aux = slist->aux;
return ret;
}
void skiplist_set_config(skiplist_raw *slist,
skiplist_raw_config config)
{
slist->fanout = config.fanout;
slist->max_layer = config.maxLayer;
if (slist->layer_entries) FREE_(slist->layer_entries);
ALLOC_(atm_uint32_t, slist->layer_entries, slist->max_layer);
slist->aux = config.aux;
}
static inline int _sl_cmp(skiplist_raw *slist,
skiplist_node *a,
skiplist_node *b)
{
if (a == b) return 0;
if (a == &slist->head || b == &slist->tail) return -1;
if (a == &slist->tail || b == &slist->head) return 1;
return slist->cmp_func(a, b, slist->aux);
}
static inline bool _sl_valid_node(skiplist_node *node) {
bool is_fully_linked = false;
ATM_LOAD(node->is_fully_linked, is_fully_linked);
return is_fully_linked;
}
static inline void _sl_read_lock_an(skiplist_node* node) {
for(;;) {
// Wait for active writer to release the lock
uint32_t accessing_next = 0;
ATM_LOAD(node->accessing_next, accessing_next);
while (accessing_next & 0xfff00000) {
YIELD();
ATM_LOAD(node->accessing_next, accessing_next);
}
ATM_FETCH_ADD(node->accessing_next, 0x1);
ATM_LOAD(node->accessing_next, accessing_next);
if ((accessing_next & 0xfff00000) == 0) {
return;
}
ATM_FETCH_SUB(node->accessing_next, 0x1);
}
}
static inline void _sl_read_unlock_an(skiplist_node* node) {
ATM_FETCH_SUB(node->accessing_next, 0x1);
}
static inline void _sl_write_lock_an(skiplist_node* node) {
for(;;) {
// Wait for active writer to release the lock
uint32_t accessing_next = 0;
ATM_LOAD(node->accessing_next, accessing_next);
while (accessing_next & 0xfff00000) {
YIELD();
ATM_LOAD(node->accessing_next, accessing_next);
}
ATM_FETCH_ADD(node->accessing_next, 0x100000);
ATM_LOAD(node->accessing_next, accessing_next);
if((accessing_next & 0xfff00000) == 0x100000) {
// Wait until there's no more readers
while (accessing_next & 0x000fffff) {
YIELD();
ATM_LOAD(node->accessing_next, accessing_next);
}
return;
}
ATM_FETCH_SUB(node->accessing_next, 0x100000);
}
}
static inline void _sl_write_unlock_an(skiplist_node* node) {
ATM_FETCH_SUB(node->accessing_next, 0x100000);
}
// Note: it increases the `ref_count` of returned node.
// Caller is responsible to decrease it.
static inline skiplist_node* _sl_next(skiplist_raw* slist,
skiplist_node* cur_node,
int layer,
skiplist_node* node_to_find,
bool* found)
{
skiplist_node *next_node = NULL;
// Turn on `accessing_next`:
// now `cur_node` is not removable from skiplist,
// which means that `cur_node->next` will be consistent
// until clearing `accessing_next`.
_sl_read_lock_an(cur_node); {
if (!_sl_valid_node(cur_node)) {
_sl_read_unlock_an(cur_node);
return NULL;
}
ATM_LOAD(cur_node->next[layer], next_node);
// Increase ref count of `next_node`:
// now `next_node` is not destroyable.
// << Remaining issue >>
// 1) initially: A -> B
// 2) T1: call _sl_next(A):
// A.accessing_next := true;
// next_node := B;
// ----- context switch happens here -----
// 3) T2: insert C:
// A -> C -> B
// 4) T2: and then erase B, and free B.
// A -> C B(freed)
// ----- context switch back again -----
// 5) T1: try to do something with B,
// but crash happens.
//
// ... maybe resolved using RW spinlock (Aug 21, 2017).
__SLD_ASSERT(next_node);
ATM_FETCH_ADD(next_node->ref_count, 1);
__SLD_ASSERT(next_node->top_layer >= layer);
} _sl_read_unlock_an(cur_node);
size_t num_nodes = 0;
skiplist_node* nodes[256];
while ( (next_node && !_sl_valid_node(next_node)) ||
next_node == node_to_find ) {
if (found && node_to_find == next_node) *found = true;
skiplist_node* temp = next_node;
_sl_read_lock_an(temp); {
__SLD_ASSERT(next_node);
if (!_sl_valid_node(temp)) {
_sl_read_unlock_an(temp);
ATM_FETCH_SUB(temp->ref_count, 1);
next_node = NULL;
break;
}
ATM_LOAD(temp->next[layer], next_node);
ATM_FETCH_ADD(next_node->ref_count, 1);
nodes[num_nodes++] = temp;
__SLD_ASSERT(next_node->top_layer >= layer);
} _sl_read_unlock_an(temp);
}
for (size_t ii=0; ii<num_nodes; ++ii) {
ATM_FETCH_SUB(nodes[ii]->ref_count, 1);
}
return next_node;
}
static inline size_t _sl_decide_top_layer(skiplist_raw *slist)
{
size_t layer = 0;
while (layer+1 < slist->max_layer) {
// coin filp
if (rand() % slist->fanout == 0) {
// grow: 1/fanout probability
layer++;
} else {
// stop: 1 - 1/fanout probability
break;
}
}
return layer;
}
static inline void _sl_clr_flags(skiplist_node** node_arr,
int start_layer,
int top_layer)
{
int layer;
for (layer = start_layer; layer <= top_layer; ++layer) {
if ( layer == top_layer ||
node_arr[layer] != node_arr[layer+1] ) {
bool exp = true;
bool bool_false = false;
if (!ATM_CAS(node_arr[layer]->being_modified, exp, bool_false)) {
__SLD_ASSERT(0);
}
}
}
}
static inline bool _sl_valid_prev_next(skiplist_node *prev,
skiplist_node *next) {
return _sl_valid_node(prev) && _sl_valid_node(next);
}
static inline int _skiplist_insert(skiplist_raw *slist,
skiplist_node *node,
bool no_dup)
{
__SLD_(
thread_local std::thread::id tid = std::this_thread::get_id();
thread_local size_t tid_hash = std::hash<std::thread::id>{}(tid) % 256;
(void)tid_hash;
)
int top_layer = _sl_decide_top_layer(slist);
bool bool_true = true;
// init node before insertion
_sl_node_init(node, top_layer);
_sl_write_lock_an(node);
skiplist_node* prevs[SKIPLIST_MAX_LAYER];
skiplist_node* nexts[SKIPLIST_MAX_LAYER];
__SLD_P("%02x ins %p begin\n", (int)tid_hash, node);
insert_retry:
// in pure C, a label can only be part of a stmt.
(void)top_layer;
int cmp = 0, cur_layer = 0, layer;
skiplist_node *cur_node = &slist->head;
ATM_FETCH_ADD(cur_node->ref_count, 1);
__SLD_(size_t nh = 0);
__SLD_(thread_local skiplist_node* history[1024]; (void)history);
int sl_top_layer = slist->top_layer;
if (top_layer > sl_top_layer) sl_top_layer = top_layer;
for (cur_layer = sl_top_layer; cur_layer >= 0; --cur_layer) {
do {
__SLD_( history[nh++] = cur_node );
skiplist_node *next_node = _sl_next(slist, cur_node, cur_layer,
NULL, NULL);
if (!next_node) {
_sl_clr_flags(prevs, cur_layer+1, top_layer);
ATM_FETCH_SUB(cur_node->ref_count, 1);
YIELD();
goto insert_retry;
}
cmp = _sl_cmp(slist, node, next_node);
if (cmp > 0) {
// cur_node < next_node < node
// => move to next node
skiplist_node* temp = cur_node;
cur_node = next_node;
ATM_FETCH_SUB(temp->ref_count, 1);
continue;
} else {
// otherwise: cur_node < node <= next_node
ATM_FETCH_SUB(next_node->ref_count, 1);
}
if (no_dup && cmp == 0) {
// Duplicate key is not allowed.
_sl_clr_flags(prevs, cur_layer+1, top_layer);
ATM_FETCH_SUB(cur_node->ref_count, 1);
return -1;
}
if (cur_layer <= top_layer) {
prevs[cur_layer] = cur_node;
nexts[cur_layer] = next_node;
// both 'prev' and 'next' should be fully linked before
// insertion, and no other thread should not modify 'prev'
// at the same time.
int error_code = 0;
int locked_layer = cur_layer + 1;
// check if prev node is duplicated with upper layer
if (cur_layer < top_layer &&
prevs[cur_layer] == prevs[cur_layer+1]) {
// duplicate
// => which means that 'being_modified' flag is already true
// => do nothing
} else {
bool expected = false;
if (ATM_CAS(prevs[cur_layer]->being_modified,
expected, bool_true)) {
locked_layer = cur_layer;
} else {
error_code = -1;
}
}
if (error_code == 0 &&
!_sl_valid_prev_next(prevs[cur_layer], nexts[cur_layer])) {
error_code = -2;
}
if (error_code != 0) {
__SLD_RT_INS(error_code, node, top_layer, cur_layer);
_sl_clr_flags(prevs, locked_layer, top_layer);
ATM_FETCH_SUB(cur_node->ref_count, 1);
YIELD();
goto insert_retry;
}
// set current node's pointers
ATM_STORE(node->next[cur_layer], nexts[cur_layer]);
// check if `cur_node->next` has been changed from `next_node`.
skiplist_node* next_node_again =
_sl_next(slist, cur_node, cur_layer, NULL, NULL);
ATM_FETCH_SUB(next_node_again->ref_count, 1);
if (next_node_again != next_node) {
__SLD_NC_INS(cur_node, next_node, top_layer, cur_layer);
// clear including the current layer
// as we already set modification flag above.
_sl_clr_flags(prevs, cur_layer, top_layer);
ATM_FETCH_SUB(cur_node->ref_count, 1);
YIELD();
goto insert_retry;
}
}
if (cur_layer) {
// non-bottom layer => go down
break;
}
// bottom layer => insertion succeeded
// change prev/next nodes' prev/next pointers from 0 ~ top_layer
for (layer = 0; layer <= top_layer; ++layer) {
// `accessing_next` works as a spin-lock.
_sl_write_lock_an(prevs[layer]);
skiplist_node* exp = nexts[layer];
if ( !ATM_CAS(prevs[layer]->next[layer], exp, node) ) {
__SLD_P("%02x ASSERT ins %p[%d] -> %p (expected %p)\n",
(int)tid_hash, prevs[layer], cur_layer,
ATM_GET(prevs[layer]->next[layer]), nexts[layer] );
__SLD_ASSERT(0);
}
__SLD_P("%02x ins %p[%d] -> %p -> %p\n",
(int)tid_hash, prevs[layer], layer,
node, ATM_GET(node->next[layer]) );
_sl_write_unlock_an(prevs[layer]);
}
// now this node is fully linked
ATM_STORE(node->is_fully_linked, bool_true);
// allow removing next nodes
_sl_write_unlock_an(node);
__SLD_P("%02x ins %p done\n", (int)tid_hash, node);
ATM_FETCH_ADD(slist->num_entries, 1);
ATM_FETCH_ADD(slist->layer_entries[node->top_layer], 1);
for (int ii=slist->max_layer-1; ii>=0; --ii) {
if (slist->layer_entries[ii] > 0) {
slist->top_layer = ii;
break;
}
}
// modification is done for all layers
_sl_clr_flags(prevs, 0, top_layer);
ATM_FETCH_SUB(cur_node->ref_count, 1);
return 0;
} while (cur_node != &slist->tail);
}
return 0;
}
int skiplist_insert(skiplist_raw *slist,
skiplist_node *node)
{
return _skiplist_insert(slist, node, false);
}
int skiplist_insert_nodup(skiplist_raw *slist,
skiplist_node *node)
{
return _skiplist_insert(slist, node, true);
}
typedef enum {
SM = -2,
SMEQ = -1,
EQ = 0,
GTEQ = 1,
GT = 2
} _sl_find_mode;
// Note: it increases the `ref_count` of returned node.
// Caller is responsible to decrease it.
static inline skiplist_node* _sl_find(skiplist_raw *slist,
skiplist_node *query,
_sl_find_mode mode)
{
// mode:
// SM -2: smaller
// SMEQ -1: smaller or equal
// EQ 0: equal
// GTEQ 1: greater or equal
// GT 2: greater
find_retry:
(void)mode;
int cmp = 0;
int cur_layer = 0;
skiplist_node *cur_node = &slist->head;
ATM_FETCH_ADD(cur_node->ref_count, 1);
__SLD_(size_t nh = 0);
__SLD_(thread_local skiplist_node* history[1024]; (void)history);
uint8_t sl_top_layer = slist->top_layer;
for (cur_layer = sl_top_layer; cur_layer >= 0; --cur_layer) {
do {
__SLD_(history[nh++] = cur_node);
skiplist_node *next_node = _sl_next(slist, cur_node, cur_layer,
NULL, NULL);
if (!next_node) {
ATM_FETCH_SUB(cur_node->ref_count, 1);
YIELD();
goto find_retry;
}
cmp = _sl_cmp(slist, query, next_node);
if (cmp > 0) {
// cur_node < next_node < query
// => move to next node
skiplist_node* temp = cur_node;
cur_node = next_node;
ATM_FETCH_SUB(temp->ref_count, 1);
continue;
} else if (-1 <= mode && mode <= 1 && cmp == 0) {
// cur_node < query == next_node .. return
ATM_FETCH_SUB(cur_node->ref_count, 1);
return next_node;
}
// otherwise: cur_node < query < next_node
if (cur_layer) {
// non-bottom layer => go down
ATM_FETCH_SUB(next_node->ref_count, 1);
break;
}
// bottom layer
if (mode < 0 && cur_node != &slist->head) {
// smaller mode
ATM_FETCH_SUB(next_node->ref_count, 1);
return cur_node;
} else if (mode > 0 && next_node != &slist->tail) {
// greater mode
ATM_FETCH_SUB(cur_node->ref_count, 1);
return next_node;
}
// otherwise: exact match mode OR not found
ATM_FETCH_SUB(cur_node->ref_count, 1);
ATM_FETCH_SUB(next_node->ref_count, 1);
return NULL;
} while (cur_node != &slist->tail);
}
return NULL;
}
skiplist_node* skiplist_find(skiplist_raw *slist,
skiplist_node *query)
{
return _sl_find(slist, query, EQ);
}
skiplist_node* skiplist_find_smaller_or_equal(skiplist_raw *slist,
skiplist_node *query)
{
return _sl_find(slist, query, SMEQ);
}
skiplist_node* skiplist_find_greater_or_equal(skiplist_raw *slist,
skiplist_node *query)
{
return _sl_find(slist, query, GTEQ);
}
int skiplist_erase_node_passive(skiplist_raw *slist,
skiplist_node *node)
{
__SLD_(
thread_local std::thread::id tid = std::this_thread::get_id();
thread_local size_t tid_hash = std::hash<std::thread::id>{}(tid) % 256;
(void)tid_hash;
)
int top_layer = node->top_layer;
bool bool_true = true, bool_false = false;
bool removed = false;
bool is_fully_linked = false;
ATM_LOAD(node->removed, removed);
if (removed) {
// already removed
return -1;
}
skiplist_node* prevs[SKIPLIST_MAX_LAYER];
skiplist_node* nexts[SKIPLIST_MAX_LAYER];
bool expected = false;
if (!ATM_CAS(node->being_modified, expected, bool_true)) {
// already being modified .. cannot work on this node for now.
__SLD_BM(node);
return -2;
}
// set removed flag first, so that reader cannot read this node.
ATM_STORE(node->removed, bool_true);
__SLD_P("%02x rmv %p begin\n", (int)tid_hash, node);
erase_node_retry:
ATM_LOAD(node->is_fully_linked, is_fully_linked);
if (!is_fully_linked) {
// already unlinked .. remove is done by other thread
ATM_STORE(node->removed, bool_false);
ATM_STORE(node->being_modified, bool_false);
return -3;
}
int cmp = 0;
bool found_node_to_erase = false;
(void)found_node_to_erase;
skiplist_node *cur_node = &slist->head;
ATM_FETCH_ADD(cur_node->ref_count, 1);
__SLD_(size_t nh = 0);
__SLD_(thread_local skiplist_node* history[1024]; (void)history);
int cur_layer = slist->top_layer;
for (; cur_layer >= 0; --cur_layer) {
do {
__SLD_( history[nh++] = cur_node );
bool node_found = false;
skiplist_node *next_node = _sl_next(slist, cur_node, cur_layer,
node, &node_found);
if (!next_node) {
_sl_clr_flags(prevs, cur_layer+1, top_layer);
ATM_FETCH_SUB(cur_node->ref_count, 1);
YIELD();
goto erase_node_retry;
}
// Note: unlike insert(), we should find exact position of `node`.
cmp = _sl_cmp(slist, node, next_node);
if (cmp > 0 || (cur_layer <= top_layer && !node_found) ) {
// cur_node <= next_node < node
// => move to next node
skiplist_node* temp = cur_node;
cur_node = next_node;
__SLD_( if (cmp > 0) {
int cmp2 = _sl_cmp(slist, cur_node, node);
if (cmp2 > 0) {
// node < cur_node <= next_node: not found.
_sl_clr_flags(prevs, cur_layer+1, top_layer);
ATM_FETCH_SUB(temp->ref_count, 1);
ATM_FETCH_SUB(next_node->ref_count, 1);
__SLD_ASSERT(0);
}
} )
ATM_FETCH_SUB(temp->ref_count, 1);
continue;
} else {
// otherwise: cur_node <= node <= next_node
ATM_FETCH_SUB(next_node->ref_count, 1);
}
if (cur_layer <= top_layer) {
prevs[cur_layer] = cur_node;
// note: 'next_node' and 'node' should not be the same,
// as 'removed' flag is already set.
__SLD_ASSERT(next_node != node);
nexts[cur_layer] = next_node;
// check if prev node duplicates with upper layer
int error_code = 0;
int locked_layer = cur_layer + 1;
if (cur_layer < top_layer &&
prevs[cur_layer] == prevs[cur_layer+1]) {
// duplicate with upper layer
// => which means that 'being_modified' flag is already true
// => do nothing.
} else {
expected = false;
if (ATM_CAS(prevs[cur_layer]->being_modified,
expected, bool_true)) {
locked_layer = cur_layer;
} else {
error_code = -1;
}
}
if (error_code == 0 &&
!_sl_valid_prev_next(prevs[cur_layer], nexts[cur_layer])) {
error_code = -2;
}
if (error_code != 0) {
__SLD_RT_RMV(error_code, node, top_layer, cur_layer);
_sl_clr_flags(prevs, locked_layer, top_layer);
ATM_FETCH_SUB(cur_node->ref_count, 1);
YIELD();
goto erase_node_retry;
}
skiplist_node* next_node_again =
_sl_next(slist, cur_node, cur_layer, node, NULL);
ATM_FETCH_SUB(next_node_again->ref_count, 1);
if (next_node_again != nexts[cur_layer]) {
// `next` pointer has been changed, retry.
__SLD_NC_RMV(cur_node, nexts[cur_layer], top_layer, cur_layer);
_sl_clr_flags(prevs, cur_layer, top_layer);
ATM_FETCH_SUB(cur_node->ref_count, 1);
YIELD();
goto erase_node_retry;
}
}
if (cur_layer == 0) found_node_to_erase = true;
// go down
break;
} while (cur_node != &slist->tail);
}
// Not exist in the skiplist, should not happen.
__SLD_ASSERT(found_node_to_erase);
// bottom layer => removal succeeded.
// mark this node unlinked
_sl_write_lock_an(node); {
ATM_STORE(node->is_fully_linked, bool_false);
} _sl_write_unlock_an(node);
// change prev nodes' next pointer from 0 ~ top_layer
for (cur_layer = 0; cur_layer <= top_layer; ++cur_layer) {
_sl_write_lock_an(prevs[cur_layer]);
skiplist_node* exp = node;
__SLD_ASSERT(exp != nexts[cur_layer]);
__SLD_ASSERT(nexts[cur_layer]->is_fully_linked);
if ( !ATM_CAS(prevs[cur_layer]->next[cur_layer],
exp, nexts[cur_layer]) ) {
__SLD_P("%02x ASSERT rmv %p[%d] -> %p (node %p)\n",
(int)tid_hash, prevs[cur_layer], cur_layer,
ATM_GET(prevs[cur_layer]->next[cur_layer]), node );
__SLD_ASSERT(0);
}
__SLD_ASSERT(nexts[cur_layer]->top_layer >= cur_layer);
__SLD_P("%02x rmv %p[%d] -> %p (node %p)\n",
(int)tid_hash, prevs[cur_layer], cur_layer,
nexts[cur_layer], node);
_sl_write_unlock_an(prevs[cur_layer]);
}
__SLD_P("%02x rmv %p done\n", (int)tid_hash, node);
ATM_FETCH_SUB(slist->num_entries, 1);
ATM_FETCH_SUB(slist->layer_entries[node->top_layer], 1);
for (int ii=slist->max_layer-1; ii>=0; --ii) {
if (slist->layer_entries[ii] > 0) {
slist->top_layer = ii;
break;
}
}
// modification is done for all layers
_sl_clr_flags(prevs, 0, top_layer);
ATM_FETCH_SUB(cur_node->ref_count, 1);
ATM_STORE(node->being_modified, bool_false);
return 0;
}
int skiplist_erase_node(skiplist_raw *slist,
skiplist_node *node)
{
int ret = 0;
do {
ret = skiplist_erase_node_passive(slist, node);
// if ret == -2, other thread is accessing the same node
// at the same time. try again.
} while (ret == -2);
return ret;
}
int skiplist_erase(skiplist_raw *slist,
skiplist_node *query)
{
skiplist_node *found = skiplist_find(slist, query);
if (!found) {
// key not found
return -4;
}
int ret = 0;
do {
ret = skiplist_erase_node_passive(slist, found);
// if ret == -2, other thread is accessing the same node
// at the same time. try again.
} while (ret == -2);
ATM_FETCH_SUB(found->ref_count, 1);
return ret;
}
int skiplist_is_valid_node(skiplist_node* node) {
return _sl_valid_node(node);
}
int skiplist_is_safe_to_free(skiplist_node* node) {
if (node->accessing_next) return 0;
if (node->being_modified) return 0;
if (!node->removed) return 0;
uint16_t ref_count = 0;
ATM_LOAD(node->ref_count, ref_count);
if (ref_count) return 0;
return 1;
}
void skiplist_wait_for_free(skiplist_node* node) {
while (!skiplist_is_safe_to_free(node)) {
YIELD();
}
}
void skiplist_grab_node(skiplist_node* node) {
ATM_FETCH_ADD(node->ref_count, 1);
}
void skiplist_release_node(skiplist_node* node) {
__SLD_ASSERT(node->ref_count);
ATM_FETCH_SUB(node->ref_count, 1);
}
skiplist_node* skiplist_next(skiplist_raw *slist,
skiplist_node *node) {
// << Issue >>
// If `node` is already removed and its next node is also removed
// and then released, the link update will not be applied to `node`
// as it is already unrechable from skiplist. `node` still points to
// the released node so that `_sl_next(node)` may return corrupted
// memory region.
//
// 0) initial:
// A -> B -> C -> D
//
// 1) B is `node`, which is removed but not yet released:
// B --+-> C -> D
// |
// A --+
//
// 2) remove C, and then release:
// B -> !C! +-> D
// |
// A --------+
//
// 3) skiplist_next(B):
// will fetch C, which is already released so that
// may contain garbage data.
//
// In this case, start over from the top layer,
// to find valid link (same as in prev()).
skiplist_node *next = _sl_next(slist, node, 0, NULL, NULL);
if (!next) next = _sl_find(slist, node, GT);
if (next == &slist->tail) return NULL;
return next;
}
skiplist_node* skiplist_prev(skiplist_raw *slist,
skiplist_node *node) {
skiplist_node *prev = _sl_find(slist, node, SM);
if (prev == &slist->head) return NULL;
return prev;
}
skiplist_node* skiplist_begin(skiplist_raw *slist) {
skiplist_node *next = NULL;
while (!next) {