crossbeam icon indicating copy to clipboard operation
crossbeam copied to clipboard

Fine-tune the ordering for received

Open wang384670111 opened this issue 1 year ago • 2 comments
trafficstars

https://github.com/crossbeam-rs/crossbeam/blob/95d0bd04624473b546826fdb97f10abe849443e4/crossbeam-channel/src/flavors/at.rs#L51 https://github.com/crossbeam-rs/crossbeam/blob/95d0bd04624473b546826fdb97f10abe849443e4/crossbeam-channel/src/flavors/at.rs#L89 https://github.com/crossbeam-rs/crossbeam/blob/95d0bd04624473b546826fdb97f10abe849443e4/crossbeam-channel/src/flavors/at.rs#L120

I believe that using a Release operation in swap and an Acquire operation in load can effectively synchronize with the writing of the message to memory. Using SeqCst is not necessary in this context.

(glad to make a pull request if this seems sensible)

wang384670111 avatar Jan 07 '24 07:01 wang384670111

Similarly, in wake.rs, using SeqCst is unnecessary. Using Acquire/Release would be sufficient.

https://github.com/crossbeam-rs/crossbeam/blob/95d0bd04624473b546826fdb97f10abe849443e4/crossbeam-channel/src/waker.rs#L200-L203 https://github.com/crossbeam-rs/crossbeam/blob/95d0bd04624473b546826fdb97f10abe849443e4/crossbeam-channel/src/waker.rs#L211-L214 https://github.com/crossbeam-rs/crossbeam/blob/95d0bd04624473b546826fdb97f10abe849443e4/crossbeam-channel/src/waker.rs#L221 https://github.com/crossbeam-rs/crossbeam/blob/95d0bd04624473b546826fdb97f10abe849443e4/crossbeam-channel/src/waker.rs#L223 https://github.com/crossbeam-rs/crossbeam/blob/95d0bd04624473b546826fdb97f10abe849443e4/crossbeam-channel/src/waker.rs#L226-L229 https://github.com/crossbeam-rs/crossbeam/blob/95d0bd04624473b546826fdb97f10abe849443e4/crossbeam-channel/src/waker.rs#L239-L242 https://github.com/crossbeam-rs/crossbeam/blob/95d0bd04624473b546826fdb97f10abe849443e4/crossbeam-channel/src/waker.rs#L250-L253 https://github.com/crossbeam-rs/crossbeam/blob/95d0bd04624473b546826fdb97f10abe849443e4/crossbeam-channel/src/waker.rs#L261-L264

wang384670111 avatar Jan 07 '24 08:01 wang384670111

The SeqCst throughout crossbeam-channel is necessary to establish a total order between send/recv and is_empty. Without it the optimistic check in https://github.com/crossbeam-rs/crossbeam/blob/master/crossbeam-channel/src/waker.rs#L222 would be incoherent.

ibraheemdev avatar Feb 29 '24 17:02 ibraheemdev