Russ Cox works at Google on the Go programming language. He previously worked at Bell Labs where he contributed to Plan9.
<!-- more -->
+<h1 id="who-are-you">Who are you?</h1>
+
<p>
I’m a programmer.
The first, simplest approach, is to try again, and again, and again.
Eventually thread A will release the lock (by marking the core <i>unlocked</i>),
at which point thread B’s atomic operation will succeed.
-This approach is called spinning, and a lock using this approach is called a spin lock.
-Spinning only makes sense if the lock is never held for very long,
-so that B’s spin loop only executes a small number of times.
+This approach is called spinning, and a lock using this approach is called a <a href="http://en.wikipedia.org/wiki/Spinlock">spin lock</a>.
<p>
A simple spin lock implementation looks like:
The <code>atomic_cmp_and_set</code> and <code>atomic_set</code>
use special machine instructions to manipulate <code>lk->bit</code> atomically.
+<p>
+Spinning only makes sense if the lock is never held for very long,
+so that B’s spin loop only executes a small number of times.
+If the lock can be held for longer periods of time, spinning while it is held
+wastes CPU and can interact badly with the operating system scheduler.
+
<p>
The second, more general approach is to maintain a queue of threads interested in acquiring the lock.
In this approach, when thread B finds the lock already held,
<p>
For more about this early history, see Brian Hayes’s “<a href="http://bit-player.org/wp-content/extras/bph-publications/Sciences-1993-07-Hayes-EDSAC.pdf">The Discovery of Debugging</a>”
and Martin Campbell-Kelly’s “<a href="http://dx.doi.org/10.1109/85.194051">The Airy Tape: An Early Chapter in the History of Debugging</a>.”
-
+