kernel/sync/
atomic.rs

1//! Atomic abstractions for single and multi-core systems.
2
3#[cfg(all(feature = "multi-core", feature = "no-atomic-cas"))]
4compile_error!(
5    "The `multi-core` feature requires atomic-cas operations to be available on the target. Enable the `atomic-cas` feature."
6);
7
8#[cfg(all(feature = "no-atomic-cas", not(target_has_atomic = "8")))]
9compile_error!(
10    "The `atomic-cas` feature requires the target to have atomic operations on at least 8-bit integers."
11);
12
13// ----------------------------AtomicU8----------------------------
14#[cfg(all(feature = "no-atomic-cas"))]
15pub use core::sync::atomic::Ordering;
16
17#[cfg(all(feature = "no-atomic-cas"))]
18use core::cell::UnsafeCell;
19
20#[cfg(all(feature = "no-atomic-cas"))]
21/// An atomic `u8`.
22pub struct AtomicU8 {
23    value: UnsafeCell<u8>,
24}
25
26#[cfg(not(all(feature = "no-atomic-cas")))]
27pub use core::sync::atomic::AtomicU8;
28
29#[cfg(all(feature = "no-atomic-cas"))]
30impl AtomicU8 {
31    /// Creates a new atomic u8.
32    pub const fn new(value: u8) -> Self {
33        Self {
34            value: UnsafeCell::new(value),
35        }
36    }
37
38    /// Loads the value.
39    pub fn load(&self, _: Ordering) -> u8 {
40        todo!("Implement atomic load for u8");
41    }
42
43    /// Stores a value.
44    pub fn store(&self, value: u8, _: Ordering) {
45        todo!("Implement atomic store for u8");
46    }
47
48    /// Compares the value and exchanges it.
49    pub fn compare_exchange(
50        &self,
51        current: u8,
52        new: u8,
53        _: Ordering,
54        _: Ordering,
55    ) -> Result<u8, u8> {
56        todo!("Implement atomic compare_exchange for u8");
57    }
58
59    ///fetch a value, apply the function and write back the modified value atomically
60    pub fn fetch_update<F>(&self, _: Ordering, _: Ordering, f: F) -> Result<u8, u8>
61    where
62        F: FnMut(u8) -> Option<u8>,
63    {
64        todo!("Implement atomic fetch_update for u8");
65    }
66}
67
68#[cfg(not(all(feature = "no-atomic-cas")))]
69pub use core::sync::atomic::AtomicBool;
70
71#[cfg(all(feature = "no-atomic-cas"))]
72/// An atomic `bool`.
73pub struct AtomicBool {
74    value: UnsafeCell<bool>,
75}
76
77#[cfg(all(feature = "no-atomic-cas"))]
78impl AtomicBool {
79    /// Creates a new atomic bool.
80    pub const fn new(value: bool) -> Self {
81        Self {
82            value: UnsafeCell::new(value),
83        }
84    }
85
86    /// Loads the value.
87    pub fn load(&self, _: Ordering) -> bool {
88        todo!("Implement atomic load for bool");
89    }
90
91    /// Stores a value.
92    pub fn store(&self, value: bool, _: Ordering) {
93        todo!("Implement atomic store for bool");
94    }
95
96    /// Compares the value and exchanges it.
97    pub fn compare_exchange(
98        &self,
99        current: bool,
100        new: bool,
101        _: Ordering,
102        _: Ordering,
103    ) -> Result<bool, bool> {
104        todo!("Implement atomic compare_exchange for bool");
105    }
106}