Does this work for multiprocessors?
void blocking_lock(mutex_t *mut) {
spin_lock(mut->spinlock);
if (mut->holder != 0)
enqueue(mut->wait_queue,
CurrentThread);
spin_unlock(mut->spinlock);
thread_switch();
} else {
mut->holder = CurrentThread;
spin_unlock(mut->spinlock);
}
}
void blocking_unlock(mutex_t *mut) {
spin_lock(mut->spinlock);
if (queue_empty(
mut->wait_queue)) {
mut->holder = 0;
else {
mut->holder =
dequeue(mut->wait_queue);
enqueue(RunQueue,
mut->holder);
}
spin_unlock(mut->spinlock);
}
Suppose
- Mutex locked, but its wait queue is empty
- Holder is unlocking it
- Another thread is trying to lock it.