Open zpzigi754 opened 2 years ago
Correctness of Peterson's algorithm assumes that there is a total order among the instructions. This is not true in weak memory models such as C/C++/Rust's, because for example a load()
can read from a "stale" store()
. To make this work as expected, you will need to recover sequential consistency by passing SeqCst
instead of Acquire
and Release
.
But unfortunately, due to technical limitations, Loom does not understand SeqCst
in load()
, store()
, etc. methods of Atomic*
types. However, Loom does support fence(SeqCst)
. See https://github.com/tokio-rs/loom/issues/180 for details.
For Peterson's algorithm, you can simply insert fence(SeqCst)
between all the accesses.
I've used the loom to test peterson's algorithm.
Peterson's algorithm does not seem to provide mutual exclusion with the below memory ordering.
The below is the result.
How could I know in which order the code have been executed? Does the ones printed with the above
println!
macro represent the exact order which have been executed?How could the value of P1's
flag0_curr
containfalse
even after flag0 has been written astrue
by P0? Does the result mean that only P0'sturn
has been correctly stored while other stores have not been reflected?