Subversion Repositories HelenOS

Rev

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

Rev 2087 Rev 2089
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   Reader/Writer locks.
35
 * @brief   Reader/Writer locks.
36
 *
36
 *
37
 * A reader/writer lock can be held by multiple readers at a time.
37
 * A reader/writer lock can be held by multiple readers at a time.
38
 * Or it can be exclusively held by a sole writer at a time.
38
 * Or it can be exclusively held by a sole writer at a time.
39
 *
39
 *
40
 * These locks are not recursive.
40
 * These locks are not recursive.
41
 * Because a technique called direct hand-off is used and because
41
 * Because a technique called direct hand-off is used and because
42
 * waiting takes place in a single wait queue, neither readers
42
 * waiting takes place in a single wait queue, neither readers
43
 * nor writers will suffer starvation.
43
 * nor writers will suffer starvation.
44
 *
44
 *
45
 * If there is a writer followed by a reader waiting for the rwlock
45
 * If there is a writer followed by a reader waiting for the rwlock
46
 * and the writer times out, all leading readers are automatically woken up
46
 * and the writer times out, all leading readers are automatically woken up
47
 * and allowed in.
47
 * and allowed in.
48
 */
48
 */
49
 
49
 
50
/*
50
/*
51
 * NOTE ON rwlock_holder_type
51
 * NOTE ON rwlock_holder_type
52
 * This field is set on an attempt to acquire the exclusive mutex
52
 * This field is set on an attempt to acquire the exclusive mutex
53
 * to the respective value depending whether the caller is a reader
53
 * to the respective value depending whether the caller is a reader
54
 * or a writer. The field is examined only if the thread had been
54
 * or a writer. The field is examined only if the thread had been
55
 * previously blocked on the exclusive mutex. Thus it is save
55
 * previously blocked on the exclusive mutex. Thus it is save
56
 * to store the rwlock type in the thread structure, because
56
 * to store the rwlock type in the thread structure, because
57
 * each thread can block on only one rwlock at a time.
57
 * each thread can block on only one rwlock at a time.
58
 */
58
 */
59
 
59
 
60
#include <synch/rwlock.h>
60
#include <synch/rwlock.h>
61
#include <synch/spinlock.h>
61
#include <synch/spinlock.h>
62
#include <synch/mutex.h>
62
#include <synch/mutex.h>
63
#include <synch/waitq.h>
63
#include <synch/waitq.h>
64
#include <synch/synch.h>
64
#include <synch/synch.h>
65
#include <adt/list.h>
65
#include <adt/list.h>
66
#include <typedefs.h>
-
 
67
#include <arch/asm.h>
66
#include <arch/asm.h>
68
#include <arch.h>
67
#include <arch.h>
69
#include <proc/thread.h>
68
#include <proc/thread.h>
70
#include <panic.h>
69
#include <panic.h>
71
 
70
 
72
#define ALLOW_ALL       0
71
#define ALLOW_ALL       0
73
#define ALLOW_READERS_ONLY  1
72
#define ALLOW_READERS_ONLY  1
74
 
73
 
75
static void let_others_in(rwlock_t *rwl, int readers_only);
74
static void let_others_in(rwlock_t *rwl, int readers_only);
76
static void release_spinlock(void *arg);
75
static void release_spinlock(void *arg);
77
 
76
 
78
/** Initialize reader/writer lock
77
/** Initialize reader/writer lock
79
 *
78
 *
80
 * Initialize reader/writer lock.
79
 * Initialize reader/writer lock.
81
 *
80
 *
82
 * @param rwl Reader/Writer lock.
81
 * @param rwl Reader/Writer lock.
83
 */
82
 */
84
void rwlock_initialize(rwlock_t *rwl) {
83
void rwlock_initialize(rwlock_t *rwl) {
85
    spinlock_initialize(&rwl->lock, "rwlock_t");
84
    spinlock_initialize(&rwl->lock, "rwlock_t");
86
    mutex_initialize(&rwl->exclusive);
85
    mutex_initialize(&rwl->exclusive);
87
    rwl->readers_in = 0;
86
    rwl->readers_in = 0;
88
}
87
}
89
 
88
 
90
/** Acquire reader/writer lock for reading
89
/** Acquire reader/writer lock for reading
91
 *
90
 *
92
 * Acquire reader/writer lock for reading.
91
 * Acquire reader/writer lock for reading.
93
 * Timeout and willingness to block may be specified.
92
 * Timeout and willingness to block may be specified.
94
 *
93
 *
95
 * @param rwl Reader/Writer lock.
94
 * @param rwl Reader/Writer lock.
96
 * @param usec Timeout in microseconds.
95
 * @param usec Timeout in microseconds.
97
 * @param flags Specify mode of operation.
96
 * @param flags Specify mode of operation.
98
 *
97
 *
99
 * For exact description of possible combinations of
98
 * For exact description of possible combinations of
100
 * usec and flags, see comment for waitq_sleep_timeout().
99
 * usec and flags, see comment for waitq_sleep_timeout().
101
 *
100
 *
102
 * @return See comment for waitq_sleep_timeout().
101
 * @return See comment for waitq_sleep_timeout().
103
 */
102
 */
104
int _rwlock_write_lock_timeout(rwlock_t *rwl, uint32_t usec, int flags)
103
int _rwlock_write_lock_timeout(rwlock_t *rwl, uint32_t usec, int flags)
105
{
104
{
106
    ipl_t ipl;
105
    ipl_t ipl;
107
    int rc;
106
    int rc;
108
   
107
   
109
    ipl = interrupts_disable();
108
    ipl = interrupts_disable();
110
    spinlock_lock(&THREAD->lock);
109
    spinlock_lock(&THREAD->lock);
111
    THREAD->rwlock_holder_type = RWLOCK_WRITER;
110
    THREAD->rwlock_holder_type = RWLOCK_WRITER;
112
    spinlock_unlock(&THREAD->lock);
111
    spinlock_unlock(&THREAD->lock);
113
    interrupts_restore(ipl);
112
    interrupts_restore(ipl);
114
 
113
 
115
    /*
114
    /*
116
     * Writers take the easy part.
115
     * Writers take the easy part.
117
     * They just need to acquire the exclusive mutex.
116
     * They just need to acquire the exclusive mutex.
118
     */
117
     */
119
    rc = _mutex_lock_timeout(&rwl->exclusive, usec, flags);
118
    rc = _mutex_lock_timeout(&rwl->exclusive, usec, flags);
120
    if (SYNCH_FAILED(rc)) {
119
    if (SYNCH_FAILED(rc)) {
121
 
120
 
122
        /*
121
        /*
123
         * Lock operation timed out or was interrupted.
122
         * Lock operation timed out or was interrupted.
124
         * The state of rwl is UNKNOWN at this point.
123
         * The state of rwl is UNKNOWN at this point.
125
         * No claims about its holder can be made.
124
         * No claims about its holder can be made.
126
         */
125
         */
127
         
126
         
128
        ipl = interrupts_disable();
127
        ipl = interrupts_disable();
129
        spinlock_lock(&rwl->lock);
128
        spinlock_lock(&rwl->lock);
130
        /*
129
        /*
131
         * Now when rwl is locked, we can inspect it again.
130
         * Now when rwl is locked, we can inspect it again.
132
         * If it is held by some readers already, we can let
131
         * If it is held by some readers already, we can let
133
         * readers from the head of the wait queue in.
132
         * readers from the head of the wait queue in.
134
         */
133
         */
135
        if (rwl->readers_in)
134
        if (rwl->readers_in)
136
            let_others_in(rwl, ALLOW_READERS_ONLY);
135
            let_others_in(rwl, ALLOW_READERS_ONLY);
137
        spinlock_unlock(&rwl->lock);
136
        spinlock_unlock(&rwl->lock);
138
        interrupts_restore(ipl);
137
        interrupts_restore(ipl);
139
    }
138
    }
140
   
139
   
141
    return rc;
140
    return rc;
142
}
141
}
143
 
142
 
144
/** Acquire reader/writer lock for writing
143
/** Acquire reader/writer lock for writing
145
 *
144
 *
146
 * Acquire reader/writer lock for writing.
145
 * Acquire reader/writer lock for writing.
147
 * Timeout and willingness to block may be specified.
146
 * Timeout and willingness to block may be specified.
148
 *
147
 *
149
 * @param rwl Reader/Writer lock.
148
 * @param rwl Reader/Writer lock.
150
 * @param usec Timeout in microseconds.
149
 * @param usec Timeout in microseconds.
151
 * @param flags Select mode of operation.
150
 * @param flags Select mode of operation.
152
 *
151
 *
153
 * For exact description of possible combinations of
152
 * For exact description of possible combinations of
154
 * usec and flags, see comment for waitq_sleep_timeout().
153
 * usec and flags, see comment for waitq_sleep_timeout().
155
 *
154
 *
156
 * @return See comment for waitq_sleep_timeout().
155
 * @return See comment for waitq_sleep_timeout().
157
 */
156
 */
158
int _rwlock_read_lock_timeout(rwlock_t *rwl, uint32_t usec, int flags)
157
int _rwlock_read_lock_timeout(rwlock_t *rwl, uint32_t usec, int flags)
159
{
158
{
160
    int rc;
159
    int rc;
161
    ipl_t ipl;
160
    ipl_t ipl;
162
   
161
   
163
    ipl = interrupts_disable();
162
    ipl = interrupts_disable();
164
    spinlock_lock(&THREAD->lock);
163
    spinlock_lock(&THREAD->lock);
165
    THREAD->rwlock_holder_type = RWLOCK_READER;
164
    THREAD->rwlock_holder_type = RWLOCK_READER;
166
    spinlock_unlock(&THREAD->lock);
165
    spinlock_unlock(&THREAD->lock);
167
 
166
 
168
    spinlock_lock(&rwl->lock);
167
    spinlock_lock(&rwl->lock);
169
 
168
 
170
    /*
169
    /*
171
     * Find out whether we can get what we want without blocking.
170
     * Find out whether we can get what we want without blocking.
172
     */
171
     */
173
    rc = mutex_trylock(&rwl->exclusive);
172
    rc = mutex_trylock(&rwl->exclusive);
174
    if (SYNCH_FAILED(rc)) {
173
    if (SYNCH_FAILED(rc)) {
175
 
174
 
176
        /*
175
        /*
177
         * 'exclusive' mutex is being held by someone else.
176
         * 'exclusive' mutex is being held by someone else.
178
         * If the holder is a reader and there is no one
177
         * If the holder is a reader and there is no one
179
         * else waiting for it, we can enter the critical
178
         * else waiting for it, we can enter the critical
180
         * section.
179
         * section.
181
         */
180
         */
182
 
181
 
183
        if (rwl->readers_in) {
182
        if (rwl->readers_in) {
184
            spinlock_lock(&rwl->exclusive.sem.wq.lock);
183
            spinlock_lock(&rwl->exclusive.sem.wq.lock);
185
            if (list_empty(&rwl->exclusive.sem.wq.head)) {
184
            if (list_empty(&rwl->exclusive.sem.wq.head)) {
186
                /*
185
                /*
187
                 * We can enter.
186
                 * We can enter.
188
                 */
187
                 */
189
                spinlock_unlock(&rwl->exclusive.sem.wq.lock);
188
                spinlock_unlock(&rwl->exclusive.sem.wq.lock);
190
                goto shortcut;
189
                goto shortcut;
191
            }
190
            }
192
            spinlock_unlock(&rwl->exclusive.sem.wq.lock);
191
            spinlock_unlock(&rwl->exclusive.sem.wq.lock);
193
        }
192
        }
194
 
193
 
195
        /*
194
        /*
196
         * In order to prevent a race condition when a reader
195
         * In order to prevent a race condition when a reader
197
         * could block another reader at the head of the waitq,
196
         * could block another reader at the head of the waitq,
198
         * we register a function to unlock rwl->lock
197
         * we register a function to unlock rwl->lock
199
         * after this thread is put asleep.
198
         * after this thread is put asleep.
200
         */
199
         */
201
        #ifdef CONFIG_SMP
200
        #ifdef CONFIG_SMP
202
        thread_register_call_me(release_spinlock, &rwl->lock);
201
        thread_register_call_me(release_spinlock, &rwl->lock);
203
        #else
202
        #else
204
        thread_register_call_me(release_spinlock, NULL);
203
        thread_register_call_me(release_spinlock, NULL);
205
        #endif
204
        #endif
206
                 
205
                 
207
        rc = _mutex_lock_timeout(&rwl->exclusive, usec, flags);
206
        rc = _mutex_lock_timeout(&rwl->exclusive, usec, flags);
208
        switch (rc) {
207
        switch (rc) {
209
        case ESYNCH_WOULD_BLOCK:
208
        case ESYNCH_WOULD_BLOCK:
210
            /*
209
            /*
211
             * release_spinlock() wasn't called
210
             * release_spinlock() wasn't called
212
             */
211
             */
213
            thread_register_call_me(NULL, NULL);
212
            thread_register_call_me(NULL, NULL);
214
            spinlock_unlock(&rwl->lock);
213
            spinlock_unlock(&rwl->lock);
215
        case ESYNCH_TIMEOUT:
214
        case ESYNCH_TIMEOUT:
216
        case ESYNCH_INTERRUPTED:
215
        case ESYNCH_INTERRUPTED:
217
            /*
216
            /*
218
             * The sleep timed out.
217
             * The sleep timed out.
219
             * We just restore interrupt priority level.
218
             * We just restore interrupt priority level.
220
             */
219
             */
221
        case ESYNCH_OK_BLOCKED:    
220
        case ESYNCH_OK_BLOCKED:    
222
            /*
221
            /*
223
             * We were woken with rwl->readers_in already
222
             * We were woken with rwl->readers_in already
224
             * incremented.
223
             * incremented.
225
             *
224
             *
226
             * Note that this arrangement avoids race condition
225
             * Note that this arrangement avoids race condition
227
             * between two concurrent readers. (Race is avoided if
226
             * between two concurrent readers. (Race is avoided if
228
             * 'exclusive' is locked at the same time as
227
             * 'exclusive' is locked at the same time as
229
             * 'readers_in' is incremented. Same time means both
228
             * 'readers_in' is incremented. Same time means both
230
             * events happen atomically when rwl->lock is held.)
229
             * events happen atomically when rwl->lock is held.)
231
             */
230
             */
232
            interrupts_restore(ipl);
231
            interrupts_restore(ipl);
233
            break;
232
            break;
234
        case ESYNCH_OK_ATOMIC:
233
        case ESYNCH_OK_ATOMIC:
235
            panic("_mutex_lock_timeout()==ESYNCH_OK_ATOMIC\n");
234
            panic("_mutex_lock_timeout()==ESYNCH_OK_ATOMIC\n");
236
            break;
235
            break;
237
        default:
236
        default:
238
            panic("invalid ESYNCH\n");
237
            panic("invalid ESYNCH\n");
239
            break;
238
            break;
240
        }
239
        }
241
        return rc;
240
        return rc;
242
    }
241
    }
243
 
242
 
244
shortcut:
243
shortcut:
245
 
244
 
246
    /*
245
    /*
247
     * We can increment readers_in only if we didn't go to sleep.
246
     * We can increment readers_in only if we didn't go to sleep.
248
     * For sleepers, rwlock_let_others_in() will do the job.
247
     * For sleepers, rwlock_let_others_in() will do the job.
249
     */
248
     */
250
    rwl->readers_in++;
249
    rwl->readers_in++;
251
   
250
   
252
    spinlock_unlock(&rwl->lock);
251
    spinlock_unlock(&rwl->lock);
253
    interrupts_restore(ipl);
252
    interrupts_restore(ipl);
254
 
253
 
255
    return ESYNCH_OK_ATOMIC;
254
    return ESYNCH_OK_ATOMIC;
256
}
255
}
257
 
256
 
258
/** Release reader/writer lock held by writer
257
/** Release reader/writer lock held by writer
259
 *
258
 *
260
 * Release reader/writer lock held by writer.
259
 * Release reader/writer lock held by writer.
261
 * Handoff reader/writer lock ownership directly
260
 * Handoff reader/writer lock ownership directly
262
 * to waiting readers or a writer.
261
 * to waiting readers or a writer.
263
 *
262
 *
264
 * @param rwl Reader/Writer lock.
263
 * @param rwl Reader/Writer lock.
265
 */
264
 */
266
void rwlock_write_unlock(rwlock_t *rwl)
265
void rwlock_write_unlock(rwlock_t *rwl)
267
{
266
{
268
    ipl_t ipl;
267
    ipl_t ipl;
269
   
268
   
270
    ipl = interrupts_disable();
269
    ipl = interrupts_disable();
271
    spinlock_lock(&rwl->lock);
270
    spinlock_lock(&rwl->lock);
272
    let_others_in(rwl, ALLOW_ALL);
271
    let_others_in(rwl, ALLOW_ALL);
273
    spinlock_unlock(&rwl->lock);
272
    spinlock_unlock(&rwl->lock);
274
    interrupts_restore(ipl);
273
    interrupts_restore(ipl);
275
   
274
   
276
}
275
}
277
 
276
 
278
/** Release reader/writer lock held by reader
277
/** Release reader/writer lock held by reader
279
 *
278
 *
280
 * Release reader/writer lock held by reader.
279
 * Release reader/writer lock held by reader.
281
 * Handoff reader/writer lock ownership directly
280
 * Handoff reader/writer lock ownership directly
282
 * to a waiting writer or don't do anything if more
281
 * to a waiting writer or don't do anything if more
283
 * readers poses the lock.
282
 * readers poses the lock.
284
 *
283
 *
285
 * @param rwl Reader/Writer lock.
284
 * @param rwl Reader/Writer lock.
286
 */
285
 */
287
void rwlock_read_unlock(rwlock_t *rwl)
286
void rwlock_read_unlock(rwlock_t *rwl)
288
{
287
{
289
    ipl_t ipl;
288
    ipl_t ipl;
290
 
289
 
291
    ipl = interrupts_disable();
290
    ipl = interrupts_disable();
292
    spinlock_lock(&rwl->lock);
291
    spinlock_lock(&rwl->lock);
293
    if (!--rwl->readers_in)
292
    if (!--rwl->readers_in)
294
        let_others_in(rwl, ALLOW_ALL);
293
        let_others_in(rwl, ALLOW_ALL);
295
    spinlock_unlock(&rwl->lock);
294
    spinlock_unlock(&rwl->lock);
296
    interrupts_restore(ipl);
295
    interrupts_restore(ipl);
297
}
296
}
298
 
297
 
299
 
298
 
300
/** Direct handoff of reader/writer lock ownership.
299
/** Direct handoff of reader/writer lock ownership.
301
 *
300
 *
302
 * Direct handoff of reader/writer lock ownership
301
 * Direct handoff of reader/writer lock ownership
303
 * to waiting readers or a writer.
302
 * to waiting readers or a writer.
304
 *
303
 *
305
 * Must be called with rwl->lock locked.
304
 * Must be called with rwl->lock locked.
306
 * Must be called with interrupts_disable()'d.
305
 * Must be called with interrupts_disable()'d.
307
 *
306
 *
308
 * @param rwl Reader/Writer lock.
307
 * @param rwl Reader/Writer lock.
309
 * @param readers_only See the description below.
308
 * @param readers_only See the description below.
310
 *
309
 *
311
 * If readers_only is false: (unlock scenario)
310
 * If readers_only is false: (unlock scenario)
312
 * Let the first sleeper on 'exclusive' mutex in, no matter
311
 * Let the first sleeper on 'exclusive' mutex in, no matter
313
 * whether it is a reader or a writer. If there are more leading
312
 * whether it is a reader or a writer. If there are more leading
314
 * readers in line, let each of them in.
313
 * readers in line, let each of them in.
315
 *
314
 *
316
 * Otherwise: (timeout scenario)
315
 * Otherwise: (timeout scenario)
317
 * Let all leading readers in.
316
 * Let all leading readers in.
318
 */
317
 */
319
void let_others_in(rwlock_t *rwl, int readers_only)
318
void let_others_in(rwlock_t *rwl, int readers_only)
320
{
319
{
321
    rwlock_type_t type = RWLOCK_NONE;
320
    rwlock_type_t type = RWLOCK_NONE;
322
    thread_t *t = NULL;
321
    thread_t *t = NULL;
323
    bool one_more = true;
322
    bool one_more = true;
324
   
323
   
325
    spinlock_lock(&rwl->exclusive.sem.wq.lock);
324
    spinlock_lock(&rwl->exclusive.sem.wq.lock);
326
 
325
 
327
    if (!list_empty(&rwl->exclusive.sem.wq.head))
326
    if (!list_empty(&rwl->exclusive.sem.wq.head))
328
        t = list_get_instance(rwl->exclusive.sem.wq.head.next, thread_t,
327
        t = list_get_instance(rwl->exclusive.sem.wq.head.next, thread_t,
329
            wq_link);
328
            wq_link);
330
    do {
329
    do {
331
        if (t) {
330
        if (t) {
332
            spinlock_lock(&t->lock);
331
            spinlock_lock(&t->lock);
333
            type = t->rwlock_holder_type;
332
            type = t->rwlock_holder_type;
334
            spinlock_unlock(&t->lock);         
333
            spinlock_unlock(&t->lock);         
335
        }
334
        }
336
   
335
   
337
        /*
336
        /*
338
         * If readers_only is true, we wake all leading readers
337
         * If readers_only is true, we wake all leading readers
339
         * if and only if rwl is locked by another reader.
338
         * if and only if rwl is locked by another reader.
340
         * Assumption: readers_only ==> rwl->readers_in
339
         * Assumption: readers_only ==> rwl->readers_in
341
         */
340
         */
342
        if (readers_only && (type != RWLOCK_READER))
341
        if (readers_only && (type != RWLOCK_READER))
343
            break;
342
            break;
344
 
343
 
345
 
344
 
346
        if (type == RWLOCK_READER) {
345
        if (type == RWLOCK_READER) {
347
            /*
346
            /*
348
             * Waking up a reader.
347
             * Waking up a reader.
349
             * We are responsible for incrementing rwl->readers_in
348
             * We are responsible for incrementing rwl->readers_in
350
             * for it.
349
             * for it.
351
             */
350
             */
352
             rwl->readers_in++;
351
             rwl->readers_in++;
353
        }
352
        }
354
 
353
 
355
        /*
354
        /*
356
         * Only the last iteration through this loop can increment
355
         * Only the last iteration through this loop can increment
357
         * rwl->exclusive.sem.wq.missed_wakeup's. All preceeding
356
         * rwl->exclusive.sem.wq.missed_wakeup's. All preceeding
358
         * iterations will wake up a thread.
357
         * iterations will wake up a thread.
359
         */
358
         */
360
        /* We call the internal version of waitq_wakeup, which
359
        /* We call the internal version of waitq_wakeup, which
361
         * relies on the fact that the waitq is already locked.
360
         * relies on the fact that the waitq is already locked.
362
         */
361
         */
363
        _waitq_wakeup_unsafe(&rwl->exclusive.sem.wq, WAKEUP_FIRST);
362
        _waitq_wakeup_unsafe(&rwl->exclusive.sem.wq, WAKEUP_FIRST);
364
       
363
       
365
        t = NULL;
364
        t = NULL;
366
        if (!list_empty(&rwl->exclusive.sem.wq.head)) {
365
        if (!list_empty(&rwl->exclusive.sem.wq.head)) {
367
            t = list_get_instance(rwl->exclusive.sem.wq.head.next,
366
            t = list_get_instance(rwl->exclusive.sem.wq.head.next,
368
                thread_t, wq_link);
367
                thread_t, wq_link);
369
            if (t) {
368
            if (t) {
370
                spinlock_lock(&t->lock);
369
                spinlock_lock(&t->lock);
371
                if (t->rwlock_holder_type != RWLOCK_READER)
370
                if (t->rwlock_holder_type != RWLOCK_READER)
372
                    one_more = false;
371
                    one_more = false;
373
                spinlock_unlock(&t->lock); 
372
                spinlock_unlock(&t->lock); 
374
            }
373
            }
375
        }
374
        }
376
    } while ((type == RWLOCK_READER) && t && one_more);
375
    } while ((type == RWLOCK_READER) && t && one_more);
377
 
376
 
378
    spinlock_unlock(&rwl->exclusive.sem.wq.lock);
377
    spinlock_unlock(&rwl->exclusive.sem.wq.lock);
379
}
378
}
380
 
379
 
381
/** Release spinlock callback
380
/** Release spinlock callback
382
 *
381
 *
383
 * This is a callback function invoked from the scheduler.
382
 * This is a callback function invoked from the scheduler.
384
 * The callback is registered in _rwlock_read_lock_timeout().
383
 * The callback is registered in _rwlock_read_lock_timeout().
385
 *
384
 *
386
 * @param arg Spinlock.
385
 * @param arg Spinlock.
387
 */
386
 */
388
void release_spinlock(void *arg)
387
void release_spinlock(void *arg)
389
{
388
{
390
    spinlock_unlock((spinlock_t *) arg);
389
    spinlock_unlock((spinlock_t *) arg);
391
}
390
}
392
 
391
 
393
/** @}
392
/** @}
394
 */
393
 */
395
 
394