Alex Rivera | Logout

Mutex implementation and signaling

Asked 2013-02-26T13:20:56.737
11

When a mutex is already locked by T1, and T2 tries to lock it, what is the process for T2?

I think it goes something like this:

-T2 tries to lock, fails, maybe spinlocks a bit, then calls yield...
-T2 is scheduled for execution a couple of times, tries to lock fails, yields...
-Eventually T1 unlocks, T2 is scheduled for execution and manages to lock the mutex...

Does T1 unlocking explicitly signal to the scheduler or other threads that the mutex is unlocked? Or does it just unlock, and leave the scheduler to schedule blocked threads when it feels suitable to do so (aka scheduler has no notion of blocked threads and doesn't treat them as special)?

Edit
Report

1 Answer

1

Say we have following scenario:

 1. T1 got M1. M1 locked.
 2. T2 tries to get M1 and gets blocked as M1 is locked.
 3. T3 tries to get M1 and gets blocked as M1 is locked.
 4. ...some time later...
 5. T1 unlocks M1.*
 6. T2 got M1.
 7. T3 is unblocked and tries to get M1 but is blocked again as T2 got M1 first.

*The system call, unlock, should notify all blocked tasks/processes/threads which are blocked on the mutex's lock call. They are then scheduled to execute. That does not mean they are executed as there might be already someone in execution. As others state it depends on the implementation how is that done. If you really want to learn this well I'd recommend this book

answered 2013-02-26T14:02:57.433

Your Answer