urcu2 0.1.3

Safe API to liburcu
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::ffi::c_void;

use urcu_sys::{RcuFlavorApi, RcuHead, RcuPollState};

use crate::rcu::builder::RcuContextBuilder;
use crate::rcu::cleanup::{RcuCleaner, RcuCleanup, RcuCleanupMut};
use crate::rcu::context::{RcuContext, RcuDeferContext, RcuReadContext};

/// This trait defines the API from the C library.
pub trait RcuFlavor {
    /// Performs initialization on the RCU thread.
    ///
    /// #### Safety
    ///
    /// * Should be called once per thread.
    /// * Should be called before any other functions.
    unsafe fn unchecked_rcu_init();

    /// Registers a read-side RCU thread.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must not be registered with [`RcuFlavor::unchecked_rcu_read_register_thread`].
    /// * The thread must unregister with [`RcuFlavor::unchecked_rcu_read_unregister_thread`].
    unsafe fn unchecked_rcu_read_register_thread();

    /// Unregisters a read-side RCU thread.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must be registered with [`RcuFlavor::unchecked_rcu_read_register_thread`].
    unsafe fn unchecked_rcu_read_unregister_thread();

    /// Starts a RCU critical section.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must be registered with [`RcuFlavor::unchecked_rcu_read_register_thread`].
    /// * The thread must call [`RcuFlavor::unchecked_rcu_read_unlock`] later.
    unsafe fn unchecked_rcu_read_lock();

    /// Stops a RCU critical section.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must be registered with [`RcuFlavor::unchecked_rcu_read_register_thread`].
    /// * The thread must have called [`RcuFlavor::unchecked_rcu_read_lock`] before.
    unsafe fn unchecked_rcu_read_unlock();

    /// Registers a defer-enabled RCU thread.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must not be registered with [`RcuFlavor::unchecked_rcu_defer_register_thread`].
    /// * The thread must unregister with [`RcuFlavor::unchecked_rcu_defer_unregister_thread`].
    unsafe fn unchecked_rcu_defer_register_thread();

    /// Unregisters a defer-enabled RCU thread.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must be registered with [`RcuFlavor::unchecked_rcu_defer_register_thread`].
    unsafe fn unchecked_rcu_defer_unregister_thread();

    /// Executes a call after the next RCU grace period.
    ///
    /// #### Note
    ///
    /// The callback will be executed on the same thread. If the internal queue is full
    /// the call might block and the callback will be executed immediatly. In such case,
    /// [`RcuFlavor::unchecked_rcu_synchronize`] will be called internally.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must be registered with [`RcuFlavor::unchecked_rcu_defer_register_thread`].
    /// * The thread must call [`RcuFlavor::unchecked_rcu_defer_barrier`] at exit.
    /// * The thread must not be inside a RCU critical section.
    /// * The pointers must be valid until the callback is executed.
    unsafe fn unchecked_rcu_defer_call(
        func: Option<unsafe extern "C" fn(head: *mut c_void)>,
        head: *mut c_void,
    );

    /// Wait for all RCU deferred callbacks initiated by the current thread.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must be registered with [`RcuFlavor::unchecked_rcu_defer_register_thread`].
    /// * The thread must not be inside a RCU critical section.
    unsafe fn unchecked_rcu_defer_barrier();

    /// Waits until the RCU grace period is over.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must not be inside a RCU critical section.
    unsafe fn unchecked_rcu_synchronize();

    /// Creates an [`RcuPollState`] used for checking if the grace period has ended.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must be registered with [`RcuFlavor::unchecked_rcu_read_register_thread`].
    unsafe fn unchecked_rcu_poll_start() -> RcuPollState;

    /// Polls if the grace period has ended.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must be registered with [`RcuFlavor::unchecked_rcu_read_register_thread`].
    /// * The thread must use an [`RcuPollState`] from [`RcuFlavor::unchecked_rcu_poll_start`].
    unsafe fn unchecked_rcu_poll_check(state: RcuPollState) -> bool;

    /// Executes a call after the next RCU grace period.
    ///
    /// #### Note
    ///
    /// This call nevers blocks because the callback will be executed on an helper thread.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must be registered with [`RcuFlavor::unchecked_rcu_read_register_thread`].
    /// * The thread must call [`RcuFlavor::unchecked_rcu_call_barrier`] at exit.
    /// * The pointers must be valid until the callback is executed.
    unsafe fn unchecked_rcu_call(
        func: Option<unsafe extern "C" fn(ptr: *mut RcuHead)>,
        ptr: *mut RcuHead,
    );

    /// Wait for all RCU callbacks initiated before the call by any thread to be completed.
    ///
    /// #### Safety
    ///
    /// * The thread must be initialized with [`RcuFlavor::unchecked_rcu_init`].
    /// * The thread must be registered with [`RcuFlavor::unchecked_rcu_read_register_thread`].
    unsafe fn unchecked_rcu_call_barrier();

    /// Returns the API list for this RCU flavor.
    ///
    /// #### Safety
    ///
    /// * The thread have the same requirements as the other functions when using this API.
    unsafe fn unchecked_rcu_api() -> &'static RcuFlavorApi;

    /// Defines the context used in cleanup calls.
    type CleanupContext: RcuContext + RcuReadContext + RcuDeferContext;

    /// Configures a callback to be called after the next RCU grace period is finished.
    ///
    /// Unlike [`RcuReadContext::rcu_call`] or [`RcuDeferContext::rcu_defer`], this
    /// function can be called by any thread whether it is registered or not.
    ///
    /// #### Note
    ///
    /// The callback must be [`Send`] because it will be executed by an helper thread.
    fn rcu_cleanup(callback: RcuCleanupMut<Self::CleanupContext>);

    /// Configures a callback to be called after the next RCU grace period is finished.
    ///
    /// Unlike [`RcuReadContext::rcu_call`] or [`RcuDeferContext::rcu_defer`], this
    /// function can be called by any thread whether it is registered or not.
    ///
    /// #### Note
    ///
    /// The callback must be [`Send`] because it will be executed by an helper thread.
    ///
    /// The callback does not receive a mutable context in order to prevent deadlock.
    fn rcu_cleanup_and_block(callback: RcuCleanup<Self::CleanupContext>);

    /// Creates a builder for a context of this flavor.
    fn rcu_context_builder() -> RcuContextBuilder<Self>
    where
        Self: Sized;
}

macro_rules! urcu_func {
    ($flavor:ident, $name:ident) => {
        paste::paste! {
            [<urcu _ $flavor _ $name>]
        }
    };
}

macro_rules! define_flavor {
    ($name:ident, $flavor:ident, $context:ident) => {
        #[doc = concat!("Defines a RCU flavor (`liburcu-", stringify!($flavor), "`).")]
        pub struct $name;

        impl RcuFlavor for $name {
            unsafe fn unchecked_rcu_init() {
                urcu_func!($flavor, init)()
            }

            unsafe fn unchecked_rcu_read_register_thread() {
                urcu_func!($flavor, register_thread)()
            }

            unsafe fn unchecked_rcu_read_unregister_thread() {
                urcu_func!($flavor, unregister_thread)()
            }

            unsafe fn unchecked_rcu_read_lock() {
                urcu_func!($flavor, read_lock)()
            }

            unsafe fn unchecked_rcu_read_unlock() {
                urcu_func!($flavor, read_unlock)()
            }

            unsafe fn unchecked_rcu_defer_register_thread() {
                urcu_func!($flavor, defer_register_thread)();
            }

            unsafe fn unchecked_rcu_defer_unregister_thread() {
                urcu_func!($flavor, defer_unregister_thread)()
            }

            unsafe fn unchecked_rcu_defer_call(
                func: Option<unsafe extern "C" fn(head: *mut c_void)>,
                head: *mut c_void,
            ) {
                urcu_func!($flavor, defer_rcu)(func, head)
            }

            unsafe fn unchecked_rcu_defer_barrier() {
                urcu_func!($flavor, defer_barrier)()
            }

            unsafe fn unchecked_rcu_synchronize() {
                urcu_func!($flavor, synchronize_rcu)()
            }

            unsafe fn unchecked_rcu_poll_start() -> RcuPollState {
                urcu_func!($flavor, start_poll_synchronize_rcu)()
            }

            unsafe fn unchecked_rcu_poll_check(state: RcuPollState) -> bool {
                urcu_func!($flavor, poll_state_synchronize_rcu)(state)
            }

            unsafe fn unchecked_rcu_call(
                func: Option<unsafe extern "C" fn(ptr: *mut RcuHead)>,
                ptr: *mut RcuHead,
            ) {
                urcu_func!($flavor, call_rcu)(ptr, func)
            }

            unsafe fn unchecked_rcu_call_barrier() {
                urcu_func!($flavor, barrier)()
            }

            unsafe fn unchecked_rcu_api() -> &'static RcuFlavorApi {
                &RCU_API
            }

            type CleanupContext = $context<true, true>;

            fn rcu_cleanup(callback: RcuCleanupMut<Self::CleanupContext>) {
                RcuCleaner::<Self>::get().send_mut(callback);
            }

            fn rcu_cleanup_and_block(callback: RcuCleanup<Self::CleanupContext>) {
                RcuCleaner::<Self>::get().send(callback).barrier();
            }

            fn rcu_context_builder() -> RcuContextBuilder<Self>
            where
                Self: Sized,
            {
                RcuContextBuilder::<Self>::new()
            }
        }
    };
}

#[cfg(feature = "flavor-bp")]
pub(crate) mod bp {
    use super::*;

    use urcu_bp_sys::{
        urcu_bp_barrier,
        urcu_bp_call_rcu,
        urcu_bp_defer_barrier,
        urcu_bp_defer_rcu,
        urcu_bp_defer_register_thread,
        urcu_bp_defer_unregister_thread,
        urcu_bp_init,
        urcu_bp_poll_state_synchronize_rcu,
        urcu_bp_read_lock,
        urcu_bp_read_unlock,
        urcu_bp_register_thread,
        urcu_bp_start_poll_synchronize_rcu,
        urcu_bp_synchronize_rcu,
        urcu_bp_unregister_thread,
        RCU_API,
    };

    use crate::rcu::context::RcuContextBp;

    define_flavor!(RcuFlavorBp, bp, RcuContextBp);
}

#[cfg(feature = "flavor-mb")]
pub(crate) mod mb {
    use super::*;

    use urcu_mb_sys::{
        urcu_mb_barrier,
        urcu_mb_call_rcu,
        urcu_mb_defer_barrier,
        urcu_mb_defer_rcu,
        urcu_mb_defer_register_thread,
        urcu_mb_defer_unregister_thread,
        urcu_mb_init,
        urcu_mb_poll_state_synchronize_rcu,
        urcu_mb_read_lock,
        urcu_mb_read_unlock,
        urcu_mb_register_thread,
        urcu_mb_start_poll_synchronize_rcu,
        urcu_mb_synchronize_rcu,
        urcu_mb_unregister_thread,
        RCU_API,
    };

    use crate::rcu::context::RcuContextMb;

    define_flavor!(RcuFlavorMb, mb, RcuContextMb);
}

#[cfg(feature = "flavor-memb")]
pub(crate) mod memb {
    use super::*;

    use urcu_memb_sys::{
        urcu_memb_barrier,
        urcu_memb_call_rcu,
        urcu_memb_defer_barrier,
        urcu_memb_defer_rcu,
        urcu_memb_defer_register_thread,
        urcu_memb_defer_unregister_thread,
        urcu_memb_init,
        urcu_memb_poll_state_synchronize_rcu,
        urcu_memb_read_lock,
        urcu_memb_read_unlock,
        urcu_memb_register_thread,
        urcu_memb_start_poll_synchronize_rcu,
        urcu_memb_synchronize_rcu,
        urcu_memb_unregister_thread,
        RCU_API,
    };

    use crate::rcu::context::RcuContextMemb;

    define_flavor!(RcuFlavorMemb, memb, RcuContextMemb);
}

#[cfg(feature = "flavor-qsbr")]
pub(crate) mod qsbr {
    use super::*;

    use urcu_qsbr_sys::{
        urcu_qsbr_barrier,
        urcu_qsbr_call_rcu,
        urcu_qsbr_defer_barrier,
        urcu_qsbr_defer_rcu,
        urcu_qsbr_defer_register_thread,
        urcu_qsbr_defer_unregister_thread,
        urcu_qsbr_init,
        urcu_qsbr_poll_state_synchronize_rcu,
        urcu_qsbr_read_lock,
        urcu_qsbr_read_unlock,
        urcu_qsbr_register_thread,
        urcu_qsbr_start_poll_synchronize_rcu,
        urcu_qsbr_synchronize_rcu,
        urcu_qsbr_unregister_thread,
        RCU_API,
    };

    use crate::rcu::context::RcuContextQsbr;

    define_flavor!(RcuFlavorQsbr, qsbr, RcuContextQsbr);
}

#[cfg(feature = "flavor-bp")]
pub use bp::*;

#[cfg(feature = "flavor-mb")]
pub use mb::*;

#[cfg(feature = "flavor-memb")]
pub use memb::*;

#[cfg(feature = "flavor-qsbr")]
pub use qsbr::*;