Mutex: Difference between revisions

Grammar
(Grammar)
Line 2:
[[Category:Encyclopedia]]
 
A '''Mutexmutex''' (''abbreviated'' '''Mut'''ually '''Ex'''clusive access) is a synchronization object, a variant of [[semaphore]] with ''k''=1. MutexA mutex is said to be seized by a [[task]] decreasing ''k''. It is released when the task restores ''k''. Mutexes are typically used to protect a shared resource from concurrent access. A [[task]] seizes (or acquires) the mutex, then accesses the resource, and after that releases the mutex.
 
MutexA mutex is a low-level synchronization primitive exposed to deadlocking. A deadlock can occur with just two tasks and two mutexes (if each task attempts to acquire both mutexes, but in the opposite order). Entering the deadlock is usually aggravated by a [[race condition]] state, which leads to sporadic hangups, which are very difficult to track down.
 
=Variants of mutexes=
 
==Global and local mutexes==
Usually the [[OS]] provides various implementations of mutexes corresponding to the variants of [[task]]s available in the [[OS]]. For example, system-wide mutexes can be used by [[process]]es. Local mutexes can be used only by [[threads]] etc. This distinction is maintained because, depending on the hardware, seizing a global mutex might be a thousand times slower than seizing a local one.
 
==Reentrant mutex==
A reentrant mutex can be seized by the same [[task]] multiple times. Each seizing of the mutex is matched by releasing it, in order to allow otheranother task to seize it.
 
==Read write mutex==
A read write mutex can be seized at two levels for ''read'' and for ''write''. The mutex can be seized for ''read'' by any number of tasks. Only one task may seize it for '''write''. Read write mutexmutexes are usually used to protect resources which can be accessed in a mutable and immutable ways. Immutable (read) access is granted concurrently for many tasks because they do not change the resource state. Read write mutexes can be reentrant, global or local. Further, promotion operations may be provided,. thatThat's when a [[task]] that havehas seized the mutex for ''write'' releases it while keeping seized for ''read''. Note that the reverse operation is potentially deadlocking and requires some additional access policy control.
 
=Deadlock prevention=
Line 23:
 
=={{header|Ada}}==
[[Ada]] provides higher-level concurrency primitives, which are complete in the sense that they also allow implementations of the lower-level ones, like mutexmutexes. Here is an implementation of a plain non-reentrant mutex based on protected objects.
 
The mutex interface:
<lang ada>protected type Mutex is
protected type Mutex is
entry Seize;
procedure Release;
private
Owned : Boolean := False;
end Mutex;</lang>
</lang>
The implementation of:
<lang ada>protected body Mutex is
protected body Mutex is
entry Seize when not Owned is
begin
Line 45 ⟶ 42:
Owned := False;
end Release;
end Mutex;</lang>
Here the entry Seize has a queue of the [[task]]s waiting for the mutex. The entry's barrier is closed when Owned is true. So any task calling to the entry will be queued. When the barrier is open the first task from the queue executes the entry and Owned becomes falsetrue closing the barrier again. The procedure Release simply sets Owned to false. Both Seize and Release are protected actions whichwhose execution causes reevaluation of all barriers, in this case one of Seize.
</lang>
Here the entry Seize has a queue of the [[task]]s waiting for the mutex. The entry's barrier is closed when Owned is true. So any task calling to the entry will be queued. When the barrier is open the first task from the queue executes the entry and Owned becomes false closing the barrier again. The procedure Release simply sets Owned to false. Both Seize and Release are protected actions which execution causes reevaluation of all barriers, in this case one of Seize.
 
Use:
Anonymous user