Is this a data race?

I can come up with an utterly insane but spec compliant atomic implementation that causes the assert to fail:

  • There is a global Mutex<()>.
  • Any atomic access must hold the lock to said mutex.
  • The atomic write first acquires the mutex lock, then (unsynchronized) zeroes the location, then (unsynchronized) writes the desired value, then unlocks the mutex.
  • The unsynchronized read happens between the two "halves" of the atomic write.
    • If the read were synchronized, it could not happen while the write to the location holds the lock, thus would be guaranteed to read the correct value
    • But because the read is not synchronized with a (potentially?) concurrent write, it can read any possible value (or be a UB data race, depending on your exact memory model (IIUC, rustc/LLVM says this is UB)) from the memory location.

(In practice the assert will probably be optimized out.)

1 Like