Skip to content

Commit

Permalink
Replace compare_exchange with swap
Browse files Browse the repository at this point in the history
  • Loading branch information
Stjepan Glavina committed Apr 7, 2017
1 parent 5c5a518 commit f7ffe5b
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/libcore/sync/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,20 +539,15 @@ impl AtomicBool {
// We can't use atomic_nand here because it can result in a bool with
// an invalid value. This happens because the atomic operation is done
// with an 8-bit integer internally, which would set the upper 7 bits.
// So we just use fetch_xor or compare_exchange instead.
// So we just use fetch_xor or swap instead.
if val {
// !(x & true) == !x
// We must invert the bool.
self.fetch_xor(true, order)
} else {
// !(x & false) == true
// We must set the bool to true. Instead of delegating to swap or fetch_or, use
// compare_exchange instead in order to avoid unnecessary writes to memory, which
// might minimize cache-coherence traffic.
match self.compare_exchange(false, true, order, Ordering::Relaxed) {
Ok(_) => false,
Err(_) => true,
}
// We must set the bool to true.
self.swap(true, order)
}
}

Expand Down

0 comments on commit f7ffe5b

Please sign in to comment.