Mutex: Difference between revisions
Content added Content deleted
(Grammar) |
|||
Line 2:
[[Category:Encyclopedia]]
A '''
=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
==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
==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
=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
The mutex interface:
<lang ada>protected type Mutex is
entry Seize;
procedure Release;
private
Owned : Boolean := False;
end Mutex;</lang>
The implementation of:
<lang ada>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
▲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:
|