Skip to content

Commit 340cfb9

Browse files
committed
zephyr: Make symbols from portable-atomic available
Provide the atomic types from portable-atomic in `zephyr::sync::atomic`, and, if allocation is enabled, provide `zephyr::sync::Arc`. The `alloc::arc` will only be available on Zephyr targets that have atomic intrinsics, and the portable one can be supported on any Zephyr target. There are some limitations described in the documentation. Signed-off-by: David Brown <[email protected]>
1 parent c88376c commit 340cfb9

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

zephyr/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#![no_std]
8585
#![allow(unexpected_cfgs)]
8686

87+
pub mod sync;
8788
pub mod sys;
8889
pub mod time;
8990

zephyr/src/sync.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//! Higher level synchronization primitives.
2+
//!
3+
//! These are modeled after the synchronization primitives in
4+
//! [`std::sync`](https://doc.rust-lang.org/stable/std/sync/index.html) and those from
5+
//! [`crossbeam-channel`](https://docs.rs/crossbeam-channel/latest/crossbeam_channel/), in as much
6+
//! as it makes sense.
7+
8+
pub mod atomic {
9+
//! Re-export portable atomic.
10+
//!
11+
//! Although `core` contains a
12+
//! [`sync::atomic`](https://doc.rust-lang.org/stable/core/sync/atomic/index.html) module,
13+
//! these are dependent on the target having atomic instructions, and the types are missing
14+
//! when the platform cannot support them. Zephyr, however, does provide atomics on platforms
15+
//! that don't support atomic instructions, using spinlocks. In the Rust-embedded world, this
16+
//! is done through the [`portable-atomic`](https://crates.io/crates/portable-atomic) crate,
17+
//! which will either just re-export the types from core, or provide an implementation using
18+
//! spinlocks when those aren't available.
19+
20+
pub use portable_atomic::*;
21+
}
22+
23+
/// Re-export Arc from the portable-atomic library.
24+
///
25+
/// Note that the `Arc` from portable-atomic is missing a few features from the one from `alloc`,
26+
/// notably the ability to upcast Arcs. This can be worked around by putting the object in a
27+
/// `Box`, with the downcasting, and then using `Arc::from` to move the object from the box into
28+
/// the Arc.
29+
#[cfg(CONFIG_RUST_ALLOC)]
30+
pub use portable_atomic_util::Arc;

0 commit comments

Comments
 (0)