Skip to content

Commit 5d1af26

Browse files
committed
zephyr: device: Use AtomicBool instead of AtomicUsize
Change the AtomicUsize to an AtomicBool to clarify the use. The flag is still stored inverted, so false is the correct initializer, and we will still initialize from zero-initted memory. Signed-off-by: David Brown <[email protected]>
1 parent 301ccb7 commit 5d1af26

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

zephyr/src/device.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//!
66
//! Most of these instances come from the device tree.
77
8-
use crate::sync::atomic::{AtomicUsize, Ordering};
8+
use crate::sync::atomic::{AtomicBool, Ordering};
99

1010
pub mod gpio;
1111
pub mod flash;
@@ -21,12 +21,14 @@ pub mod flash;
2121
/// driver will be shared among then. Generally, the constructor for the individual device will
2222
/// call `get_instance_raw()` on the underlying device.
2323
#[allow(dead_code)]
24-
pub(crate) struct Unique(pub(crate) AtomicUsize);
24+
pub(crate) struct Unique(pub(crate) AtomicBool);
2525

2626
impl Unique {
27+
// Note that there are circumstances where these are in zero-initialized memory, so false must
28+
// be used here, and the result of `once` inverted.
2729
/// Construct a new unique counter.
2830
pub(crate) const fn new() -> Unique {
29-
Unique(AtomicUsize::new(0))
31+
Unique(AtomicBool::new(false))
3032
}
3133

3234
/// Indicates if this particular entity can be used. This function, on a given `Unique` value
@@ -35,6 +37,6 @@ impl Unique {
3537
pub(crate) fn once(&self) -> bool {
3638
// `fetch_add` is likely to be faster than compare_exchage. This does have the limitation
3739
// that `once` is not called more than `usize::MAX` times.
38-
self.0.fetch_add(1, Ordering::AcqRel) == 0
40+
!self.0.fetch_or(true, Ordering::AcqRel)
3941
}
4042
}

0 commit comments

Comments
 (0)