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