Skip to content

Commit 4301b5c

Browse files
committed
Add notes about memory ordering to futex parker implementation.
1 parent 485f882 commit 4301b5c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

library/std/src/thread/parker/futex.rs

+20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ pub struct Parker {
1111
state: AtomicI32,
1212
}
1313

14+
// Notes about memory ordering:
15+
//
16+
// Memory ordering is only relevant for the relative ordering of operations
17+
// between different variables. Even Ordering::Relaxed guarantees a
18+
// monotonic/consistent order when looking at just a single atomic variable.
19+
//
20+
// So, since this parker is just a single atomic variable, we only need to look
21+
// at the ordering guarantees we need to provide to the 'outside world'.
22+
//
23+
// The only memory ordering guarantee that parking and unparking provide, is
24+
// that things which happened before unpark() are visible on the thread
25+
// returning from park() afterwards. Otherwise, it was effectively unparked
26+
// before unpark() was called while still consuming the 'token'.
27+
//
28+
// In other words, unpark() needs to synchronize with the part of park() that
29+
// consumes the token and returns.
30+
//
31+
// This is done with a release-acquire synchronization, by using
32+
// Ordering::Release when writing NOTIFIED (the 'token') in unpark(), and using
33+
// Ordering::Acquire when checking for this state in park().
1434
impl Parker {
1535
#[inline]
1636
pub const fn new() -> Self {

0 commit comments

Comments
 (0)