Skip to content

Commit

Permalink
Always use atomic-polyfill with critical-section implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Sep 7, 2022
1 parent 62ae6ca commit 42e9c79
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -56,7 +56,7 @@ sync = []

parking_lot = ["parking_lot_core"]

critical-section = ["critical_section", "sync"]
critical-section = ["critical_section", "atomic-polyfill", "sync"]

[[example]]
name = "bench"
Expand Down
18 changes: 2 additions & 16 deletions src/imp_cs.rs
@@ -1,13 +1,11 @@
use core::panic::{RefUnwindSafe, UnwindSafe};

#[cfg(feature = "atomic-polyfill")]
use atomic_polyfill::{AtomicBool, Ordering};
use critical_section::{CriticalSection, Mutex};

use crate::unsync::OnceCell as UnsyncOnceCell;

pub(crate) struct OnceCell<T> {
#[cfg(feature = "atomic-polyfill")]
initialized: AtomicBool,
value: Mutex<UnsyncOnceCell<T>>,
}
Expand All @@ -25,30 +23,19 @@ impl<T: UnwindSafe> UnwindSafe for OnceCell<T> {}

impl<T> OnceCell<T> {
pub(crate) const fn new() -> OnceCell<T> {
OnceCell {
#[cfg(feature = "atomic-polyfill")]
initialized: AtomicBool::new(false),
value: Mutex::new(UnsyncOnceCell::new()),
}
OnceCell { initialized: AtomicBool::new(false), value: Mutex::new(UnsyncOnceCell::new()) }
}

pub(crate) const fn with_value(value: T) -> OnceCell<T> {
OnceCell {
#[cfg(feature = "atomic-polyfill")]
initialized: AtomicBool::new(true),
value: Mutex::new(UnsyncOnceCell::with_value(value)),
}
}

#[inline]
pub(crate) fn is_initialized(&self) -> bool {
#[cfg(feature = "atomic-polyfill")]
{
self.initialized.load(Ordering::Acquire)
}

#[cfg(not(feature = "atomic-polyfill"))]
critical_section::with(|cs| self.value.borrow(cs).get().is_some())
self.initialized.load(Ordering::Acquire)
}

#[cold]
Expand All @@ -59,7 +46,6 @@ impl<T> OnceCell<T> {
critical_section::with(|cs| {
let cell = self.value.borrow(cs);
cell.get_or_try_init(f).map(|_| {
#[cfg(feature = "atomic-polyfill")]
self.initialized.store(true, Ordering::Release);

()
Expand Down
7 changes: 0 additions & 7 deletions xtask/src/main.rs
Expand Up @@ -46,13 +46,6 @@ fn try_main() -> Result<()> {
"cargo test --features 'unstable std critical-section' --no-default-features --release"
)
.run()?;

cmd!("cargo test --features 'unstable std atomic-polyfill critical-section' --no-default-features")
.run()?;
cmd!(
"cargo test --features 'unstable std atomic-polyfill critical-section' --no-default-features --release"
)
.run()?;
}

{
Expand Down

0 comments on commit 42e9c79

Please sign in to comment.