11fb62fb0SOlivier Houchard /*
21fb62fb0SOlivier Houchard * Copyright 2011-2015 Samy Al Bahra.
31fb62fb0SOlivier Houchard * All rights reserved.
41fb62fb0SOlivier Houchard *
51fb62fb0SOlivier Houchard * Redistribution and use in source and binary forms, with or without
61fb62fb0SOlivier Houchard * modification, are permitted provided that the following conditions
71fb62fb0SOlivier Houchard * are met:
81fb62fb0SOlivier Houchard * 1. Redistributions of source code must retain the above copyright
91fb62fb0SOlivier Houchard * notice, this list of conditions and the following disclaimer.
101fb62fb0SOlivier Houchard * 2. Redistributions in binary form must reproduce the above copyright
111fb62fb0SOlivier Houchard * notice, this list of conditions and the following disclaimer in the
121fb62fb0SOlivier Houchard * documentation and/or other materials provided with the distribution.
131fb62fb0SOlivier Houchard *
141fb62fb0SOlivier Houchard * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151fb62fb0SOlivier Houchard * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161fb62fb0SOlivier Houchard * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171fb62fb0SOlivier Houchard * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181fb62fb0SOlivier Houchard * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191fb62fb0SOlivier Houchard * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201fb62fb0SOlivier Houchard * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211fb62fb0SOlivier Houchard * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221fb62fb0SOlivier Houchard * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231fb62fb0SOlivier Houchard * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241fb62fb0SOlivier Houchard * SUCH DAMAGE.
251fb62fb0SOlivier Houchard */
261fb62fb0SOlivier Houchard
271fb62fb0SOlivier Houchard #ifndef CK_EPOCH_H
281fb62fb0SOlivier Houchard #define CK_EPOCH_H
291fb62fb0SOlivier Houchard
301fb62fb0SOlivier Houchard /*
311fb62fb0SOlivier Houchard * The implementation here is inspired from the work described in:
321fb62fb0SOlivier Houchard * Fraser, K. 2004. Practical Lock-Freedom. PhD Thesis, University
331fb62fb0SOlivier Houchard * of Cambridge Computing Laboratory.
341fb62fb0SOlivier Houchard */
351fb62fb0SOlivier Houchard
361fb62fb0SOlivier Houchard #include <ck_cc.h>
371fb62fb0SOlivier Houchard #include <ck_md.h>
381fb62fb0SOlivier Houchard #include <ck_pr.h>
391fb62fb0SOlivier Houchard #include <ck_stack.h>
401fb62fb0SOlivier Houchard #include <ck_stdbool.h>
411fb62fb0SOlivier Houchard
421fb62fb0SOlivier Houchard #ifndef CK_EPOCH_LENGTH
431fb62fb0SOlivier Houchard #define CK_EPOCH_LENGTH 4
441fb62fb0SOlivier Houchard #endif
451fb62fb0SOlivier Houchard
461fb62fb0SOlivier Houchard /*
471fb62fb0SOlivier Houchard * This is used for sense detection with-respect to concurrent
481fb62fb0SOlivier Houchard * epoch sections.
491fb62fb0SOlivier Houchard */
501fb62fb0SOlivier Houchard #define CK_EPOCH_SENSE (2)
511fb62fb0SOlivier Houchard
521fb62fb0SOlivier Houchard struct ck_epoch_entry;
531fb62fb0SOlivier Houchard typedef struct ck_epoch_entry ck_epoch_entry_t;
541fb62fb0SOlivier Houchard typedef void ck_epoch_cb_t(ck_epoch_entry_t *);
551fb62fb0SOlivier Houchard
561fb62fb0SOlivier Houchard /*
571fb62fb0SOlivier Houchard * This should be embedded into objects you wish to be the target of
581fb62fb0SOlivier Houchard * ck_epoch_cb_t functions (with ck_epoch_call).
591fb62fb0SOlivier Houchard */
601fb62fb0SOlivier Houchard struct ck_epoch_entry {
611fb62fb0SOlivier Houchard ck_epoch_cb_t *function;
621fb62fb0SOlivier Houchard ck_stack_entry_t stack_entry;
631fb62fb0SOlivier Houchard };
641fb62fb0SOlivier Houchard
651fb62fb0SOlivier Houchard /*
661fb62fb0SOlivier Houchard * A section object may be passed to every begin-end pair to allow for
671fb62fb0SOlivier Houchard * forward progress guarantees with-in prolonged active sections.
681fb62fb0SOlivier Houchard */
691fb62fb0SOlivier Houchard struct ck_epoch_section {
701fb62fb0SOlivier Houchard unsigned int bucket;
711fb62fb0SOlivier Houchard };
721fb62fb0SOlivier Houchard typedef struct ck_epoch_section ck_epoch_section_t;
731fb62fb0SOlivier Houchard
741fb62fb0SOlivier Houchard /*
751fb62fb0SOlivier Houchard * Return pointer to ck_epoch_entry container object.
761fb62fb0SOlivier Houchard */
771fb62fb0SOlivier Houchard #define CK_EPOCH_CONTAINER(T, M, N) \
781fb62fb0SOlivier Houchard CK_CC_CONTAINER(struct ck_epoch_entry, T, M, N)
791fb62fb0SOlivier Houchard
801fb62fb0SOlivier Houchard struct ck_epoch_ref {
811fb62fb0SOlivier Houchard unsigned int epoch;
821fb62fb0SOlivier Houchard unsigned int count;
831fb62fb0SOlivier Houchard };
841fb62fb0SOlivier Houchard
851fb62fb0SOlivier Houchard struct ck_epoch_record {
867e8cd4e1SOlivier Houchard ck_stack_entry_t record_next;
871fb62fb0SOlivier Houchard struct ck_epoch *global;
881fb62fb0SOlivier Houchard unsigned int state;
891fb62fb0SOlivier Houchard unsigned int epoch;
901fb62fb0SOlivier Houchard unsigned int active;
911fb62fb0SOlivier Houchard struct {
921fb62fb0SOlivier Houchard struct ck_epoch_ref bucket[CK_EPOCH_SENSE];
931fb62fb0SOlivier Houchard } local CK_CC_CACHELINE;
941fb62fb0SOlivier Houchard unsigned int n_pending;
951fb62fb0SOlivier Houchard unsigned int n_peak;
967e8cd4e1SOlivier Houchard unsigned int n_dispatch;
977e8cd4e1SOlivier Houchard void *ct;
981fb62fb0SOlivier Houchard ck_stack_t pending[CK_EPOCH_LENGTH];
991fb62fb0SOlivier Houchard } CK_CC_CACHELINE;
1001fb62fb0SOlivier Houchard typedef struct ck_epoch_record ck_epoch_record_t;
1011fb62fb0SOlivier Houchard
1021fb62fb0SOlivier Houchard struct ck_epoch {
1031fb62fb0SOlivier Houchard unsigned int epoch;
1041fb62fb0SOlivier Houchard unsigned int n_free;
1057e8cd4e1SOlivier Houchard ck_stack_t records;
1061fb62fb0SOlivier Houchard };
1071fb62fb0SOlivier Houchard typedef struct ck_epoch ck_epoch_t;
1081fb62fb0SOlivier Houchard
1091fb62fb0SOlivier Houchard /*
1101fb62fb0SOlivier Houchard * Internal functions.
1111fb62fb0SOlivier Houchard */
1121fb62fb0SOlivier Houchard void _ck_epoch_addref(ck_epoch_record_t *, ck_epoch_section_t *);
1137e8cd4e1SOlivier Houchard bool _ck_epoch_delref(ck_epoch_record_t *, ck_epoch_section_t *);
1147e8cd4e1SOlivier Houchard
1157e8cd4e1SOlivier Houchard CK_CC_FORCE_INLINE static void *
ck_epoch_record_ct(const ck_epoch_record_t * record)1167e8cd4e1SOlivier Houchard ck_epoch_record_ct(const ck_epoch_record_t *record)
1177e8cd4e1SOlivier Houchard {
1187e8cd4e1SOlivier Houchard
1197e8cd4e1SOlivier Houchard return ck_pr_load_ptr(&record->ct);
1207e8cd4e1SOlivier Houchard }
1211fb62fb0SOlivier Houchard
1221fb62fb0SOlivier Houchard /*
1231fb62fb0SOlivier Houchard * Marks the beginning of an epoch-protected section.
1241fb62fb0SOlivier Houchard */
1251fb62fb0SOlivier Houchard CK_CC_FORCE_INLINE static void
ck_epoch_begin(ck_epoch_record_t * record,ck_epoch_section_t * section)1261fb62fb0SOlivier Houchard ck_epoch_begin(ck_epoch_record_t *record, ck_epoch_section_t *section)
1271fb62fb0SOlivier Houchard {
1281fb62fb0SOlivier Houchard struct ck_epoch *epoch = record->global;
1291fb62fb0SOlivier Houchard
1301fb62fb0SOlivier Houchard /*
1311fb62fb0SOlivier Houchard * Only observe new epoch if thread is not recursing into a read
1321fb62fb0SOlivier Houchard * section.
1331fb62fb0SOlivier Houchard */
1341fb62fb0SOlivier Houchard if (record->active == 0) {
1351fb62fb0SOlivier Houchard unsigned int g_epoch;
1361fb62fb0SOlivier Houchard
1371fb62fb0SOlivier Houchard /*
1381fb62fb0SOlivier Houchard * It is possible for loads to be re-ordered before the store
1391fb62fb0SOlivier Houchard * is committed into the caller's epoch and active fields.
1401fb62fb0SOlivier Houchard * For this reason, store to load serialization is necessary.
1411fb62fb0SOlivier Houchard */
1421fb62fb0SOlivier Houchard #if defined(CK_MD_TSO)
1431fb62fb0SOlivier Houchard ck_pr_fas_uint(&record->active, 1);
1441fb62fb0SOlivier Houchard ck_pr_fence_atomic_load();
1451fb62fb0SOlivier Houchard #else
1461fb62fb0SOlivier Houchard ck_pr_store_uint(&record->active, 1);
1471fb62fb0SOlivier Houchard ck_pr_fence_memory();
1481fb62fb0SOlivier Houchard #endif
1491fb62fb0SOlivier Houchard
1501fb62fb0SOlivier Houchard /*
1511fb62fb0SOlivier Houchard * This load is allowed to be re-ordered prior to setting
1521fb62fb0SOlivier Houchard * active flag due to monotonic nature of the global epoch.
1531fb62fb0SOlivier Houchard * However, stale values lead to measurable performance
1541fb62fb0SOlivier Houchard * degradation in some torture tests so we disallow early load
1551fb62fb0SOlivier Houchard * of global epoch.
1561fb62fb0SOlivier Houchard */
1571fb62fb0SOlivier Houchard g_epoch = ck_pr_load_uint(&epoch->epoch);
1581fb62fb0SOlivier Houchard ck_pr_store_uint(&record->epoch, g_epoch);
1591fb62fb0SOlivier Houchard } else {
1601fb62fb0SOlivier Houchard ck_pr_store_uint(&record->active, record->active + 1);
1611fb62fb0SOlivier Houchard }
1621fb62fb0SOlivier Houchard
1631fb62fb0SOlivier Houchard if (section != NULL)
1641fb62fb0SOlivier Houchard _ck_epoch_addref(record, section);
1651fb62fb0SOlivier Houchard
1661fb62fb0SOlivier Houchard return;
1671fb62fb0SOlivier Houchard }
1681fb62fb0SOlivier Houchard
1691fb62fb0SOlivier Houchard /*
1707e8cd4e1SOlivier Houchard * Marks the end of an epoch-protected section. Returns true if no more
1717e8cd4e1SOlivier Houchard * sections exist for the caller.
1721fb62fb0SOlivier Houchard */
1737e8cd4e1SOlivier Houchard CK_CC_FORCE_INLINE static bool
ck_epoch_end(ck_epoch_record_t * record,ck_epoch_section_t * section)1741fb62fb0SOlivier Houchard ck_epoch_end(ck_epoch_record_t *record, ck_epoch_section_t *section)
1751fb62fb0SOlivier Houchard {
1761fb62fb0SOlivier Houchard
1771fb62fb0SOlivier Houchard ck_pr_fence_release();
1781fb62fb0SOlivier Houchard ck_pr_store_uint(&record->active, record->active - 1);
1791fb62fb0SOlivier Houchard
1801fb62fb0SOlivier Houchard if (section != NULL)
1817e8cd4e1SOlivier Houchard return _ck_epoch_delref(record, section);
1821fb62fb0SOlivier Houchard
1837e8cd4e1SOlivier Houchard return record->active == 0;
1841fb62fb0SOlivier Houchard }
1851fb62fb0SOlivier Houchard
1861fb62fb0SOlivier Houchard /*
1871fb62fb0SOlivier Houchard * Defers the execution of the function pointed to by the "cb"
1881fb62fb0SOlivier Houchard * argument until an epoch counter loop. This allows for a
1891fb62fb0SOlivier Houchard * non-blocking deferral.
1907e8cd4e1SOlivier Houchard *
1917e8cd4e1SOlivier Houchard * We can get away without a fence here due to the monotonic nature
1927e8cd4e1SOlivier Houchard * of the epoch counter. Worst case, this will result in some delays
1937e8cd4e1SOlivier Houchard * before object destruction.
1941fb62fb0SOlivier Houchard */
1951fb62fb0SOlivier Houchard CK_CC_FORCE_INLINE static void
ck_epoch_call(ck_epoch_record_t * record,ck_epoch_entry_t * entry,ck_epoch_cb_t * function)1961fb62fb0SOlivier Houchard ck_epoch_call(ck_epoch_record_t *record,
1971fb62fb0SOlivier Houchard ck_epoch_entry_t *entry,
1981fb62fb0SOlivier Houchard ck_epoch_cb_t *function)
1991fb62fb0SOlivier Houchard {
2001fb62fb0SOlivier Houchard struct ck_epoch *epoch = record->global;
2011fb62fb0SOlivier Houchard unsigned int e = ck_pr_load_uint(&epoch->epoch);
2021fb62fb0SOlivier Houchard unsigned int offset = e & (CK_EPOCH_LENGTH - 1);
2031fb62fb0SOlivier Houchard
2041fb62fb0SOlivier Houchard record->n_pending++;
2051fb62fb0SOlivier Houchard entry->function = function;
2061fb62fb0SOlivier Houchard ck_stack_push_spnc(&record->pending[offset], &entry->stack_entry);
2071fb62fb0SOlivier Houchard return;
2081fb62fb0SOlivier Houchard }
2091fb62fb0SOlivier Houchard
2107e8cd4e1SOlivier Houchard /*
2117e8cd4e1SOlivier Houchard * Same as ck_epoch_call, but allows for records to be shared and is reentrant.
2127e8cd4e1SOlivier Houchard */
2137e8cd4e1SOlivier Houchard CK_CC_FORCE_INLINE static void
ck_epoch_call_strict(ck_epoch_record_t * record,ck_epoch_entry_t * entry,ck_epoch_cb_t * function)2147e8cd4e1SOlivier Houchard ck_epoch_call_strict(ck_epoch_record_t *record,
2157e8cd4e1SOlivier Houchard ck_epoch_entry_t *entry,
2167e8cd4e1SOlivier Houchard ck_epoch_cb_t *function)
2177e8cd4e1SOlivier Houchard {
2187e8cd4e1SOlivier Houchard struct ck_epoch *epoch = record->global;
2197e8cd4e1SOlivier Houchard unsigned int e = ck_pr_load_uint(&epoch->epoch);
2207e8cd4e1SOlivier Houchard unsigned int offset = e & (CK_EPOCH_LENGTH - 1);
2217e8cd4e1SOlivier Houchard
2227e8cd4e1SOlivier Houchard ck_pr_inc_uint(&record->n_pending);
2237e8cd4e1SOlivier Houchard entry->function = function;
2247e8cd4e1SOlivier Houchard
2257e8cd4e1SOlivier Houchard /* Store fence is implied by push operation. */
2267e8cd4e1SOlivier Houchard ck_stack_push_upmc(&record->pending[offset], &entry->stack_entry);
2277e8cd4e1SOlivier Houchard return;
2287e8cd4e1SOlivier Houchard }
2297e8cd4e1SOlivier Houchard
2307e8cd4e1SOlivier Houchard /*
2317e8cd4e1SOlivier Houchard * This callback is used for synchronize_wait to allow for custom blocking
2327e8cd4e1SOlivier Houchard * behavior.
2337e8cd4e1SOlivier Houchard */
2347e8cd4e1SOlivier Houchard typedef void ck_epoch_wait_cb_t(ck_epoch_t *, ck_epoch_record_t *,
2357e8cd4e1SOlivier Houchard void *);
2367e8cd4e1SOlivier Houchard
2377e8cd4e1SOlivier Houchard /*
2387e8cd4e1SOlivier Houchard * Return latest epoch value. This operation provides load ordering.
2397e8cd4e1SOlivier Houchard */
2407e8cd4e1SOlivier Houchard CK_CC_FORCE_INLINE static unsigned int
ck_epoch_value(const ck_epoch_t * ep)2417e8cd4e1SOlivier Houchard ck_epoch_value(const ck_epoch_t *ep)
2427e8cd4e1SOlivier Houchard {
2437e8cd4e1SOlivier Houchard
2447e8cd4e1SOlivier Houchard ck_pr_fence_load();
2457e8cd4e1SOlivier Houchard return ck_pr_load_uint(&ep->epoch);
2467e8cd4e1SOlivier Houchard }
2477e8cd4e1SOlivier Houchard
2481fb62fb0SOlivier Houchard void ck_epoch_init(ck_epoch_t *);
2497e8cd4e1SOlivier Houchard
2507e8cd4e1SOlivier Houchard /*
2517e8cd4e1SOlivier Houchard * Attempts to recycle an unused epoch record. If one is successfully
2527e8cd4e1SOlivier Houchard * allocated, the record context pointer is also updated.
2537e8cd4e1SOlivier Houchard */
2547e8cd4e1SOlivier Houchard ck_epoch_record_t *ck_epoch_recycle(ck_epoch_t *, void *);
2557e8cd4e1SOlivier Houchard
2567e8cd4e1SOlivier Houchard /*
2577e8cd4e1SOlivier Houchard * Registers an epoch record. An optional context pointer may be passed that
2587e8cd4e1SOlivier Houchard * is retrievable with ck_epoch_record_ct.
2597e8cd4e1SOlivier Houchard */
2607e8cd4e1SOlivier Houchard void ck_epoch_register(ck_epoch_t *, ck_epoch_record_t *, void *);
2617e8cd4e1SOlivier Houchard
2627e8cd4e1SOlivier Houchard /*
2637e8cd4e1SOlivier Houchard * Marks a record as available for re-use by a subsequent recycle operation.
2647e8cd4e1SOlivier Houchard * Note that the record cannot be physically destroyed.
2657e8cd4e1SOlivier Houchard */
2661fb62fb0SOlivier Houchard void ck_epoch_unregister(ck_epoch_record_t *);
2677e8cd4e1SOlivier Houchard
2681fb62fb0SOlivier Houchard bool ck_epoch_poll(ck_epoch_record_t *);
269*4c7d0d92SMatt Macy bool ck_epoch_poll_deferred(struct ck_epoch_record *record, ck_stack_t *deferred);
2701fb62fb0SOlivier Houchard void ck_epoch_synchronize(ck_epoch_record_t *);
2717e8cd4e1SOlivier Houchard void ck_epoch_synchronize_wait(ck_epoch_t *, ck_epoch_wait_cb_t *, void *);
2721fb62fb0SOlivier Houchard void ck_epoch_barrier(ck_epoch_record_t *);
2737e8cd4e1SOlivier Houchard void ck_epoch_barrier_wait(ck_epoch_record_t *, ck_epoch_wait_cb_t *, void *);
2747e8cd4e1SOlivier Houchard
2757e8cd4e1SOlivier Houchard /*
2767e8cd4e1SOlivier Houchard * Reclaim entries associated with a record. This is safe to call only on
2777e8cd4e1SOlivier Houchard * the caller's record or records that are using call_strict.
2787e8cd4e1SOlivier Houchard */
2791fb62fb0SOlivier Houchard void ck_epoch_reclaim(ck_epoch_record_t *);
2801fb62fb0SOlivier Houchard
2811fb62fb0SOlivier Houchard #endif /* CK_EPOCH_H */
282