Rev 62 | Rev 73 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 62 | Rev 72 | ||
|---|---|---|---|
| Line 18... | Line 18... | ||
| 18 | 18 | ||
| 19 | <section> |
19 | <section> |
| 20 | <title>Active kernel primitives</title> |
20 | <title>Active kernel primitives</title> |
| 21 | 21 | ||
| 22 | <section> |
22 | <section> |
| - | 23 | <indexterm> |
|
| - | 24 | <primary>synchronization</primary> |
|
| - | 25 | ||
| - | 26 | <secondary>- spinlock</secondary> |
|
| - | 27 | </indexterm> |
|
| - | 28 | ||
| 23 | <title>Spinlocks</title> |
29 | <title>Spinlocks</title> |
| 24 | 30 | ||
| 25 | <para>The basic mutual exclusion primitive is the spinlock. The spinlock |
31 | <para>The basic mutual exclusion primitive is the spinlock. The spinlock |
| 26 | implements active waiting for the availability of a memory lock (i.e. |
32 | implements active waiting for the availability of a memory lock (i.e. |
| 27 | simple variable) in a multiprocessor-safe manner. This safety is |
33 | simple variable) in a multiprocessor-safe manner. This safety is |
| Line 97... | Line 103... | ||
| 97 | 103 | ||
| 98 | <section> |
104 | <section> |
| 99 | <title>Passive kernel synchronization</title> |
105 | <title>Passive kernel synchronization</title> |
| 100 | 106 | ||
| 101 | <section> |
107 | <section> |
| - | 108 | <indexterm> |
|
| - | 109 | <primary>synchronization</primary> |
|
| - | 110 | ||
| - | 111 | <secondary>- wait queues</secondary> |
|
| - | 112 | </indexterm> |
|
| - | 113 | ||
| 102 | <title>Wait queues</title> |
114 | <title>Wait queues</title> |
| 103 | 115 | ||
| 104 | <para>A wait queue is the basic passive synchronization primitive on |
116 | <para>A wait queue is the basic passive synchronization primitive on |
| 105 | which all other passive synchronization primitives are built. Simply |
117 | which all other passive synchronization primitives are built. Simply |
| 106 | put, it allows a thread to sleep until an event associated with the |
118 | put, it allows a thread to sleep until an event associated with the |
| Line 163... | Line 175... | ||
| 163 | characteristic for all passive HelenOS synchronization primitives, with |
175 | characteristic for all passive HelenOS synchronization primitives, with |
| 164 | the exception as described below.</para> |
176 | the exception as described below.</para> |
| 165 | </section> |
177 | </section> |
| 166 | 178 | ||
| 167 | <section> |
179 | <section> |
| - | 180 | <indexterm> |
|
| - | 181 | <primary>synchronization</primary> |
|
| - | 182 | ||
| - | 183 | <secondary>- semaphores</secondary> |
|
| - | 184 | </indexterm> |
|
| - | 185 | ||
| 168 | <title>Semaphores</title> |
186 | <title>Semaphores</title> |
| 169 | 187 | ||
| 170 | <para>The interesting point about wait queues is that the number of |
188 | <para>The interesting point about wait queues is that the number of |
| 171 | missed wakeups is equal to the number of threads that will not block in |
189 | missed wakeups is equal to the number of threads that will not block in |
| 172 | <code>watiq_sleep_timeout</code> and would immediately succeed instead. |
190 | <code>watiq_sleep_timeout</code> and would immediately succeed instead. |
| Line 189... | Line 207... | ||
| 189 | </section> |
207 | </section> |
| 190 | 208 | ||
| 191 | <section> |
209 | <section> |
| 192 | <title>Mutexes</title> |
210 | <title>Mutexes</title> |
| 193 | 211 | ||
| - | 212 | <indexterm> |
|
| - | 213 | <primary>synchronization</primary> |
|
| - | 214 | ||
| - | 215 | <secondary>- mutex</secondary> |
|
| - | 216 | </indexterm> |
|
| - | 217 | ||
| 194 | <para>Mutexes are sometimes referred to as binary sempahores. That means |
218 | <para>Mutexes are sometimes referred to as binary sempahores. That means |
| 195 | that mutexes are like semaphores that allow only one thread in its |
219 | that mutexes are like semaphores that allow only one thread in its |
| 196 | critical section. Indeed, mutexes in HelenOS are implemented exactly in |
220 | critical section. Indeed, mutexes in HelenOS are implemented exactly in |
| 197 | this way: they are built on top of semaphores. From another point of |
221 | this way: they are built on top of semaphores. From another point of |
| 198 | view, they can be viewed as spinlocks without busy waiting. Their |
222 | view, they can be viewed as spinlocks without busy waiting. Their |
| Line 204... | Line 228... | ||
| 204 | </section> |
228 | </section> |
| 205 | 229 | ||
| 206 | <section> |
230 | <section> |
| 207 | <title>Reader/writer locks</title> |
231 | <title>Reader/writer locks</title> |
| 208 | 232 | ||
| - | 233 | <indexterm> |
|
| - | 234 | <primary>synchronization</primary> |
|
| - | 235 | ||
| - | 236 | <secondary>- read/write lock</secondary> |
|
| - | 237 | </indexterm> |
|
| - | 238 | ||
| 209 | <para>Reader/writer locks, or rwlocks, are by far the most complicated |
239 | <para>Reader/writer locks, or rwlocks, are by far the most complicated |
| 210 | synchronization primitive within the kernel. The goal of these locks is |
240 | synchronization primitive within the kernel. The goal of these locks is |
| 211 | to improve concurrency of applications, in which threads need to |
241 | to improve concurrency of applications, in which threads need to |
| 212 | synchronize access to a shared resource, and that access can be |
242 | synchronize access to a shared resource, and that access can be |
| 213 | partitioned into a read-only mode and a write mode. Reader/writer locks |
243 | partitioned into a read-only mode and a write mode. Reader/writer locks |
| Line 285... | Line 315... | ||
| 285 | </section> |
315 | </section> |
| 286 | 316 | ||
| 287 | <section> |
317 | <section> |
| 288 | <title>Condition variables</title> |
318 | <title>Condition variables</title> |
| 289 | 319 | ||
| - | 320 | <indexterm> |
|
| - | 321 | <primary>synchronization</primary> |
|
| - | 322 | ||
| - | 323 | <secondary>- condition variable</secondary> |
|
| - | 324 | </indexterm> |
|
| - | 325 | ||
| 290 | <para>Condition variables can be used for waiting until a condition |
326 | <para>Condition variables can be used for waiting until a condition |
| 291 | becomes true. In this respect, they are similar to wait queues. But |
327 | becomes true. In this respect, they are similar to wait queues. But |
| 292 | contrary to wait queues, condition variables have different semantics |
328 | contrary to wait queues, condition variables have different semantics |
| 293 | that allows events to be lost when there is no thread waiting for them. |
329 | that allows events to be lost when there is no thread waiting for them. |
| 294 | In order to support this, condition variables don't use direct hand-off |
330 | In order to support this, condition variables don't use direct hand-off |
| 295 | and operate in a way similar to the example below. A thread waiting for |
331 | and operate in a way similar to the example below. A thread waiting for |
| 296 | the condition becoming true does the following:</para> |
332 | the condition becoming true does the following:</para> |
| 297 | 333 | ||
| 298 | <example> |
334 | <example> |
| 299 | <title>Use of <code>condvar_wait_timeout</code>.</title> |
335 | <title>Use of <code>condvar_wait_timeout</code>.</title> |
| - | 336 | ||
| 300 | <programlisting language="C"><function>mutex_lock</function>(<varname>mtx</varname>); |
337 | <programlisting language="C"><function>mutex_lock</function>(<varname>mtx</varname>); |
| 301 | while (!<varname>condition</varname>) |
338 | while (!<varname>condition</varname>) |
| 302 | <function>condvar_wait_timeout</function>(<varname>cv</varname>, <varname>mtx</varname>); |
339 | <function>condvar_wait_timeout</function>(<varname>cv</varname>, <varname>mtx</varname>); |
| 303 | /* <remark>the condition is true, do something</remark> */ |
340 | /* <remark>the condition is true, do something</remark> */ |
| 304 | <function>mutex_unlock</function>(<varname>mtx</varname>);</programlisting> |
341 | <function>mutex_unlock</function>(<varname>mtx</varname>);</programlisting> |
| 305 | </example> |
342 | </example> |
| 306 | 343 | ||
| 307 | <para>A thread that causes the condition become true signals this event |
344 | <para>A thread that causes the condition become true signals this event |
| 308 | like this:</para> |
345 | like this:</para> |
| 309 | 346 | ||
| 310 | <example> |
347 | <example> |
| 311 | <title>Use of <code>condvar_signal</code>.</title> |
348 | <title>Use of <code>condvar_signal</code>.</title> |
| - | 349 | ||
| 312 | <programlisting><function>mutex_lock</function>(<varname>mtx</varname>); |
350 | <programlisting><function>mutex_lock</function>(<varname>mtx</varname>); |
| 313 | <varname>condition</varname> = <constant>true</constant>; |
351 | <varname>condition</varname> = <constant>true</constant>; |
| 314 | <function>condvar_signal</function>(<varname>cv</varname>); /* <remark>condvar_broadcast(cv);</remark> */ |
352 | <function>condvar_signal</function>(<varname>cv</varname>); /* <remark>condvar_broadcast(cv);</remark> */ |
| 315 | <function>mutex_unlock</function>(<varname>mtx</varname>);</programlisting></example> |
353 | <function>mutex_unlock</function>(<varname>mtx</varname>);</programlisting> |
| - | 354 | </example> |
|
| 316 | 355 | ||
| 317 | <para>The wait operation, <code>condvar_wait_timeout</code>, always puts |
356 | <para>The wait operation, <code>condvar_wait_timeout</code>, always puts |
| 318 | the calling thread to sleep. The thread then sleeps until another thread |
357 | the calling thread to sleep. The thread then sleeps until another thread |
| 319 | invokes <code>condvar_broadcast</code> on the same condition variable or |
358 | invokes <code>condvar_broadcast</code> on the same condition variable or |
| 320 | until it is woken up by <code>condvar_signal</code>. The |
359 | until it is woken up by <code>condvar_signal</code>. The |
| Line 419... | Line 458... | ||
| 419 | <title>Userspace synchronization</title> |
458 | <title>Userspace synchronization</title> |
| 420 | 459 | ||
| 421 | <section> |
460 | <section> |
| 422 | <title>Futexes</title> |
461 | <title>Futexes</title> |
| 423 | 462 | ||
| - | 463 | <indexterm> |
|
| - | 464 | <primary>synchronization</primary> |
|
| - | 465 | ||
| - | 466 | <secondary>- futex</secondary> |
|
| - | 467 | </indexterm> |
|
| - | 468 | ||
| 424 | <para></para> |
469 | <para></para> |
| 425 | </section> |
470 | </section> |
| 426 | </section> |
471 | </section> |
| 427 | </chapter> |
472 | </chapter> |
| 428 | 473 | ||