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 support for platforms without atomic ptr #248

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

LinuxHeki
Copy link

@LinuxHeki LinuxHeki commented Sep 20, 2023

Add support for platforms without support for atomic operations on pointers (such as thumbv6m-none-eabi).

#[inline]
pub fn set(&self, value: NonZeroUsize) -> Result<(), ()> {
if self.inner.load(Ordering::Acquire) == 0 {
self.inner.store(value.get(), Ordering::Release);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this feels incorrect to me.

Consider threads A, B, C and the following sequence of events:

   A   |   B     |   C
       |         |
load 0 |         |
       | load 0  |
       | store 1 |
       |         | load 1
store 2|         |
       |         | load 2

Here, A and B race when initializing the inner, so C observes two different values of once cell, which violates the invariant that makes the whole construct a safe abstraction.

@LinuxHeki
Copy link
Author

True... Does that mean this crate can't be ported platforms without atomic ptr?

@taiki-e
Copy link

taiki-e commented Dec 16, 2023

platforms without atomic ptr

"critical-section" feature is a feature to use sync/race in such targets.

once_cell/Cargo.toml

Lines 48 to 51 in c48d3c2

# Uses `critical-section` to implement `sync` and `race` modules. in
# `#![no_std]` mode. Please read `critical-section` docs carefully
# before enabling this feature.
critical-section = ["dep:critical-section", "portable-atomic" ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants