Subversion Repositories HelenOS-historic

Rev

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

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