Rev 44 | Rev 48 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 44 | Rev 45 | ||
|---|---|---|---|
| Line 5... | Line 5... | ||
| 5 | <title>Mutual exclusion and synchronization</title> |
5 | <title>Mutual exclusion and synchronization</title> |
| 6 | 6 | ||
| 7 | <section> |
7 | <section> |
| 8 | <title>Introduction</title> |
8 | <title>Introduction</title> |
| 9 | 9 | ||
| 10 | <para>The HelenOS operating system is designed to make use of parallelism |
10 | <para>The HelenOS operating system is designed to make use of the |
| 11 | offered by hardware and to exploit concurrency of both the kernel and |
11 | parallelism offered by the hardware and to exploit concurrency of both the |
| 12 | userspace tasks. This is achieved through multiprocessor support and |
12 | kernel and userspace tasks. This is achieved through multiprocessor |
| 13 | several levels of multiprogramming (i.e. multitasking, multithreading and |
13 | support and several levels of multiprogramming such as multitasking, |
| 14 | through userspace pseudo threads). However, such a highly concurrent |
14 | multithreading and also through userspace pseudo threads. However, such a |
| 15 | environment needs safe and efficient ways to handle mutual exclusion and |
15 | highly concurrent environment needs safe and efficient ways to handle |
| 16 | synchronization of many execution flows.</para> |
16 | mutual exclusion and synchronization of many execution flows.</para> |
| 17 | </section> |
17 | </section> |
| 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 | <title>Spinlocks</title> |
23 | <title>Spinlocks</title> |
| 24 | 24 | ||
| 25 | <para>The basic mutual exclusion primitive is the spinlock. Spinlock |
25 | <para>The basic mutual exclusion primitive is the spinlock. The spinlock |
| 26 | implements busy waiting for an availability of a memory lock (i.e. |
26 | implements active waiting for the availability of a memory lock (i.e. |
| 27 | simple variable) in a multiprocessor-safe manner. This safety is |
27 | simple variable) in a multiprocessor-safe manner. This safety is |
| 28 | achieved through the use of a specialized, architecture-dependent, |
28 | achieved through the use of a specialized, architecture-dependent, |
| 29 | atomic test-and-set operation which either locks the spinlock (i.e. sets |
29 | atomic test-and-set operation which either locks the spinlock (i.e. sets |
| 30 | the variable) or, provided that it is already locked, leaves it |
30 | the variable) or, provided that it is already locked, leaves it |
| 31 | unaltered. In any case, the test-and-set operation returns a value, thus |
31 | unaltered. In any case, the test-and-set operation returns a value, thus |
| 32 | signalling either success (i.e. zero return value) or failure (i.e. |
32 | signalling either success (i.e. zero return value) or failure (i.e. |
| 33 | non-zero value) in acquiring the lock. Note that this makes the |
33 | non-zero value) in acquiring the lock. Note that this makes a |
| 34 | fundamental difference between the naive algorithm that doesn't use the |
34 | fundamental difference between the naive algorithm that doesn't use the |
| 35 | atomic operation and the spinlock algortihm. While the naive algorithm |
35 | atomic operation and the spinlock algortihm. While the naive algorithm |
| 36 | is prone to race conditions on SMP configuratinos and thus is completely |
36 | is prone to race conditions on SMP configurations and thus is completely |
| 37 | SMP-unsafe, the spinlock algorithm eliminates the possibility of race |
37 | SMP-unsafe, the spinlock algorithm eliminates the possibility of race |
| 38 | conditions and is suitable for mutual exclusion use.</para> |
38 | conditions and is suitable for mutual exclusion use.</para> |
| 39 | 39 | ||
| 40 | <para>The semantics of the test-and-set operation is that the spinlock |
40 | <para>The semantics of the test-and-set operation is that the spinlock |
| 41 | remains unavailable until this operation called on the respective |
41 | remains unavailable until this operation called on the respective |
| 42 | spinlock returns zero. HelenOS builds two functions on top of |
42 | spinlock returns zero. HelenOS builds two functions on top of the |
| 43 | test-and-set operation. The first is the unconditional attempt to |
43 | test-and-set operation. The first function is the unconditional attempt |
| 44 | acquire the spinlock and is called <emphasis>spinlock_lock</emphasis>. |
44 | to acquire the spinlock and is called |
| - | 45 | <emphasis>spinlock_lock</emphasis>. It simply loops until the |
|
| 45 | It simply loops until test-and-set returns zero. The other operation, |
46 | test-and-set returns a zero value. The other function, |
| 46 | <emphasis>spinlock_trylock</emphasis>, is the conditional lock operation |
47 | <emphasis>spinlock_trylock</emphasis>, is the conditional lock operation |
| 47 | and calls the test-and-set only once to find out wheter it managed to |
48 | and calls the test-and-set only once to find out whether it managed to |
| 48 | acquire the spinlock or not. The conditional operation is useful in |
49 | acquire the spinlock or not. The conditional operation is useful in |
| 49 | situations in which an algorithm cannot acquire more spinlocks in the |
50 | situations in which an algorithm cannot acquire more spinlocks in the |
| 50 | proper order and a deadlock cannot be avoided. In such a case, the |
51 | proper order and a deadlock cannot be avoided. In such a case, the |
| 51 | algorithm would detect the danger and instead of possibly deadlocking |
52 | algorithm would detect the danger and instead of possibly deadlocking |
| 52 | the system it would simply release some spinlocks it already holds and |
53 | the system it would simply release some spinlocks it already holds and |
| 53 | retry the whole operation with the hope that it will succeed next time. |
54 | retry the whole operation with the hope that it will succeed next time. |
| 54 | The unlock operation, <emphasis>spinlock_unlock</emphasis>, is quite |
55 | The unlock function, <emphasis>spinlock_unlock</emphasis>, is quite easy |
| 55 | easy - it merely clears the spinlock variable.</para> |
56 | - it merely clears the spinlock variable.</para> |
| 56 | 57 | ||
| 57 | <para>Nevertheless, there is a special issue related to hardware |
58 | <para>Nevertheless, there is a special issue related to hardware |
| 58 | optimizations that modern processors implement. Particularily |
59 | optimizations that modern processors implement. Particularly problematic |
| 59 | problematic is the out-of-order execution of instructions within the |
60 | is the out-of-order execution of instructions within the critical |
| 60 | critical section protected by a spinlock. The processors are always |
61 | section protected by a spinlock. The processors are always |
| 61 | self-consistent so that they can carry out speculatively executed |
62 | self-consistent so that they can carry out speculatively executed |
| 62 | instructions in the right order with regard to dependencies among those |
63 | instructions in the right order with regard to dependencies among those |
| 63 | instructions. However, the dependency between instructions inside the |
64 | instructions. However, the dependency between instructions inside the |
| 64 | critical section and those that implement locking and unlocking of the |
65 | critical section and those that implement locking and unlocking of the |
| 65 | respective spinlock is not implicit on some processor architectures and |
66 | respective spinlock is not implicit on some processor architectures. As |
| 66 | the processor needs to be explicitly told about each occurrence of such |
67 | a result, the processor needs to be explicitly told about each |
| 67 | a dependency. Therefore, HelenOS adds architecture-specific hooks to all |
68 | occurrence of such a dependency. Therefore, HelenOS adds |
| 68 | <emphasis>spinlock_lock</emphasis>, |
69 | architecture-specific hooks to all <emphasis>spinlock_lock</emphasis>, |
| 69 | <emphasis>spinlock_trylock</emphasis> and |
70 | <emphasis>spinlock_trylock</emphasis> and |
| 70 | <emphasis>spinlock_unlock</emphasis> to prevent the instructions inside |
71 | <emphasis>spinlock_unlock</emphasis> functions to prevent the |
| 71 | the critical section from bleeding out. On some architectures, these |
72 | instructions inside the critical section from permeating out. On some |
| 72 | hooks can be a no-op because the dependencies are implicitly there |
73 | architectures, these hooks can be void because the dependencies are |
| 73 | because of the special properties of locking and unlocking instructions. |
74 | implicitly there because of the special properties of locking and |
| 74 | However, other architectures need to instrument these hooks with |
75 | unlocking instructions. However, other architectures need to instrument |
| 75 | different memory barriers, depending on what operations can bleed |
76 | these hooks with different memory barriers, depending on what operations |
| 76 | out.</para> |
77 | could permeate out.</para> |
| 77 | 78 | ||
| 78 | <para>Spinlocks have one significant drawback: when held for longer time |
79 | <para>Spinlocks have one significant drawback: when held for longer time |
| 79 | periods, they harm both parallelism and concurrency. Processor executing |
80 | periods, they harm both parallelism and concurrency. The processor |
| 80 | <emphasis>spinlock_lock</emphasis> does not do any fruitful work and is |
81 | executing <emphasis>spinlock_lock</emphasis> does not do any fruitful |
| 81 | effectively halted until it can grab the lock and proceed. Similarily, |
82 | work and is effectively halted until it can grab the lock and proceed. |
| 82 | other threads cannot execute on the processor that holds the spinlock |
83 | Similarily, other execution flows cannot execute on the processor that |
| 83 | because the kernel disables preemption on that processor when a spinlock |
84 | holds the spinlock because the kernel disables preemption on that |
| 84 | is held. The reason behind disabling preemption is priority inversion |
85 | processor when a spinlock is held. The reason behind disabling |
| 85 | problem avoidance. For the same reason, threads are strongly discouraged |
86 | preemption is priority inversion problem avoidance. For the same reason, |
| - | 87 | threads are strongly discouraged from sleeping when they hold a |
|
| 86 | from sleeping when they hold a spinlock.</para> |
88 | spinlock.</para> |
| 87 | 89 | ||
| 88 | <para>To summarize, spinlocks represent very simple and essential mutual |
90 | <para>To summarize, spinlocks represent very simple and essential mutual |
| 89 | exclusion primitive for SMP systems. On the other hand, spinlocks scale |
91 | exclusion primitive for SMP systems. On the other hand, spinlocks scale |
| 90 | poorly because of the active loop they are based on. Therefore, |
92 | poorly because of the active loop they are based on. Therefore, |
| 91 | spinlocks are used in HelenOS only for a short-time mutual exclusion and |
93 | spinlocks are used in HelenOS only for short-time mutual exclusion and |
| 92 | in cases where the mutual exclusion is required out of thread context. |
94 | in cases where the mutual exclusion is required out of thread context. |
| 93 | Lastly, spinlocks are used in the construction of passive |
95 | Lastly, spinlocks are used in the construction of passive |
| 94 | synchronization primitives.</para> |
96 | synchronization primitives.</para> |
| 95 | </section> |
97 | </section> |
| 96 | </section> |
98 | </section> |
| Line 100... | Line 102... | ||
| 100 | 102 | ||
| 101 | <section> |
103 | <section> |
| 102 | <title>Wait queues</title> |
104 | <title>Wait queues</title> |
| 103 | 105 | ||
| 104 | <para>A wait queue is the basic passive synchronization primitive on |
106 | <para>A wait queue is the basic passive synchronization primitive on |
| 105 | which all other passive synchronization primitives build. Simply put, it |
107 | which all other passive synchronization primitives are built. Simply |
| 106 | allows a thread to sleep until an event associated with the particular |
108 | put, it allows a thread to sleep until an event associated with the |
| 107 | wait queue occurs. Multiple threads are notified about incoming events |
109 | particular wait queue occurs. Multiple threads are notified about |
| 108 | in first come, first served fashion. Moreover, should the event come |
110 | incoming events in a first come, first served fashion. Moreover, should |
| 109 | before any thread waits for it, it is recorded in the wait queue as a |
111 | the event come before any thread waits for it, it is recorded in the |
| 110 | missed wakeup and later forwarded to the first thread that decides to |
112 | wait queue as a missed wakeup and later forwarded to the first thread |
| 111 | wait in the queue. The inner structures of the wait queue are protected |
113 | that decides to wait in the queue. The inner structures of the wait |
| 112 | by a spinlock.</para> |
114 | queue are protected by a spinlock.</para> |
| 113 | 115 | ||
| 114 | <para>The thread that wants to wait for a wait queue event uses the |
116 | <para>The thread that wants to wait for a wait queue event uses the |
| 115 | <emphasis>waitq_sleep_timeout</emphasis> function. The algorithm then |
117 | <emphasis>waitq_sleep_timeout</emphasis> function. The algorithm then |
| 116 | checks the wait queue's counter of missed wakeups and if there are any |
118 | checks the wait queue's counter of missed wakeups and if there are any |
| 117 | missed wakeups, the call returns immediately. The call also returns |
119 | missed wakeups, the call returns immediately. The call also returns |
| Line 122... | Line 124... | ||
| 122 | 124 | ||
| 123 | <orderedlist> |
125 | <orderedlist> |
| 124 | <listitem> |
126 | <listitem> |
| 125 | <para>another thread calls <emphasis>waitq_wakeup</emphasis> and the |
127 | <para>another thread calls <emphasis>waitq_wakeup</emphasis> and the |
| 126 | thread is the first thread in the wait queue's list of sleeping |
128 | thread is the first thread in the wait queue's list of sleeping |
| 127 | threads</para> |
129 | threads;</para> |
| 128 | </listitem> |
130 | </listitem> |
| 129 | 131 | ||
| 130 | <listitem> |
132 | <listitem> |
| 131 | <para>another thread calls |
133 | <para>another thread calls |
| 132 | <emphasis>waitq_interrupt_sleep</emphasis> on the sleeping |
134 | <emphasis>waitq_interrupt_sleep</emphasis> on the sleeping |
| 133 | thread</para> |
135 | thread;</para> |
| 134 | </listitem> |
136 | </listitem> |
| 135 | 137 | ||
| 136 | <listitem> |
138 | <listitem> |
| 137 | <para>the sleep timeouts provided that none of the previous occurred |
139 | <para>the sleep times out provided that none of the previous |
| 138 | within a specified time limit; the limit can be infinity</para> |
140 | occurred within a specified time limit; the limit can be |
| - | 141 | infinity.</para> |
|
| 139 | </listitem> |
142 | </listitem> |
| 140 | </orderedlist> |
143 | </orderedlist> |
| 141 | 144 | ||
| 142 | <para>All five possibilities (immediate return on success, immediate |
145 | <para>All five possibilities (immediate return on success, immediate |
| 143 | return on failure, wakeup after sleep, interruption and timeout) are |
146 | return on failure, wakeup after sleep, interruption and timeout) are |
| 144 | distinguishable by the return value of |
147 | distinguishable by the return value of |
| 145 | <emphasis>waitq_sleep_timeout</emphasis>. The ability to interrupt a |
148 | <emphasis>waitq_sleep_timeout</emphasis>. Being able to interrupt a |
| 146 | sleeping thread is essential for externally initiated thread termination |
149 | sleeping thread is essential for externally initiated thread |
| 147 | and the ability to wait only for a certain amount of time is used, for |
150 | termination. The ability to wait only for a certain amount of time is |
| 148 | instance, to passively delay thread execution by several microseconds or |
151 | used, for instance, to passively delay thread execution by several |
| 149 | even seconds in <emphasis>thread_sleep</emphasis> function. Because all |
152 | microseconds or even seconds in <emphasis>thread_sleep</emphasis> |
| 150 | other passive kernel synchronization primitives are based on wait |
153 | function. Due to the fact that all other passive kernel synchronization |
| 151 | queues, they also have the option of being interrutped and, more |
154 | primitives are based on wait queues, they also have the option of being |
| 152 | importantly, can timeout. All of them also implement the conditional |
155 | interrutped and, more importantly, can timeout. All of them also |
| 153 | operation. Furthemore, this very fundamental interface reaches up to the |
156 | implement the conditional operation. Furthemore, this very fundamental |
| - | 157 | interface reaches up to the implementation of futexes - userspace |
|
| 154 | implementation of futexes - userspace synchronization primitive, which |
158 | synchronization primitive, which makes it possible for a userspace |
| 155 | makes it possible for a userspace thread to request synchronization |
159 | thread to request a synchronization operation with a timeout or a |
| 156 | operation with a timeout or a conditional operation.</para> |
160 | conditional operation.</para> |
| 157 | 161 | ||
| 158 | <para>From the description above, it should be apparent, that when a |
162 | <para>From the description above, it should be apparent, that when a |
| 159 | sleeping thread is woken by <emphasis>waitq_wakeup</emphasis> or when |
163 | sleeping thread is woken by <emphasis>waitq_wakeup</emphasis> or when |
| 160 | <emphasis>waitq_sleep_timeout</emphasis> succeeds immediatelly, the |
164 | <emphasis>waitq_sleep_timeout</emphasis> succeeds immediately, the |
| 161 | thread can be sure the event has come and the thread need not and should |
165 | thread can be sure that the event has occurred. The thread need not and |
| 162 | not verify this fact. This approach is called direct hand-off and is |
166 | should not verify this fact. This approach is called direct hand-off and |
| 163 | characteristic for all passive HelenOS synchronization primitives with |
167 | is characteristic for all passive HelenOS synchronization primitives, |
| 164 | one exception described below.</para> |
168 | with the exception as described below.</para> |
| 165 | </section> |
169 | </section> |
| 166 | 170 | ||
| 167 | <section> |
171 | <section> |
| 168 | <title>Semaphores</title> |
172 | <title>Semaphores</title> |
| 169 | 173 | ||
| 170 | <para>The interesting point about wait queues is that the number of |
174 | <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 |
175 | missed wakeups is equal to the number of threads that will not block in |
| 172 | <emphasis>watiq_sleep_timeout</emphasis> and would immediately succeed |
176 | <emphasis>watiq_sleep_timeout</emphasis> and would immediately succeed |
| 173 | instead. On the other hand, semaphores are synchronization primitives |
177 | instead. On the other hand, semaphores are synchronization primitives |
| 174 | that will let predefined amount of threads in its critical section and |
178 | that will let predefined amount of threads into its critical section and |
| 175 | block any other threads above this count. However, these two cases are |
179 | block any other threads above this count. However, these two cases are |
| 176 | exactly the same. Semaphores in HelenOS are therefore implemented as |
180 | exactly the same. Semaphores in HelenOS are therefore implemented as |
| 177 | wait queues with a single semantic change: their wait queue is |
181 | wait queues with a single semantic change: their wait queue is |
| 178 | initialized to have so many missed wakeups as is the number of threads |
182 | initialized to have so many missed wakeups as is the number of threads |
| 179 | that the semphore intends to let into its critical section |
183 | that the semphore intends to let into its critical section |
| Line 190... | Line 194... | ||
| 190 | </section> |
194 | </section> |
| 191 | 195 | ||
| 192 | <section> |
196 | <section> |
| 193 | <title>Mutexes</title> |
197 | <title>Mutexes</title> |
| 194 | 198 | ||
| 195 | <para>Mutexes are are sometimes referred to as binary sempahores. That |
199 | <para>Mutexes are sometimes referred to as binary sempahores. That means |
| 196 | means that mutexes are like semaphores that allow only one thread in its |
200 | that mutexes are like semaphores that allow only one thread in its |
| 197 | critical section. Indeed, mutexes in HelenOS are implemented exactly in |
201 | critical section. Indeed, mutexes in HelenOS are implemented exactly in |
| 198 | this way: they are built atop semaphores. From another point of view, |
202 | this way: they are built on top of semaphores. From another point of |
| 199 | they can be viewed as spinlocks without busy waiting. Their semaphore |
203 | view, they can be viewed as spinlocks without busy waiting. Their |
| 200 | heritage provides good basics for both conditional operation and |
204 | semaphore heritage provides good basics for both conditional operation |
| 201 | operation with timeout. The locking operation is called |
205 | and operation with timeout. The locking operation is called |
| 202 | <emphasis>mutex_lock</emphasis>, the conditional locking operation is |
206 | <emphasis>mutex_lock</emphasis>, the conditional locking operation is |
| 203 | called <emphasis>mutex_trylock</emphasis> and the unlocking operation is |
207 | called <emphasis>mutex_trylock</emphasis> and the unlocking operation is |
| 204 | called <emphasis>mutex_unlock</emphasis>.</para> |
208 | called <emphasis>mutex_unlock</emphasis>.</para> |
| 205 | </section> |
209 | </section> |
| 206 | 210 | ||
| 207 | <section> |
211 | <section> |
| 208 | <title>Reader/writer locks</title> |
212 | <title>Reader/writer locks</title> |
| 209 | 213 | ||
| 210 | <para>Reader/writer locks, or rwlocks, are by far the most complicated |
214 | <para>Reader/writer locks, or rwlocks, are by far the most complicated |
| 211 | synchronization primitive within the kernel. The goal of these locks is |
215 | synchronization primitive within the kernel. The goal of these locks is |
| 212 | to improve concurrency of applications in which threads need to |
216 | to improve concurrency of applications, in which threads need to |
| 213 | synchronize access to a shared resource and that access can be |
217 | synchronize access to a shared resource, and that access can be |
| 214 | partitioned into a read-only mode and a write mode. Reader/writer locks |
218 | partitioned into a read-only mode and a write mode. Reader/writer locks |
| 215 | should make it possible for several, possibly many, readers to enter the |
219 | should make it possible for several, possibly many, readers to enter the |
| 216 | critical section, provided that no writer is currently in the critical |
220 | critical section, provided that no writer is currently in the critical |
| 217 | section, or to be in the critical section contemporarily. Writers are |
221 | section, or to be in the critical section contemporarily. Writers are |
| 218 | allowed to enter the critical section only individually, provided that |
222 | allowed to enter the critical section only individually, provided that |
| 219 | no reader is in the critical section already. Applications in which the |
223 | no reader is in the critical section already. Applications, in which the |
| 220 | majority of operations can be done in the read-only mode can benefit |
224 | majority of operations can be done in the read-only mode, can benefit |
| 221 | from increased concurrency created by reader/writer locks.</para> |
225 | from increased concurrency created by reader/writer locks.</para> |
| 222 | 226 | ||
| 223 | <para>During reader/writer locks construction, a decision should be made |
227 | <para>During reader/writer lock construction, a decision should be made |
| 224 | whether readers will be prefered over writers or whether writers will be |
228 | whether readers will be prefered over writers or whether writers will be |
| 225 | prefered over readers in cases when the lock is not currently held and |
229 | prefered over readers in cases when the lock is not currently held and |
| 226 | both a reader and a writer want to gain the lock. Some operating systems |
230 | both a reader and a writer want to gain the lock. Some operating systems |
| 227 | prefer one group over the other, creating thus a possibility for |
231 | prefer one group over the other, creating thus a possibility for |
| 228 | starving the unprefered group. In the HelenOS operating system, none of |
232 | starving the unprefered group. In the HelenOS operating system, none of |
| 229 | the two groups is prefered. The lock is granted on the first come, first |
233 | the two groups is prefered. The lock is granted on a first come, first |
| 230 | served basis with the additional note that readers are granted the lock |
234 | served basis with the additional note that readers are granted the lock |
| 231 | in biggest possible batches.</para> |
235 | in the biggest possible batch.</para> |
| 232 | 236 | ||
| 233 | <para>With this policy and the timeout modes of operation, the direct |
237 | <para>With this policy and the timeout modes of operation, the direct |
| 234 | hand-off becomes much more complicated. For instance, a writer leaving |
238 | hand-off becomes much more complicated. For instance, a writer leaving |
| 235 | the critical section must wake up all leading readers in the rwlock's |
239 | the critical section must wake up all leading readers in the rwlock's |
| 236 | wait queue or one leading writer or no-one if no thread is waiting. |
240 | wait queue or one leading writer or no-one if no thread is waiting. |
| 237 | Similarily, the last reader leaving the critical section must wakeup the |
241 | Similarily, the last reader leaving the critical section must wakeup the |
| 238 | sleeping writer, if there are any sleeping threads at all. As another |
242 | sleeping writer if there are any sleeping threads left at all. As |
| 239 | example, if a writer at the beginning of the rwlock's wait queue |
243 | another example, if a writer at the beginning of the rwlock's wait queue |
| 240 | timeouts and the lock is held by at least one reader, the timeouting |
244 | times out and the lock is held by at least one reader, the writer which |
| 241 | writer must first wake up all readers that follow him in the queue prior |
245 | has timed out must first wake up all readers that follow him in the |
| 242 | to signalling the timeout itself and giving up.</para> |
246 | queue prior to signalling the timeout itself and giving up.</para> |
| 243 | 247 | ||
| 244 | <para>Because of the issues mentioned in the previous paragraph, the |
248 | <para>Due to the issues mentioned in the previous paragraph, the |
| 245 | reader/writer locks imlpementation needs to walk the rwlock wait queue's |
249 | reader/writer lock imlpementation needs to walk the rwlock wait queue's |
| 246 | list of sleeping threads directly in order to find out the type of |
250 | list of sleeping threads directly, in order to find out the type of |
| 247 | access that the queueing threads demand. This makes the code difficult |
251 | access that the queueing threads demand. This makes the code difficult |
| 248 | to understand and dependent on the internal implementation of the wait |
252 | to understand and dependent on the internal implementation of the wait |
| 249 | queue. Nevertheless, it remains unclear to the authors of HelenOS |
253 | queue. Nevertheless, it remains unclear to the authors of HelenOS |
| 250 | whether a simpler but equivalently fair solution exists.</para> |
254 | whether a simpler but equivalently fair solution exists.</para> |
| 251 | 255 | ||
| Line 256... | Line 260... | ||
| 256 | and is used to synchronize writers. The writer's lock operation, |
260 | and is used to synchronize writers. The writer's lock operation, |
| 257 | <emphasis>rwlock_write_lock_timeout</emphasis>, simply tries to acquire |
261 | <emphasis>rwlock_write_lock_timeout</emphasis>, simply tries to acquire |
| 258 | the exclusive mutex. If it succeeds, the writer is granted the rwlock. |
262 | the exclusive mutex. If it succeeds, the writer is granted the rwlock. |
| 259 | However, if the operation fails (e.g. times out), the writer must check |
263 | However, if the operation fails (e.g. times out), the writer must check |
| 260 | for potential readers at the head of the list of sleeping threads |
264 | for potential readers at the head of the list of sleeping threads |
| 261 | associated with the mutex's wait queue and proceed according to the |
265 | associated with the mutex's wait queue and then proceed according to the |
| 262 | procedure outlined above.</para> |
266 | procedure outlined above.</para> |
| 263 | 267 | ||
| 264 | <para>The exclusive mutex plays an important role in reader |
268 | <para>The exclusive mutex plays an important role in reader |
| 265 | synchronization as well. However, a reader doing the reader's lock |
269 | synchronization as well. However, a reader doing the reader's lock |
| 266 | operation, <emphasis>rwlock_read_lock_timeout</emphasis>, may bypass |
270 | operation, <emphasis>rwlock_read_lock_timeout</emphasis>, may bypass |
| 267 | this mutex when it detects that:</para> |
271 | this mutex when it detects that:</para> |
| 268 | 272 | ||
| 269 | <orderedlist> |
273 | <orderedlist> |
| 270 | <listitem> |
274 | <listitem> |
| 271 | <para>there are other readers in the critical section</para> |
275 | <para>there are other readers in the critical section and</para> |
| 272 | </listitem> |
276 | </listitem> |
| 273 | 277 | ||
| 274 | <listitem> |
278 | <listitem> |
| 275 | <para>there are no sleeping threads waiting for the exclusive |
279 | <para>there are no sleeping threads waiting for the exclusive |
| 276 | mutex</para> |
280 | mutex.</para> |
| 277 | </listitem> |
281 | </listitem> |
| 278 | </orderedlist> |
282 | </orderedlist> |
| 279 | 283 | ||
| 280 | <para>If both conditions are true, the reader will bypass the mutex, |
284 | <para>If both conditions are true, the reader will bypass the mutex, |
| 281 | increment the number of readers in the critical section and enter the |
285 | increment the number of readers in the critical section and then enter |
| 282 | critical section. Note that if there are any sleeping threads at the |
286 | the critical section. Note that if there are any sleeping threads at the |
| 283 | beginning of the wait queue, the first of them must be a writer. If the |
287 | beginning of the wait queue, the first must be a writer. If the |
| 284 | conditions are not fulfilled, the reader normally waits until the |
288 | conditions are not fulfilled, the reader normally waits until the |
| 285 | exclusive mutex is granted to it.</para> |
289 | exclusive mutex is granted to it.</para> |
| 286 | </section> |
290 | </section> |
| 287 | 291 | ||
| 288 | <section> |
292 | <section> |