14
14
#include <string.h>
15
15
#include <threads.h>
16
16
17
+ static atomic_uint_fast64_t deletes = 0 , inserts = 0 ;
18
+
19
+ #ifdef RUNTIME_STAT
20
+ /*
21
+ * "rtry" the number of retries in the __list_find function.
22
+ * "cons" the number of wait-free contains in the __list_find function that curr
23
+ * pointer pointed.
24
+ * "trav" the number of list element traversal in the __list_find function.
25
+ * "fail" the number of CAS() failures.
26
+ * "del" the number of list_delete operation failed and restart again.
27
+ * "ins" the number of list_insert operation failed and restart again.
28
+ * "load" is the number of atomic_load operation in list_delete, list_insert
29
+ * and __list_find.
30
+ * "store" is the number of atomic_store operation in list_delete, list_insert
31
+ * and __list_find.
32
+ */
33
+ struct runtime_stat {
34
+ atomic_uint_fast64_t rtry , cons , trav , fail ;
35
+ atomic_uint_fast64_t del , ins ;
36
+ atomic_uint_fast64_t load , store ;
37
+ };
38
+ static struct runtime_stat stats = {0 };
39
+
40
+ enum {
41
+ TRACE_nothing = 0 ,
42
+ TRACE_rtry ,
43
+ TRACE_cons ,
44
+ TRACE_trav ,
45
+ TRACE_del ,
46
+ TRACE_ins ,
47
+ TRACE_inserts ,
48
+ TRACE_deletes
49
+ };
50
+
51
+ #define CAS (obj , expected , desired ) \
52
+ ({ \
53
+ bool __ret = atomic_compare_exchange_strong(obj, expected, desired); \
54
+ if (!__ret) \
55
+ atomic_fetch_add(&stats.fail, 1); \
56
+ __ret; \
57
+ })
58
+ #define ATOMIC_LOAD (obj ) \
59
+ ({ \
60
+ atomic_fetch_add(&stats.load, 1); \
61
+ atomic_load(obj); \
62
+ })
63
+ #define ATOMIC_STORE_EXPLICIT (obj , desired , order ) \
64
+ do { \
65
+ atomic_fetch_add(&stats.store, 1); \
66
+ atomic_store_explicit(obj, desired, order); \
67
+ } while (0)
68
+ #define TRACE (ops ) \
69
+ ({ \
70
+ if (TRACE_##ops) \
71
+ atomic_fetch_add(&stats.ops, 1); \
72
+ })
73
+
74
+ static void do_analysis (void )
75
+ {
76
+ printf (
77
+ "\"rtry\" is the number of retries in the __list_find function.\n" );
78
+ printf (
79
+ "\"cons\" is the number of wait-free contains in the __list_find "
80
+ "function that curr pointer pointed.\n" );
81
+ printf (
82
+ "\"trav\" is the number of list element traversal in the "
83
+ "__list_find function.\n" );
84
+ printf ("\"fail\" is the number of CAS() failures.\n" );
85
+ printf (
86
+ "\"del\" is the number of list_delete operation failed and "
87
+ "restart again.\n" );
88
+ printf (
89
+ "\"ins\" is the number of list_insert operation failed and "
90
+ "restart again.\n" );
91
+ printf ("\"deletes\" is the number of linked list elements deleted.\n" );
92
+ printf ("\"inserts\" is the number of linked list elements created.\n" );
93
+ printf (
94
+ "\"load\" is the number of atomic_load operation in list_delete, "
95
+ "list_insert and __list_find.\n" );
96
+ printf (
97
+ "\"store\" is the number of atomic_store operation in list_delete, "
98
+ "list_insert and __list_find.\n" );
99
+ printf ("\n%10s %10s %10s %10s %10s %10s %10s %10s %10s %10s\n" , "rtry" ,
100
+ "cons" , "trav" , "fail" , "del" , "ins" , "load" , "store" , "deletes" ,
101
+ "inserts" );
102
+ for (int i = 0 ; i < 109 ; i ++ )
103
+ printf ("-" );
104
+ printf ("\n%10ld %10ld %10ld %10ld %10ld %10ld %10ld %10ld %10ld %10ld\n" ,
105
+ stats .rtry , stats .cons , stats .trav , stats .fail , stats .del , stats .ins ,
106
+ stats .load , stats .store , deletes , inserts );
107
+ }
108
+
109
+ #else
110
+
111
+ #define CAS (obj , expected , desired ) \
112
+ ({ atomic_compare_exchange_strong(obj, expected, desired); })
113
+ #define ATOMIC_LOAD (obj ) ({ atomic_load(obj); })
114
+ #define ATOMIC_STORE_EXPLICIT (obj , desired , order ) \
115
+ do { \
116
+ atomic_store_explicit(obj, desired, order); \
117
+ } while (0)
118
+ #define TRACE (ops ) ({})
119
+
120
+ static void do_analysis (void )
121
+ {
122
+ fprintf (stderr , "inserts = %zu, deletes = %zu\n" , atomic_load (& inserts ),
123
+ atomic_load (& deletes ));
124
+ }
125
+
126
+ #endif /* RUNTIME_STAT */
127
+
128
+ #define RUNTIME_STAT_INIT () atexit(do_analysis)
129
+
17
130
#define HP_MAX_THREADS 128
18
131
#define HP_MAX_HPS 5 /* This is named 'K' in the HP paper */
19
132
#define CLPAD (128 / sizeof(uintptr_t))
@@ -162,8 +275,6 @@ void list_hp_retire(list_hp_t *hp, uintptr_t ptr)
162
275
#define N_THREADS (128 / 2)
163
276
#define MAX_THREADS 128
164
277
165
- static atomic_uint_fast32_t deletes = 0 , inserts = 0 ;
166
-
167
278
enum { HP_NEXT = 0 , HP_CURR = 1 , HP_PREV };
168
279
169
280
#define is_marked (p ) (bool) ((uintptr_t)(p) &0x01)
@@ -225,21 +336,29 @@ static bool __list_find(list_t *list,
225
336
226
337
try_again :
227
338
prev = & list -> head ;
228
- curr = (list_node_t * ) atomic_load (prev );
339
+ curr = (list_node_t * ) ATOMIC_LOAD (prev );
229
340
(void ) list_hp_protect_ptr (list -> hp , HP_CURR , (uintptr_t ) curr );
230
- if (atomic_load (prev ) != get_unmarked (curr ))
341
+ if (ATOMIC_LOAD (prev ) != get_unmarked (curr )) {
342
+ TRACE (rtry );
231
343
goto try_again ;
344
+ }
232
345
while (true) {
233
- next = (list_node_t * ) atomic_load (& get_unmarked_node (curr )-> next );
346
+ if (is_marked (curr ))
347
+ TRACE (cons );
348
+ next = (list_node_t * ) ATOMIC_LOAD (& get_unmarked_node (curr )-> next );
234
349
(void ) list_hp_protect_ptr (list -> hp , HP_NEXT , get_unmarked (next ));
235
350
/* On a CAS failure, the search function, "__list_find," will simply
236
351
* have to go backwards in the list until an unmarked element is found
237
352
* from which the search in increasing key order can be started.
238
353
*/
239
- if (atomic_load (& get_unmarked_node (curr )-> next ) != (uintptr_t ) next )
354
+ if (ATOMIC_LOAD (& get_unmarked_node (curr )-> next ) != (uintptr_t ) next ) {
355
+ TRACE (rtry );
240
356
goto try_again ;
241
- if (atomic_load (prev ) != get_unmarked (curr ))
357
+ }
358
+ if (ATOMIC_LOAD (prev ) != get_unmarked (curr )) {
359
+ TRACE (rtry );
242
360
goto try_again ;
361
+ }
243
362
if (get_unmarked_node (next ) == next ) {
244
363
if (!(get_unmarked_node (curr )-> key < * key )) {
245
364
* par_curr = curr ;
@@ -252,12 +371,15 @@ static bool __list_find(list_t *list,
252
371
get_unmarked (curr ));
253
372
} else {
254
373
uintptr_t tmp = get_unmarked (curr );
255
- if (!atomic_compare_exchange_strong (prev , & tmp , get_unmarked (next )))
374
+ if (!CAS (prev , & tmp , get_unmarked (next ))) {
375
+ TRACE (rtry );
256
376
goto try_again ;
377
+ }
257
378
list_hp_retire (list -> hp , get_unmarked (curr ));
258
379
}
259
380
curr = next ;
260
381
(void ) list_hp_protect_release (list -> hp , HP_CURR , get_unmarked (next ));
382
+ TRACE (trav );
261
383
}
262
384
}
263
385
@@ -274,13 +396,14 @@ bool list_insert(list_t *list, list_key_t key)
274
396
list_hp_clear (list -> hp );
275
397
return false;
276
398
}
277
- atomic_store_explicit (& node -> next , (uintptr_t ) curr ,
399
+ ATOMIC_STORE_EXPLICIT (& node -> next , (uintptr_t ) curr ,
278
400
memory_order_relaxed );
279
401
uintptr_t tmp = get_unmarked (curr );
280
- if (atomic_compare_exchange_strong (prev , & tmp , (uintptr_t ) node )) {
402
+ if (CAS (prev , & tmp , (uintptr_t ) node )) {
281
403
list_hp_clear (list -> hp );
282
404
return true;
283
405
}
406
+ TRACE (ins );
284
407
}
285
408
}
286
409
@@ -296,12 +419,13 @@ bool list_delete(list_t *list, list_key_t key)
296
419
297
420
uintptr_t tmp = get_unmarked (next );
298
421
299
- if (!atomic_compare_exchange_strong (& curr -> next , & tmp ,
300
- get_marked ( next )))
422
+ if (!CAS (& curr -> next , & tmp , get_marked ( next ))) {
423
+ TRACE ( del );
301
424
continue ;
425
+ }
302
426
303
427
tmp = get_unmarked (curr );
304
- if (atomic_compare_exchange_strong (prev , & tmp , get_unmarked (next ))) {
428
+ if (CAS (prev , & tmp , get_unmarked (next ))) {
305
429
list_hp_clear (list -> hp );
306
430
list_hp_retire (list -> hp , get_unmarked (curr ));
307
431
} else {
@@ -364,6 +488,7 @@ static void *delete_thread(void *arg)
364
488
365
489
int main (void )
366
490
{
491
+ RUNTIME_STAT_INIT ();
367
492
list_t * list = list_new ();
368
493
369
494
pthread_t thr [N_THREADS ];
@@ -382,8 +507,5 @@ int main(void)
382
507
383
508
list_destroy (list );
384
509
385
- fprintf (stderr , "inserts = %zu, deletes = %zu\n" , atomic_load (& inserts ),
386
- atomic_load (& deletes ));
387
-
388
510
return 0 ;
389
511
}
0 commit comments