concurrencykit / ck

Concurrency primitives, safe memory reclamation mechanisms and non-blocking (including lock-free) data structures designed to aid in the research, design and implementation of high performance concurrent systems developed in C99+.
http://concurrencykit.org/
Other
2.38k stars 313 forks source link

ck_hs_apply question #168

Closed zackpete closed 3 years ago

zackpete commented 3 years ago

Is ck_hs_apply safe to use in the presence of another concurrent writer? Specifically, when an item is not being replaced or removed i.e. ck_hs_apply_fn_t returns the same pointer that was passed to it.

The reason I ask is I'm trying to figure out how to do reference counting on the items in the hash set, and need to avoid a race where an item is freed between ck_hs_get and a reference count increment:

================================================================================
Thread 0 (reader)                    | Thread 1 (writer)
================================================================================
ck_hs_get(&hs, hash, item)           |
                                     | item_t i = ck_hs_remove(&hs, hash, item);
                                     | decrement_refcount(item);
                                     | if (refcount(item) == 0) {
                                     |     free(item);
                                     | }
increment_refcount(item);

Will inc be applied "atomically" before ck_hs_remove?

================================================================================
Thread 0 (reader)                    | Thread 1 (writer)
================================================================================
ck_hs_apply(&hs,hash,item,inc,...);  |
                                     | item_t i = ck_hs_remove(&hs, hash, item);
                                     | decrement_refcount(item);
                                     | if (refcount(item) == 0) {
                                     |     free(item);
                                     | }
zackpete commented 3 years ago

Realized I'm thinking about this the wrong way, it seems I will have to use ck_epoch_* instead of doing reference counting.