Rev 2787 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2787 | Rev 2925 | ||
|---|---|---|---|
| Line 52... | Line 52... | ||
| 52 | #include <time/timeout.h> |
52 | #include <time/timeout.h> |
| 53 | #include <arch.h> |
53 | #include <arch.h> |
| 54 | #include <context.h> |
54 | #include <context.h> |
| 55 | #include <adt/list.h> |
55 | #include <adt/list.h> |
| 56 | 56 | ||
| 57 | static void waitq_timeouted_sleep(void *data); |
57 | static void waitq_sleep_timed_out(void *data); |
| 58 | 58 | ||
| 59 | /** Initialize wait queue |
59 | /** Initialize wait queue |
| 60 | * |
60 | * |
| 61 | * Initialize wait queue. |
61 | * Initialize wait queue. |
| 62 | * |
62 | * |
| 63 | * @param wq Pointer to wait queue to be initialized. |
63 | * @param wq Pointer to wait queue to be initialized. |
| 64 | */ |
64 | */ |
| 65 | void waitq_initialize(waitq_t *wq) |
65 | void waitq_initialize(waitq_t *wq) |
| 66 | { |
66 | { |
| 67 | spinlock_initialize(&wq->lock, "waitq_lock"); |
67 | spinlock_initialize(&wq->lock, "waitq_lock"); |
| 68 | list_initialize(&wq->head); |
68 | list_initialize(&wq->head); |
| 69 | wq->missed_wakeups = 0; |
69 | wq->missed_wakeups = 0; |
| 70 | } |
70 | } |
| 71 | 71 | ||
| 72 | /** Handle timeout during waitq_sleep_timeout() call |
72 | /** Handle timeout during waitq_sleep_timeout() call |
| 73 | * |
73 | * |
| 74 | * This routine is called when waitq_sleep_timeout() timeouts. |
74 | * This routine is called when waitq_sleep_timeout() times out. |
| 75 | * Interrupts are disabled. |
75 | * Interrupts are disabled. |
| 76 | * |
76 | * |
| 77 | * It is supposed to try to remove 'its' thread from the wait queue; |
77 | * It is supposed to try to remove 'its' thread from the wait queue; |
| 78 | * it can eventually fail to achieve this goal when these two events |
78 | * it can eventually fail to achieve this goal when these two events |
| 79 | * overlap. In that case it behaves just as though there was no |
79 | * overlap. In that case it behaves just as though there was no |
| 80 | * timeout at all. |
80 | * timeout at all. |
| 81 | * |
81 | * |
| 82 | * @param data Pointer to the thread that called waitq_sleep_timeout(). |
82 | * @param data Pointer to the thread that called waitq_sleep_timeout(). |
| 83 | */ |
83 | */ |
| 84 | void waitq_timeouted_sleep(void *data) |
84 | void waitq_sleep_timed_out(void *data) |
| 85 | { |
85 | { |
| 86 | thread_t *t = (thread_t *) data; |
86 | thread_t *t = (thread_t *) data; |
| 87 | waitq_t *wq; |
87 | waitq_t *wq; |
| 88 | bool do_wakeup = false; |
88 | bool do_wakeup = false; |
| 89 | DEADLOCK_PROBE_INIT(p_wqlock); |
89 | DEADLOCK_PROBE_INIT(p_wqlock); |
| Line 121... | Line 121... | ||
| 121 | /** Interrupt sleeping thread. |
121 | /** Interrupt sleeping thread. |
| 122 | * |
122 | * |
| 123 | * This routine attempts to interrupt a thread from its sleep in a waitqueue. |
123 | * This routine attempts to interrupt a thread from its sleep in a waitqueue. |
| 124 | * If the thread is not found sleeping, no action is taken. |
124 | * If the thread is not found sleeping, no action is taken. |
| 125 | * |
125 | * |
| 126 | * @param t Thread to be interrupted. |
126 | * @param t Thread to be interrupted. |
| 127 | */ |
127 | */ |
| 128 | void waitq_interrupt_sleep(thread_t *t) |
128 | void waitq_interrupt_sleep(thread_t *t) |
| 129 | { |
129 | { |
| 130 | waitq_t *wq; |
130 | waitq_t *wq; |
| 131 | bool do_wakeup = false; |
131 | bool do_wakeup = false; |
| Line 181... | Line 181... | ||
| 181 | * Sleepers are organised in a FIFO fashion in a structure called wait queue. |
181 | * Sleepers are organised in a FIFO fashion in a structure called wait queue. |
| 182 | * |
182 | * |
| 183 | * This function is really basic in that other functions as waitq_sleep() |
183 | * This function is really basic in that other functions as waitq_sleep() |
| 184 | * and all the *_timeout() functions use it. |
184 | * and all the *_timeout() functions use it. |
| 185 | * |
185 | * |
| 186 | * @param wq Pointer to wait queue. |
186 | * @param wq Pointer to wait queue. |
| 187 | * @param usec Timeout in microseconds. |
187 | * @param usec Timeout in microseconds. |
| 188 | * @param flags Specify mode of the sleep. |
188 | * @param flags Specify mode of the sleep. |
| 189 | * |
189 | * |
| 190 | * The sleep can be interrupted only if the |
190 | * The sleep can be interrupted only if the |
| 191 | * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags. |
191 | * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags. |
| 192 | * |
192 | * |
| 193 | * If usec is greater than zero, regardless of the value of the |
193 | * If usec is greater than zero, regardless of the value of the |
| Line 198... | Line 198... | ||
| 198 | * the call will not return until wakeup or interruption comes. |
198 | * the call will not return until wakeup or interruption comes. |
| 199 | * |
199 | * |
| 200 | * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the |
200 | * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the |
| 201 | * call will immediately return, reporting either success or failure. |
201 | * call will immediately return, reporting either success or failure. |
| 202 | * |
202 | * |
| 203 | * @return One of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, ESYNCH_INTERRUPTED, |
203 | * @return Returns one of ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, |
| 204 | * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED. |
204 | * ESYNCH_INTERRUPTED, ESYNCH_OK_ATOMIC and |
| - | 205 | * ESYNCH_OK_BLOCKED. |
|
| 205 | * |
206 | * |
| 206 | * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time of the |
207 | * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time of |
| 207 | * call there was no pending wakeup. |
208 | * the call there was no pending wakeup. |
| 208 | * |
209 | * |
| 209 | * @li ESYNCH_TIMEOUT means that the sleep timed out. |
210 | * @li ESYNCH_TIMEOUT means that the sleep timed out. |
| 210 | * |
211 | * |
| 211 | * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread. |
212 | * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread. |
| 212 | * |
213 | * |
| 213 | * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was |
214 | * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was |
| 214 | * a pending wakeup at the time of the call. The caller was not put |
215 | * a pending wakeup at the time of the call. The caller was not put |
| 215 | * asleep at all. |
216 | * asleep at all. |
| 216 | * |
217 | * |
| 217 | * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was |
218 | * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was |
| 218 | * attempted. |
219 | * attempted. |
| 219 | */ |
220 | */ |
| 220 | int waitq_sleep_timeout(waitq_t *wq, uint32_t usec, int flags) |
221 | int waitq_sleep_timeout(waitq_t *wq, uint32_t usec, int flags) |
| 221 | { |
222 | { |
| 222 | ipl_t ipl; |
223 | ipl_t ipl; |
| 223 | int rc; |
224 | int rc; |
| Line 231... | Line 232... | ||
| 231 | /** Prepare to sleep in a waitq. |
232 | /** Prepare to sleep in a waitq. |
| 232 | * |
233 | * |
| 233 | * This function will return holding the lock of the wait queue |
234 | * This function will return holding the lock of the wait queue |
| 234 | * and interrupts disabled. |
235 | * and interrupts disabled. |
| 235 | * |
236 | * |
| 236 | * @param wq Wait queue. |
237 | * @param wq Wait queue. |
| 237 | * |
238 | * |
| 238 | * @return Interrupt level as it existed on entry to this function. |
239 | * @return Interrupt level as it existed on entry to this function. |
| 239 | */ |
240 | */ |
| 240 | ipl_t waitq_sleep_prepare(waitq_t *wq) |
241 | ipl_t waitq_sleep_prepare(waitq_t *wq) |
| 241 | { |
242 | { |
| 242 | ipl_t ipl; |
243 | ipl_t ipl; |
| 243 | 244 | ||
| Line 269... | Line 270... | ||
| 269 | * |
270 | * |
| 270 | * This function restores interrupts to the state that existed prior |
271 | * This function restores interrupts to the state that existed prior |
| 271 | * to the call to waitq_sleep_prepare(). If necessary, the wait queue |
272 | * to the call to waitq_sleep_prepare(). If necessary, the wait queue |
| 272 | * lock is released. |
273 | * lock is released. |
| 273 | * |
274 | * |
| 274 | * @param wq Wait queue. |
275 | * @param wq Wait queue. |
| 275 | * @param rc Return code of waitq_sleep_timeout_unsafe(). |
276 | * @param rc Return code of waitq_sleep_timeout_unsafe(). |
| 276 | * @param ipl Interrupt level returned by waitq_sleep_prepare(). |
277 | * @param ipl Interrupt level returned by waitq_sleep_prepare(). |
| 277 | */ |
278 | */ |
| 278 | void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl) |
279 | void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl) |
| 279 | { |
280 | { |
| 280 | switch (rc) { |
281 | switch (rc) { |
| 281 | case ESYNCH_WOULD_BLOCK: |
282 | case ESYNCH_WOULD_BLOCK: |
| Line 289... | Line 290... | ||
| 289 | } |
290 | } |
| 290 | 291 | ||
| 291 | /** Internal implementation of waitq_sleep_timeout(). |
292 | /** Internal implementation of waitq_sleep_timeout(). |
| 292 | * |
293 | * |
| 293 | * This function implements logic of sleeping in a wait queue. |
294 | * This function implements logic of sleeping in a wait queue. |
| 294 | * This call must be preceeded by a call to waitq_sleep_prepare() |
295 | * This call must be preceded by a call to waitq_sleep_prepare() |
| 295 | * and followed by a call to waitq_slee_finish(). |
296 | * and followed by a call to waitq_sleep_finish(). |
| 296 | * |
297 | * |
| 297 | * @param wq See waitq_sleep_timeout(). |
298 | * @param wq See waitq_sleep_timeout(). |
| 298 | * @param usec See waitq_sleep_timeout(). |
299 | * @param usec See waitq_sleep_timeout(). |
| 299 | * @param flags See waitq_sleep_timeout(). |
300 | * @param flags See waitq_sleep_timeout(). |
| 300 | * |
301 | * |
| 301 | * @return See waitq_sleep_timeout(). |
302 | * @return See waitq_sleep_timeout(). |
| 302 | */ |
303 | */ |
| 303 | int waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, int flags) |
304 | int waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, int flags) |
| 304 | { |
305 | { |
| 305 | /* checks whether to go to sleep at all */ |
306 | /* checks whether to go to sleep at all */ |
| 306 | if (wq->missed_wakeups) { |
307 | if (wq->missed_wakeups) { |
| Line 353... | Line 354... | ||
| 353 | spinlock_unlock(&THREAD->lock); |
354 | spinlock_unlock(&THREAD->lock); |
| 354 | return ESYNCH_TIMEOUT; |
355 | return ESYNCH_TIMEOUT; |
| 355 | } |
356 | } |
| 356 | THREAD->timeout_pending = true; |
357 | THREAD->timeout_pending = true; |
| 357 | timeout_register(&THREAD->sleep_timeout, (uint64_t) usec, |
358 | timeout_register(&THREAD->sleep_timeout, (uint64_t) usec, |
| 358 | waitq_timeouted_sleep, THREAD); |
359 | waitq_sleep_timed_out, THREAD); |
| 359 | } |
360 | } |
| 360 | 361 | ||
| 361 | list_append(&THREAD->wq_link, &wq->head); |
362 | list_append(&THREAD->wq_link, &wq->head); |
| 362 | 363 | ||
| 363 | /* |
364 | /* |
| Line 381... | Line 382... | ||
| 381 | * wrapper meant for general use. |
382 | * wrapper meant for general use. |
| 382 | * |
383 | * |
| 383 | * Besides its 'normal' wakeup operation, it attempts to unregister possible |
384 | * Besides its 'normal' wakeup operation, it attempts to unregister possible |
| 384 | * timeout. |
385 | * timeout. |
| 385 | * |
386 | * |
| 386 | * @param wq Pointer to wait queue. |
387 | * @param wq Pointer to wait queue. |
| 387 | * @param mode Wakeup mode. |
388 | * @param mode Wakeup mode. |
| 388 | */ |
389 | */ |
| 389 | void waitq_wakeup(waitq_t *wq, wakeup_mode_t mode) |
390 | void waitq_wakeup(waitq_t *wq, wakeup_mode_t mode) |
| 390 | { |
391 | { |
| 391 | ipl_t ipl; |
392 | ipl_t ipl; |
| 392 | 393 | ||
| Line 402... | Line 403... | ||
| 402 | /** Internal SMP- and IRQ-unsafe version of waitq_wakeup() |
403 | /** Internal SMP- and IRQ-unsafe version of waitq_wakeup() |
| 403 | * |
404 | * |
| 404 | * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup(). It |
405 | * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup(). It |
| 405 | * assumes wq->lock is already locked and interrupts are already disabled. |
406 | * assumes wq->lock is already locked and interrupts are already disabled. |
| 406 | * |
407 | * |
| 407 | * @param wq Pointer to wait queue. |
408 | * @param wq Pointer to wait queue. |
| 408 | * @param mode If mode is WAKEUP_FIRST, then the longest waiting thread, |
409 | * @param mode If mode is WAKEUP_FIRST, then the longest waiting |
| 409 | * if any, is woken up. If mode is WAKEUP_ALL, then all |
410 | * thread, if any, is woken up. If mode is WAKEUP_ALL, then |
| 410 | * waiting threads, if any, are woken up. If there are no |
411 | * all waiting threads, if any, are woken up. If there are |
| 411 | * waiting threads to be woken up, the missed wakeup is |
412 | * no waiting threads to be woken up, the missed wakeup is |
| 412 | * recorded in the wait queue. |
413 | * recorded in the wait queue. |
| 413 | */ |
414 | */ |
| 414 | void _waitq_wakeup_unsafe(waitq_t *wq, wakeup_mode_t mode) |
415 | void _waitq_wakeup_unsafe(waitq_t *wq, wakeup_mode_t mode) |
| 415 | { |
416 | { |
| 416 | thread_t *t; |
417 | thread_t *t; |
| 417 | count_t count = 0; |
418 | count_t count = 0; |
| Line 429... | Line 430... | ||
| 429 | 430 | ||
| 430 | /* |
431 | /* |
| 431 | * Lock the thread prior to removing it from the wq. |
432 | * Lock the thread prior to removing it from the wq. |
| 432 | * This is not necessary because of mutual exclusion |
433 | * This is not necessary because of mutual exclusion |
| 433 | * (the link belongs to the wait queue), but because |
434 | * (the link belongs to the wait queue), but because |
| 434 | * of synchronization with waitq_timeouted_sleep() |
435 | * of synchronization with waitq_sleep_timed_out() |
| 435 | * and thread_interrupt_sleep(). |
436 | * and thread_interrupt_sleep(). |
| 436 | * |
437 | * |
| 437 | * In order for these two functions to work, the following |
438 | * In order for these two functions to work, the following |
| 438 | * invariant must hold: |
439 | * invariant must hold: |
| 439 | * |
440 | * |