Subversion Repositories HelenOS-historic

Rev

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

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