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 
7 use 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.
16 pub struct Guard(NotThreadSafe);
17 
18 impl Guard {
19     /// Acquires the RCU read side lock and returns a guard.
new() -> Self20     pub fn new() -> Self {
21         // SAFETY: An FFI call with no additional requirements.
22         unsafe { bindings::rcu_read_lock() };
23         // INVARIANT: The RCU read side lock was just acquired above.
24         Self(NotThreadSafe)
25     }
26 
27     /// Explicitly releases the RCU read side lock.
unlock(self)28     pub fn unlock(self) {}
29 }
30 
31 impl Default for Guard {
default() -> Self32     fn default() -> Self {
33         Self::new()
34     }
35 }
36 
37 impl Drop for Guard {
drop(&mut self)38     fn drop(&mut self) {
39         // SAFETY: By the type invariants, the RCU read side is locked, so it is ok to unlock it.
40         unsafe { bindings::rcu_read_unlock() };
41     }
42 }
43 
44 /// Acquires the RCU read side lock.
read_lock() -> Guard45 pub fn read_lock() -> Guard {
46     Guard::new()
47 }
48