Skip to content

Commit 5daf664

Browse files
committed
Add features for queues.
1 parent c544954 commit 5daf664

File tree

5 files changed

+63
-43
lines changed

5 files changed

+63
-43
lines changed

CHANGELOG.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- Added `mpmc` and `spsc` features.
1213
- Added `bytes::Buf` and `bytes::BufMut` implementations for `Vec`.
1314
- Added `format` macro.
1415
- Added `String::from_utf16`.
@@ -89,13 +90,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
8990
- The `IndexMapValues` type is now inside the `index_map` module and has been renamed to `Values`.
9091
- The `IndexMapValuesMut` type is now inside the `index_map` module and has been renamed to `ValuesMut`.
9192
- The `histbuf` module has been renamed to `history_buf`.
92-
- The `HistoryBuffer` type has been renamed to `HistoryBuf`.
93-
- The `HistoryBufferView` type has been renamed to `HistoryBufView`.
94-
- The `OwnedHistBufStorage` type has been renamed to `OwnedHistoryBufStorage`.
95-
- The `ViewHistBufStorage` type has been renamed to `ViewHistoryBufStorage`.
96-
- The `MpMcQueue` type has been renamed to `Queue`.
97-
- The `MpMcQueueView` type has been renamed to `QueueView`.
98-
- The `MpMcQueueInner` type has been renamed to `QueueInner`.
93+
- The `HistoryBuffer` type has been renamed to `HistoryBuf`.
94+
- The `HistoryBufferView` type has been renamed to `HistoryBufView`.
95+
- The `OwnedHistBufStorage` type has been renamed to `OwnedHistoryBufStorage`.
96+
- The `ViewHistBufStorage` type has been renamed to `ViewHistoryBufStorage`.
97+
- The `MpMcQueue` type has been renamed to `Queue`.
98+
- The `MpMcQueueView` type has been renamed to `QueueView`.
99+
- The `MpMcQueueInner` type has been renamed to `QueueInner`.
99100
- Changed `Queue::split` to be `const`.
100101

101102
### Fixed
@@ -441,7 +442,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
441442

442443
- The `pool!` macro now accepts attributes.
443444

444-
- `mpmc::Q*` a family of fixed capacity multiple-producer multiple-consumer
445+
- `mpmc::Q*` a family of fixed capacity multi-producer multi-consumer
445446
lock-free queues.
446447

447448
### Changed

Cargo.toml

+29-16
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,53 @@ repository = "https://github.com/rust-embedded/heapless"
1515
version = "0.8.0"
1616

1717
[features]
18+
default = ["mpmc", "spsc"]
19+
20+
# Enable interoperability with `alloc` types.
21+
alloc = []
22+
1823
bytes = ["dep:bytes"]
1924

25+
# Enable the `mpmc` module.
26+
mpmc = ["portable-atomic?/require-cas"]
27+
# Enable larger sizes for `mpmc::Queue`.
28+
mpmc_large = []
29+
30+
# Enable the `spsc` module.
31+
spsc = []
32+
2033
# Enable polyfilling of atomics via `portable-atomic`.
2134
# `portable-atomic` polyfills some functionality by default, but to get full atomics you must
2235
# enable one of its features to tell it how to do it. See `portable-atomic` documentation for details.
2336
portable-atomic = ["dep:portable-atomic"]
2437

2538
# Enable polyfilling of atomics via portable-atomic, using critical section for locking
26-
portable-atomic-critical-section = ["dep:portable-atomic", "portable-atomic", "portable-atomic?/critical-section"]
39+
portable-atomic-critical-section = [
40+
"dep:portable-atomic",
41+
"portable-atomic",
42+
"portable-atomic?/critical-section",
43+
]
2744

2845
# Enable polyfilling of atomics via portable-atomic, using disabling interrupts for locking.
2946
# WARNING: this is only sound for single-core bare-metal privileged-mode targets!
30-
portable-atomic-unsafe-assume-single-core = ["dep:portable-atomic", "portable-atomic", "portable-atomic?/unsafe-assume-single-core"]
47+
portable-atomic-unsafe-assume-single-core = [
48+
"dep:portable-atomic",
49+
"portable-atomic",
50+
"portable-atomic?/unsafe-assume-single-core",
51+
]
3152

32-
# implement serde traits.
53+
# Implement `serde` traits.
3354
serde = ["dep:serde"]
34-
35-
# implement ufmt traits.
55+
# Implement `ufmt` traits.
3656
ufmt = ["dep:ufmt", "dep:ufmt-write"]
37-
38-
# Implement `defmt::Format`.
57+
# Implement `defmt` traits.
3958
defmt = ["dep:defmt"]
4059

41-
# Enable larger MPMC sizes.
42-
mpmc_large = []
43-
44-
# Implement some alloc Vec interoperability
45-
alloc = []
46-
4760
nightly = []
4861

4962
[dependencies]
5063
bytes = { version = "1", default-features = false, optional = true }
51-
portable-atomic = { version = "1.0", optional = true }
64+
portable-atomic = { version = "1.3", optional = true }
5265
hash32 = "0.3.0"
5366
serde = { version = "1", optional = true, default-features = false }
5467
ufmt = { version = "0.2", optional = true }
@@ -65,13 +78,13 @@ static_assertions = "1.1.0"
6578

6679
[package.metadata.docs.rs]
6780
features = [
81+
"alloc",
6882
"bytes",
83+
"mpmc_large",
6984
"ufmt",
7085
"serde",
7186
"defmt",
72-
"mpmc_large",
7387
"portable-atomic-critical-section",
74-
"alloc",
7588
]
7689
# for the pool module
7790
targets = ["i686-unknown-linux-gnu"]

src/lib.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@
117117
//! - [`IndexMap`]: A hash table.
118118
//! - [`IndexSet`]: A hash set.
119119
//! - [`LinearMap`]: A linear map.
120+
//! - [`mpmc::Queue`](mpmc): A lock-free multi-producer, multi-consumer queue.
121+
//! - [`spsc::Queue`](spsc): A lock-free single-producer, single-consumer queue.
120122
//! - [`SortedLinkedList`](sorted_linked_list::SortedLinkedList): A sorted linked list.
121123
//! - [`String`]: A string.
122124
//! - [`Vec`]: A vector.
123-
//! - [`mpmc::MpMcQueue`](mpmc): A lock-free multiple-producer, multiple-consumer queue.
124-
//! - [`spsc::Queue`](spsc): A lock-free single-producer, single-consumer queue.
125125
//!
126126
//! # Minimum Supported Rust Version (MSRV)
127127
//!
@@ -199,12 +199,15 @@ pub mod binary_heap;
199199
mod bytes;
200200
#[cfg(feature = "defmt")]
201201
mod defmt;
202-
#[cfg(any(
203-
// assume we have all atomics available if we're using portable-atomic
204-
feature = "portable-atomic",
205-
// target has native atomic CAS (mpmc_large requires usize, otherwise just u8)
206-
all(feature = "mpmc_large", target_has_atomic = "ptr"),
207-
all(not(feature = "mpmc_large"), target_has_atomic = "8")
202+
#[cfg(all(
203+
feature = "mpmc",
204+
any(
205+
// Assume all atomics are available if we're using `portable-atomic`.
206+
feature = "portable-atomic",
207+
// Target has native atomic CAS (`mpmc_large` requires `usize`, otherwise just `u8`).
208+
all(feature = "mpmc_large", target_has_atomic = "ptr"),
209+
all(not(feature = "mpmc_large"), target_has_atomic = "8"),
210+
)
208211
))]
209212
pub mod mpmc;
210213
#[cfg(any(
@@ -223,14 +226,17 @@ pub mod mpmc;
223226
))]
224227
pub mod pool;
225228
pub mod sorted_linked_list;
226-
#[cfg(any(
227-
// assume we have all atomics available if we're using portable-atomic
228-
feature = "portable-atomic",
229-
// target has native atomic CAS. Note this is too restrictive, spsc requires load/store only, not CAS.
230-
// This should be `cfg(target_has_atomic_load_store)`, but that's not stable yet.
231-
target_has_atomic = "ptr",
232-
// or the current target is in a list in build.rs of targets known to have load/store but no CAS.
233-
has_atomic_load_store
229+
#[cfg(all(
230+
feature = "spsc",
231+
any(
232+
// Assume all atomics are available if we're using `portable-atomic`.
233+
feature = "portable-atomic",
234+
// Target has native atomic CAS. This is too restrictive since `spsc` only requires
235+
// load/store, but `cfg(target_has_atomic_load_store = "ptr")` is not stable yet.
236+
target_has_atomic = "ptr",
237+
// The current target is in a list in `build.rs` known to have load/store but no CAS.
238+
has_atomic_load_store,
239+
)
234240
))]
235241
pub mod spsc;
236242

src/mpmc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! A fixed capacity multiple-producer, multiple-consumer (MPMC) lock-free queue.
1+
//! A lock-free multi-producer/multi-consumer (MPMC) queue with fixed capacity.
22
//!
33
//! **Note:** This module requires atomic compare-and-swap (CAS) instructions. On
44
//! targets where they're not natively available, they are emulated by the

src/spsc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! A fixed capacity single-producer, single-consumer (SPSC) lock-free queue.
1+
//! A lock-free single-producer, single-consumer (SPSC) queue with fixed capacity.
22
//!
3-
//! *Note:* This module requires atomic load and store instructions. On
3+
//! **Note:** This module requires atomic load and store instructions. On
44
//! targets where they're not natively available, they are emulated by the
55
//! [`portable-atomic`](https://crates.io/crates/portable-atomic) crate.
66
//!

0 commit comments

Comments
 (0)