From 340cfb92a8b461c7518f7f56c60f3a4f25a91678 Mon Sep 17 00:00:00 2001 From: David Brown Date: Tue, 17 Sep 2024 17:03:51 -0600 Subject: [PATCH] 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 --- zephyr/src/lib.rs | 1 + zephyr/src/sync.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 zephyr/src/sync.rs diff --git a/zephyr/src/lib.rs b/zephyr/src/lib.rs index 4b28bc90..a1016d7d 100644 --- a/zephyr/src/lib.rs +++ b/zephyr/src/lib.rs @@ -84,6 +84,7 @@ #![no_std] #![allow(unexpected_cfgs)] +pub mod sync; pub mod sys; pub mod time; diff --git a/zephyr/src/sync.rs b/zephyr/src/sync.rs new file mode 100644 index 00000000..b7a68d14 --- /dev/null +++ b/zephyr/src/sync.rs @@ -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;