Skip to content

Commit 86e74bb

Browse files
1.2.0 release
1 parent e6ccedb commit 86e74bb

File tree

7 files changed

+80
-10
lines changed

7 files changed

+80
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
9+
## 1.2.0 - 2024-09-17
10+
11+
- Switch to `portable-atomic`
12+
813
## 1.1.0 - 2024-08-19
914

1015
- Add async functionality

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "async-pool"
3-
version = "1.1.1"
3+
version = "1.2.0"
44
authors = ["Daniel Stuart <[email protected]>"]
55
description = "Statically allocated pool providing a std-like Box, with async functionality."
66
repository = "https://github.com/danielstuart14/async-pool"

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,38 @@
22

33
[![Documentation](https://docs.rs/async-pool/badge.svg)](https://docs.rs/async-pool)
44

5-
Statically allocated pool providing a std-like Box, with hability to asynchronously wait for a pool slot to become available.
5+
Statically allocated pool providing a std-like Box, allowing to asynchronously await for a pool slot to become available.
66

7-
This crate is tailored to be used with no-std async runtimes, like [Embassy](https://embassy.dev/), but can also be used in std environments (check examples).
7+
It is tailored to be used with no-std async runtimes, like [Embassy](https://embassy.dev/), but can also be used in std environments (check examples).
8+
9+
The most common use-case is sharing large memory regions on constrained devices (e.g. microcontrollers), where multiple tasks may need to use the memory for buffering an I/O or performing calculations, and having separate static buffers would be too costly.
10+
11+
It is important to know that waiting forever for a memory slot to be available may dead-lock your code if done wrong. With that in mind, you should consider using a timeout when allocating asynchronously (e.g. [embassy_time::with_timeout](https://docs.rs/embassy-time/0.3.2/embassy_time/fn.with_timeout.html)).
812

913
## Dependencies
1014

11-
This crate uses the `AtomicWaker` functionality from `embassy-sync` crate, which in turn requires a critical section implementation. Check [critical-section](https://crates.io/crates/critical-section).
15+
This crate requires a critical section implementation. Check [critical-section](https://crates.io/crates/critical-section).
16+
17+
## Example
18+
19+
```
20+
use async_pool::{pool, Box};
21+
22+
struct Buffer([u8; 256]);
23+
24+
// A maximum of 2 Packet instances can be allocated at a time.
25+
// A maximum of 3 futures can be waiting at a time.
26+
pool!(BufferPool: [Buffer; 2], 3);
27+
28+
async fn run() {
29+
// Allocate non-blocking (will return None if no data slot is available)
30+
let box1 = Box::<BufferPool>::new(Buffer([0; 256]));
31+
32+
// Allocate asynchronously (will wait if no data slot is available)
33+
// This can return None if all future slots are taken
34+
let box2 = Box::<BufferPool>::new_async(Buffer([0; 256])).await;
35+
}
36+
```
1237

1338
## Previous work
1439

examples/tokio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This example demonstrates the use of the `async_pool` crate with the `tokio` framework.
22
3-
use tokio::time::sleep;
43
use tokio::join;
4+
use tokio::time::sleep;
55

66
use std::mem;
77
use std::time::Duration;
@@ -72,4 +72,4 @@ async fn main() {
7272
};
7373

7474
join!(fut1, fut2, fut3, fut4, fut5);
75-
}
75+
}

src/atomic_bitset.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ where
2020
}
2121

2222
pub fn alloc_droppable(&self) -> Option<droppable_bit::DroppableBit<N, K>> {
23-
self.alloc().map(|i| droppable_bit::DroppableBit::new(self, i))
23+
self.alloc()
24+
.map(|i| droppable_bit::DroppableBit::new(self, i))
2425
}
2526

2627
pub fn alloc(&self) -> Option<usize> {

src/atomic_bitset/droppable_bit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ mod test {
4949
v.pop();
5050
assert!(s.alloc().is_some());
5151
}
52-
}
52+
}

src/lib.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
1+
//! Statically allocated pool providing a std-like Box,
2+
//! allowing to asynchronously await for a pool slot to become available.
3+
//!
4+
//! It is tailored to be used with no-std async runtimes, like [Embassy](https://embassy.dev/), but
5+
//! can also be used in std environments (check examples).
6+
//!
7+
//! The most common use-case is sharing large memory regions on constrained
8+
//! devices (e.g. microcontrollers), where multiple tasks may need to use the
9+
//! memory for buffering an I/O or performing calculations, and having
10+
//! separate static buffers would be too costly.
11+
//!
12+
//! It is important to know that waiting forever for a memory slot to be
13+
//! available may dead-lock your code if done wrong. With that in mind,
14+
//! you should consider using a timeout when allocating asynchronously (e.g. [embassy_time::with_timeout](https://docs.rs/embassy-time/0.3.2/embassy_time/fn.with_timeout.html)).
15+
//!
16+
//! #### Dependencies
17+
//!
18+
//! This crate requires a critical section implementation. Check [critical-section](https://crates.io/crates/critical-section).
19+
//!
20+
//! #### Example
21+
//!
22+
//! ```
23+
//! use async_pool::{pool, Box};
24+
//!
25+
//!struct Buffer([u8; 256]);
26+
//!
27+
//!// A maximum of 2 Packet instances can be allocated at a time.
28+
//!// A maximum of 3 futures can be waiting at a time.
29+
//!pool!(BufferPool: [Buffer; 2], 3);
30+
//!
31+
//!async fn run() {
32+
//! // Allocate non-blocking (will return None if no data slot is available)
33+
//! let box1 = Box::<BufferPool>::new(Buffer([0; 256]));
34+
//!
35+
//! // Allocate asynchronously (will wait if no data slot is available)
36+
//! // This can return None if all future slots are taken
37+
//! let box2 = Box::<BufferPool>::new_async(Buffer([0; 256])).await;
38+
//!}
39+
//! ```
140
#![cfg_attr(not(test), no_std)]
241

342
mod atomic_bitset;
443

5-
use portable_atomic::AtomicU32;
644
use core::cell::UnsafeCell;
745
use core::future::{poll_fn, Future};
846
use core::hash::{Hash, Hasher};
@@ -11,6 +49,7 @@ use core::ops::{Deref, DerefMut};
1149
use core::task::Poll;
1250
use core::{cmp, mem, ptr::NonNull};
1351
use embassy_sync::waitqueue::AtomicWaker;
52+
use portable_atomic::AtomicU32;
1453

1554
use crate::atomic_bitset::AtomicBitset;
1655

@@ -299,7 +338,7 @@ where
299338
}
300339

301340
/// Create a item pool of a given type and size, as well as a waker pool of a given length.
302-
///
341+
///
303342
/// The waker pool is used to wake up tasks waiting for an item to become available in the data pool.
304343
/// Its length should be at least the number of tasks that can be waiting for an item at the same time.
305344
/// Example:

0 commit comments

Comments
 (0)