Subversion Repositories HelenOS

Rev

Rev 2089 | Rev 2183 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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