Is a mutex lock always implemented as spin waiting? Can a mutex lock be implemented as block waiting? (Operating System Concepts section 5.4 only mentions the implementation by spin waiting. See below.) (For comparison, a semaphore's waiting can be implemented either by busy spinning in a loop or by being blocked. See Operating System Concepts 9ed Section 5.5 and 5.6 and Is there still busy waiting in the process-blocking implementation of a semaphore?)
If a mutex lock can be implemented as block waiting, is a mutex lock implemented as such the same as a binary semaphore? (Stalling's OS book says a mutex lock and a binary semaphore differ in whether the process that locks the mutex (sets the value to zero) must be the one to unlock it. It doesn't mention whether they differ in spinning waiting only. See below.)
In Operating System Concepts, Section 5.5 Mutex Locks defines a mutex lock as:
We use the mutex lock to protect critical regions and thus prevent race conditions. That is, a process must acquire the lock before entering a critical section; it releases the lock when it exits the critical section. The acquire()function acquires the lock, and the release() function releases the lock, as illustrated in Figure 5.8.
A mutex lock has a boolean variable available whose value indicates if the lock is available or not. If the lock is available, a call to acquire() succeeds, and the lock is then considered unavailable. A process that attempts to acquire an unavailable lock is blocked until the lock is released.
The definition of acquire() is as follows:
acquire() { while (!available) ; /* busy wait */ available = false;; }
The definition of release() is as follows:
release() { available = true; }
Calls to either acquire() or release() must be performed atomically. Thus, mutex locks are often implemented using one of the hardware mecha- nisms described in Section 5.4, and we leave the description of this technique as an exercise.
The main disadvantage of the implementation given here is that it requires busy waiting. While a process is in its critical section, any other process that tries to enter its critical section must loop continuously in the call to acquire(). In fact, this type of mutex lock is also called a spinlock because the process “spins” while waiting for the lock to become available.
Stalling's Operating Systems book says
A concept related to the binary semaphore is the mutex . A key difference between the two is that the process that locks the mutex (sets the value to zero) must be the one to unlock it (sets the value to 1). In contrast, it is possible for one process to lock a binary semaphore and for another to unlock it.
Thanks.