Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an atomic-polyfill optional dependancy #165

Merged
merged 2 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.9

- Added an `atomic-polyfill` optional dependency to compile `race` on platforms without atomics

## 1.8.0

- Add `try_insert` API -- a version of `set` that returns a reference.
Expand Down
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "once_cell"
version = "1.8.0"
version = "1.9.0"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
license = "MIT OR Apache-2.0"
edition = "2018"
Expand All @@ -24,6 +24,13 @@ members = ["xtask"]
# for up to 16 bytes smaller, depending on the size of the T.
parking_lot = { version = "0.11", optional = true, default_features = false }

# To be used in order to enable the race feature on targets
# that do not have atomics
# *Warning:* This can be unsound. Please read the README of
# [atomic-polyfill](https://github.com/embassy-rs/atomic-polyfill)
# and make sure you understand all the implications
atomic-polyfill = { version = "0.1", optional = true }

[dev-dependencies]
lazy_static = "1.0.0"
crossbeam-utils = "0.7.2"
Expand Down
25 changes: 15 additions & 10 deletions src/race.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
//!
//! This module does not require `std` feature.

use core::{
num::NonZeroUsize,
sync::atomic::{AtomicUsize, Ordering},
};
#[cfg(feature = "atomic-polyfill")]
use atomic_polyfill as atomic;
#[cfg(not(feature = "atomic-polyfill"))]
use core::sync::atomic;

use atomic::{AtomicUsize, Ordering};
use core::num::NonZeroUsize;

/// A thread-safe cell which can be written to only once.
#[derive(Default, Debug)]
Expand Down Expand Up @@ -160,21 +163,23 @@ pub use self::once_box::OnceBox;

#[cfg(feature = "alloc")]
mod once_box {
use core::{
marker::PhantomData,
ptr,
sync::atomic::{AtomicPtr, Ordering},
};
use super::atomic::{AtomicPtr, Ordering};
use core::{marker::PhantomData, ptr};

use alloc::boxed::Box;

/// A thread-safe cell which can be written to only once.
#[derive(Debug)]
pub struct OnceBox<T> {
inner: AtomicPtr<T>,
ghost: PhantomData<Option<Box<T>>>,
}

impl<T> core::fmt::Debug for OnceBox<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "OnceBox({:?})", self.inner.load(Ordering::Relaxed))
}
}

impl<T> Default for OnceBox<T> {
fn default() -> Self {
Self::new()
Expand Down