The fallback implementation ignores the provided Ordering and always locks and unlocks the lock with Acquire and Release operations. However, this may not be sufficient to provide sequential consistency when requested by the user.
For example, this code:
fn main() {
let a = Atomic::<u128>::new(0);
let b = Atomic::<u128>::new(0);
let c = Atomic::<u8>::new(0);
std::thread::scope(|s| {
s.spawn(|| { a.store(1, Ordering::SeqCst); });
s.spawn(|| { b.store(1, Ordering::SeqCst); });
s.spawn(|| {
if a.load(Ordering::SeqCst) > b.load(Ordering::SeqCst) {
c.fetch_add(1, Ordering::Relaxed);
}
});
s.spawn(|| {
if b.load(Ordering::SeqCst) > a.load(Ordering::SeqCst) {
c.fetch_add(1, Ordering::Relaxed);
}
});
});
println!("{}", c.load(Ordering::Relaxed));
}
should never print 2, because there should be a single total modification order of all SeqCst atomic writes that prevents one thread from seeing the write to a before the write to b, while the other sees the write to b before the write to a.
However, if Atomic<u128> uses the fallback implementation, each SeqCst operation will be replaced by an Acquire followed by a Release, as part of the spinlock code. I could be wrong, but I don't believe this is sufficient to prevent the program from printing 2. There is no happens-before relationship between the two stores, and I believe nothing else to enforce a globally consistent view across all threads. This wouldn't happen on x86 but it could potentially occur on certain weakly-ordered architectures like IBM Power.
Although it's unlikely many practical issues could come of this, I think it would be a good idea to use SeqCst orderings for the spinlocks whenever the ordering of the requested atomic operation is SeqCst. In such cases, I think it would be sufficient to use SeqCst only for the unlock operation; the lock operation could remain Acquire.
The fallback implementation ignores the provided
Orderingand always locks and unlocks the lock withAcquireandReleaseoperations. However, this may not be sufficient to provide sequential consistency when requested by the user.For example, this code:
should never print
2, because there should be a single total modification order of allSeqCstatomic writes that prevents one thread from seeing the write toabefore the write tob, while the other sees the write tobbefore the write toa.However, if
Atomic<u128>uses the fallback implementation, eachSeqCstoperation will be replaced by anAcquirefollowed by aRelease, as part of the spinlock code. I could be wrong, but I don't believe this is sufficient to prevent the program from printing2. There is no happens-before relationship between the two stores, and I believe nothing else to enforce a globally consistent view across all threads. This wouldn't happen on x86 but it could potentially occur on certain weakly-ordered architectures like IBM Power.Although it's unlikely many practical issues could come of this, I think it would be a good idea to use
SeqCstorderings for the spinlocks whenever the ordering of the requested atomic operation isSeqCst. In such cases, I think it would be sufficient to useSeqCstonly for the unlock operation; the lock operation could remainAcquire.