Skip to content

atomics: Add support for targets without atomics - #413

Merged
sfackler merged 5 commits into
rust-lang:masterfrom
alistair23:alistair/atomics
Sep 22, 2020
Merged

atomics: Add support for targets without atomics#413
sfackler merged 5 commits into
rust-lang:masterfrom
alistair23:alistair/atomics

Conversation

@alistair23

Copy link
Copy Markdown
Contributor

Some targets (such as RV32IMC) don't have atomic support. This PR allows the log crate to build for those targets.

As the platform doesn't have atomics we can't implement an Atomic version of AtomicUsize, so this version is not atomic. Any platform without atomics is unlikely to have multiple cores, so this shouldn't be a problem.

This is somewhat based on implementations in: https://github.com/japaric/heapless

Based on the implementation in the headless
crate (https://github.com/japaric/heapless/blob/master/build.rs#L26) let's
not enable CAS for three more targets.
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
Some platforms don't provide a AtomicUsize. Instead of just failing to
build with this error:
291 | use std::sync::atomic::{AtomicUsize, Ordering};
| ^^^^^^^^^^^ no `AtomicUsize` in `sync::atomic`
let's instead add a fake AtomicUsize.
As the platform doesn't have atomics we can't implement an Atomic
version of AtomicUsize, so this version is not atomic. Any platform
without atomics is unlikely to have multiple cores, so this shouldn't be
a problem.
This is somewhat based on:
rust-embedded/heapless@940d2e9
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
@alistair23

Copy link
Copy Markdown
ContributorAuthor

Ping

@sfackler

Copy link
Copy Markdown
Member

This seems like a plausible approach, though I think we'll need to have CI target at least one of these architectures so the not(has_atomics) code doesn't rot.

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
@alistair23

Copy link
Copy Markdown
ContributorAuthor

Done, I have added a CI test to build RISC-V without atomics.

@sfackler

Copy link
Copy Markdown
Member

Thanks!

cc @KodrAus does this seem okay to you as well? It's a bit scary, but it seems reasonable that a platform with no atomics can't really have threads either.

@dtolnaydtolnay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would like the rationale from the PR description to be documented on the unsafe impl.

// Any platform without atomics is unlikely to have multiple cores, so// writing via Cell will not be a race condition.

Comment threadbuild.rs Outdated
@KodrAus

Copy link
Copy Markdown
Contributor

I think I'd feel just a bit safer if we encapsulated the global logger code into a module, so that we limit the reachability of our "fake" AtomicUsize as much as possible, but I don't think that's a blocker.

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
@alistair23

Copy link
Copy Markdown
ContributorAuthor

I think I have addressed all comments, let me know if there is anything else.

@sfackler
sfackler merged commit 9a1902d into rust-lang:masterSep 22, 2020
@sfackler

Copy link
Copy Markdown
Member

Thanks!

@alistair23
alistair23 deleted the alistair/atomics branch September 23, 2020 00:04
@alistair23

Copy link
Copy Markdown
ContributorAuthor

Thanks for merging. Do you know when the next release will be?

@KodrAusKodrAus mentioned this pull request Dec 23, 2020
EFanZh pushed a commit to EFanZh/log that referenced this pull request Jul 23, 2023
* Bump dep cargo_toml to v0.12.0
* FIx compilation error
* Fix test parse-meta
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
Sign up for freeto 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.

4 participants

@alistair23@sfackler@KodrAus@dtolnay