Suppose we have a spinlock
implementation:
struct Lock {
locked : Atomic(bool),
}
Then an unlock function could be:
fun unlock(lock : &Lock) {
atomic_store(&lock.locked, false, release);
}
But what about lock
? Commonly, it uses a compare-and-swap like this:
fun lock(lock : &Lock) {
while atomic_compare_and_swap(&lock.locked, false, true, acquire) {}
}
But wouldn't a swap be enough for this? Something like this:
fun lock(lock : &Lock) {
while atomic_swap(&lock.locked, true, acquire) {}
}
Is there any problem with this?