NVIDIA Interview Question
Software Engineer / DevelopersQ could be a simple FCFS or a thread priority based queue.
Pseudocode:
void mutex_lock_blocking(Mutex* mhandle, Thread* thandle)
{
spinlock_lock(mhandle->spinlock);
if(! is_Q_empty(mhandle->queue))
{
Q_push(thandle);
// Blocks thread. Scheduler can schedule threads with runnable == TRUE
thread->sched_runnable = FALSE;
spinlock_unlock(mhandle->spinlock);
// Calls the thread scheduler. Returns only when some other threas sets runnable = TRUE
// for the current thread.
scheduler_yield();
}
thread->sched_runnable = FALSE;
Q_push(thandle);
spinlock_unlock(mhandle->spinlock);
return;
}
void mutex_unlock(Mutex* mhandle, Thread* thandle)
{
spinlock_lock(mhandle->spinlock);
// Remove self from the Q
Q_remove(thandle);
// Mark next thread on Q. But do not pop it yet. next_runnable_thandle will resume
// resume execution just after returning from scheduler_yield() from mutex_lock_blocking
next_runnable_thandle = Q_get_next();
next_runnable_thandle->runnable = TRUE;
spinlock_unlock(mhandle->spinlock);
}
Mutex has ownership, ie. a process that owns the mutex can only lock and release the mutex, also if the process gets killed, the OS releases all the mutexes owned by the process. How do we achieve process ownership by any of the methods mentioned above?
- Prakash March 18, 2012