Information Technology Reference
In-Depth Information
Interrupt discipline and invariants. You may notice that a thread calls
suspend() with interrupts turned off. Who turns them back on?
The next thread to run will turn interrupts back on. In particular, when
we implement a thread system, we typically enforce the invarient that a thread
always disables interrupts before performing a context switch and always as-
sumes that interrupts are disabled when they run again after a context switch.
So, whenever a thread returns from a context switch, it must reenable inter-
rupts. For example, just as the Lock::Acquire() code in Figure 5.11 reen-
ables interrupts before returning, the yield() code in Figure 4.12 on page 162
disables interrupts before the context switch and reenables interrupts before
returning, and the interrupt handling approach described for context switches
(Section 4.4.3) disables interrupts in the handler but restores interrupts when
the interrupted thread is resumed.
Multiprocessor spinlocks
On a multiprocessor machine, however, disabling interrupts is insucient. Even
if interrupts are turned of a sequence of operations by a thread on one processor
can be interleaved with operations by another thread on another processor.
Atomic read-modify-write instructions. Since turning off interrupts is
insucient, most processor architectures provide atomic read-modify-write in-
structions to support synchronization. These instructions can read a value from
a memory location to a register, modify the value, and write the modified value
to memory atomically with respect to all instructions on other processors.
As an example, some architectures provide a test and set instruction, which
atomically reads a value from memory to a register and writes a \1" to that
memory location.
We can implement a spinlock that works on a multiprocessor (or a unipro-
cessor) using test and set as follows:
classSpinLock{
private:
intvalue=0; //0=FREE;1=BUSY
public:
voidSpinLock::Acquire(){
while(test_and_set(&value)) //Whilebusy
; //spin
}
voidSpinLock::Release(){
value=0;
}
}
Such a lock is called a
denitionspinlock because a thread waiting for aBusylock \spins" in a tight
Search WWH ::




Custom Search