Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
1 jermar 1
/*
2
 * Copyright (C) 2001-2004 Jakub 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
 
382 jermar 29
#include <synch/waitq.h>
1 jermar 30
#include <synch/synch.h>
31
#include <synch/spinlock.h>
382 jermar 32
#include <proc/thread.h>
1 jermar 33
#include <arch/asm.h>
34
#include <arch/types.h>
382 jermar 35
#include <time/timeout.h>
1 jermar 36
#include <arch.h>
382 jermar 37
#include <context.h>
1 jermar 38
#include <list.h>
39
 
382 jermar 40
/** Initialize wait queue
41
 *
42
 * Initialize wait queue.
43
 *
44
 * @param wq Pointer to wait queue to be initialized.
45
 */
1 jermar 46
void waitq_initialize(waitq_t *wq)
47
{
48
    spinlock_initialize(&wq->lock);
49
    list_initialize(&wq->head);
50
    wq->missed_wakeups = 0;
51
}
52
 
382 jermar 53
/** Handle timeout during waitq_sleep_timeout() call
1 jermar 54
 *
382 jermar 55
 * This routine is called when waitq_sleep_timeout() timeouts.
56
 * Interrupts are disabled.
57
 *
58
 * It is supposed to try to remove 'its' thread from the wait queue;
59
 * it can eventually fail to achieve this goal when these two events
60
 * overlap. In that case it behaves just as though there was no
61
 * timeout at all.
62
 *
63
 * @param data Pointer to the thread that called waitq_sleep_timeout().
1 jermar 64
 */
65
void waitq_interrupted_sleep(void *data)
66
{
67
    thread_t *t = (thread_t *) data;
68
    waitq_t *wq;
69
    int do_wakeup = 0;
70
 
71
    spinlock_lock(&threads_lock);
72
    if (!list_member(&t->threads_link, &threads_head))
73
        goto out;
74
 
75
grab_locks:
76
    spinlock_lock(&t->lock);
77
    if (wq = t->sleep_queue) {
78
        if (!spinlock_trylock(&wq->lock)) {
79
            spinlock_unlock(&t->lock);
80
            goto grab_locks; /* avoid deadlock */
81
        }
82
 
83
        list_remove(&t->wq_link);
84
        t->saved_context = t->sleep_timeout_context;
85
        do_wakeup = 1;
86
 
87
        spinlock_unlock(&wq->lock);
88
        t->sleep_queue = NULL;
89
    }
90
 
91
    t->timeout_pending = 0;
92
    spinlock_unlock(&t->lock);
93
 
94
    if (do_wakeup) thread_ready(t);
95
 
96
out:
97
    spinlock_unlock(&threads_lock);
98
}
99
 
382 jermar 100
/** Sleep until either wakeup or timeout occurs
101
 *
1 jermar 102
 * This is a sleep implementation which allows itself to be
103
 * interrupted from the sleep, restoring a failover context.
104
 *
382 jermar 105
 * Sleepers are organised in FIFO fashion in a structure called wait queue.
106
 *
1 jermar 107
 * This function is really basic in that other functions as waitq_sleep()
108
 * and all the *_timeout() functions use it.
109
 *
382 jermar 110
 * @param wq Pointer to wait queue.
404 jermar 111
 * @param usec Timeout in microseconds.
112
 * @param nonblocking Blocking vs. non-blocking operation mode switch.
1 jermar 113
 *
405 jermar 114
 * If usec is greater than zero, regardless of the value of nonblocking,
404 jermar 115
 * the call will not return until either timeout or wakeup comes.
382 jermar 116
 *
404 jermar 117
 * If usec is zero and nonblocking is zero (false), the call
118
 * will not return until wakeup comes.
1 jermar 119
 *
404 jermar 120
 * If usec is zero and nonblocking is non-zero (true), the call will
121
 * immediately return, reporting either success or failure.
122
 *
382 jermar 123
 * @return Returns one of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT,
124
 *         ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED.
125
 *
404 jermar 126
 * ESYNCH_WOULD_BLOCK means that the sleep failed because at the time
127
 * of the call there was no pending wakeup.
382 jermar 128
 *
404 jermar 129
 * ESYNCH_TIMEOUT means that the sleep timed out.
130
 *
131
 * ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was
132
 * a pending wakeup at the time of the call. The caller was not put
133
 * asleep at all.
134
 *
135
 * ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was
136
 * attempted.
1 jermar 137
 */
138
int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking)
139
{
413 jermar 140
    volatile ipl_t ipl; /* must be live after context_restore() */
1 jermar 141
 
142
 
143
restart:
413 jermar 144
    ipl = interrupts_disable();
1 jermar 145
 
146
    /*
147
     * Busy waiting for a delayed timeout.
148
     * This is an important fix for the race condition between
149
     * a delayed timeout and a next call to waitq_sleep_timeout().
150
     * Simply, the thread is not allowed to go to sleep if
151
     * there are timeouts in progress.
152
     */
15 jermar 153
    spinlock_lock(&THREAD->lock);
154
    if (THREAD->timeout_pending) {
155
        spinlock_unlock(&THREAD->lock);
413 jermar 156
        interrupts_restore(ipl);       
1 jermar 157
        goto restart;
158
    }
15 jermar 159
    spinlock_unlock(&THREAD->lock);
1 jermar 160
 
161
    spinlock_lock(&wq->lock);
162
 
163
    /* checks whether to go to sleep at all */
164
    if (wq->missed_wakeups) {
165
        wq->missed_wakeups--;
166
        spinlock_unlock(&wq->lock);
413 jermar 167
        interrupts_restore(ipl);
1 jermar 168
        return ESYNCH_OK_ATOMIC;
169
    }
170
    else {
171
        if (nonblocking && (usec == 0)) {
172
            /* return immediatelly instead of going to sleep */
173
            spinlock_unlock(&wq->lock);
413 jermar 174
            interrupts_restore(ipl);
1 jermar 175
            return ESYNCH_WOULD_BLOCK;
176
        }
177
    }
178
 
179
 
180
    /*
181
     * Now we are firmly decided to go to sleep.
182
     */
15 jermar 183
    spinlock_lock(&THREAD->lock);
1 jermar 184
    if (usec) {
185
        /* We use the timeout variant. */
15 jermar 186
        if (!context_save(&THREAD->sleep_timeout_context)) {
1 jermar 187
            /*
188
             * Short emulation of scheduler() return code.
189
             */
25 jermar 190
            before_thread_runs();
15 jermar 191
            spinlock_unlock(&THREAD->lock);
413 jermar 192
            interrupts_restore(ipl);
1 jermar 193
            return ESYNCH_TIMEOUT;
194
        }
15 jermar 195
        THREAD->timeout_pending = 1;
196
        timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_interrupted_sleep, THREAD);
1 jermar 197
    }
198
 
15 jermar 199
    list_append(&THREAD->wq_link, &wq->head);
1 jermar 200
 
201
    /*
202
     * Suspend execution.
203
     */
15 jermar 204
    THREAD->state = Sleeping;
205
    THREAD->sleep_queue = wq;
1 jermar 206
 
15 jermar 207
    spinlock_unlock(&THREAD->lock);
1 jermar 208
 
209
    scheduler();    /* wq->lock is released in scheduler_separated_stack() */
413 jermar 210
    interrupts_restore(ipl);
1 jermar 211
 
212
    return ESYNCH_OK_BLOCKED;
213
}
214
 
215
 
382 jermar 216
/** Wake up first thread sleeping in a wait queue
217
 *
218
 * Wake up first thread sleeping in a wait queue.
219
 * This is the SMP- and IRQ-safe wrapper meant for
220
 * general use.
221
 *
222
 * Besides its 'normal' wakeup operation, it attempts
223
 * to unregister possible timeout.
224
 *
225
 * @param wq Pointer to wait queue.
226
 * @param all If this is non-zero, all sleeping threads
227
 *        will be woken up and missed count will be zeroed.
1 jermar 228
 */
229
void waitq_wakeup(waitq_t *wq, int all)
230
{
413 jermar 231
    ipl_t ipl;
1 jermar 232
 
413 jermar 233
    ipl = interrupts_disable();
1 jermar 234
    spinlock_lock(&wq->lock);
235
 
236
    _waitq_wakeup_unsafe(wq, all);
237
 
238
    spinlock_unlock(&wq->lock);
413 jermar 239
    interrupts_restore(ipl);   
1 jermar 240
}
241
 
382 jermar 242
/** Internal SMP- and IRQ-unsafe version of waitq_wakeup()
243
 *
244
 * This is the internal SMP- and IRQ-unsafe version
245
 * of waitq_wakeup(). It assumes wq->lock is already
246
 * locked and interrupts are already disabled.
247
 *
248
 * @param wq Pointer to wait queue.
249
 * @param all If this is non-zero, all sleeping threads
250
 *        will be woken up and missed count will be zeroed.
1 jermar 251
 */
252
void _waitq_wakeup_unsafe(waitq_t *wq, int all)
253
{
254
    thread_t *t;
255
 
256
loop:  
257
    if (list_empty(&wq->head)) {
258
        wq->missed_wakeups++;
259
        if (all) wq->missed_wakeups = 0;
260
        return;
261
    }
262
 
263
    t = list_get_instance(wq->head.next, thread_t, wq_link);
264
 
265
    list_remove(&t->wq_link);
266
    spinlock_lock(&t->lock);
267
    if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
268
        t->timeout_pending = 0;
269
    t->sleep_queue = NULL;
270
    spinlock_unlock(&t->lock);
271
 
272
    thread_ready(t);
273
 
274
    if (all) goto loop;
275
}