kernel/sync/
rcu.rs

1// SPDX-License-Identifier: GPL-2.0
2
3//! RCU support.
4//!
5//! C header: [`include/linux/rcupdate.h`](srctree/include/linux/rcupdate.h)
6
7use crate::{bindings, types::NotThreadSafe};
8
9/// Evidence that the RCU read side lock is held on the current thread/CPU.
10///
11/// The type is explicitly not `Send` because this property is per-thread/CPU.
12///
13/// # Invariants
14///
15/// The RCU read side lock is actually held while instances of this guard exist.
16pub struct Guard(NotThreadSafe);
17
18impl Guard {
19    /// Acquires the RCU read side lock and returns a guard.
20    #[inline]
21    pub fn new() -> Self {
22        // SAFETY: An FFI call with no additional requirements.
23        unsafe { bindings::rcu_read_lock() };
24        // INVARIANT: The RCU read side lock was just acquired above.
25        Self(NotThreadSafe)
26    }
27
28    /// Explicitly releases the RCU read side lock.
29    #[inline]
30    pub fn unlock(self) {}
31}
32
33impl Default for Guard {
34    #[inline]
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40impl Drop for Guard {
41    #[inline]
42    fn drop(&mut self) {
43        // SAFETY: By the type invariants, the RCU read side is locked, so it is ok to unlock it.
44        unsafe { bindings::rcu_read_unlock() };
45    }
46}
47
48/// Acquires the RCU read side lock.
49#[inline]
50pub fn read_lock() -> Guard {
51    Guard::new()
52}