This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
library/std/src/thread/parker Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff 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().
1434impl Parker {
1535 #[ inline]
1636 pub const fn new ( ) -> Self {
You can’t perform that action at this time.
0 commit comments