Skip to content

feat(cordyceps): Initial support for non-CAS targets #526

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

Merged
merged 8 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,26 @@ jobs:
- name: run Miri tests
run: just miri cordyceps

# run no-atomics target tests
cordyceps_no_atomics_compat:
needs: changed_paths
if: needs.changed_paths.outputs.should_skip != 'true' || !fromJSON(needs.changed_paths.outputs.paths_result).cordyceps.should_skip
runs-on: ubuntu-latest
name: no-atomics compat (cordyceps)
steps:
- name: install rust toolchain
run: rustup show
- uses: actions/checkout@v4
- name: run check
# we add the target HERE instead of in the 'install rust toolchain' step
# because we need the `rust-toolchain.toml` file to be checked out, otherwise
# the target is only added for `stable`. We could also add this to the
# `targets` item in the toolchain file, but that would be a waste for all
# other tests.
run: |
rustup target add thumbv6m-none-eabi
cargo check -p cordyceps --target thumbv6m-none-eabi

### maitake ###

# test with `--no-default-features`
Expand Down Expand Up @@ -313,6 +333,7 @@ jobs:
- docs
- cordyceps_loom
- cordyceps_miri
- cordyceps_no_atomics_compat
- maitake_no_default_features
- maitake_loom
- maitake_miri
Expand Down
15 changes: 15 additions & 0 deletions cordyceps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ than owning those values.
*must* be pinned in memory; they may not move (or be dropped) while linked
into an intrusive collection.

## Compatibility

Rudimentary support for targets without CAS (Compare and Swap) atomics, such as
Cortex-M0+/`thumbv6m-none-eabi`, is provided, however not all structures and
features may be available.

CAS atomic support is automatically detected with `cfg(target_has_atomic = "ptr")`,
which notes that a [platform has support] for both load/store operations as well
as support for CAS atomics.

No crate-level features are necessary to enable/disable structures that require
CAS atomics.

[platform has support]: https://doc.rust-lang.org/reference/conditional-compilation.html#r-cfg.target_has_atomic

## about the name

In keeping with Mycelium's fungal naming theme, _Cordyceps_ is a genus of
Expand Down
29 changes: 25 additions & 4 deletions cordyceps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
//! [`MpscQueue`]s can be used to efficiently share data from multiple
//! concurrent producers with a consumer.
//!
//! This structure is only available if the target supports CAS (Compare and
//! Swap) atomics.
//!
//! - **[`SortedList`]: a mutable, singly-linked list, with elements stored
//! in sorted order.**
//!
Expand Down Expand Up @@ -71,6 +74,9 @@
//! A [`TransferStack`] can be used to efficiently transfer ownership of
//! resources from multiple producers to a consumer, such as for reuse or
//! cleanup.
//!
//! This structure is only available if the target supports CAS (Compare and
//! Swap) atomics.
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(test)]
Expand All @@ -80,18 +86,33 @@ extern crate std;
pub(crate) mod util;

pub mod list;
pub mod mpsc_queue;
pub mod sorted_list;
pub mod stack;

#[doc(inline)]
pub use list::List;
#[doc(inline)]
pub use mpsc_queue::MpscQueue;
#[doc(inline)]
pub use sorted_list::{SortedList, SortedListIter};
#[doc(inline)]
pub use stack::{Stack, TransferStack};
pub use stack::Stack;

//
// The following items are only available if we have atomics
//
#[cfg(target_has_atomic = "ptr")]
pub mod mpsc_queue;

#[cfg(target_has_atomic = "ptr")]
pub use has_cas_atomics::*;

#[cfg(target_has_atomic = "ptr")]
mod has_cas_atomics {
#[doc(inline)]
pub use crate::mpsc_queue::MpscQueue;

#[doc(inline)]
pub use crate::stack::TransferStack;
}

pub(crate) mod loom;

Expand Down
12 changes: 12 additions & 0 deletions cordyceps/src/mpsc_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ use core::{
ptr::{self, NonNull},
};

macro_rules! feature {
(
#![$meta:meta]
$($item:item)*
) => {
$(
#[cfg($meta)]
$item
)*
}
}

/// A multi-producer, single-consumer (MPSC) queue, implemented using a
/// lock-free [intrusive] singly-linked list.
///
Expand Down
Loading
Loading