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