20,6 → 20,12 |
<title>Active kernel primitives</title> |
|
<section> |
<indexterm> |
<primary>synchronization</primary> |
|
<secondary>- spinlock</secondary> |
</indexterm> |
|
<title>Spinlocks</title> |
|
<para>The basic mutual exclusion primitive is the spinlock. The spinlock |
99,6 → 105,12 |
<title>Passive kernel synchronization</title> |
|
<section> |
<indexterm> |
<primary>synchronization</primary> |
|
<secondary>- wait queues</secondary> |
</indexterm> |
|
<title>Wait queues</title> |
|
<para>A wait queue is the basic passive synchronization primitive on |
165,6 → 177,12 |
</section> |
|
<section> |
<indexterm> |
<primary>synchronization</primary> |
|
<secondary>- semaphores</secondary> |
</indexterm> |
|
<title>Semaphores</title> |
|
<para>The interesting point about wait queues is that the number of |
191,6 → 209,12 |
<section> |
<title>Mutexes</title> |
|
<indexterm> |
<primary>synchronization</primary> |
|
<secondary>- mutex</secondary> |
</indexterm> |
|
<para>Mutexes are sometimes referred to as binary sempahores. That means |
that mutexes are like semaphores that allow only one thread in its |
critical section. Indeed, mutexes in HelenOS are implemented exactly in |
206,6 → 230,12 |
<section> |
<title>Reader/writer locks</title> |
|
<indexterm> |
<primary>synchronization</primary> |
|
<secondary>- read/write lock</secondary> |
</indexterm> |
|
<para>Reader/writer locks, or rwlocks, are by far the most complicated |
synchronization primitive within the kernel. The goal of these locks is |
to improve concurrency of applications, in which threads need to |
287,6 → 317,12 |
<section> |
<title>Condition variables</title> |
|
<indexterm> |
<primary>synchronization</primary> |
|
<secondary>- condition variable</secondary> |
</indexterm> |
|
<para>Condition variables can be used for waiting until a condition |
becomes true. In this respect, they are similar to wait queues. But |
contrary to wait queues, condition variables have different semantics |
296,23 → 332,26 |
the condition becoming true does the following:</para> |
|
<example> |
<title>Use of <code>condvar_wait_timeout</code>.</title> |
<programlisting language="C"><function>mutex_lock</function>(<varname>mtx</varname>); |
<title>Use of <code>condvar_wait_timeout</code>.</title> |
|
<programlisting language="C"><function>mutex_lock</function>(<varname>mtx</varname>); |
while (!<varname>condition</varname>) |
<function>condvar_wait_timeout</function>(<varname>cv</varname>, <varname>mtx</varname>); |
/* <remark>the condition is true, do something</remark> */ |
<function>mutex_unlock</function>(<varname>mtx</varname>);</programlisting> |
</example> |
</example> |
|
<para>A thread that causes the condition become true signals this event |
like this:</para> |
|
<example> |
<title>Use of <code>condvar_signal</code>.</title> |
<programlisting><function>mutex_lock</function>(<varname>mtx</varname>); |
<title>Use of <code>condvar_signal</code>.</title> |
|
<programlisting><function>mutex_lock</function>(<varname>mtx</varname>); |
<varname>condition</varname> = <constant>true</constant>; |
<function>condvar_signal</function>(<varname>cv</varname>); /* <remark>condvar_broadcast(cv);</remark> */ |
<function>mutex_unlock</function>(<varname>mtx</varname>);</programlisting></example> |
<function>mutex_unlock</function>(<varname>mtx</varname>);</programlisting> |
</example> |
|
<para>The wait operation, <code>condvar_wait_timeout</code>, always puts |
the calling thread to sleep. The thread then sleeps until another thread |
421,6 → 460,12 |
<section> |
<title>Futexes</title> |
|
<indexterm> |
<primary>synchronization</primary> |
|
<secondary>- futex</secondary> |
</indexterm> |
|
<para></para> |
</section> |
</section> |