Subversion Repositories HelenOS-historic

Rev

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

Rev 1467 Rev 1502
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
/**
29
/**
30
 * @file    waitq.c
30
 * @file    waitq.c
31
 * @brief   Wait queue.
31
 * @brief   Wait queue.
32
 *
32
 *
33
 * Wait queue is the basic synchronization primitive upon which all
33
 * Wait queue is the basic synchronization primitive upon which all
34
 * other synchronization primitives build.
34
 * other synchronization primitives build.
35
 *
35
 *
36
 * It allows threads to wait for an event in first-come, first-served
36
 * It allows threads to wait for an event in first-come, first-served
37
 * fashion. Conditional operation as well as timeouts and interruptions
37
 * fashion. Conditional operation as well as timeouts and interruptions
38
 * are supported.
38
 * are supported.
39
 */
39
 */
40
 
40
 
41
#include <synch/waitq.h>
41
#include <synch/waitq.h>
42
#include <synch/synch.h>
42
#include <synch/synch.h>
43
#include <synch/spinlock.h>
43
#include <synch/spinlock.h>
44
#include <proc/thread.h>
44
#include <proc/thread.h>
45
#include <proc/scheduler.h>
45
#include <proc/scheduler.h>
46
#include <arch/asm.h>
46
#include <arch/asm.h>
47
#include <arch/types.h>
47
#include <arch/types.h>
48
#include <typedefs.h>
48
#include <typedefs.h>
49
#include <time/timeout.h>
49
#include <time/timeout.h>
50
#include <arch.h>
50
#include <arch.h>
51
#include <context.h>
51
#include <context.h>
52
#include <adt/list.h>
52
#include <adt/list.h>
53
 
53
 
54
static void waitq_timeouted_sleep(void *data);
54
static void waitq_timeouted_sleep(void *data);
55
 
55
 
56
/** Initialize wait queue
56
/** Initialize wait queue
57
 *
57
 *
58
 * Initialize wait queue.
58
 * Initialize wait queue.
59
 *
59
 *
60
 * @param wq Pointer to wait queue to be initialized.
60
 * @param wq Pointer to wait queue to be initialized.
61
 */
61
 */
62
void waitq_initialize(waitq_t *wq)
62
void waitq_initialize(waitq_t *wq)
63
{
63
{
64
    spinlock_initialize(&wq->lock, "waitq_lock");
64
    spinlock_initialize(&wq->lock, "waitq_lock");
65
    list_initialize(&wq->head);
65
    list_initialize(&wq->head);
66
    wq->missed_wakeups = 0;
66
    wq->missed_wakeups = 0;
67
}
67
}
68
 
68
 
69
/** Handle timeout during waitq_sleep_timeout() call
69
/** Handle timeout during waitq_sleep_timeout() call
70
 *
70
 *
71
 * This routine is called when waitq_sleep_timeout() timeouts.
71
 * This routine is called when waitq_sleep_timeout() timeouts.
72
 * Interrupts are disabled.
72
 * Interrupts are disabled.
73
 *
73
 *
74
 * It is supposed to try to remove 'its' thread from the wait queue;
74
 * It is supposed to try to remove 'its' thread from the wait queue;
75
 * it can eventually fail to achieve this goal when these two events
75
 * it can eventually fail to achieve this goal when these two events
76
 * overlap. In that case it behaves just as though there was no
76
 * overlap. In that case it behaves just as though there was no
77
 * timeout at all.
77
 * timeout at all.
78
 *
78
 *
79
 * @param data Pointer to the thread that called waitq_sleep_timeout().
79
 * @param data Pointer to the thread that called waitq_sleep_timeout().
80
 */
80
 */
81
void waitq_timeouted_sleep(void *data)
81
void waitq_timeouted_sleep(void *data)
82
{
82
{
83
    thread_t *t = (thread_t *) data;
83
    thread_t *t = (thread_t *) data;
84
    waitq_t *wq;
84
    waitq_t *wq;
85
    bool do_wakeup = false;
85
    bool do_wakeup = false;
86
 
86
 
87
    spinlock_lock(&threads_lock);
87
    spinlock_lock(&threads_lock);
88
    if (!thread_exists(t))
88
    if (!thread_exists(t))
89
        goto out;
89
        goto out;
90
 
90
 
91
grab_locks:
91
grab_locks:
92
    spinlock_lock(&t->lock);
92
    spinlock_lock(&t->lock);
93
    if ((wq = t->sleep_queue)) {        /* assignment */
93
    if ((wq = t->sleep_queue)) {        /* assignment */
94
        if (!spinlock_trylock(&wq->lock)) {
94
        if (!spinlock_trylock(&wq->lock)) {
95
            spinlock_unlock(&t->lock);
95
            spinlock_unlock(&t->lock);
96
            goto grab_locks;    /* avoid deadlock */
96
            goto grab_locks;    /* avoid deadlock */
97
        }
97
        }
98
 
98
 
99
        list_remove(&t->wq_link);
99
        list_remove(&t->wq_link);
100
        t->saved_context = t->sleep_timeout_context;
100
        t->saved_context = t->sleep_timeout_context;
101
        do_wakeup = true;
101
        do_wakeup = true;
102
       
102
       
103
        spinlock_unlock(&wq->lock);
103
        spinlock_unlock(&wq->lock);
104
        t->sleep_queue = NULL;
104
        t->sleep_queue = NULL;
105
    }
105
    }
106
   
106
   
107
    t->timeout_pending = false;
107
    t->timeout_pending = false;
108
    spinlock_unlock(&t->lock);
108
    spinlock_unlock(&t->lock);
109
   
109
   
110
    if (do_wakeup)
110
    if (do_wakeup)
111
        thread_ready(t);
111
        thread_ready(t);
112
 
112
 
113
out:
113
out:
114
    spinlock_unlock(&threads_lock);
114
    spinlock_unlock(&threads_lock);
115
}
115
}
116
 
116
 
117
/** Interrupt sleeping thread.
117
/** Interrupt sleeping thread.
118
 *
118
 *
119
 * This routine attempts to interrupt a thread from its sleep in a waitqueue.
119
 * This routine attempts to interrupt a thread from its sleep in a waitqueue.
120
 * If the thread is not found sleeping, no action is taken.
120
 * If the thread is not found sleeping, no action is taken.
121
 *
121
 *
122
 * @param t Thread to be interrupted.
122
 * @param t Thread to be interrupted.
123
 */
123
 */
124
void waitq_interrupt_sleep(thread_t *t)
124
void waitq_interrupt_sleep(thread_t *t)
125
{
125
{
126
    waitq_t *wq;
126
    waitq_t *wq;
127
    bool do_wakeup = false;
127
    bool do_wakeup = false;
128
    ipl_t ipl;
128
    ipl_t ipl;
129
 
129
 
130
    ipl = interrupts_disable();
130
    ipl = interrupts_disable();
131
    spinlock_lock(&threads_lock);
131
    spinlock_lock(&threads_lock);
132
    if (!thread_exists(t))
132
    if (!thread_exists(t))
133
        goto out;
133
        goto out;
134
 
134
 
135
grab_locks:
135
grab_locks:
136
    spinlock_lock(&t->lock);
136
    spinlock_lock(&t->lock);
137
    if ((wq = t->sleep_queue)) {        /* assignment */
137
    if ((wq = t->sleep_queue)) {        /* assignment */
-
 
138
        if (!(t->sleep_interruptible)) {
-
 
139
            /*
-
 
140
             * The sleep cannot be interrupted.
-
 
141
             */
-
 
142
            spinlock_unlock(&t->lock);
-
 
143
            goto out;
-
 
144
        }
-
 
145
           
138
        if (!spinlock_trylock(&wq->lock)) {
146
        if (!spinlock_trylock(&wq->lock)) {
139
            spinlock_unlock(&t->lock);
147
            spinlock_unlock(&t->lock);
140
            goto grab_locks;    /* avoid deadlock */
148
            goto grab_locks;    /* avoid deadlock */
141
        }
149
        }
142
 
150
 
143
        list_remove(&t->wq_link);
151
        list_remove(&t->wq_link);
144
        t->saved_context = t->sleep_interruption_context;
152
        t->saved_context = t->sleep_interruption_context;
145
        do_wakeup = true;
153
        do_wakeup = true;
146
       
154
       
147
        spinlock_unlock(&wq->lock);
155
        spinlock_unlock(&wq->lock);
148
        t->sleep_queue = NULL;
156
        t->sleep_queue = NULL;
149
    }
157
    }
150
    spinlock_unlock(&t->lock);
158
    spinlock_unlock(&t->lock);
151
 
159
 
152
    if (do_wakeup)
160
    if (do_wakeup)
153
        thread_ready(t);
161
        thread_ready(t);
154
 
162
 
155
out:
163
out:
156
    spinlock_unlock(&threads_lock);
164
    spinlock_unlock(&threads_lock);
157
    interrupts_restore(ipl);
165
    interrupts_restore(ipl);
158
}
166
}
159
 
167
 
160
/** Sleep until either wakeup, timeout or interruption occurs
168
/** Sleep until either wakeup, timeout or interruption occurs
161
 *
169
 *
162
 * This is a sleep implementation which allows itself to be
170
 * This is a sleep implementation which allows itself to time out or to be
163
 * interrupted from the sleep, restoring a failover context.
171
 * interrupted from the sleep, restoring a failover context.
164
 *
172
 *
165
 * Sleepers are organised in a FIFO fashion in a structure called wait queue.
173
 * Sleepers are organised in a FIFO fashion in a structure called wait queue.
166
 *
174
 *
167
 * This function is really basic in that other functions as waitq_sleep()
175
 * This function is really basic in that other functions as waitq_sleep()
168
 * and all the *_timeout() functions use it.
176
 * and all the *_timeout() functions use it.
169
 *
177
 *
170
 * @param wq Pointer to wait queue.
178
 * @param wq Pointer to wait queue.
171
 * @param usec Timeout in microseconds.
179
 * @param usec Timeout in microseconds.
172
 * @param nonblocking Blocking vs. non-blocking operation mode switch.
180
 * @param flags Specify mode of the sleep.
173
 *
181
 *
-
 
182
 * The sleep can be interrupted only if the
-
 
183
 * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
-
 
184
 
174
 * If usec is greater than zero, regardless of the value of nonblocking,
185
 * If usec is greater than zero, regardless of the value of the
175
 * the call will not return until either timeout or wakeup comes.
186
 * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either timeout,
-
 
187
 * interruption or wakeup comes.
176
 *
188
 *
177
 * If usec is zero and @nonblocking is zero (false), the call
189
 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags, the call
178
 * will not return until wakeup comes.
190
 * will not return until wakeup or interruption comes.
179
 *
191
 *
180
 * If usec is zero and nonblocking is non-zero (true), the call will
192
 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the call will
181
 * immediately return, reporting either success or failure.
193
 * immediately return, reporting either success or failure.
182
 *
194
 *
183
 * @return  Returns one of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT,
195
 * @return  Returns one of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, ESYNCH_INTERRUPTED,
184
 *          ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED.
196
 *          ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED.
185
 *
197
 *
186
 * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time
198
 * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time
187
 * of the call there was no pending wakeup.
199
 * of the call there was no pending wakeup.
188
 *
200
 *
189
 * @li ESYNCH_TIMEOUT means that the sleep timed out.
201
 * @li ESYNCH_TIMEOUT means that the sleep timed out.
190
 *
202
 *
191
 * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread.
203
 * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread.
192
 *
204
 *
193
 * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was
205
 * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was
194
 * a pending wakeup at the time of the call. The caller was not put
206
 * a pending wakeup at the time of the call. The caller was not put
195
 * asleep at all.
207
 * asleep at all.
196
 *
208
 *
197
 * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was
209
 * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was
198
 * attempted.
210
 * attempted.
199
 */
211
 */
200
int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking)
212
int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int flags)
201
{
213
{
202
    ipl_t ipl;
214
    ipl_t ipl;
203
    int rc;
215
    int rc;
204
   
216
   
205
    ipl = waitq_sleep_prepare(wq);
217
    ipl = waitq_sleep_prepare(wq);
206
    rc = waitq_sleep_timeout_unsafe(wq, usec, nonblocking);
218
    rc = waitq_sleep_timeout_unsafe(wq, usec, flags);
207
    waitq_sleep_finish(wq, rc, ipl);
219
    waitq_sleep_finish(wq, rc, ipl);
208
    return rc;
220
    return rc;
209
}
221
}
210
 
222
 
211
/** Prepare to sleep in a waitq.
223
/** Prepare to sleep in a waitq.
212
 *
224
 *
213
 * This function will return holding the lock of the wait queue
225
 * This function will return holding the lock of the wait queue
214
 * and interrupts disabled.
226
 * and interrupts disabled.
215
 *
227
 *
216
 * @param wq Wait queue.
228
 * @param wq Wait queue.
217
 *
229
 *
218
 * @return Interrupt level as it existed on entry to this function.
230
 * @return Interrupt level as it existed on entry to this function.
219
 */
231
 */
220
ipl_t waitq_sleep_prepare(waitq_t *wq)
232
ipl_t waitq_sleep_prepare(waitq_t *wq)
221
{
233
{
222
    ipl_t ipl;
234
    ipl_t ipl;
223
   
235
   
224
restart:
236
restart:
225
    ipl = interrupts_disable();
237
    ipl = interrupts_disable();
226
 
238
 
227
    if (THREAD) {   /* needed during system initiailzation */
239
    if (THREAD) {   /* needed during system initiailzation */
228
        /*
240
        /*
229
         * Busy waiting for a delayed timeout.
241
         * Busy waiting for a delayed timeout.
230
         * This is an important fix for the race condition between
242
         * This is an important fix for the race condition between
231
         * a delayed timeout and a next call to waitq_sleep_timeout().
243
         * a delayed timeout and a next call to waitq_sleep_timeout().
232
         * Simply, the thread is not allowed to go to sleep if
244
         * Simply, the thread is not allowed to go to sleep if
233
         * there are timeouts in progress.
245
         * there are timeouts in progress.
234
         */
246
         */
235
        spinlock_lock(&THREAD->lock);
247
        spinlock_lock(&THREAD->lock);
236
        if (THREAD->timeout_pending) {
248
        if (THREAD->timeout_pending) {
237
            spinlock_unlock(&THREAD->lock);
249
            spinlock_unlock(&THREAD->lock);
238
            interrupts_restore(ipl);
250
            interrupts_restore(ipl);
239
            goto restart;
251
            goto restart;
240
        }
252
        }
241
        spinlock_unlock(&THREAD->lock);
253
        spinlock_unlock(&THREAD->lock);
242
    }
254
    }
243
                                                   
255
                                                   
244
    spinlock_lock(&wq->lock);
256
    spinlock_lock(&wq->lock);
245
    return ipl;
257
    return ipl;
246
}
258
}
247
 
259
 
248
/** Finish waiting in a wait queue.
260
/** Finish waiting in a wait queue.
249
 *
261
 *
250
 * This function restores interrupts to the state that existed prior
262
 * This function restores interrupts to the state that existed prior
251
 * to the call to waitq_sleep_prepare(). If necessary, the wait queue
263
 * to the call to waitq_sleep_prepare(). If necessary, the wait queue
252
 * lock is released.
264
 * lock is released.
253
 *
265
 *
254
 * @param wq Wait queue.
266
 * @param wq Wait queue.
255
 * @param rc Return code of waitq_sleep_timeout_unsafe().
267
 * @param rc Return code of waitq_sleep_timeout_unsafe().
256
 * @param ipl Interrupt level returned by waitq_sleep_prepare().
268
 * @param ipl Interrupt level returned by waitq_sleep_prepare().
257
 */
269
 */
258
void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl)
270
void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl)
259
{
271
{
260
    switch (rc) {
272
    switch (rc) {
261
    case ESYNCH_WOULD_BLOCK:
273
    case ESYNCH_WOULD_BLOCK:
262
    case ESYNCH_OK_ATOMIC:
274
    case ESYNCH_OK_ATOMIC:
263
        spinlock_unlock(&wq->lock);
275
        spinlock_unlock(&wq->lock);
264
        break;
276
        break;
265
    default:
277
    default:
266
        break;
278
        break;
267
    }
279
    }
268
    interrupts_restore(ipl);
280
    interrupts_restore(ipl);
269
}
281
}
270
 
282
 
271
/** Internal implementation of waitq_sleep_timeout().
283
/** Internal implementation of waitq_sleep_timeout().
272
 *
284
 *
273
 * This function implements logic of sleeping in a wait queue.
285
 * This function implements logic of sleeping in a wait queue.
274
 * This call must be preceeded by a call to waitq_sleep_prepare()
286
 * This call must be preceeded by a call to waitq_sleep_prepare()
275
 * and followed by a call to waitq_slee_finish().
287
 * and followed by a call to waitq_slee_finish().
276
 *
288
 *
277
 * @param wq See waitq_sleep_timeout().
289
 * @param wq See waitq_sleep_timeout().
278
 * @param usec See waitq_sleep_timeout().
290
 * @param usec See waitq_sleep_timeout().
279
 * @param nonblocking See waitq_sleep_timeout().
291
 * @param flags See waitq_sleep_timeout().
280
 *
292
 *
281
 * @return See waitq_sleep_timeout().
293
 * @return See waitq_sleep_timeout().
282
 */
294
 */
283
int waitq_sleep_timeout_unsafe(waitq_t *wq, __u32 usec, int nonblocking)
295
int waitq_sleep_timeout_unsafe(waitq_t *wq, __u32 usec, int flags)
284
{
296
{
285
    /* checks whether to go to sleep at all */
297
    /* checks whether to go to sleep at all */
286
    if (wq->missed_wakeups) {
298
    if (wq->missed_wakeups) {
287
        wq->missed_wakeups--;
299
        wq->missed_wakeups--;
288
        return ESYNCH_OK_ATOMIC;
300
        return ESYNCH_OK_ATOMIC;
289
    }
301
    }
290
    else {
302
    else {
291
        if (nonblocking && (usec == 0)) {
303
        if ((flags & SYNCH_FLAGS_NON_BLOCKING) && (usec == 0)) {
292
            /* return immediatelly instead of going to sleep */
304
            /* return immediatelly instead of going to sleep */
293
            return ESYNCH_WOULD_BLOCK;
305
            return ESYNCH_WOULD_BLOCK;
294
        }
306
        }
295
    }
307
    }
296
   
308
   
297
    /*
309
    /*
298
     * Now we are firmly decided to go to sleep.
310
     * Now we are firmly decided to go to sleep.
299
     */
311
     */
300
    spinlock_lock(&THREAD->lock);
312
    spinlock_lock(&THREAD->lock);
301
 
313
 
-
 
314
    if (flags & SYNCH_FLAGS_INTERRUPTIBLE) {
302
    /*
315
        /*
303
     * Set context that will be restored if the sleep
316
         * Set context that will be restored if the sleep
304
     * of this thread is ever interrupted.
317
         * of this thread is ever interrupted.
305
     */
318
         */
-
 
319
        THREAD->sleep_interruptible = true;
306
    if (!context_save(&THREAD->sleep_interruption_context)) {
320
        if (!context_save(&THREAD->sleep_interruption_context)) {
307
        /* Short emulation of scheduler() return code. */
321
            /* Short emulation of scheduler() return code. */
308
        spinlock_unlock(&THREAD->lock);
322
            spinlock_unlock(&THREAD->lock);
309
        return ESYNCH_INTERRUPTED;
323
            return ESYNCH_INTERRUPTED;
-
 
324
        }
-
 
325
    } else {
-
 
326
        THREAD->sleep_interruptible = false;
310
    }
327
    }
311
 
328
 
312
    if (usec) {
329
    if (usec) {
313
        /* We use the timeout variant. */
330
        /* We use the timeout variant. */
314
        if (!context_save(&THREAD->sleep_timeout_context)) {
331
        if (!context_save(&THREAD->sleep_timeout_context)) {
315
            /* Short emulation of scheduler() return code. */
332
            /* Short emulation of scheduler() return code. */
316
            spinlock_unlock(&THREAD->lock);
333
            spinlock_unlock(&THREAD->lock);
317
            return ESYNCH_TIMEOUT;
334
            return ESYNCH_TIMEOUT;
318
        }
335
        }
319
        THREAD->timeout_pending = true;
336
        THREAD->timeout_pending = true;
320
        timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_timeouted_sleep, THREAD);
337
        timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_timeouted_sleep, THREAD);
321
    }
338
    }
322
 
339
 
323
    list_append(&THREAD->wq_link, &wq->head);
340
    list_append(&THREAD->wq_link, &wq->head);
324
 
341
 
325
    /*
342
    /*
326
     * Suspend execution.
343
     * Suspend execution.
327
     */
344
     */
328
    THREAD->state = Sleeping;
345
    THREAD->state = Sleeping;
329
    THREAD->sleep_queue = wq;
346
    THREAD->sleep_queue = wq;
330
 
347
 
331
    spinlock_unlock(&THREAD->lock);
348
    spinlock_unlock(&THREAD->lock);
332
 
349
 
333
    scheduler();    /* wq->lock is released in scheduler_separated_stack() */
350
    scheduler();    /* wq->lock is released in scheduler_separated_stack() */
334
   
351
   
335
    return ESYNCH_OK_BLOCKED;
352
    return ESYNCH_OK_BLOCKED;
336
}
353
}
337
 
354
 
338
 
355
 
339
/** Wake up first thread sleeping in a wait queue
356
/** Wake up first thread sleeping in a wait queue
340
 *
357
 *
341
 * Wake up first thread sleeping in a wait queue.
358
 * Wake up first thread sleeping in a wait queue.
342
 * This is the SMP- and IRQ-safe wrapper meant for
359
 * This is the SMP- and IRQ-safe wrapper meant for
343
 * general use.
360
 * general use.
344
 *
361
 *
345
 * Besides its 'normal' wakeup operation, it attempts
362
 * Besides its 'normal' wakeup operation, it attempts
346
 * to unregister possible timeout.
363
 * to unregister possible timeout.
347
 *
364
 *
348
 * @param wq Pointer to wait queue.
365
 * @param wq Pointer to wait queue.
349
 * @param all If this is non-zero, all sleeping threads
366
 * @param all If this is non-zero, all sleeping threads
350
 *        will be woken up and missed count will be zeroed.
367
 *        will be woken up and missed count will be zeroed.
351
 */
368
 */
352
void waitq_wakeup(waitq_t *wq, bool all)
369
void waitq_wakeup(waitq_t *wq, bool all)
353
{
370
{
354
    ipl_t ipl;
371
    ipl_t ipl;
355
 
372
 
356
    ipl = interrupts_disable();
373
    ipl = interrupts_disable();
357
    spinlock_lock(&wq->lock);
374
    spinlock_lock(&wq->lock);
358
 
375
 
359
    _waitq_wakeup_unsafe(wq, all);
376
    _waitq_wakeup_unsafe(wq, all);
360
 
377
 
361
    spinlock_unlock(&wq->lock);
378
    spinlock_unlock(&wq->lock);
362
    interrupts_restore(ipl);   
379
    interrupts_restore(ipl);   
363
}
380
}
364
 
381
 
365
/** Internal SMP- and IRQ-unsafe version of waitq_wakeup()
382
/** Internal SMP- and IRQ-unsafe version of waitq_wakeup()
366
 *
383
 *
367
 * This is the internal SMP- and IRQ-unsafe version
384
 * This is the internal SMP- and IRQ-unsafe version
368
 * of waitq_wakeup(). It assumes wq->lock is already
385
 * of waitq_wakeup(). It assumes wq->lock is already
369
 * locked and interrupts are already disabled.
386
 * locked and interrupts are already disabled.
370
 *
387
 *
371
 * @param wq Pointer to wait queue.
388
 * @param wq Pointer to wait queue.
372
 * @param all If this is non-zero, all sleeping threads
389
 * @param all If this is non-zero, all sleeping threads
373
 *        will be woken up and missed count will be zeroed.
390
 *        will be woken up and missed count will be zeroed.
374
 */
391
 */
375
void _waitq_wakeup_unsafe(waitq_t *wq, bool all)
392
void _waitq_wakeup_unsafe(waitq_t *wq, bool all)
376
{
393
{
377
    thread_t *t;
394
    thread_t *t;
378
 
395
 
379
loop:  
396
loop:  
380
    if (list_empty(&wq->head)) {
397
    if (list_empty(&wq->head)) {
381
        wq->missed_wakeups++;
398
        wq->missed_wakeups++;
382
        if (all)
399
        if (all)
383
            wq->missed_wakeups = 0;
400
            wq->missed_wakeups = 0;
384
        return;
401
        return;
385
    }
402
    }
386
 
403
 
387
    t = list_get_instance(wq->head.next, thread_t, wq_link);
404
    t = list_get_instance(wq->head.next, thread_t, wq_link);
388
   
405
   
389
    list_remove(&t->wq_link);
406
    list_remove(&t->wq_link);
390
    spinlock_lock(&t->lock);
407
    spinlock_lock(&t->lock);
391
    if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
408
    if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
392
        t->timeout_pending = false;
409
        t->timeout_pending = false;
393
    t->sleep_queue = NULL;
410
    t->sleep_queue = NULL;
394
    spinlock_unlock(&t->lock);
411
    spinlock_unlock(&t->lock);
395
 
412
 
396
    thread_ready(t);
413
    thread_ready(t);
397
 
414
 
398
    if (all)
415
    if (all)
399
        goto loop;
416
        goto loop;
400
}
417
}
401
 
418