Skip to content

Commit

Permalink
Merge pull request #690 from AstroHQ/dispatch_soundness_fix
Browse files Browse the repository at this point in the history
fix(dispatch2): Add `'static` bound to `exec_async`
  • Loading branch information
madsmtm authored Jan 12, 2025
2 parents f31d879 + a69ad08 commit 3d888a0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 7 additions & 0 deletions crates/dispatch2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed
- **BREAKING**: Use `extern "C-unwind"` instead of `extern "C"` in certain functions that required that.
- **BREAKING**: Use `isize` instead of `usize` in certain functions where that is more correct.
- **BREAKING**: An `F: 'static` bound was added to the following methods to make
sure any referenced values passed to them are borrowed for long
enough since they perform their `work` asynchronously (#689):
- `Group::exec_async`
- `Queue::exec_async`
- `Queue::barrier_async`
- `Queue::barrier_async_and_wait`


## 0.1.0 - 2022-10-02
Expand Down
4 changes: 3 additions & 1 deletion crates/dispatch2/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ impl Group {
/// Submit a function to a [Queue] and associates it with the [Group].
pub fn exec_async<F>(&self, queue: &Queue, work: F)
where
F: Send + FnOnce(),
// We need `'static` to make sure any referenced values are borrowed for
// long enough since `work` will be performed asynchronously.
F: Send + FnOnce() + 'static,
{
let work_boxed = Box::into_raw(Box::new(work)).cast::<c_void>();

Expand Down
12 changes: 9 additions & 3 deletions crates/dispatch2/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ impl Queue {
/// Submit a function for asynchronous execution on the [Queue].
pub fn exec_async<F>(&self, work: F)
where
F: Send + FnOnce(),
// We need `'static` to make sure any referenced values are borrowed for
// long enough since `work` will be performed asynchronously.
F: Send + FnOnce() + 'static,
{
let work_boxed = Box::into_raw(Box::new(work)).cast();

Expand Down Expand Up @@ -254,7 +256,9 @@ impl Queue {
/// Enqueue a barrier function for asynchronous execution on the [Queue] and return immediately.
pub fn barrier_async<F>(&self, work: F)
where
F: Send + FnOnce(),
// We need `'static` to make sure any referenced values are borrowed for
// long enough since `work` will be performed asynchronously.
F: Send + FnOnce() + 'static,
{
let work_boxed = Box::into_raw(Box::new(work)).cast();

Expand All @@ -276,7 +280,9 @@ impl Queue {
/// Submit a function for synchronous execution and mark the function as a barrier for subsequent concurrent tasks.
pub fn barrier_async_and_wait<F>(&self, work: F)
where
F: Send + FnOnce(),
// We need `'static` to make sure any referenced values are borrowed for
// long enough since `work` will be performed asynchronously.
F: Send + FnOnce() + 'static,
{
let work_boxed = Box::into_raw(Box::new(work)).cast();

Expand Down

0 comments on commit 3d888a0

Please sign in to comment.