ostd/task/preempt/
guard.rs

1// SPDX-License-Identifier: MPL-2.0
2
3use crate::{sync::GuardTransfer, task::atomic_mode::InAtomicMode};
4
5/// A guard for disable preempt.
6#[clippy::has_significant_drop]
7#[must_use]
8#[derive(Debug)]
9pub struct DisabledPreemptGuard {
10    // This private field prevents user from constructing values of this type directly.
11    _private: (),
12}
13
14impl !Send for DisabledPreemptGuard {}
15
16// SAFETY: The guard disables preemptions, which meets the second
17// sufficient condition for atomic mode.
18unsafe impl InAtomicMode for DisabledPreemptGuard {}
19
20impl DisabledPreemptGuard {
21    fn new() -> Self {
22        super::cpu_local::inc_guard_count();
23        Self { _private: () }
24    }
25}
26
27impl GuardTransfer for DisabledPreemptGuard {
28    fn transfer_to(&mut self) -> Self {
29        disable_preempt()
30    }
31}
32
33impl Drop for DisabledPreemptGuard {
34    fn drop(&mut self) {
35        super::cpu_local::dec_guard_count();
36    }
37}
38
39/// Disables preemption.
40pub fn disable_preempt() -> DisabledPreemptGuard {
41    DisabledPreemptGuard::new()
42}