A flag type that supports waiting asynchronously

I feel like (but not overlooking it really) that depends on whether the Notify establishes some synchronization (or even has to establish synchronization, which might be the case). Looking into its source, I see it currently does some SeqCst accesses (which isn't always guaranteeing synchronization in both ways, btw., because stores synchronize-with loads, and not vice versa). Perhaps, if that (likely unneeded ordering) is partly relaxed in future, it might behave differently then?


I just found this possibly relevant comment:

// The compare-exchange from `NOTIFIED` -> `NOTIFIED` is intended. A
// happens-before synchronization must happen between this atomic
// operation and a task calling `notified().await`.
let new = set_state(curr, NOTIFIED);
let res = self.state.compare_exchange(curr, new, SeqCst, SeqCst);

So maybe you can rely on this synchronization taking place. :thinking: However, this talks about notified().await and not notified().

Note that between your atomic accesses, you have only a notified() and not a notified().await, so that comment on a happens-before synchronization might not apply there:

But I'm not sure really.

Update: Looks like the notified method does a load of that aforementioned compare-exchange:

    pub fn notified(&self) -> Notified<'_> {
        // we load the number of times notify_waiters
        // was called and store that in the future.
        let state = self.state.load(SeqCst);

So I would deduce that the notification happens-before the call of self.notify.notify() synchronizes-with the load of that value[1] during calling notify() here:

This could mean that your Relaxed load is sufficient. But again: totally not sure. :sweat_smile: I feel like it is sufficient that you use Relaxed, because it's the task of Notify to do that synchronization for you.

For definitions of happens-before and sychronize-with you can refer to the (non-official) C++ memory_order reference.


  1. as long as the load reads the particular value that was actually written during the compare-exchange ↩︎

1 Like