pthread_rwlock.c revision 1.1.2.3 1 1.1.2.3 thorpej /* $NetBSD: pthread_rwlock.c,v 1.1.2.3 2003/01/08 19:34:23 thorpej Exp $ */
2 1.1.2.1 nathanw
3 1.1.2.1 nathanw /*-
4 1.1.2.1 nathanw * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1.2.1 nathanw * All rights reserved.
6 1.1.2.1 nathanw *
7 1.1.2.1 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 nathanw * by Nathan J. Williams.
9 1.1.2.1 nathanw *
10 1.1.2.1 nathanw * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 nathanw * modification, are permitted provided that the following conditions
12 1.1.2.1 nathanw * are met:
13 1.1.2.1 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 nathanw * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 nathanw * documentation and/or other materials provided with the distribution.
18 1.1.2.1 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.1.2.1 nathanw * must display the following acknowledgement:
20 1.1.2.1 nathanw * This product includes software developed by the NetBSD
21 1.1.2.1 nathanw * Foundation, Inc. and its contributors.
22 1.1.2.1 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.1 nathanw * contributors may be used to endorse or promote products derived
24 1.1.2.1 nathanw * from this software without specific prior written permission.
25 1.1.2.1 nathanw *
26 1.1.2.1 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.1 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.1 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.1 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.1 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.1 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.1 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.1 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.1 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.1 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.1 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.1 nathanw */
38 1.1.2.1 nathanw
39 1.1.2.1 nathanw #include <assert.h>
40 1.1.2.1 nathanw #include <errno.h>
41 1.1.2.1 nathanw #include <sys/cdefs.h>
42 1.1.2.1 nathanw
43 1.1.2.1 nathanw #include "pthread.h"
44 1.1.2.1 nathanw #include "pthread_int.h"
45 1.1.2.1 nathanw
46 1.1.2.1 nathanw static void pthread_rwlock__callback(void *);
47 1.1.2.3 thorpej
48 1.1.2.3 thorpej __strong_alias(__libc_rwlock_init,pthread_rwlock_init);
49 1.1.2.3 thorpej __strong_alias(__libc_rwlock_rdlock,pthread_rwlock_rdlock);
50 1.1.2.3 thorpej __strong_alias(__libc_rwlock_wrlock,pthread_rwlock_wrlock);
51 1.1.2.3 thorpej __strong_alias(__libc_rwlock_tryrdlock,pthread_rwlock_tryrdlock);
52 1.1.2.3 thorpej __strong_alias(__libc_rwlock_trywrlock,pthread_rwlock_trywrlock);
53 1.1.2.3 thorpej __strong_alias(__libc_rwlock_unlock,pthread_rwlock_unlock);
54 1.1.2.3 thorpej __strong_alias(__libc_rwlock_destroy,pthread_rwlock_destroy);
55 1.1.2.1 nathanw
56 1.1.2.1 nathanw int
57 1.1.2.1 nathanw pthread_rwlock_init(pthread_rwlock_t *rwlock,
58 1.1.2.1 nathanw const pthread_rwlockattr_t *attr)
59 1.1.2.1 nathanw {
60 1.1.2.1 nathanw #ifdef ERRORCHECK
61 1.1.2.1 nathanw if ((rwlock == NULL) ||
62 1.1.2.1 nathanw (attr && (attr->ptra_magic != _PT_RWLOCKATTR_MAGIC)))
63 1.1.2.1 nathanw return EINVAL;
64 1.1.2.1 nathanw #endif
65 1.1.2.1 nathanw rwlock->ptr_magic = _PT_RWLOCK_MAGIC;
66 1.1.2.1 nathanw pthread_lockinit(&rwlock->ptr_interlock);
67 1.1.2.1 nathanw PTQ_INIT(&rwlock->ptr_rblocked);
68 1.1.2.1 nathanw PTQ_INIT(&rwlock->ptr_wblocked);
69 1.1.2.1 nathanw rwlock->ptr_nreaders = 0;
70 1.1.2.1 nathanw rwlock->ptr_writer = NULL;
71 1.1.2.1 nathanw
72 1.1.2.1 nathanw return 0;
73 1.1.2.1 nathanw }
74 1.1.2.1 nathanw
75 1.1.2.1 nathanw
76 1.1.2.1 nathanw int
77 1.1.2.1 nathanw pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
78 1.1.2.1 nathanw {
79 1.1.2.1 nathanw #ifdef ERRORCHECK
80 1.1.2.1 nathanw if ((rwlock == NULL) ||
81 1.1.2.1 nathanw (rwlock->ptr_magic != _PT_RWLOCK_MAGIC) ||
82 1.1.2.1 nathanw (!PTQ_EMPTY(&rwlock->ptr_rblocked)) ||
83 1.1.2.1 nathanw (!PTQ_EMPTY(&rwlock->ptr_wblocked)) ||
84 1.1.2.1 nathanw (rwlock->ptr_nreaders != 0) ||
85 1.1.2.1 nathanw (rwlock->ptr_writer != NULL))
86 1.1.2.1 nathanw return EINVAL;
87 1.1.2.1 nathanw #endif
88 1.1.2.1 nathanw rwlock->ptr_magic = _PT_RWLOCK_DEAD;
89 1.1.2.1 nathanw
90 1.1.2.1 nathanw return 0;
91 1.1.2.1 nathanw }
92 1.1.2.1 nathanw
93 1.1.2.1 nathanw
94 1.1.2.1 nathanw int
95 1.1.2.1 nathanw pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
96 1.1.2.1 nathanw {
97 1.1.2.1 nathanw pthread_t self;
98 1.1.2.1 nathanw #ifdef ERRORCHECK
99 1.1.2.1 nathanw if ((rwlock == NULL) || (rwlock->ptr_magic != _PT_RWLOCK_MAGIC))
100 1.1.2.1 nathanw return EINVAL;
101 1.1.2.1 nathanw #endif
102 1.1.2.1 nathanw self = pthread__self();
103 1.1.2.1 nathanw
104 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
105 1.1.2.1 nathanw #ifdef ERRORCHECK
106 1.1.2.1 nathanw if (rwlock->ptr_writer == self) {
107 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
108 1.1.2.1 nathanw return EDEADLK;
109 1.1.2.1 nathanw }
110 1.1.2.1 nathanw #endif
111 1.1.2.1 nathanw /*
112 1.1.2.1 nathanw * Don't get a readlock if there is a writer or if there are waiting
113 1.1.2.1 nathanw * writers; i.e. prefer writers to readers. This strategy is dictated
114 1.1.2.1 nathanw * by SUSv3.
115 1.1.2.1 nathanw */
116 1.1.2.1 nathanw while ((rwlock->ptr_writer != NULL) ||
117 1.1.2.1 nathanw (!PTQ_EMPTY(&rwlock->ptr_wblocked))) {
118 1.1.2.1 nathanw PTQ_INSERT_TAIL(&rwlock->ptr_rblocked, self, pt_sleep);
119 1.1.2.1 nathanw /* Locking a rwlock is not a cancellation point; don't check */
120 1.1.2.1 nathanw pthread_spinlock(self, &self->pt_statelock);
121 1.1.2.1 nathanw self->pt_state = PT_STATE_BLOCKED_QUEUE;
122 1.1.2.1 nathanw self->pt_sleepobj = rwlock;
123 1.1.2.1 nathanw self->pt_sleepq = &rwlock->ptr_rblocked;
124 1.1.2.1 nathanw self->pt_sleeplock = &rwlock->ptr_interlock;
125 1.1.2.1 nathanw pthread_spinunlock(self, &self->pt_statelock);
126 1.1.2.1 nathanw pthread__block(self, &rwlock->ptr_interlock);
127 1.1.2.1 nathanw /* interlock is not held when we return */
128 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
129 1.1.2.1 nathanw }
130 1.1.2.1 nathanw
131 1.1.2.1 nathanw rwlock->ptr_nreaders++;
132 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
133 1.1.2.1 nathanw
134 1.1.2.1 nathanw return 0;
135 1.1.2.1 nathanw }
136 1.1.2.1 nathanw
137 1.1.2.1 nathanw
138 1.1.2.1 nathanw int
139 1.1.2.1 nathanw pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
140 1.1.2.1 nathanw {
141 1.1.2.1 nathanw pthread_t self;
142 1.1.2.1 nathanw #ifdef ERRORCHECK
143 1.1.2.1 nathanw if ((rwlock == NULL) || (rwlock->ptr_magic != _PT_RWLOCK_MAGIC))
144 1.1.2.1 nathanw return EINVAL;
145 1.1.2.1 nathanw #endif
146 1.1.2.1 nathanw self = pthread__self();
147 1.1.2.1 nathanw
148 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
149 1.1.2.1 nathanw #ifdef ERRORCHECK
150 1.1.2.1 nathanw if (rwlock->ptr_writer == self) {
151 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
152 1.1.2.1 nathanw return EDEADLK;
153 1.1.2.1 nathanw }
154 1.1.2.1 nathanw #endif
155 1.1.2.1 nathanw /*
156 1.1.2.1 nathanw * Don't get a readlock if there is a writer or if there are waiting
157 1.1.2.1 nathanw * writers; i.e. prefer writers to readers. This strategy is dictated
158 1.1.2.1 nathanw * by SUSv3.
159 1.1.2.1 nathanw */
160 1.1.2.1 nathanw if ((rwlock->ptr_writer != NULL) ||
161 1.1.2.1 nathanw (!PTQ_EMPTY(&rwlock->ptr_wblocked))) {
162 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
163 1.1.2.1 nathanw return EBUSY;
164 1.1.2.1 nathanw }
165 1.1.2.1 nathanw
166 1.1.2.1 nathanw rwlock->ptr_nreaders++;
167 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
168 1.1.2.1 nathanw
169 1.1.2.1 nathanw return 0;
170 1.1.2.1 nathanw }
171 1.1.2.1 nathanw
172 1.1.2.1 nathanw
173 1.1.2.1 nathanw int
174 1.1.2.1 nathanw pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
175 1.1.2.1 nathanw {
176 1.1.2.1 nathanw pthread_t self;
177 1.1.2.1 nathanw #ifdef ERRORCHECK
178 1.1.2.1 nathanw if ((rwlock == NULL) || (rwlock->ptr_magic != _PT_RWLOCK_MAGIC))
179 1.1.2.1 nathanw return EINVAL;
180 1.1.2.1 nathanw #endif
181 1.1.2.1 nathanw self = pthread__self();
182 1.1.2.1 nathanw
183 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
184 1.1.2.1 nathanw /*
185 1.1.2.1 nathanw * Prefer writers to readers here; permit writers even if there are
186 1.1.2.1 nathanw * waiting readers.
187 1.1.2.1 nathanw */
188 1.1.2.1 nathanw while ((rwlock->ptr_nreaders > 0) || (rwlock->ptr_writer != NULL)) {
189 1.1.2.1 nathanw PTQ_INSERT_TAIL(&rwlock->ptr_wblocked, self, pt_sleep);
190 1.1.2.1 nathanw /* Locking a rwlock is not a cancellation point; don't check */
191 1.1.2.1 nathanw pthread_spinlock(self, &self->pt_statelock);
192 1.1.2.1 nathanw self->pt_state = PT_STATE_BLOCKED_QUEUE;
193 1.1.2.1 nathanw self->pt_sleepobj = rwlock;
194 1.1.2.1 nathanw self->pt_sleepq = &rwlock->ptr_wblocked;
195 1.1.2.1 nathanw self->pt_sleeplock = &rwlock->ptr_interlock;
196 1.1.2.1 nathanw pthread_spinunlock(self, &self->pt_statelock);
197 1.1.2.1 nathanw pthread__block(self, &rwlock->ptr_interlock);
198 1.1.2.1 nathanw /* interlock is not held when we return */
199 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
200 1.1.2.1 nathanw }
201 1.1.2.1 nathanw
202 1.1.2.1 nathanw rwlock->ptr_writer = self;
203 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
204 1.1.2.1 nathanw
205 1.1.2.1 nathanw return 0;
206 1.1.2.1 nathanw }
207 1.1.2.1 nathanw
208 1.1.2.1 nathanw
209 1.1.2.1 nathanw int
210 1.1.2.1 nathanw pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
211 1.1.2.1 nathanw {
212 1.1.2.1 nathanw pthread_t self;
213 1.1.2.1 nathanw #ifdef ERRORCHECK
214 1.1.2.1 nathanw if ((rwlock == NULL) || (rwlock->ptr_magic != _PT_RWLOCK_MAGIC))
215 1.1.2.1 nathanw return EINVAL;
216 1.1.2.1 nathanw #endif
217 1.1.2.1 nathanw self = pthread__self();
218 1.1.2.1 nathanw
219 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
220 1.1.2.1 nathanw /*
221 1.1.2.1 nathanw * Prefer writers to readers here; permit writers even if there are
222 1.1.2.1 nathanw * waiting readers.
223 1.1.2.1 nathanw */
224 1.1.2.1 nathanw if ((rwlock->ptr_nreaders > 0) || (rwlock->ptr_writer != NULL)) {
225 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
226 1.1.2.1 nathanw return EBUSY;
227 1.1.2.1 nathanw }
228 1.1.2.1 nathanw
229 1.1.2.1 nathanw rwlock->ptr_writer = self;
230 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
231 1.1.2.1 nathanw
232 1.1.2.1 nathanw return 0;
233 1.1.2.1 nathanw }
234 1.1.2.1 nathanw
235 1.1.2.1 nathanw
236 1.1.2.1 nathanw struct pthread_rwlock__waitarg {
237 1.1.2.1 nathanw pthread_t ptw_thread;
238 1.1.2.1 nathanw pthread_rwlock_t *ptw_rwlock;
239 1.1.2.1 nathanw struct pthread_queue_t *ptw_queue;
240 1.1.2.1 nathanw };
241 1.1.2.1 nathanw
242 1.1.2.1 nathanw int
243 1.1.2.1 nathanw pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
244 1.1.2.1 nathanw const struct timespec *abs_timeout)
245 1.1.2.1 nathanw {
246 1.1.2.1 nathanw pthread_t self;
247 1.1.2.1 nathanw struct pthread_rwlock__waitarg wait;
248 1.1.2.1 nathanw struct pt_alarm_t alarm;
249 1.1.2.2 nathanw int retval;
250 1.1.2.1 nathanw #ifdef ERRORCHECK
251 1.1.2.1 nathanw if ((rwlock == NULL) || (rwlock->ptr_magic != _PT_RWLOCK_MAGIC))
252 1.1.2.1 nathanw return EINVAL;
253 1.1.2.1 nathanw #endif
254 1.1.2.1 nathanw self = pthread__self();
255 1.1.2.1 nathanw
256 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
257 1.1.2.1 nathanw #ifdef ERRORCHECK
258 1.1.2.1 nathanw if (rwlock->ptr_writer == self) {
259 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
260 1.1.2.1 nathanw return EDEADLK;
261 1.1.2.1 nathanw }
262 1.1.2.1 nathanw #endif
263 1.1.2.1 nathanw /*
264 1.1.2.1 nathanw * Don't get a readlock if there is a writer or if there are waiting
265 1.1.2.1 nathanw * writers; i.e. prefer writers to readers. This strategy is dictated
266 1.1.2.1 nathanw * by SUSv3.
267 1.1.2.1 nathanw */
268 1.1.2.2 nathanw retval = 0;
269 1.1.2.1 nathanw while ((retval == 0) && ((rwlock->ptr_writer != NULL) ||
270 1.1.2.1 nathanw (!PTQ_EMPTY(&rwlock->ptr_wblocked)))) {
271 1.1.2.2 nathanw wait.ptw_thread = self;
272 1.1.2.2 nathanw wait.ptw_rwlock = rwlock;
273 1.1.2.2 nathanw wait.ptw_queue = &rwlock->ptr_rblocked;
274 1.1.2.2 nathanw pthread__alarm_add(self, &alarm, abs_timeout,
275 1.1.2.2 nathanw pthread_rwlock__callback, &wait);
276 1.1.2.1 nathanw PTQ_INSERT_TAIL(&rwlock->ptr_rblocked, self, pt_sleep);
277 1.1.2.1 nathanw /* Locking a rwlock is not a cancellation point; don't check */
278 1.1.2.1 nathanw pthread_spinlock(self, &self->pt_statelock);
279 1.1.2.1 nathanw self->pt_state = PT_STATE_BLOCKED_QUEUE;
280 1.1.2.1 nathanw self->pt_sleepobj = rwlock;
281 1.1.2.1 nathanw self->pt_sleepq = &rwlock->ptr_rblocked;
282 1.1.2.1 nathanw self->pt_sleeplock = &rwlock->ptr_interlock;
283 1.1.2.1 nathanw pthread_spinunlock(self, &self->pt_statelock);
284 1.1.2.1 nathanw pthread__block(self, &rwlock->ptr_interlock);
285 1.1.2.1 nathanw /* interlock is not held when we return */
286 1.1.2.2 nathanw pthread__alarm_del(self, &alarm);
287 1.1.2.1 nathanw if (pthread__alarm_fired(&alarm))
288 1.1.2.1 nathanw retval = ETIMEDOUT;
289 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
290 1.1.2.1 nathanw }
291 1.1.2.1 nathanw
292 1.1.2.1 nathanw if (retval == 0)
293 1.1.2.1 nathanw rwlock->ptr_nreaders++;
294 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
295 1.1.2.1 nathanw
296 1.1.2.1 nathanw return retval;
297 1.1.2.1 nathanw }
298 1.1.2.1 nathanw
299 1.1.2.1 nathanw
300 1.1.2.1 nathanw int
301 1.1.2.1 nathanw pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
302 1.1.2.1 nathanw const struct timespec *abs_timeout)
303 1.1.2.1 nathanw {
304 1.1.2.1 nathanw struct pthread_rwlock__waitarg wait;
305 1.1.2.1 nathanw struct pt_alarm_t alarm;
306 1.1.2.2 nathanw int retval;
307 1.1.2.1 nathanw pthread_t self;
308 1.1.2.1 nathanw #ifdef ERRORCHECK
309 1.1.2.1 nathanw if ((rwlock == NULL) || (rwlock->ptr_magic != _PT_RWLOCK_MAGIC))
310 1.1.2.1 nathanw return EINVAL;
311 1.1.2.1 nathanw #endif
312 1.1.2.1 nathanw self = pthread__self();
313 1.1.2.1 nathanw
314 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
315 1.1.2.1 nathanw /*
316 1.1.2.1 nathanw * Prefer writers to readers here; permit writers even if there are
317 1.1.2.1 nathanw * waiting readers.
318 1.1.2.1 nathanw */
319 1.1.2.2 nathanw retval = 0;
320 1.1.2.1 nathanw while (retval == 0 &&
321 1.1.2.1 nathanw ((rwlock->ptr_nreaders > 0) || (rwlock->ptr_writer != NULL))) {
322 1.1.2.2 nathanw wait.ptw_thread = self;
323 1.1.2.2 nathanw wait.ptw_rwlock = rwlock;
324 1.1.2.2 nathanw wait.ptw_queue = &rwlock->ptr_wblocked;
325 1.1.2.2 nathanw pthread__alarm_add(self, &alarm, abs_timeout,
326 1.1.2.2 nathanw pthread_rwlock__callback, &wait);
327 1.1.2.1 nathanw PTQ_INSERT_TAIL(&rwlock->ptr_wblocked, self, pt_sleep);
328 1.1.2.1 nathanw /* Locking a rwlock is not a cancellation point; don't check */
329 1.1.2.1 nathanw pthread_spinlock(self, &self->pt_statelock);
330 1.1.2.1 nathanw self->pt_state = PT_STATE_BLOCKED_QUEUE;
331 1.1.2.1 nathanw self->pt_sleepobj = rwlock;
332 1.1.2.1 nathanw self->pt_sleepq = &rwlock->ptr_wblocked;
333 1.1.2.1 nathanw self->pt_sleeplock = &rwlock->ptr_interlock;
334 1.1.2.1 nathanw pthread_spinunlock(self, &self->pt_statelock);
335 1.1.2.1 nathanw pthread__block(self, &rwlock->ptr_interlock);
336 1.1.2.1 nathanw /* interlock is not held when we return */
337 1.1.2.2 nathanw pthread__alarm_del(self, &alarm);
338 1.1.2.1 nathanw if (pthread__alarm_fired(&alarm))
339 1.1.2.1 nathanw retval = ETIMEDOUT;
340 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
341 1.1.2.1 nathanw }
342 1.1.2.1 nathanw
343 1.1.2.1 nathanw if (retval == 0)
344 1.1.2.1 nathanw rwlock->ptr_writer = self;
345 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
346 1.1.2.1 nathanw
347 1.1.2.1 nathanw return 0;
348 1.1.2.1 nathanw }
349 1.1.2.1 nathanw
350 1.1.2.1 nathanw
351 1.1.2.1 nathanw static void
352 1.1.2.1 nathanw pthread_rwlock__callback(void *arg)
353 1.1.2.1 nathanw {
354 1.1.2.1 nathanw struct pthread_rwlock__waitarg *a;
355 1.1.2.1 nathanw pthread_t self;
356 1.1.2.1 nathanw
357 1.1.2.1 nathanw a = arg;
358 1.1.2.1 nathanw self = pthread__self();
359 1.1.2.1 nathanw
360 1.1.2.1 nathanw pthread_spinlock(self, &a->ptw_rwlock->ptr_interlock);
361 1.1.2.2 nathanw /*
362 1.1.2.2 nathanw * Don't dequeue and schedule the thread if it's already been
363 1.1.2.2 nathanw * queued up by a signal or broadcast (but hasn't yet run as far
364 1.1.2.2 nathanw * as pthread__alarm_del(), or we wouldn't be here, and hence can't
365 1.1.2.2 nathanw * have become blocked on some *other* queue).
366 1.1.2.2 nathanw */
367 1.1.2.2 nathanw if (a->ptw_thread->pt_state == PT_STATE_BLOCKED_QUEUE) {
368 1.1.2.2 nathanw PTQ_REMOVE(a->ptw_queue, a->ptw_thread, pt_sleep);
369 1.1.2.2 nathanw pthread__sched(self, a->ptw_thread);
370 1.1.2.2 nathanw }
371 1.1.2.1 nathanw pthread_spinunlock(self, &a->ptw_rwlock->ptr_interlock);
372 1.1.2.2 nathanw
373 1.1.2.1 nathanw }
374 1.1.2.1 nathanw
375 1.1.2.1 nathanw
376 1.1.2.1 nathanw int
377 1.1.2.1 nathanw pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
378 1.1.2.1 nathanw {
379 1.1.2.1 nathanw pthread_t self, reader, writer;
380 1.1.2.1 nathanw struct pthread_queue_t blockedq;
381 1.1.2.1 nathanw #ifdef ERRORCHECK
382 1.1.2.1 nathanw if ((rwlock == NULL) || (rwlock->ptr_magic != _PT_RWLOCK_MAGIC))
383 1.1.2.1 nathanw return EINVAL;
384 1.1.2.1 nathanw #endif
385 1.1.2.1 nathanw writer = NULL;
386 1.1.2.1 nathanw PTQ_INIT(&blockedq);
387 1.1.2.1 nathanw self = pthread__self();
388 1.1.2.1 nathanw
389 1.1.2.1 nathanw pthread_spinlock(self, &rwlock->ptr_interlock);
390 1.1.2.1 nathanw if (rwlock->ptr_writer != NULL) {
391 1.1.2.1 nathanw /* Releasing a write lock. */
392 1.1.2.1 nathanw #ifdef ERRORCHECK
393 1.1.2.1 nathanw if (rwlock->ptr_writer != self) {
394 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
395 1.1.2.1 nathanw return EPERM;
396 1.1.2.1 nathanw }
397 1.1.2.1 nathanw #endif
398 1.1.2.1 nathanw rwlock->ptr_writer = NULL;
399 1.1.2.1 nathanw writer = PTQ_FIRST(&rwlock->ptr_wblocked);
400 1.1.2.1 nathanw if (writer != NULL) {
401 1.1.2.1 nathanw PTQ_REMOVE(&rwlock->ptr_wblocked, writer, pt_sleep);
402 1.1.2.1 nathanw } else {
403 1.1.2.1 nathanw blockedq = rwlock->ptr_rblocked;
404 1.1.2.1 nathanw PTQ_INIT(&rwlock->ptr_rblocked);
405 1.1.2.1 nathanw }
406 1.1.2.1 nathanw } else {
407 1.1.2.1 nathanw /* Releasing a read lock. */
408 1.1.2.1 nathanw rwlock->ptr_nreaders--;
409 1.1.2.1 nathanw if (rwlock->ptr_nreaders == 0) {
410 1.1.2.1 nathanw writer = PTQ_FIRST(&rwlock->ptr_wblocked);
411 1.1.2.1 nathanw if (writer != NULL)
412 1.1.2.1 nathanw PTQ_REMOVE(&rwlock->ptr_wblocked, writer,
413 1.1.2.1 nathanw pt_sleep);
414 1.1.2.1 nathanw }
415 1.1.2.1 nathanw }
416 1.1.2.1 nathanw
417 1.1.2.1 nathanw pthread_spinunlock(self, &rwlock->ptr_interlock);
418 1.1.2.1 nathanw
419 1.1.2.1 nathanw if (writer != NULL)
420 1.1.2.1 nathanw pthread__sched(self, writer);
421 1.1.2.1 nathanw else
422 1.1.2.1 nathanw PTQ_FOREACH(reader, &blockedq, pt_sleep)
423 1.1.2.1 nathanw pthread__sched(self, reader);
424 1.1.2.1 nathanw
425 1.1.2.1 nathanw return 0;
426 1.1.2.1 nathanw }
427 1.1.2.1 nathanw
428 1.1.2.1 nathanw
429 1.1.2.1 nathanw int
430 1.1.2.1 nathanw pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
431 1.1.2.1 nathanw {
432 1.1.2.1 nathanw #ifdef ERRORCHECK
433 1.1.2.1 nathanw if (attr == NULL)
434 1.1.2.1 nathanw return EINVAL;
435 1.1.2.1 nathanw #endif
436 1.1.2.1 nathanw attr->ptra_magic = _PT_RWLOCKATTR_MAGIC;
437 1.1.2.1 nathanw
438 1.1.2.1 nathanw return 0;
439 1.1.2.1 nathanw }
440 1.1.2.1 nathanw
441 1.1.2.1 nathanw
442 1.1.2.1 nathanw int
443 1.1.2.1 nathanw pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
444 1.1.2.1 nathanw {
445 1.1.2.1 nathanw #ifdef ERRORCHECK
446 1.1.2.1 nathanw if ((attr == NULL) ||
447 1.1.2.1 nathanw (attr->ptra_magic != _PT_RWLOCKATTR_MAGIC))
448 1.1.2.1 nathanw return EINVAL;
449 1.1.2.1 nathanw #endif
450 1.1.2.1 nathanw attr->ptra_magic = _PT_RWLOCKATTR_DEAD;
451 1.1.2.1 nathanw
452 1.1.2.1 nathanw return 0;
453 1.1.2.1 nathanw }
454