2 * Copyright 2011-2015 Samy Al Bahra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * The implementation here is inspired from the work described in:
29 * Fraser, K. 2004. Practical Lock-Freedom. PhD Thesis, University
30 * of Cambridge Computing Laboratory.
33 #include <ck_backoff.h>
38 #include <ck_stdbool.h>
39 #include <ck_string.h>
42 * Only three distinct values are used for reclamation, but reclamation occurs
43 * at e+2 rather than e+1. Any thread in a "critical section" would have
44 * acquired some snapshot (e) of the global epoch value (e_g) and set an active
45 * flag. Any hazardous references will only occur after a full memory barrier.
46 * For example, assume an initial e_g value of 1, e value of 0 and active value
54 * Any serialized reads may observe e = 0 or e = 1 with active = 0, or e = 0 or
55 * e = 1 with active = 1. The e_g value can only go from 1 to 2 if every thread
56 * has already observed the value of "1" (or the value we are incrementing
57 * from). This guarantees us that for any given value e_g, any threads with-in
58 * critical sections (referred to as "active" threads from here on) would have
59 * an e value of e_g-1 or e_g. This also means that hazardous references may be
60 * shared in both e_g-1 and e_g even if they are logically deleted in e_g.
62 * For example, assume all threads have an e value of e_g. Another thread may
63 * increment to e_g to e_g+1. Older threads may have a reference to an object
64 * which is only deleted in e_g+1. It could be that reader threads are
65 * executing some hash table look-ups, while some other writer thread (which
66 * causes epoch counter tick) actually deletes the same items that reader
67 * threads are looking up (this writer thread having an e value of e_g+1).
68 * This is possible if the writer thread re-observes the epoch after the
71 * Psuedo-code for writer:
79 * Psuedo-code for reader:
82 * ck_pr_inc(&x->value);
85 * Of course, it is also possible for references logically deleted at e_g-1 to
86 * still be accessed at e_g as threads are "active" at the same time
87 * (real-world time) mutating shared objects.
89 * Now, if the epoch counter is ticked to e_g+1, then no new hazardous
90 * references could exist to objects logically deleted at e_g-1. The reason for
91 * this is that at e_g+1, all epoch read-side critical sections started at
92 * e_g-1 must have been completed. If any epoch read-side critical sections at
93 * e_g-1 were still active, then we would never increment to e_g+1 (active != 0
94 * ^ e != e_g). Additionally, e_g may still have hazardous references to
95 * objects logically deleted at e_g-1 which means objects logically deleted at
96 * e_g-1 cannot be deleted at e_g+1 unless all threads have observed e_g+1
97 * (since it is valid for active threads to be at e_g and threads at e_g still
98 * require safe memory accesses).
100 * However, at e_g+2, all active threads must be either at e_g+1 or e_g+2.
101 * Though e_g+2 may share hazardous references with e_g+1, and e_g+1 shares
102 * hazardous references to e_g, no active threads are at e_g or e_g-1. This
103 * means no hazardous references could exist to objects deleted at e_g-1 (at
106 * To summarize these important points,
107 * 1) Active threads will always have a value of e_g or e_g-1.
108 * 2) Items that are logically deleted e_g or e_g-1 cannot be physically
110 * 3) Objects logically deleted at e_g-1 can be physically destroyed at e_g+2
111 * or at e_g+1 if no threads are at e_g.
113 * Last but not least, if we are at e_g+2, then no active thread is at e_g
114 * which means it is safe to apply modulo-3 arithmetic to e_g value in order to
115 * re-use e_g to represent the e_g+3 state. This means it is sufficient to
116 * represent e_g using only the values 0, 1 or 2. Every time a thread re-visits
117 * a e_g (which can be determined with a non-empty deferral list) it can assume
118 * objects in the e_g deferral list involved at least three e_g transitions and
119 * are thus, safe, for physical deletion.
121 * Blocking semantics for epoch reclamation have additional restrictions.
122 * Though we only require three deferral lists, reasonable blocking semantics
123 * must be able to more gracefully handle bursty write work-loads which could
124 * easily cause e_g wrap-around if modulo-3 arithmetic is used. This allows for
125 * easy-to-trigger live-lock situations. The work-around to this is to not
126 * apply modulo arithmetic to e_g but only to deferral list indexing.
128 #define CK_EPOCH_GRACE 3U
131 * CK_EPOCH_LENGTH must be a power-of-2 (because (CK_EPOCH_LENGTH - 1) is used
132 * as a mask, and it must be at least 3 (see comments above).
134 #if (CK_EPOCH_LENGTH < 3 || (CK_EPOCH_LENGTH & (CK_EPOCH_LENGTH - 1)) != 0)
135 #error "CK_EPOCH_LENGTH must be a power of 2 and >= 3"
139 CK_EPOCH_STATE_USED = 0,
140 CK_EPOCH_STATE_FREE = 1
143 CK_STACK_CONTAINER(struct ck_epoch_record, record_next,
144 ck_epoch_record_container)
145 CK_STACK_CONTAINER(struct ck_epoch_entry, stack_entry,
146 ck_epoch_entry_container)
148 #define CK_EPOCH_SENSE_MASK (CK_EPOCH_SENSE - 1)
151 _ck_epoch_delref(struct ck_epoch_record *record,
152 struct ck_epoch_section *section)
154 struct ck_epoch_ref *current, *other;
155 unsigned int i = section->bucket;
157 current = &record->local.bucket[i];
160 if (current->count > 0)
164 * If the current bucket no longer has any references, then
165 * determine whether we have already transitioned into a newer
166 * epoch. If so, then make sure to update our shared snapshot
167 * to allow for forward progress.
169 * If no other active bucket exists, then the record will go
170 * inactive in order to allow for forward progress.
172 other = &record->local.bucket[(i + 1) & CK_EPOCH_SENSE_MASK];
173 if (other->count > 0 &&
174 ((int)(current->epoch - other->epoch) < 0)) {
176 * The other epoch value is actually the newest,
179 ck_pr_store_uint(&record->epoch, other->epoch);
186 _ck_epoch_addref(struct ck_epoch_record *record,
187 struct ck_epoch_section *section)
189 struct ck_epoch *global = record->global;
190 struct ck_epoch_ref *ref;
191 unsigned int epoch, i;
193 epoch = ck_pr_load_uint(&global->epoch);
194 i = epoch & CK_EPOCH_SENSE_MASK;
195 ref = &record->local.bucket[i];
197 if (ref->count++ == 0) {
199 struct ck_epoch_ref *previous;
202 * The system has already ticked. If another non-zero bucket
203 * exists, make sure to order our observations with respect
204 * to it. Otherwise, it is possible to acquire a reference
205 * from the previous epoch generation.
207 * On TSO architectures, the monoticity of the global counter
208 * and load-{store, load} ordering are sufficient to guarantee
211 previous = &record->local.bucket[(i + 1) &
212 CK_EPOCH_SENSE_MASK];
213 if (previous->count > 0)
214 ck_pr_fence_acqrel();
215 #endif /* !CK_MD_TSO */
218 * If this is this is a new reference into the current
219 * bucket then cache the associated epoch value.
229 ck_epoch_init(struct ck_epoch *global)
232 ck_stack_init(&global->records);
239 struct ck_epoch_record *
240 ck_epoch_recycle(struct ck_epoch *global, void *ct)
242 struct ck_epoch_record *record;
243 ck_stack_entry_t *cursor;
246 if (ck_pr_load_uint(&global->n_free) == 0)
249 CK_STACK_FOREACH(&global->records, cursor) {
250 record = ck_epoch_record_container(cursor);
252 if (ck_pr_load_uint(&record->state) == CK_EPOCH_STATE_FREE) {
253 /* Serialize with respect to deferral list clean-up. */
255 state = ck_pr_fas_uint(&record->state,
256 CK_EPOCH_STATE_USED);
257 if (state == CK_EPOCH_STATE_FREE) {
258 ck_pr_dec_uint(&global->n_free);
259 ck_pr_store_ptr(&record->ct, ct);
262 * The context pointer is ordered by a
263 * subsequent protected section.
274 ck_epoch_register(struct ck_epoch *global, struct ck_epoch_record *record,
279 record->global = global;
280 record->state = CK_EPOCH_STATE_USED;
283 record->n_dispatch = 0;
285 record->n_pending = 0;
287 memset(&record->local, 0, sizeof record->local);
289 for (i = 0; i < CK_EPOCH_LENGTH; i++)
290 ck_stack_init(&record->pending[i]);
293 ck_stack_push_upmc(&global->records, &record->record_next);
298 ck_epoch_unregister(struct ck_epoch_record *record)
300 struct ck_epoch *global = record->global;
305 record->n_dispatch = 0;
307 record->n_pending = 0;
308 memset(&record->local, 0, sizeof record->local);
310 for (i = 0; i < CK_EPOCH_LENGTH; i++)
311 ck_stack_init(&record->pending[i]);
313 ck_pr_store_ptr(&record->ct, NULL);
315 ck_pr_store_uint(&record->state, CK_EPOCH_STATE_FREE);
316 ck_pr_inc_uint(&global->n_free);
320 static struct ck_epoch_record *
321 ck_epoch_scan(struct ck_epoch *global,
322 struct ck_epoch_record *cr,
326 ck_stack_entry_t *cursor;
329 cursor = CK_STACK_FIRST(&global->records);
332 cursor = &cr->record_next;
336 while (cursor != NULL) {
337 unsigned int state, active;
339 cr = ck_epoch_record_container(cursor);
341 state = ck_pr_load_uint(&cr->state);
342 if (state & CK_EPOCH_STATE_FREE) {
343 cursor = CK_STACK_NEXT(cursor);
347 active = ck_pr_load_uint(&cr->active);
350 if (active != 0 && ck_pr_load_uint(&cr->epoch) != epoch)
353 cursor = CK_STACK_NEXT(cursor);
360 ck_epoch_dispatch(struct ck_epoch_record *record, unsigned int e, ck_stack_t *deferred)
362 unsigned int epoch = e & (CK_EPOCH_LENGTH - 1);
363 ck_stack_entry_t *head, *next, *cursor;
364 unsigned int n_pending, n_peak;
367 head = ck_stack_batch_pop_upmc(&record->pending[epoch]);
368 for (cursor = head; cursor != NULL; cursor = next) {
369 struct ck_epoch_entry *entry =
370 ck_epoch_entry_container(cursor);
372 next = CK_STACK_NEXT(cursor);
373 if (deferred != NULL)
374 ck_stack_push_spnc(deferred, &entry->stack_entry);
376 entry->function(entry);
381 n_peak = ck_pr_load_uint(&record->n_peak);
382 n_pending = ck_pr_load_uint(&record->n_pending);
384 /* We don't require accuracy around peak calculation. */
385 if (n_pending > n_peak)
386 ck_pr_store_uint(&record->n_peak, n_peak);
389 ck_pr_add_uint(&record->n_dispatch, i);
390 ck_pr_sub_uint(&record->n_pending, i);
397 * Reclaim all objects associated with a record.
400 ck_epoch_reclaim(struct ck_epoch_record *record)
404 for (epoch = 0; epoch < CK_EPOCH_LENGTH; epoch++)
405 ck_epoch_dispatch(record, epoch, NULL);
410 CK_CC_FORCE_INLINE static void
411 epoch_block(struct ck_epoch *global, struct ck_epoch_record *cr,
412 ck_epoch_wait_cb_t *cb, void *ct)
422 * This function must not be called with-in read section.
425 ck_epoch_synchronize_wait(struct ck_epoch *global,
426 ck_epoch_wait_cb_t *cb, void *ct)
428 struct ck_epoch_record *cr;
429 unsigned int delta, epoch, goal, i;
432 ck_pr_fence_memory();
435 * The observation of the global epoch must be ordered with respect to
436 * all prior operations. The re-ordering of loads is permitted given
437 * monoticity of global epoch counter.
439 * If UINT_MAX concurrent mutations were to occur then it is possible
440 * to encounter an ABA-issue. If this is a concern, consider tuning
441 * write-side concurrency.
443 delta = epoch = ck_pr_load_uint(&global->epoch);
444 goal = epoch + CK_EPOCH_GRACE;
446 for (i = 0, cr = NULL; i < CK_EPOCH_GRACE - 1; cr = NULL, i++) {
450 * Determine whether all threads have observed the current
451 * epoch with respect to the updates on invocation.
453 while (cr = ck_epoch_scan(global, cr, delta, &active),
460 * Another writer may have already observed a grace
463 e_d = ck_pr_load_uint(&global->epoch);
465 epoch_block(global, cr, cb, ct);
470 * If the epoch has been updated, we may have already
474 if ((goal > epoch) & (delta >= goal))
477 epoch_block(global, cr, cb, ct);
480 * If the epoch has been updated, then a grace period
481 * requires that all threads are observed idle at the
488 * If we have observed all threads as inactive, then we assume
489 * we are at a grace period.
495 * Increment current epoch. CAS semantics are used to eliminate
496 * increment operations for synchronization that occurs for the
497 * same global epoch value snapshot.
499 * If we can guarantee there will only be one active barrier or
500 * epoch tick at a given time, then it is sufficient to use an
501 * increment operation. In a multi-barrier workload, however,
502 * it is possible to overflow the epoch value if we apply
503 * modulo-3 arithmetic.
505 r = ck_pr_cas_uint_value(&global->epoch, delta, delta + 1,
508 /* Order subsequent thread active checks. */
509 ck_pr_fence_atomic_load();
512 * If CAS has succeeded, then set delta to latest snapshot.
513 * Otherwise, we have just acquired latest snapshot.
519 * A majority of use-cases will not require full barrier semantics.
520 * However, if non-temporal instructions are used, full barrier
521 * semantics are necessary.
524 ck_pr_fence_memory();
529 ck_epoch_synchronize(struct ck_epoch_record *record)
532 ck_epoch_synchronize_wait(record->global, NULL, NULL);
537 ck_epoch_barrier(struct ck_epoch_record *record)
540 ck_epoch_synchronize(record);
541 ck_epoch_reclaim(record);
546 ck_epoch_barrier_wait(struct ck_epoch_record *record, ck_epoch_wait_cb_t *cb,
550 ck_epoch_synchronize_wait(record->global, cb, ct);
551 ck_epoch_reclaim(record);
556 * It may be worth it to actually apply these deferral semantics to an epoch
557 * that was observed at ck_epoch_call time. The problem is that the latter
558 * would require a full fence.
560 * ck_epoch_call will dispatch to the latest epoch snapshot that was observed.
561 * There are cases where it will fail to reclaim as early as it could. If this
562 * becomes a problem, we could actually use a heap for epoch buckets but that
563 * is far from ideal too.
566 ck_epoch_poll_deferred(struct ck_epoch_record *record, ck_stack_t *deferred)
570 struct ck_epoch_record *cr = NULL;
571 struct ck_epoch *global = record->global;
572 unsigned int n_dispatch;
574 epoch = ck_pr_load_uint(&global->epoch);
576 /* Serialize epoch snapshots with respect to global epoch. */
577 ck_pr_fence_memory();
580 * At this point, epoch is the current global epoch value.
581 * There may or may not be active threads which observed epoch - 1.
582 * (ck_epoch_scan() will tell us that). However, there should be
583 * no active threads which observed epoch - 2.
585 * Note that checking epoch - 2 is necessary, as race conditions can
586 * allow another thread to increment the global epoch before this
589 n_dispatch = ck_epoch_dispatch(record, epoch - 2, deferred);
591 cr = ck_epoch_scan(global, cr, epoch, &active);
593 return (n_dispatch > 0);
595 /* We are at a grace period if all threads are inactive. */
596 if (active == false) {
597 record->epoch = epoch;
598 for (epoch = 0; epoch < CK_EPOCH_LENGTH; epoch++)
599 ck_epoch_dispatch(record, epoch, deferred);
605 * If an active thread exists, rely on epoch observation.
607 * All the active threads entered the epoch section during
608 * the current epoch. Therefore, we can now run the handlers
609 * for the immediately preceding epoch and attempt to
610 * advance the epoch if it hasn't been already.
612 (void)ck_pr_cas_uint(&global->epoch, epoch, epoch + 1);
614 ck_epoch_dispatch(record, epoch - 1, deferred);
619 ck_epoch_poll(struct ck_epoch_record *record)
622 return ck_epoch_poll_deferred(record, NULL);