previous | start | next

Blocking Locks

For multiprocessors, spin locks should be restricted to protecting very short code.

Spin locks are not useful for uniprocessors.

The following works on uniprocessors.

(This is closer to the project's uthread_mutex_t struct.)

void blocking_lock(mutex_t *mut) {
  if (mut->holder != 0)
    enqueue(mut->wait_queue, CurrentThread);
    thread_switch();
  } else
    mut->holder = CurrentThread;
}
      
void blocking_unlock(mutex_t *mut) {
  if (queue_empty(mut->wait_queue))
    mut->holder = 0;
  else {
    mut->holder = dequeue(mut->wait_queue);
    enqueue(RunQueue, mut->holder);
  }
}

   


previous | start | next