Locks: OS Support
Hardware support got us pretty far: working locks, and even
(as with the case of the ticket lock) fairness in lock acquisition.
However, we still have a problem: what to do when a context
switch occurs in a critical section, and threads start to spin end-
lessly, waiting for the interrupt (lock-holding) thread to be run
28.1 A Simple Approach: Just Yield, Baby
Our first try is a simple and friendly approach: when you are
going to spin, instead give up the CPU to another thread. Or, as
Al Davis might say, just yield, baby [D91]. Figure 28.1 presents
In this approach, we assume an operating system primitive
yield() which a thread can call when it wants to give up the
CPU and let another thread run. Because a thread can be in one
of three states (running, ready, or blocked), you can think of this
as an OS system call that moves the caller from the running state
to the ready state, and thus promotes another thread to running.