Subversion Repositories HelenOS

Rev

Rev 2089 | Rev 2183 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2001-2004 Jakub Jermar
1 jermar 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1757 jermar 29
/** @addtogroup sync
1702 cejka 30
 * @{
31
 */
32
 
1248 jermar 33
/**
1702 cejka 34
 * @file
1248 jermar 35
 * @brief   Wait queue.
36
 *
1288 jermar 37
 * Wait queue is the basic synchronization primitive upon which all
1248 jermar 38
 * other synchronization primitives build.
39
 *
40
 * It allows threads to wait for an event in first-come, first-served
41
 * fashion. Conditional operation as well as timeouts and interruptions
42
 * are supported.
43
 */
44
 
382 jermar 45
#include <synch/waitq.h>
1 jermar 46
#include <synch/synch.h>
47
#include <synch/spinlock.h>
382 jermar 48
#include <proc/thread.h>
414 jermar 49
#include <proc/scheduler.h>
1 jermar 50
#include <arch/asm.h>
51
#include <arch/types.h>
382 jermar 52
#include <time/timeout.h>
1 jermar 53
#include <arch.h>
382 jermar 54
#include <context.h>
788 jermar 55
#include <adt/list.h>
1 jermar 56
 
1156 jermar 57
static void waitq_timeouted_sleep(void *data);
58
 
382 jermar 59
/** Initialize wait queue
60
 *
61
 * Initialize wait queue.
62
 *
63
 * @param wq Pointer to wait queue to be initialized.
64
 */
1 jermar 65
void waitq_initialize(waitq_t *wq)
66
{
552 palkovsky 67
    spinlock_initialize(&wq->lock, "waitq_lock");
1 jermar 68
    list_initialize(&wq->head);
69
    wq->missed_wakeups = 0;
70
}
71
 
382 jermar 72
/** Handle timeout during waitq_sleep_timeout() call
1 jermar 73
 *
382 jermar 74
 * This routine is called when waitq_sleep_timeout() timeouts.
75
 * Interrupts are disabled.
76
 *
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
79
 * overlap. In that case it behaves just as though there was no
80
 * timeout at all.
81
 *
82
 * @param data Pointer to the thread that called waitq_sleep_timeout().
1 jermar 83
 */
1156 jermar 84
void waitq_timeouted_sleep(void *data)
1 jermar 85
{
86
    thread_t *t = (thread_t *) data;
87
    waitq_t *wq;
557 jermar 88
    bool do_wakeup = false;
1 jermar 89
 
90
    spinlock_lock(&threads_lock);
1158 jermar 91
    if (!thread_exists(t))
1 jermar 92
        goto out;
93
 
94
grab_locks:
95
    spinlock_lock(&t->lock);
615 palkovsky 96
    if ((wq = t->sleep_queue)) {        /* assignment */
1 jermar 97
        if (!spinlock_trylock(&wq->lock)) {
98
            spinlock_unlock(&t->lock);
557 jermar 99
            goto grab_locks;    /* avoid deadlock */
1 jermar 100
        }
101
 
102
        list_remove(&t->wq_link);
103
        t->saved_context = t->sleep_timeout_context;
557 jermar 104
        do_wakeup = true;
1681 jermar 105
        t->sleep_queue = NULL;
1 jermar 106
        spinlock_unlock(&wq->lock);
107
    }
108
 
557 jermar 109
    t->timeout_pending = false;
1 jermar 110
    spinlock_unlock(&t->lock);
111
 
557 jermar 112
    if (do_wakeup)
113
        thread_ready(t);
1 jermar 114
 
115
out:
116
    spinlock_unlock(&threads_lock);
117
}
118
 
2109 jermar 119
/** Interrupt sleeping thread.
120
 *
121
 * This routine attempts to interrupt a thread from its sleep in a waitqueue.
122
 * If the thread is not found sleeping, no action is taken.
123
 *
124
 * @param t Thread to be interrupted.
125
 */
126
void waitq_interrupt_sleep(thread_t *t)
127
{
128
    waitq_t *wq;
129
    bool do_wakeup = false;
130
    ipl_t ipl;
1156 jermar 131
 
2109 jermar 132
    ipl = interrupts_disable();
133
    spinlock_lock(&threads_lock);
134
    if (!thread_exists(t))
135
        goto out;
136
 
137
grab_locks:
138
    spinlock_lock(&t->lock);
139
    if ((wq = t->sleep_queue)) {        /* assignment */
140
        if (!(t->sleep_interruptible)) {
141
            /*
142
             * The sleep cannot be interrupted.
143
             */
144
            spinlock_unlock(&t->lock);
145
            goto out;
146
        }
147
 
148
        if (!spinlock_trylock(&wq->lock)) {
149
            spinlock_unlock(&t->lock);
150
            goto grab_locks;    /* avoid deadlock */
151
        }
152
 
153
        if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
154
            t->timeout_pending = false;
155
 
156
        list_remove(&t->wq_link);
157
        t->saved_context = t->sleep_interruption_context;
158
        do_wakeup = true;
159
        t->sleep_queue = NULL;
160
        spinlock_unlock(&wq->lock);
161
    }
162
    spinlock_unlock(&t->lock);
163
 
164
    if (do_wakeup)
165
        thread_ready(t);
166
 
167
out:
168
    spinlock_unlock(&threads_lock);
169
    interrupts_restore(ipl);
170
}
171
 
1156 jermar 172
/** Sleep until either wakeup, timeout or interruption occurs
173
 *
1502 jermar 174
 * This is a sleep implementation which allows itself to time out or to be
1 jermar 175
 * interrupted from the sleep, restoring a failover context.
176
 *
1375 jermar 177
 * Sleepers are organised in a FIFO fashion in a structure called wait queue.
382 jermar 178
 *
1 jermar 179
 * This function is really basic in that other functions as waitq_sleep()
180
 * and all the *_timeout() functions use it.
181
 *
382 jermar 182
 * @param wq Pointer to wait queue.
404 jermar 183
 * @param usec Timeout in microseconds.
1502 jermar 184
 * @param flags Specify mode of the sleep.
1 jermar 185
 *
1502 jermar 186
 * The sleep can be interrupted only if the
187
 * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
2087 jermar 188
 *
1502 jermar 189
 * If usec is greater than zero, regardless of the value of the
2067 jermar 190
 * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either
191
 * timeout, interruption or wakeup comes.
382 jermar 192
 *
2067 jermar 193
 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags,
194
 * the call will not return until wakeup or interruption comes.
1 jermar 195
 *
2067 jermar 196
 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the
197
 * call will immediately return, reporting either success or failure.
404 jermar 198
 *
2067 jermar 199
 * @return One of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, ESYNCH_INTERRUPTED,
200
 * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED.
382 jermar 201
 *
2067 jermar 202
 * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time of the
203
 * call there was no pending wakeup.
382 jermar 204
 *
1248 jermar 205
 * @li ESYNCH_TIMEOUT means that the sleep timed out.
404 jermar 206
 *
1248 jermar 207
 * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread.
1156 jermar 208
 *
1248 jermar 209
 * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was
404 jermar 210
 * a pending wakeup at the time of the call. The caller was not put
211
 * asleep at all.
212
 *
1248 jermar 213
 * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was
404 jermar 214
 * attempted.
1 jermar 215
 */
1780 jermar 216
int waitq_sleep_timeout(waitq_t *wq, uint32_t usec, int flags)
1 jermar 217
{
1375 jermar 218
    ipl_t ipl;
219
    int rc;
1 jermar 220
 
1375 jermar 221
    ipl = waitq_sleep_prepare(wq);
1502 jermar 222
    rc = waitq_sleep_timeout_unsafe(wq, usec, flags);
1375 jermar 223
    waitq_sleep_finish(wq, rc, ipl);
224
    return rc;
225
}
226
 
227
/** Prepare to sleep in a waitq.
228
 *
229
 * This function will return holding the lock of the wait queue
230
 * and interrupts disabled.
231
 *
232
 * @param wq Wait queue.
233
 *
234
 * @return Interrupt level as it existed on entry to this function.
235
 */
236
ipl_t waitq_sleep_prepare(waitq_t *wq)
237
{
238
    ipl_t ipl;
1 jermar 239
 
240
restart:
413 jermar 241
    ipl = interrupts_disable();
1375 jermar 242
 
1467 jermar 243
    if (THREAD) {   /* needed during system initiailzation */
244
        /*
245
         * Busy waiting for a delayed timeout.
246
         * This is an important fix for the race condition between
247
         * a delayed timeout and a next call to waitq_sleep_timeout().
248
         * Simply, the thread is not allowed to go to sleep if
249
         * there are timeouts in progress.
250
         */
251
        spinlock_lock(&THREAD->lock);
252
        if (THREAD->timeout_pending) {
253
            spinlock_unlock(&THREAD->lock);
254
            interrupts_restore(ipl);
255
            goto restart;
256
        }
15 jermar 257
        spinlock_unlock(&THREAD->lock);
1 jermar 258
    }
1375 jermar 259
 
1 jermar 260
    spinlock_lock(&wq->lock);
1375 jermar 261
    return ipl;
262
}
263
 
264
/** Finish waiting in a wait queue.
265
 *
266
 * This function restores interrupts to the state that existed prior
267
 * to the call to waitq_sleep_prepare(). If necessary, the wait queue
268
 * lock is released.
269
 *
270
 * @param wq Wait queue.
271
 * @param rc Return code of waitq_sleep_timeout_unsafe().
272
 * @param ipl Interrupt level returned by waitq_sleep_prepare().
273
 */
274
void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl)
275
{
276
    switch (rc) {
277
    case ESYNCH_WOULD_BLOCK:
278
    case ESYNCH_OK_ATOMIC:
279
        spinlock_unlock(&wq->lock);
280
        break;
281
    default:
282
        break;
283
    }
284
    interrupts_restore(ipl);
285
}
286
 
287
/** Internal implementation of waitq_sleep_timeout().
288
 *
289
 * This function implements logic of sleeping in a wait queue.
290
 * This call must be preceeded by a call to waitq_sleep_prepare()
291
 * and followed by a call to waitq_slee_finish().
292
 *
293
 * @param wq See waitq_sleep_timeout().
294
 * @param usec See waitq_sleep_timeout().
1502 jermar 295
 * @param flags See waitq_sleep_timeout().
1375 jermar 296
 *
297
 * @return See waitq_sleep_timeout().
298
 */
1780 jermar 299
int waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, int flags)
1375 jermar 300
{
1 jermar 301
    /* checks whether to go to sleep at all */
302
    if (wq->missed_wakeups) {
303
        wq->missed_wakeups--;
304
        return ESYNCH_OK_ATOMIC;
305
    }
306
    else {
1502 jermar 307
        if ((flags & SYNCH_FLAGS_NON_BLOCKING) && (usec == 0)) {
1 jermar 308
            /* return immediatelly instead of going to sleep */
309
            return ESYNCH_WOULD_BLOCK;
310
        }
311
    }
312
 
313
    /*
314
     * Now we are firmly decided to go to sleep.
315
     */
15 jermar 316
    spinlock_lock(&THREAD->lock);
1156 jermar 317
 
1580 jermar 318
    if (flags & SYNCH_FLAGS_INTERRUPTIBLE) {
1579 jermar 319
 
1502 jermar 320
        /*
1580 jermar 321
         * If the thread was already interrupted,
322
         * don't go to sleep at all.
323
         */
324
        if (THREAD->interrupted) {
325
            spinlock_unlock(&THREAD->lock);
326
            spinlock_unlock(&wq->lock);
327
            return ESYNCH_INTERRUPTED;
328
        }
329
 
330
        /*
1502 jermar 331
         * Set context that will be restored if the sleep
332
         * of this thread is ever interrupted.
333
         */
334
        THREAD->sleep_interruptible = true;
335
        if (!context_save(&THREAD->sleep_interruption_context)) {
336
            /* Short emulation of scheduler() return code. */
337
            spinlock_unlock(&THREAD->lock);
338
            return ESYNCH_INTERRUPTED;
339
        }
1580 jermar 340
 
1502 jermar 341
    } else {
342
        THREAD->sleep_interruptible = false;
1156 jermar 343
    }
344
 
1 jermar 345
    if (usec) {
346
        /* We use the timeout variant. */
15 jermar 347
        if (!context_save(&THREAD->sleep_timeout_context)) {
1156 jermar 348
            /* Short emulation of scheduler() return code. */
15 jermar 349
            spinlock_unlock(&THREAD->lock);
1 jermar 350
            return ESYNCH_TIMEOUT;
351
        }
557 jermar 352
        THREAD->timeout_pending = true;
2067 jermar 353
        timeout_register(&THREAD->sleep_timeout, (uint64_t) usec,
2087 jermar 354
            waitq_timeouted_sleep, THREAD);
1 jermar 355
    }
356
 
15 jermar 357
    list_append(&THREAD->wq_link, &wq->head);
1 jermar 358
 
359
    /*
360
     * Suspend execution.
361
     */
15 jermar 362
    THREAD->state = Sleeping;
363
    THREAD->sleep_queue = wq;
1 jermar 364
 
15 jermar 365
    spinlock_unlock(&THREAD->lock);
1 jermar 366
 
2067 jermar 367
    /* wq->lock is released in scheduler_separated_stack() */
368
    scheduler();
1 jermar 369
 
370
    return ESYNCH_OK_BLOCKED;
371
}
372
 
373
 
382 jermar 374
/** Wake up first thread sleeping in a wait queue
375
 *
2067 jermar 376
 * Wake up first thread sleeping in a wait queue. This is the SMP- and IRQ-safe
377
 * wrapper meant for general use.
382 jermar 378
 *
2067 jermar 379
 * Besides its 'normal' wakeup operation, it attempts to unregister possible
380
 * timeout.
382 jermar 381
 *
382
 * @param wq Pointer to wait queue.
2067 jermar 383
 * @param all If this is non-zero, all sleeping threads will be woken up and
384
 *  missed count will be zeroed.
1 jermar 385
 */
557 jermar 386
void waitq_wakeup(waitq_t *wq, bool all)
1 jermar 387
{
413 jermar 388
    ipl_t ipl;
1 jermar 389
 
413 jermar 390
    ipl = interrupts_disable();
1 jermar 391
    spinlock_lock(&wq->lock);
392
 
393
    _waitq_wakeup_unsafe(wq, all);
394
 
395
    spinlock_unlock(&wq->lock);
413 jermar 396
    interrupts_restore(ipl);   
1 jermar 397
}
398
 
382 jermar 399
/** Internal SMP- and IRQ-unsafe version of waitq_wakeup()
400
 *
2067 jermar 401
 * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup(). It
402
 * assumes wq->lock is already locked and interrupts are already disabled.
382 jermar 403
 *
404
 * @param wq Pointer to wait queue.
2067 jermar 405
 * @param all If this is non-zero, all sleeping threads will be woken up and
406
 *  missed count will be zeroed.
1 jermar 407
 */
557 jermar 408
void _waitq_wakeup_unsafe(waitq_t *wq, bool all)
1 jermar 409
{
410
    thread_t *t;
411
 
412
loop:  
413
    if (list_empty(&wq->head)) {
414
        wq->missed_wakeups++;
557 jermar 415
        if (all)
416
            wq->missed_wakeups = 0;
1 jermar 417
        return;
418
    }
419
 
420
    t = list_get_instance(wq->head.next, thread_t, wq_link);
421
 
1681 jermar 422
    /*
423
     * Lock the thread prior to removing it from the wq.
424
     * This is not necessary because of mutual exclusion
425
     * (the link belongs to the wait queue), but because
426
     * of synchronization with waitq_timeouted_sleep()
2089 decky 427
     * and thread_interrupt_sleep().
1681 jermar 428
     *
429
     * In order for these two functions to work, the following
430
     * invariant must hold:
431
     *
432
     * t->sleep_queue != NULL <=> t sleeps in a wait queue
433
     *
434
     * For an observer who locks the thread, the invariant
435
     * holds only when the lock is held prior to removing
436
     * it from the wait queue.
437
     */
438
    spinlock_lock(&t->lock);
1 jermar 439
    list_remove(&t->wq_link);
1681 jermar 440
 
1 jermar 441
    if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
557 jermar 442
        t->timeout_pending = false;
1 jermar 443
    t->sleep_queue = NULL;
444
    spinlock_unlock(&t->lock);
445
 
446
    thread_ready(t);
447
 
557 jermar 448
    if (all)
449
        goto loop;
1 jermar 450
}
1702 cejka 451
 
1757 jermar 452
/** @}
1702 cejka 453
 */