libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_rcu.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * Use of this source code is governed by the ISC License.
4 *
5 * src/inc/xtc_rcu.h
6 * Read-Copy-Update epoch reclamation. Wait-free readers,
7 * deferred-reclaim writers. This is the foundation that
8 * xtc_lrlock (M13b) and xtc_chash (M11.5) build on.
9 *
10 * Model:
11 * - A global epoch counter advances on each "grace period".
12 * - Each thread records the epoch it entered a read-side
13 * critical section.
14 * - To free an object, a writer hands it to xtc_rcu_retire;
15 * the object is held until every thread has either left its
16 * read-side or moved to a strictly newer epoch.
17 *
18 * This first cut uses a single global epoch + a per-thread slot
19 * registered lazily. A periodic helper (or any writer's call to
20 * xtc_rcu_synchronize) advances the epoch. M13a-rev2 will ship
21 * a per-NUMA-node bucketing for scaling.
22 */
23
24#ifndef XTC_RCU_H
25#define XTC_RCU_H
26
27#include <stddef.h>
28#include <stdint.h>
29
30#include "xtc.h"
31
32typedef void (*xtc_rcu_free_fn)(void *p);
33
34/*
35 * PUBLIC: int xtc_rcu_init __P((void));
36 * PUBLIC: void xtc_rcu_fini __P((void));
37 *
38 * PUBLIC: void xtc_rcu_read_lock __P((void));
39 * PUBLIC: void xtc_rcu_read_unlock __P((void));
40 *
41 * PUBLIC: void xtc_rcu_retire __P((void *, xtc_rcu_free_fn));
42 * PUBLIC: void xtc_rcu_synchronize __P((void));
43 *
44 * PUBLIC: uint64_t xtc_rcu_current_epoch __P((void));
45 */
46
47/* Initialise the RCU subsystem. Idempotent. Called automatically by
48 * the first read_lock or retire; explicit init lets callers fail
49 * early if storage allocation fails. */
50int xtc_rcu_init(void);
51
52/* Finalise: drain all pending callbacks and free per-thread state.
53 * Safe to call only when no readers are active. */
54void xtc_rcu_fini(void);
55
56/* Mark the start / end of a read-side critical section. Reads
57 * between lock and unlock see a consistent snapshot of any
58 * RCU-protected pointer that was loaded after the lock.
59 *
60 * Lock/unlock are nestable on the same thread (refcount-style).
61 * No system call, no atomic compare-exchange on the fast path. */
62void xtc_rcu_read_lock(void);
63void xtc_rcu_read_unlock(void);
64
65/* Schedule `p` to be freed (via fn(p)) after every reader currently
66 * inside a read-side has finished. The actual free happens lazily
67 * on the next epoch advance + a writer's synchronize call. */
68void xtc_rcu_retire(void *p, xtc_rcu_free_fn fn);
69
70/* Advance the global epoch and reclaim everything that's now safe.
71 * Safe to call from any thread. Blocks briefly while waiting for
72 * readers in the previous epoch to drain. */
73void xtc_rcu_synchronize(void);
74
75uint64_t xtc_rcu_current_epoch(void);
76
77#endif /* XTC_RCU_H */