Skip to content

Commit

Permalink
zephyr: Make symbols from portable-atomic available
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
d3zd3z committed Sep 17, 2024
1 parent c88376c commit 340cfb9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions zephyr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#![no_std]
#![allow(unexpected_cfgs)]

pub mod sync;
pub mod sys;
pub mod time;

Expand Down
30 changes: 30 additions & 0 deletions zephyr/src/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! Higher level synchronization primitives.
//!
//! These are modeled after the synchronization primitives in
//! [`std::sync`](https://doc.rust-lang.org/stable/std/sync/index.html) and those from
//! [`crossbeam-channel`](https://docs.rs/crossbeam-channel/latest/crossbeam_channel/), in as much
//! as it makes sense.
pub mod atomic {
//! Re-export portable atomic.
//!
//! Although `core` contains a
//! [`sync::atomic`](https://doc.rust-lang.org/stable/core/sync/atomic/index.html) module,
//! these are dependent on the target having atomic instructions, and the types are missing
//! when the platform cannot support them. Zephyr, however, does provide atomics on platforms
//! that don't support atomic instructions, using spinlocks. In the Rust-embedded world, this
//! is done through the [`portable-atomic`](https://crates.io/crates/portable-atomic) crate,
//! which will either just re-export the types from core, or provide an implementation using
//! spinlocks when those aren't available.
pub use portable_atomic::*;
}

/// Re-export Arc from the portable-atomic library.
///
/// Note that the `Arc` from portable-atomic is missing a few features from the one from `alloc`,
/// notably the ability to upcast Arcs. This can be worked around by putting the object in a
/// `Box`, with the downcasting, and then using `Arc::from` to move the object from the box into
/// the Arc.
#[cfg(CONFIG_RUST_ALLOC)]
pub use portable_atomic_util::Arc;

0 comments on commit 340cfb9

Please sign in to comment.