Skip to content

Commit b56e329

Browse files
Merge #195
195: Add `no_std` implementation based on `critical-section`. r=matklad a=reitermarkus Add implementation based on `critical-section` for embedded targets. This started out using `critical_section::Mutex`, but tests were failing since `critical_section::with` blocks all threads during initialization, so I changed it to be basically the same as `imp_pl` without the `parking_lot` parts, since `atomic-polyfill` is based on `critical-section`. Depends on rust-embedded/critical-section#26. Co-authored-by: Markus Reiter <[email protected]>
2 parents 97edd07 + 7d9afdb commit b56e329

8 files changed

Lines changed: 177 additions & 51 deletions

File tree

‎Cargo.toml‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@ parking_lot_core = { version = "0.9.3", optional = true, default_features = fals
3232
# and make sure you understand all the implications
3333
atomic-polyfill = { version = "1", optional = true }
3434

35+
# Uses `critical-section` to implement `once_cell::sync::OnceCell`.
36+
critical_section = { package = "critical-section", version = "1", optional = true }
37+
3538
[dev-dependencies]
3639
lazy_static = "1.0.0"
3740
crossbeam-utils = "0.8.7"
3841
regex = "1.2.0"
42+
critical_section = { package = "critical-section", version = "1.1.1", features = ["std"] }
3943

4044
[features]
4145
default = ["std"]
4246
# Enables `once_cell::sync` module.
43-
std = ["alloc"]
47+
std = ["alloc", "sync"]
4448
# Enables `once_cell::race::OnceBox` type.
4549
alloc = ["race"]
4650
# Enables `once_cell::race` module.
@@ -49,8 +53,12 @@ race = []
4953
# At the moment, this feature is unused.
5054
unstable = []
5155

56+
sync = []
57+
5258
parking_lot = ["parking_lot_core"]
5359

60+
critical-section = ["critical_section", "atomic-polyfill", "sync"]
61+
5462
[[example]]
5563
name = "bench"
5664
required-features = ["std"]

‎src/imp_cs.rs‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use core::panic::{RefUnwindSafe, UnwindSafe};
2+
3+
use atomic_polyfill::{AtomicBool, Ordering};
4+
use critical_section::{CriticalSection, Mutex};
5+
6+
use crate::unsync;
7+
8+
pub(crate) struct OnceCell<T> {
9+
initialized: AtomicBool,
10+
// Use `unsync::OnceCell` internally since `Mutex` does not provide
11+
// interior mutability and to be able to re-use `get_or_try_init`.
12+
value: Mutex<unsync::OnceCell<T>>,
13+
}
14+
15+
// Why do we need `T: Send`?
16+
// Thread A creates a `OnceCell` and shares it with
17+
// scoped thread B, which fills the cell, which is
18+
// then destroyed by A. That is, destructor observes
19+
// a sent value.
20+
unsafe impl<T: Sync + Send> Sync for OnceCell<T> {}
21+
unsafe impl<T: Send> Send for OnceCell<T> {}
22+
23+
impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceCell<T> {}
24+
impl<T: UnwindSafe> UnwindSafe for OnceCell<T> {}
25+
26+
impl<T> OnceCell<T> {
27+
pub(crate) const fn new() -> OnceCell<T> {
28+
OnceCell { initialized: AtomicBool::new(false), value: Mutex::new(unsync::OnceCell::new()) }
29+
}
30+
31+
pub(crate) const fn with_value(value: T) -> OnceCell<T> {
32+
OnceCell {
33+
initialized: AtomicBool::new(true),
34+
value: Mutex::new(unsync::OnceCell::with_value(value)),
35+
}
36+
}
37+
38+
#[inline]
39+
pub(crate) fn is_initialized(&self) -> bool {
40+
self.initialized.load(Ordering::Acquire)
41+
}
42+
43+
#[cold]
44+
pub(crate) fn initialize<F, E>(&self, f: F) -> Result<(), E>
45+
where
46+
F: FnOnce() -> Result<T, E>,
47+
{
48+
critical_section::with(|cs| {
49+
let cell = self.value.borrow(cs);
50+
cell.get_or_try_init(f).map(|_| {
51+
self.initialized.store(true, Ordering::Release);
52+
})
53+
})
54+
}
55+
56+
/// Get the reference to the underlying value, without checking if the cell
57+
/// is initialized.
58+
///
59+
/// # Safety
60+
///
61+
/// Caller must ensure that the cell is in initialized state, and that
62+
/// the contents are acquired by (synchronized to) this thread.
63+
pub(crate) unsafe fn get_unchecked(&self) -> &T {
64+
debug_assert!(self.is_initialized());
65+
// SAFETY: The caller ensures that the value is initialized and access synchronized.
66+
self.value.borrow(CriticalSection::new()).get_unchecked()
67+
}
68+
69+
#[inline]
70+
pub(crate) fn get_mut(&mut self) -> Option<&mut T> {
71+
self.value.get_mut().get_mut()
72+
}
73+
74+
#[inline]
75+
pub(crate) fn into_inner(self) -> Option<T> {
76+
self.value.into_inner().into_inner()
77+
}
78+
}

‎src/imp_pl.rs‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
22
cell::UnsafeCell,
3-
hint,
43
panic::{RefUnwindSafe, UnwindSafe},
54
sync::atomic::{AtomicU8, Ordering},
65
};
@@ -101,15 +100,8 @@ impl<T> OnceCell<T> {
101100
/// the contents are acquired by (synchronized to) this thread.
102101
pub(crate) unsafe fn get_unchecked(&self) -> &T {
103102
debug_assert!(self.is_initialized());
104-
let slot: &Option<T> = &*self.value.get();
105-
match slot {
106-
Some(value) => value,
107-
// This unsafe does improve performance, see `examples/bench`.
108-
None => {
109-
debug_assert!(false);
110-
hint::unreachable_unchecked()
111-
}
112-
}
103+
let slot = &*self.value.get();
104+
crate::unwrap_unchecked(slot.as_ref())
113105
}
114106

115107
/// Gets the mutable reference to the underlying value.

‎src/imp_std.rs‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55

66
use std::{
77
cell::{Cell, UnsafeCell},
8-
hint::unreachable_unchecked,
98
marker::PhantomData,
109
panic::{RefUnwindSafe, UnwindSafe},
1110
sync::atomic::{AtomicBool, AtomicPtr, Ordering},
1211
thread::{self, Thread},
1312
};
1413

15-
use crate::take_unchecked;
16-
1714
#[derive(Debug)]
1815
pub(crate) struct OnceCell<T> {
1916
// This `queue` field is the core of the implementation. It encodes two
@@ -81,7 +78,7 @@ impl<T> OnceCell<T> {
8178
initialize_or_wait(
8279
&self.queue,
8380
Some(&mut || {
84-
let f = unsafe { take_unchecked(&mut f) };
81+
let f = unsafe { crate::take_unchecked(&mut f) };
8582
match f() {
8683
Ok(value) => {
8784
unsafe { *slot = Some(value) };
@@ -111,15 +108,8 @@ impl<T> OnceCell<T> {
111108
/// the contents are acquired by (synchronized to) this thread.
112109
pub(crate) unsafe fn get_unchecked(&self) -> &T {
113110
debug_assert!(self.is_initialized());
114-
let slot: &Option<T> = &*self.value.get();
115-
match slot {
116-
Some(value) => value,
117-
// This unsafe does improve performance, see `examples/bench`.
118-
None => {
119-
debug_assert!(false);
120-
unreachable_unchecked()
121-
}
122-
}
111+
let slot = &*self.value.get();
112+
crate::unwrap_unchecked(slot.as_ref())
123113
}
124114

125115
/// Gets the mutable reference to the underlying value.

‎src/lib.rs‎

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -332,25 +332,29 @@
332332
#[cfg(feature = "alloc")]
333333
extern crate alloc;
334334

335-
#[cfg(feature = "std")]
336-
#[cfg(feature = "parking_lot")]
335+
#[cfg(all(feature = "critical-section", not(feature = "std")))]
336+
#[path = "imp_cs.rs"]
337+
mod imp;
338+
339+
#[cfg(all(feature = "std", feature = "parking_lot"))]
337340
#[path = "imp_pl.rs"]
338341
mod imp;
339342

340-
#[cfg(feature = "std")]
341-
#[cfg(not(feature = "parking_lot"))]
343+
#[cfg(all(feature = "std", not(feature = "parking_lot")))]
342344
#[path = "imp_std.rs"]
343345
mod imp;
344346

345347
/// Single-threaded version of `OnceCell`.
346348
pub mod unsync {
347349
use core::{
348350
cell::{Cell, UnsafeCell},
349-
fmt, hint, mem,
351+
fmt, mem,
350352
ops::{Deref, DerefMut},
351353
panic::{RefUnwindSafe, UnwindSafe},
352354
};
353355

356+
use super::unwrap_unchecked;
357+
354358
/// A cell which can be written to only once. It is not thread safe.
355359
///
356360
/// Unlike [`std::cell::RefCell`], a `OnceCell` provides simple `&`
@@ -442,6 +446,7 @@ pub mod unsync {
442446
/// Gets a reference to the underlying value.
443447
///
444448
/// Returns `None` if the cell is empty.
449+
#[inline]
445450
pub fn get(&self) -> Option<&T> {
446451
// Safe due to `inner`'s invariant
447452
unsafe { &*self.inner.get() }.as_ref()
@@ -463,11 +468,24 @@ pub mod unsync {
463468
/// *cell.get_mut().unwrap() = 93;
464469
/// assert_eq!(cell.get(), Some(&93));
465470
/// ```
471+
#[inline]
466472
pub fn get_mut(&mut self) -> Option<&mut T> {
467473
// Safe because we have unique access
468474
unsafe { &mut *self.inner.get() }.as_mut()
469475
}
470476

477+
/// Get the reference to the underlying value, without checking if the
478+
/// cell is initialized.
479+
///
480+
/// # Safety
481+
///
482+
/// Caller must ensure that the cell is in initialized state.
483+
#[cfg(feature = "critical-section")]
484+
#[inline]
485+
pub(crate) unsafe fn get_unchecked(&self) -> &T {
486+
crate::unwrap_unchecked(self.get())
487+
}
488+
471489
/// Sets the contents of this cell to `value`.
472490
///
473491
/// Returns `Ok(())` if the cell was empty and `Err(value)` if it was
@@ -510,16 +528,14 @@ pub mod unsync {
510528
if let Some(old) = self.get() {
511529
return Err((old, value));
512530
}
531+
513532
let slot = unsafe { &mut *self.inner.get() };
514533
// This is the only place where we set the slot, no races
515534
// due to reentrancy/concurrency are possible, and we've
516535
// checked that slot is currently `None`, so this write
517536
// maintains the `inner`'s invariant.
518537
*slot = Some(value);
519-
Ok(match &*slot {
520-
Some(value) => value,
521-
None => unsafe { hint::unreachable_unchecked() },
522-
})
538+
Ok(unsafe { unwrap_unchecked(slot.as_ref()) })
523539
}
524540

525541
/// Gets the contents of the cell, initializing it with `f`
@@ -592,7 +608,7 @@ pub mod unsync {
592608
// `assert`, while keeping `set/get` would be sound, but it seems
593609
// better to panic, rather than to silently use an old value.
594610
assert!(self.set(val).is_ok(), "reentrant init");
595-
Ok(self.get().unwrap())
611+
Ok(unsafe { unwrap_unchecked(self.get()) })
596612
}
597613

598614
/// Takes the value out of this `OnceCell`, moving it back to an uninitialized state.
@@ -814,16 +830,16 @@ pub mod unsync {
814830
}
815831

816832
/// Thread-safe, blocking version of `OnceCell`.
817-
#[cfg(feature = "std")]
833+
#[cfg(feature = "sync")]
818834
pub mod sync {
819-
use std::{
835+
use core::{
820836
cell::Cell,
821837
fmt, mem,
822838
ops::{Deref, DerefMut},
823839
panic::RefUnwindSafe,
824840
};
825841

826-
use crate::{imp::OnceCell as Imp, take_unchecked};
842+
use super::{imp::OnceCell as Imp, take_unchecked};
827843

828844
/// A thread-safe cell which can be written to only once.
829845
///
@@ -942,8 +958,9 @@ pub mod sync {
942958
/// // Will return 92, but might block until the other thread does `.set`.
943959
/// let value: &u32 = cell.wait();
944960
/// assert_eq!(*value, 92);
945-
/// t.join().unwrap();;
961+
/// t.join().unwrap();
946962
/// ```
963+
#[cfg(not(feature = "critical-section"))]
947964
pub fn wait(&self) -> &T {
948965
if !self.0.is_initialized() {
949966
self.0.wait()
@@ -969,6 +986,7 @@ pub mod sync {
969986
/// cell.set(92).unwrap();
970987
/// cell = OnceCell::new();
971988
/// ```
989+
#[inline]
972990
pub fn get_mut(&mut self) -> Option<&mut T> {
973991
self.0.get_mut()
974992
}
@@ -980,6 +998,7 @@ pub mod sync {
980998
///
981999
/// Caller must ensure that the cell is in initialized state, and that
9821000
/// the contents are acquired by (synchronized to) this thread.
1001+
#[inline]
9831002
pub unsafe fn get_unchecked(&self) -> &T {
9841003
self.0.get_unchecked()
9851004
}
@@ -1109,6 +1128,7 @@ pub mod sync {
11091128
if let Some(value) = self.get() {
11101129
return Ok(value);
11111130
}
1131+
11121132
self.0.initialize(f)?;
11131133

11141134
// Safe b/c value is initialized.
@@ -1164,6 +1184,7 @@ pub mod sync {
11641184
/// cell.set("hello".to_string()).unwrap();
11651185
/// assert_eq!(cell.into_inner(), Some("hello".to_string()));
11661186
/// ```
1187+
#[inline]
11671188
pub fn into_inner(self) -> Option<T> {
11681189
self.0.into_inner()
11691190
}
@@ -1356,13 +1377,19 @@ pub mod sync {
13561377
#[cfg(feature = "race")]
13571378
pub mod race;
13581379

1359-
#[cfg(feature = "std")]
1380+
#[cfg(feature = "sync")]
1381+
#[inline]
13601382
unsafe fn take_unchecked<T>(val: &mut Option<T>) -> T {
1361-
match val.take() {
1362-
Some(it) => it,
1383+
unwrap_unchecked(val.take())
1384+
}
1385+
1386+
#[inline]
1387+
unsafe fn unwrap_unchecked<T>(val: Option<T>) -> T {
1388+
match val {
1389+
Some(value) => value,
13631390
None => {
13641391
debug_assert!(false);
1365-
std::hint::unreachable_unchecked()
1392+
core::hint::unreachable_unchecked()
13661393
}
13671394
}
13681395
}

‎src/race.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ impl OnceBool {
165165
fn from_usize(value: NonZeroUsize) -> bool {
166166
value.get() == 1
167167
}
168+
168169
#[inline]
169170
fn to_usize(value: bool) -> NonZeroUsize {
170171
unsafe { NonZeroUsize::new_unchecked(if value { 1 } else { 2 }) }

0 commit comments

Comments
 (0)