Skip to content

Commit b0639c8

Browse files
committed
Expand notes on r_task() safety
1 parent 5a99a7e commit b0639c8

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

crates/ark/src/r_task.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ extern "C" {
2222

2323
type SharedOption<T> = Arc<Mutex<Option<T>>>;
2424

25+
// The `Send` bound on `F` is necessary for safety. Although we are not
26+
// worried about data races since control flow from one thread to the other
27+
// is sequential, objects captured by `f` might have implementations
28+
// sensitive to some thread state (ID, thread-local storage, etc).
29+
2530
pub fn r_task<'env, F, T>(f: F) -> T
2631
where
2732
F: FnOnce() -> T,
@@ -66,7 +71,9 @@ where
6671
*result.lock().unwrap() = Some(res);
6772
};
6873

69-
// Move `f` to heap and erase its lifetime
74+
// Move `f` to heap and erase its lifetime so we can send it to
75+
// another thread. It is safe to do so because we block in this
76+
// scope until the closure has finished running.
7077
let closure: Box<dyn FnOnce() + 'env + Send> = Box::new(closure);
7178
let closure: Box<dyn FnOnce() + Send + 'static> = unsafe { std::mem::transmute(closure) };
7279

0 commit comments

Comments
 (0)