fuchsia_rcu/rcu_read_scope.rs
1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::state_machine::{rcu_read_lock, rcu_read_unlock};
6use std::marker::PhantomData;
7
8/// A scope that holds a read lock on the RCU state machine.
9///
10/// This scope is used to ensure that the object referenced by the pointer remains valid until the
11/// scope is dropped.
12pub struct RcuReadScope {
13 // We need to call `rcu_read_lock` and `rcu_read_unlock` from the same thread, so
14 // we need to make `RcuReadScope` non-Send.
15 _marker: PhantomData<*const ()>,
16}
17
18impl RcuReadScope {
19 /// Create a new read scope.
20 ///
21 /// This function acquires a read lock on the RCU state machine. The read lock is held until the
22 /// scope is dropped.
23 pub fn new() -> Self {
24 rcu_read_lock();
25 Self { _marker: PhantomData }
26 }
27}
28
29impl Drop for RcuReadScope {
30 fn drop(&mut self) {
31 rcu_read_unlock();
32 }
33}