OS Concepts · Chapter 6
A hardware-provided atomic TestAndSet instruction builds a simple spinlock.
TestAndSet(&lock) atomically reads the old value of lock and sets it true in one indivisible step. A process spins in a while-loop until it reads back false, meaning it just acquired the lock. Simple, but gives no fairness guarantee — a process could in theory spin forever.