2

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?

bzim
  • 1,002
  • 9
  • 17

1 Answers1

2

A compare-and-swap isn't really necessary. Atomically setting a flag true if it is false is logically equivalent to unconditionally setting it true.

An unconditional swap might be slightly faster since it does not have to compare anything, although the real cost of an atomic read-modify-write operation is in obtaining and locking the cache line.

Here is an example of a C++ spinlock that uses exchange()

#include <atomic>

class mutex {
    std::atomic<bool> flag{false};

public:
    void lock()
    {
        while (flag.exchange(true, std::memory_order_acquire));
    }

    void unlock()
    {
        flag.store(false, std::memory_order_release);
    }
};
LWimsey
  • 6,189
  • 2
  • 25
  • 53