Skip to content

Commit f7ffe5b

Browse files
author
Stjepan Glavina
committed
Replace compare_exchange with swap
1 parent 5c5a518 commit f7ffe5b

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

src/libcore/sync/atomic.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -539,20 +539,15 @@ impl AtomicBool {
539539
// We can't use atomic_nand here because it can result in a bool with
540540
// an invalid value. This happens because the atomic operation is done
541541
// with an 8-bit integer internally, which would set the upper 7 bits.
542-
// So we just use fetch_xor or compare_exchange instead.
542+
// So we just use fetch_xor or swap instead.
543543
if val {
544544
// !(x & true) == !x
545545
// We must invert the bool.
546546
self.fetch_xor(true, order)
547547
} else {
548548
// !(x & false) == true
549-
// We must set the bool to true. Instead of delegating to swap or fetch_or, use
550-
// compare_exchange instead in order to avoid unnecessary writes to memory, which
551-
// might minimize cache-coherence traffic.
552-
match self.compare_exchange(false, true, order, Ordering::Relaxed) {
553-
Ok(_) => false,
554-
Err(_) => true,
555-
}
549+
// We must set the bool to true.
550+
self.swap(true, order)
556551
}
557552
}
558553

0 commit comments

Comments
 (0)