Debian Codegraph @

src/util/ref.h

raw
1 #pragma once
2
3 /*
4 * Atomic Reference Counter
5 *
6 * This implements an atomic reference counter. That is, references can be
7 * acquired and released from multiple threads in parallel.
8 *
9 * To use CRef, an object needs a member of type `_Atomic unsigned long', which
10 * counts the references. All functions here take it as first argument and only
11 * operate on this one field.
12 */
13
14 #include <assert.h>
15 #include <c-stdaux.h>
16 #include <stdatomic.h>
17 #include <stdlib.h>
18 #include <stdnoreturn.h>
19
20 typedef void (*RefFn) (_Atomic unsigned long *ref, void *userdata);
21
22 /**
23 * REF_INIT - initialize static reference counter
24 *
25 * This provides a static initializer for a reference counter. It is meant to
26 * be used as assignment for variables or member fields.
27 */
28 #define REF_INIT ATOMIC_VAR_INIT(1UL)
29
30 /**
31 * ref_add() - acquire references
32 * @ref: reference counter to operate on, or NULL
33 * @n: number of references to acquire
34 *
35 * Acquire @n references to the reference counter @ref. The caller must
36 * guarantee that they already own a reference to @n. Furthermore, the caller
37 * must ensure that this cannot overflow 'unsigned long' (usually it is enough
38 * to ensure that a reference is associated with some allocated object).
39 *
40 * If @ref is NULL, this is a no-op. @n must not be 0.
41 *
42 * Return: @ref is returned.
43 */
44 static inline _Atomic unsigned long *ref_add(_Atomic unsigned long *ref, unsigned long n) {
45 unsigned long n_refs;
46
47 c_assert(n > 0);
48
49 if (ref) {
50 /*
51 * Acquire references but do not place any barriers. Nobody
52 * should place decisions based on this operations, ever! So no
53 * need to order it.
54 */
55 n_refs = atomic_fetch_add_explicit(ref, n, memory_order_relaxed);
56 c_assert(n_refs > 0);
57 }
58
59 return ref;
60 }
61
62 /**
63 * ref_add_unless_zero() - acquire references if possible
64 * @ref: reference counter to operate on, or NULL
65 * @n: number of references to acquire
66 *
67 * Acquire @n references to the reference counter @ref, if, and only if, it has
68 * not already dropped to 0. In case of success, this has the same effect as
69 * ref_add(). In case of failure, this will return NULL without acquiring any
70 * reference. The caller must check the return value of this function.
71 *
72 * This function does not give any memory ordering guarantees. Just like
73 * ref_add(), no decision should be placed based on the fact that a reference
74 * has been acquired. That is, even if this returns NULL, the caller must not
75 * try to deduce the state of the surrounding object. Furthermore, the caller
76 * must provide sufficient synchronization on the pointer to the object. This
77 * function just makes sure to never acquire references to possibly unlocked
78 * objects that have already been released. Any further synchronization is up
79 * to the caller.
80 *
81 * If @ref is NULL, this is a no-op. @n must not be 0.
82 *
83 * Return: @ref is returned on success, otherwise NULL is returned.
84 */
85 static inline _Atomic unsigned long *ref_add_unless_zero(_Atomic unsigned long *ref, unsigned long n) {
86 unsigned long n_refs;
87
88 c_assert(n > 0);
89
90 if (ref) {
91 /*
92 * Try replacing *ref with (*ref + n). This requires us to
93 * fetch the value and loop on cmpxchg. On failure, bail out
94 * with NULL. Otherwise, retry until we completed the task.
95 * Note that we do not provide barriers. We expect the caller
96 * to synchronize via the actual pointer of the object. If this
97 * function fails, the caller should not use this information
98 * to deduce the state of the object. It must rely on external
99 * synchronization, if that is required.
100 */
101 n_refs = atomic_load_explicit(ref, memory_order_relaxed);
102 do {
103 if (n_refs == 0)
104 return NULL;
105 } while (!atomic_compare_exchange_weak_explicit(ref, &n_refs, n_refs + n,
106 memory_order_relaxed,
107 memory_order_relaxed));
108 }
109
110 return ref;
111 }
112
113 /**
114 * ref_inc() - acquire reference
115 * @ref: reference counter to operate on, or NULL
116 *
117 * This acquires a single reference to @ref. See ref_add() for details. The
118 * caller must guarantee that it already owns a reference to @ref.
119 *
120 * If @ref is NULL, this is a no-op.
121 *
122 * Return: @ref is returned.
123 */
124 static inline _Atomic unsigned long *ref_inc(_Atomic unsigned long *ref) {
125 return ref_add(ref, 1UL);
126 }
127
128 /**
129 * ref_inc_unless_zero() - acquire reference if possible
130 * @ref: reference counter to operate on, or NULL
131 *
132 * Acquire a single reference to @ref, if the reference counter has not already
133 * dropped to zero. See ref_add_unless_zero() for details.
134 *
135 * If @ref is NULL, this is a no-op.
136 *
137 * Return: @ref is returned on success, otherwise NULL is returned.
138 */
139 static inline _Atomic unsigned long *ref_inc_unless_zero(_Atomic unsigned long *ref) {
140 return ref_add_unless_zero(ref, 1UL);
141 }
142
143 /**
144 * ref_unreachable() - convenience callback
145 * @ref: reference counter to release
146 * @userdata: userdata provided by caller
147 *
148 * This is a convenience callback to pass to ref_sub() and friends, if, and
149 * only if, you are sure that your call will not cause the reference counter to
150 * drop to 0. This release callback will abort the application if it is
151 * actually called.
152 */
153 noreturn static inline void ref_unreachable(_Atomic unsigned long *ref, void *userdata) {
154 c_assert(0);
155 abort();
156 }
157
158 /**
159 * ref_sub() - release references
160 * @ref: reference counter to operate on, or NULL
161 * @n: number of references to release
162 * @func: release function, or NULL
163 * @userdata: userdata to pass to release function
164 *
165 * Release @n references to the reference counter @ref. The caller must ensure
166 * that it actually owns @n references when calling this. If this causes the
167 * counter to drop to 0, then @func will be invoked (if non-NULL), with @ref
168 * and @userdata passed to it. Otherwise, @func is not invoked and the call
169 * simply returns to the caller after dropping @n references.
170 *
171 * This function provides sufficient read/write barriers regarding any
172 * modifications of the object associated with @ref. That is, when an object is
173 * actually released (i.e., @func is called), any prior writes of any thread
174 * that were done while holding a reference, are guaranteed to be visible to
175 * the release thread.
176 *
177 * If @ref is NULL, this is a no-op. @n must not be 0.
178 *
179 * Return: NULL is returned.
180 */
181 static inline _Atomic unsigned long *ref_sub(_Atomic unsigned long *ref, unsigned long n, RefFn func, void *userdata) {
182 unsigned long n_refs;
183
184 if (ref) {
185 /*
186 * Make sure to order all our stores to the object before
187 * releasing the reference. We must guarantee that a racing
188 * unref operation will see our stores before they release the
189 * object. We could use memory_order_acq_rel, but we rather
190 * perform the acquire-barrier only in the release-path, since
191 * it is only needed there.
192 */
193 n_refs = atomic_fetch_sub_explicit(ref, n, memory_order_release);
194 c_assert(n_refs >= n);
195 if (n_refs == n) {
196 atomic_thread_fence(memory_order_acquire);
197 if (func)
198 func(ref, userdata);
199 }
200 }
201
202 return NULL;
203 }
204
205 /**
206 * ref_dec() - release a single reference
207 * @ref: reference counter to operate on, or NULL
208 * @func: release function, or NULL
209 * @userdata: userdata to pass to release function
210 *
211 * This releases a single reference to @ref. See ref_sub() for details.
212 *
213 * If @ref is NULL, this is a no-op.
214 *
215 * Return: NULL is returned.
216 */
217 static inline _Atomic unsigned long *ref_dec(_Atomic unsigned long *ref, RefFn func, void *userdata) {
218 return ref_sub(ref, 1UL, func, userdata);
219 }