Home | History | Annotate | Line # | Download | only in libpthread
pthread_rwlock.c revision 1.41
      1  1.41        ad /*	$NetBSD: pthread_rwlock.c,v 1.41 2020/06/01 11:44:59 ad Exp $ */
      2   1.2   thorpej 
      3   1.2   thorpej /*-
      4  1.41        ad  * Copyright (c) 2002, 2006, 2007, 2008, 2020 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.27        ad  * by Nathan J. Williams, by Jason R. Thorpe, and by Andrew Doran.
      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  *
     19   1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30   1.2   thorpej  */
     31   1.2   thorpej 
     32   1.5     lukem #include <sys/cdefs.h>
     33  1.41        ad __RCSID("$NetBSD: pthread_rwlock.c,v 1.41 2020/06/01 11:44:59 ad Exp $");
     34  1.30        ad 
     35  1.30        ad #include <sys/types.h>
     36  1.30        ad #include <sys/lwpctl.h>
     37   1.5     lukem 
     38  1.35       uwe #include <assert.h>
     39  1.33  christos #include <time.h>
     40   1.2   thorpej #include <errno.h>
     41  1.27        ad #include <stddef.h>
     42   1.2   thorpej 
     43   1.2   thorpej #include "pthread.h"
     44   1.2   thorpej #include "pthread_int.h"
     45  1.33  christos #include "reentrant.h"
     46   1.2   thorpej 
     47  1.27        ad #define	_RW_LOCKED		0
     48  1.27        ad #define	_RW_WANT_WRITE		1
     49  1.27        ad #define	_RW_WANT_READ		2
     50  1.27        ad 
     51  1.30        ad #if __GNUC_PREREQ__(3, 0)
     52  1.30        ad #define	NOINLINE		__attribute ((noinline))
     53  1.30        ad #else
     54  1.30        ad #define	NOINLINE		/* nothing */
     55  1.30        ad #endif
     56  1.30        ad 
     57  1.27        ad static int pthread__rwlock_wrlock(pthread_rwlock_t *, const struct timespec *);
     58  1.27        ad static int pthread__rwlock_rdlock(pthread_rwlock_t *, const struct timespec *);
     59  1.41        ad static void pthread__rwlock_early(pthread_t, pthread_rwlock_t *,
     60  1.41        ad     pthread_mutex_t *);
     61  1.24  christos 
     62  1.23        ad int	_pthread_rwlock_held_np(pthread_rwlock_t *);
     63  1.23        ad int	_pthread_rwlock_rdheld_np(pthread_rwlock_t *);
     64  1.23        ad int	_pthread_rwlock_wrheld_np(pthread_rwlock_t *);
     65  1.23        ad 
     66  1.27        ad #ifndef lint
     67  1.32      yamt __weak_alias(pthread_rwlock_held_np,_pthread_rwlock_held_np)
     68  1.32      yamt __weak_alias(pthread_rwlock_rdheld_np,_pthread_rwlock_rdheld_np)
     69  1.32      yamt __weak_alias(pthread_rwlock_wrheld_np,_pthread_rwlock_wrheld_np)
     70  1.27        ad #endif
     71  1.27        ad 
     72   1.2   thorpej __strong_alias(__libc_rwlock_init,pthread_rwlock_init)
     73   1.2   thorpej __strong_alias(__libc_rwlock_rdlock,pthread_rwlock_rdlock)
     74   1.2   thorpej __strong_alias(__libc_rwlock_wrlock,pthread_rwlock_wrlock)
     75   1.2   thorpej __strong_alias(__libc_rwlock_tryrdlock,pthread_rwlock_tryrdlock)
     76   1.2   thorpej __strong_alias(__libc_rwlock_trywrlock,pthread_rwlock_trywrlock)
     77   1.2   thorpej __strong_alias(__libc_rwlock_unlock,pthread_rwlock_unlock)
     78   1.2   thorpej __strong_alias(__libc_rwlock_destroy,pthread_rwlock_destroy)
     79   1.2   thorpej 
     80  1.27        ad static inline uintptr_t
     81  1.27        ad rw_cas(pthread_rwlock_t *ptr, uintptr_t o, uintptr_t n)
     82  1.27        ad {
     83  1.27        ad 
     84  1.27        ad 	return (uintptr_t)atomic_cas_ptr(&ptr->ptr_owner, (void *)o,
     85  1.27        ad 	    (void *)n);
     86  1.27        ad }
     87  1.27        ad 
     88   1.2   thorpej int
     89  1.27        ad pthread_rwlock_init(pthread_rwlock_t *ptr,
     90   1.2   thorpej 	    const pthread_rwlockattr_t *attr)
     91   1.2   thorpej {
     92  1.33  christos 	if (__predict_false(__uselibcstub))
     93  1.33  christos 		return __libc_rwlock_init_stub(ptr, attr);
     94  1.27        ad 
     95  1.38     kamil 	pthread__error(EINVAL, "Invalid rwlock attribute",
     96  1.38     kamil 	    attr == NULL || attr->ptra_magic == _PT_RWLOCKATTR_MAGIC);
     97  1.38     kamil 
     98  1.27        ad 	ptr->ptr_magic = _PT_RWLOCK_MAGIC;
     99  1.27        ad 	PTQ_INIT(&ptr->ptr_rblocked);
    100  1.27        ad 	PTQ_INIT(&ptr->ptr_wblocked);
    101  1.27        ad 	ptr->ptr_nreaders = 0;
    102  1.27        ad 	ptr->ptr_owner = NULL;
    103   1.2   thorpej 
    104   1.2   thorpej 	return 0;
    105   1.2   thorpej }
    106   1.2   thorpej 
    107   1.2   thorpej 
    108   1.2   thorpej int
    109  1.27        ad pthread_rwlock_destroy(pthread_rwlock_t *ptr)
    110   1.2   thorpej {
    111  1.33  christos 	if (__predict_false(__uselibcstub))
    112  1.33  christos 		return __libc_rwlock_destroy_stub(ptr);
    113  1.27        ad 
    114  1.38     kamil 	pthread__error(EINVAL, "Invalid rwlock",
    115  1.38     kamil 	    ptr->ptr_magic == _PT_RWLOCK_MAGIC);
    116  1.38     kamil 
    117  1.38     kamil 	if ((!PTQ_EMPTY(&ptr->ptr_rblocked)) ||
    118  1.27        ad 	    (!PTQ_EMPTY(&ptr->ptr_wblocked)) ||
    119  1.27        ad 	    (ptr->ptr_nreaders != 0) ||
    120  1.27        ad 	    (ptr->ptr_owner != NULL))
    121   1.2   thorpej 		return EINVAL;
    122  1.27        ad 	ptr->ptr_magic = _PT_RWLOCK_DEAD;
    123   1.2   thorpej 
    124   1.2   thorpej 	return 0;
    125   1.2   thorpej }
    126   1.2   thorpej 
    127  1.30        ad /* We want function call overhead. */
    128  1.30        ad NOINLINE static void
    129  1.30        ad pthread__rwlock_pause(void)
    130  1.30        ad {
    131  1.30        ad 
    132  1.30        ad 	pthread__smt_pause();
    133  1.30        ad }
    134  1.30        ad 
    135  1.30        ad NOINLINE static int
    136  1.30        ad pthread__rwlock_spin(uintptr_t owner)
    137  1.30        ad {
    138  1.30        ad 	pthread_t thread;
    139  1.30        ad 	unsigned int i;
    140  1.30        ad 
    141  1.36       uwe 	if ((owner & ~RW_THREAD) != RW_WRITE_LOCKED)
    142  1.36       uwe 		return 0;
    143  1.36       uwe 
    144  1.30        ad 	thread = (pthread_t)(owner & RW_THREAD);
    145  1.36       uwe 	if (__predict_false(thread == NULL) ||
    146  1.37        ad 	    thread->pt_lwpctl->lc_curcpu == LWPCTL_CPU_NONE)
    147  1.30        ad 		return 0;
    148  1.36       uwe 
    149  1.30        ad 	for (i = 128; i != 0; i--)
    150  1.30        ad 		pthread__rwlock_pause();
    151  1.30        ad 	return 1;
    152  1.30        ad }
    153  1.30        ad 
    154  1.27        ad static int
    155  1.27        ad pthread__rwlock_rdlock(pthread_rwlock_t *ptr, const struct timespec *ts)
    156   1.2   thorpej {
    157  1.27        ad 	uintptr_t owner, next;
    158  1.30        ad 	pthread_mutex_t *interlock;
    159   1.2   thorpej 	pthread_t self;
    160  1.27        ad 	int error;
    161  1.27        ad 
    162  1.38     kamil 	pthread__error(EINVAL, "Invalid rwlock",
    163  1.38     kamil 	    ptr->ptr_magic == _PT_RWLOCK_MAGIC);
    164  1.27        ad 
    165  1.27        ad 	for (owner = (uintptr_t)ptr->ptr_owner;; owner = next) {
    166  1.27        ad 		/*
    167  1.27        ad 		 * Read the lock owner field.  If the need-to-wait
    168  1.27        ad 		 * indicator is clear, then try to acquire the lock.
    169  1.27        ad 		 */
    170  1.27        ad 		if ((owner & (RW_WRITE_LOCKED | RW_WRITE_WANTED)) == 0) {
    171  1.27        ad 			next = rw_cas(ptr, owner, owner + RW_READ_INCR);
    172  1.27        ad 			if (owner == next) {
    173  1.27        ad 				/* Got it! */
    174  1.27        ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    175  1.27        ad 				membar_enter();
    176  1.27        ad #endif
    177  1.27        ad 				return 0;
    178  1.27        ad 			}
    179  1.27        ad 
    180  1.27        ad 			/*
    181  1.27        ad 			 * Didn't get it -- spin around again (we'll
    182  1.27        ad 			 * probably sleep on the next iteration).
    183  1.27        ad 			 */
    184  1.27        ad 			continue;
    185  1.27        ad 		}
    186  1.27        ad 
    187  1.31        ad 		self = pthread__self();
    188  1.27        ad 		if ((owner & RW_THREAD) == (uintptr_t)self)
    189  1.27        ad 			return EDEADLK;
    190  1.27        ad 
    191  1.30        ad 		/* If held write locked and no waiters, spin. */
    192  1.30        ad 		if (pthread__rwlock_spin(owner)) {
    193  1.30        ad 			while (pthread__rwlock_spin(owner)) {
    194  1.30        ad 				owner = (uintptr_t)ptr->ptr_owner;
    195  1.30        ad 			}
    196  1.30        ad 			next = owner;
    197  1.30        ad 			continue;
    198  1.30        ad 		}
    199  1.30        ad 
    200  1.27        ad 		/*
    201  1.27        ad 		 * Grab the interlock.  Once we have that, we
    202  1.27        ad 		 * can adjust the waiter bits and sleep queue.
    203  1.27        ad 		 */
    204  1.30        ad 		interlock = pthread__hashlock(ptr);
    205  1.30        ad 		pthread_mutex_lock(interlock);
    206  1.27        ad 
    207  1.27        ad 		/*
    208  1.27        ad 		 * Mark the rwlock as having waiters.  If the set fails,
    209  1.27        ad 		 * then we may not need to sleep and should spin again.
    210  1.27        ad 		 */
    211  1.27        ad 		next = rw_cas(ptr, owner, owner | RW_HAS_WAITERS);
    212  1.27        ad 		if (owner != next) {
    213  1.30        ad 			pthread_mutex_unlock(interlock);
    214  1.27        ad 			continue;
    215  1.27        ad 		}
    216  1.27        ad 
    217  1.27        ad 		/* The waiters bit is set - it's safe to sleep. */
    218  1.27        ad 	    	PTQ_INSERT_HEAD(&ptr->ptr_rblocked, self, pt_sleep);
    219  1.27        ad 	    	ptr->ptr_nreaders++;
    220  1.27        ad 		self->pt_rwlocked = _RW_WANT_READ;
    221  1.27        ad 		self->pt_sleepobj = &ptr->ptr_rblocked;
    222  1.30        ad 		error = pthread__park(self, interlock, &ptr->ptr_rblocked,
    223  1.40        ad 		    ts, 0);
    224  1.27        ad 
    225  1.41        ad 		if (self->pt_sleepobj != NULL) {
    226  1.41        ad 			pthread__rwlock_early(self, ptr, interlock);
    227  1.41        ad 		}
    228  1.41        ad 
    229  1.27        ad 		/* Did we get the lock? */
    230  1.27        ad 		if (self->pt_rwlocked == _RW_LOCKED) {
    231  1.27        ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    232  1.27        ad 			membar_enter();
    233   1.2   thorpej #endif
    234  1.27        ad 			return 0;
    235  1.27        ad 		}
    236  1.27        ad 		if (error != 0)
    237  1.27        ad 			return error;
    238  1.27        ad 
    239  1.27        ad 		pthread__errorfunc(__FILE__, __LINE__, __func__,
    240  1.27        ad 		    "direct handoff failure");
    241   1.2   thorpej 	}
    242   1.2   thorpej }
    243   1.2   thorpej 
    244   1.2   thorpej 
    245   1.2   thorpej int
    246  1.27        ad pthread_rwlock_tryrdlock(pthread_rwlock_t *ptr)
    247   1.2   thorpej {
    248  1.27        ad 	uintptr_t owner, next;
    249  1.20        ad 
    250  1.33  christos 	if (__predict_false(__uselibcstub))
    251  1.33  christos 		return __libc_rwlock_tryrdlock_stub(ptr);
    252  1.33  christos 
    253  1.38     kamil 	pthread__error(EINVAL, "Invalid rwlock",
    254  1.38     kamil 	    ptr->ptr_magic == _PT_RWLOCK_MAGIC);
    255  1.27        ad 
    256   1.2   thorpej 	/*
    257   1.2   thorpej 	 * Don't get a readlock if there is a writer or if there are waiting
    258   1.2   thorpej 	 * writers; i.e. prefer writers to readers. This strategy is dictated
    259   1.2   thorpej 	 * by SUSv3.
    260   1.2   thorpej 	 */
    261  1.27        ad 	for (owner = (uintptr_t)ptr->ptr_owner;; owner = next) {
    262  1.27        ad 		if ((owner & (RW_WRITE_LOCKED | RW_WRITE_WANTED)) != 0)
    263  1.27        ad 			return EBUSY;
    264  1.27        ad 		next = rw_cas(ptr, owner, owner + RW_READ_INCR);
    265  1.27        ad 		if (owner == next) {
    266  1.27        ad 			/* Got it! */
    267  1.27        ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    268  1.27        ad 			membar_enter();
    269  1.27        ad #endif
    270  1.27        ad 			return 0;
    271  1.27        ad 		}
    272   1.2   thorpej 	}
    273   1.2   thorpej }
    274   1.2   thorpej 
    275  1.27        ad static int
    276  1.27        ad pthread__rwlock_wrlock(pthread_rwlock_t *ptr, const struct timespec *ts)
    277   1.2   thorpej {
    278  1.27        ad 	uintptr_t owner, next;
    279  1.30        ad 	pthread_mutex_t *interlock;
    280   1.2   thorpej 	pthread_t self;
    281  1.27        ad 	int error;
    282  1.27        ad 
    283  1.27        ad 	self = pthread__self();
    284  1.35       uwe 	_DIAGASSERT(((uintptr_t)self & RW_FLAGMASK) == 0);
    285  1.13       chs 
    286  1.38     kamil 	pthread__error(EINVAL, "Invalid rwlock",
    287  1.38     kamil 	    ptr->ptr_magic == _PT_RWLOCK_MAGIC);
    288  1.27        ad 
    289  1.27        ad 	for (owner = (uintptr_t)ptr->ptr_owner;; owner = next) {
    290  1.27        ad 		/*
    291  1.27        ad 		 * Read the lock owner field.  If the need-to-wait
    292  1.27        ad 		 * indicator is clear, then try to acquire the lock.
    293  1.27        ad 		 */
    294  1.27        ad 		if ((owner & RW_THREAD) == 0) {
    295  1.27        ad 			next = rw_cas(ptr, owner,
    296  1.27        ad 			    (uintptr_t)self | RW_WRITE_LOCKED);
    297  1.27        ad 			if (owner == next) {
    298  1.27        ad 				/* Got it! */
    299  1.27        ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    300  1.27        ad 				membar_enter();
    301  1.27        ad #endif
    302  1.27        ad 				return 0;
    303  1.27        ad 			}
    304  1.27        ad 
    305  1.27        ad 			/*
    306  1.27        ad 			 * Didn't get it -- spin around again (we'll
    307  1.27        ad 			 * probably sleep on the next iteration).
    308  1.27        ad 			 */
    309  1.27        ad 			continue;
    310  1.27        ad 		}
    311  1.27        ad 
    312  1.27        ad 		if ((owner & RW_THREAD) == (uintptr_t)self)
    313  1.13       chs 			return EDEADLK;
    314  1.27        ad 
    315  1.30        ad 		/* If held write locked and no waiters, spin. */
    316  1.30        ad 		if (pthread__rwlock_spin(owner)) {
    317  1.30        ad 			while (pthread__rwlock_spin(owner)) {
    318  1.30        ad 				owner = (uintptr_t)ptr->ptr_owner;
    319  1.30        ad 			}
    320  1.30        ad 			next = owner;
    321  1.30        ad 			continue;
    322  1.30        ad 		}
    323  1.30        ad 
    324  1.27        ad 		/*
    325  1.27        ad 		 * Grab the interlock.  Once we have that, we
    326  1.27        ad 		 * can adjust the waiter bits and sleep queue.
    327  1.27        ad 		 */
    328  1.30        ad 		interlock = pthread__hashlock(ptr);
    329  1.30        ad 		pthread_mutex_lock(interlock);
    330  1.27        ad 
    331  1.27        ad 		/*
    332  1.27        ad 		 * Mark the rwlock as having waiters.  If the set fails,
    333  1.27        ad 		 * then we may not need to sleep and should spin again.
    334  1.27        ad 		 */
    335  1.27        ad 		next = rw_cas(ptr, owner,
    336  1.27        ad 		    owner | RW_HAS_WAITERS | RW_WRITE_WANTED);
    337  1.27        ad 		if (owner != next) {
    338  1.30        ad 			pthread_mutex_unlock(interlock);
    339  1.27        ad 			continue;
    340  1.13       chs 		}
    341  1.27        ad 
    342  1.27        ad 		/* The waiters bit is set - it's safe to sleep. */
    343  1.27        ad 	    	PTQ_INSERT_TAIL(&ptr->ptr_wblocked, self, pt_sleep);
    344  1.27        ad 		self->pt_rwlocked = _RW_WANT_WRITE;
    345  1.27        ad 		self->pt_sleepobj = &ptr->ptr_wblocked;
    346  1.30        ad 		error = pthread__park(self, interlock, &ptr->ptr_wblocked,
    347  1.40        ad 		    ts, 0);
    348  1.27        ad 
    349  1.41        ad 		if (self->pt_sleepobj != NULL) {
    350  1.41        ad 			pthread__rwlock_early(self, ptr, interlock);
    351  1.41        ad 		}
    352  1.41        ad 
    353  1.27        ad 		/* Did we get the lock? */
    354  1.27        ad 		if (self->pt_rwlocked == _RW_LOCKED) {
    355  1.27        ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    356  1.27        ad 			membar_enter();
    357  1.13       chs #endif
    358  1.27        ad 			return 0;
    359  1.27        ad 		}
    360  1.27        ad 		if (error != 0)
    361  1.27        ad 			return error;
    362  1.27        ad 
    363  1.27        ad 		pthread__errorfunc(__FILE__, __LINE__, __func__,
    364  1.27        ad 		    "direct handoff failure");
    365   1.2   thorpej 	}
    366   1.2   thorpej }
    367   1.2   thorpej 
    368   1.2   thorpej int
    369  1.27        ad pthread_rwlock_trywrlock(pthread_rwlock_t *ptr)
    370   1.2   thorpej {
    371  1.27        ad 	uintptr_t owner, next;
    372   1.2   thorpej 	pthread_t self;
    373  1.27        ad 
    374  1.33  christos 	if (__predict_false(__uselibcstub))
    375  1.33  christos 		return __libc_rwlock_trywrlock_stub(ptr);
    376  1.33  christos 
    377  1.38     kamil 	pthread__error(EINVAL, "Invalid rwlock",
    378  1.38     kamil 	    ptr->ptr_magic == _PT_RWLOCK_MAGIC);
    379  1.27        ad 
    380   1.2   thorpej 	self = pthread__self();
    381  1.35       uwe 	_DIAGASSERT(((uintptr_t)self & RW_FLAGMASK) == 0);
    382  1.27        ad 
    383  1.27        ad 	for (owner = (uintptr_t)ptr->ptr_owner;; owner = next) {
    384  1.27        ad 		if (owner != 0)
    385  1.27        ad 			return EBUSY;
    386  1.27        ad 		next = rw_cas(ptr, owner, (uintptr_t)self | RW_WRITE_LOCKED);
    387  1.27        ad 		if (owner == next) {
    388  1.27        ad 			/* Got it! */
    389  1.27        ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    390  1.27        ad 			membar_enter();
    391  1.27        ad #endif
    392  1.27        ad 			return 0;
    393  1.27        ad 		}
    394   1.2   thorpej 	}
    395  1.27        ad }
    396   1.2   thorpej 
    397  1.27        ad int
    398  1.27        ad pthread_rwlock_rdlock(pthread_rwlock_t *ptr)
    399  1.27        ad {
    400  1.33  christos 	if (__predict_false(__uselibcstub))
    401  1.33  christos 		return __libc_rwlock_rdlock_stub(ptr);
    402   1.2   thorpej 
    403  1.27        ad 	return pthread__rwlock_rdlock(ptr, NULL);
    404   1.2   thorpej }
    405   1.2   thorpej 
    406   1.2   thorpej int
    407  1.27        ad pthread_rwlock_timedrdlock(pthread_rwlock_t *ptr,
    408  1.27        ad 			   const struct timespec *abs_timeout)
    409   1.2   thorpej {
    410  1.10   nathanw 	if (abs_timeout == NULL)
    411   1.2   thorpej 		return EINVAL;
    412  1.10   nathanw 	if ((abs_timeout->tv_nsec >= 1000000000) ||
    413  1.10   nathanw 	    (abs_timeout->tv_nsec < 0) ||
    414  1.10   nathanw 	    (abs_timeout->tv_sec < 0))
    415  1.10   nathanw 		return EINVAL;
    416  1.12       chs 
    417  1.27        ad 	return pthread__rwlock_rdlock(ptr, abs_timeout);
    418  1.27        ad }
    419   1.2   thorpej 
    420  1.27        ad int
    421  1.27        ad pthread_rwlock_wrlock(pthread_rwlock_t *ptr)
    422  1.27        ad {
    423  1.33  christos 	if (__predict_false(__uselibcstub))
    424  1.33  christos 		return __libc_rwlock_wrlock_stub(ptr);
    425   1.2   thorpej 
    426  1.27        ad 	return pthread__rwlock_wrlock(ptr, NULL);
    427   1.2   thorpej }
    428   1.2   thorpej 
    429   1.2   thorpej int
    430  1.27        ad pthread_rwlock_timedwrlock(pthread_rwlock_t *ptr,
    431  1.27        ad 			   const struct timespec *abs_timeout)
    432   1.2   thorpej {
    433  1.10   nathanw 	if (abs_timeout == NULL)
    434  1.10   nathanw 		return EINVAL;
    435  1.10   nathanw 	if ((abs_timeout->tv_nsec >= 1000000000) ||
    436  1.10   nathanw 	    (abs_timeout->tv_nsec < 0) ||
    437  1.10   nathanw 	    (abs_timeout->tv_sec < 0))
    438  1.10   nathanw 		return EINVAL;
    439  1.12       chs 
    440  1.27        ad 	return pthread__rwlock_wrlock(ptr, abs_timeout);
    441   1.2   thorpej }
    442   1.2   thorpej 
    443   1.2   thorpej 
    444   1.2   thorpej int
    445  1.27        ad pthread_rwlock_unlock(pthread_rwlock_t *ptr)
    446   1.2   thorpej {
    447  1.27        ad 	uintptr_t owner, decr, new, next;
    448  1.30        ad 	pthread_mutex_t *interlock;
    449  1.27        ad 	pthread_t self, thread;
    450  1.27        ad 
    451  1.33  christos 	if (__predict_false(__uselibcstub))
    452  1.33  christos 		return __libc_rwlock_unlock_stub(ptr);
    453  1.33  christos 
    454  1.38     kamil 	pthread__error(EINVAL, "Invalid rwlock",
    455  1.38     kamil 	    ptr->ptr_magic == _PT_RWLOCK_MAGIC);
    456  1.27        ad 
    457  1.27        ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    458  1.27        ad 	membar_exit();
    459  1.27        ad #endif
    460  1.27        ad 
    461  1.27        ad 	/*
    462  1.27        ad 	 * Since we used an add operation to set the required lock
    463  1.27        ad 	 * bits, we can use a subtract to clear them, which makes
    464  1.27        ad 	 * the read-release and write-release path similar.
    465  1.27        ad 	 */
    466  1.27        ad 	owner = (uintptr_t)ptr->ptr_owner;
    467  1.27        ad 	if ((owner & RW_WRITE_LOCKED) != 0) {
    468  1.31        ad 		self = pthread__self();
    469  1.27        ad 		decr = (uintptr_t)self | RW_WRITE_LOCKED;
    470  1.27        ad 		if ((owner & RW_THREAD) != (uintptr_t)self) {
    471  1.27        ad 			return EPERM;
    472  1.27        ad 		}
    473  1.27        ad 	} else {
    474  1.27        ad 		decr = RW_READ_INCR;
    475  1.27        ad 		if (owner == 0) {
    476   1.2   thorpej 			return EPERM;
    477   1.2   thorpej 		}
    478  1.27        ad 	}
    479  1.27        ad 
    480  1.27        ad 	for (;; owner = next) {
    481  1.27        ad 		/*
    482  1.27        ad 		 * Compute what we expect the new value of the lock to be.
    483  1.27        ad 		 * Only proceed to do direct handoff if there are waiters,
    484  1.27        ad 		 * and if the lock would become unowned.
    485  1.27        ad 		 */
    486  1.27        ad 		new = (owner - decr);
    487  1.27        ad 		if ((new & (RW_THREAD | RW_HAS_WAITERS)) != RW_HAS_WAITERS) {
    488  1.27        ad 			next = rw_cas(ptr, owner, new);
    489  1.27        ad 			if (owner == next) {
    490  1.27        ad 				/* Released! */
    491  1.27        ad 				return 0;
    492  1.27        ad 			}
    493  1.27        ad 			continue;
    494  1.27        ad 		}
    495  1.27        ad 
    496  1.27        ad 		/*
    497  1.27        ad 		 * Grab the interlock.  Once we have that, we can adjust
    498  1.27        ad 		 * the waiter bits.  We must check to see if there are
    499  1.27        ad 		 * still waiters before proceeding.
    500  1.27        ad 		 */
    501  1.30        ad 		interlock = pthread__hashlock(ptr);
    502  1.30        ad 		pthread_mutex_lock(interlock);
    503  1.27        ad 		owner = (uintptr_t)ptr->ptr_owner;
    504  1.27        ad 		if ((owner & RW_HAS_WAITERS) == 0) {
    505  1.30        ad 			pthread_mutex_unlock(interlock);
    506  1.27        ad 			next = owner;
    507  1.27        ad 			continue;
    508   1.2   thorpej 		}
    509  1.27        ad 
    510  1.27        ad 		/*
    511  1.27        ad 		 * Give the lock away.  SUSv3 dictates that we must give
    512  1.27        ad 		 * preference to writers.
    513  1.27        ad 		 */
    514  1.31        ad 		self = pthread__self();
    515  1.27        ad 		if ((thread = PTQ_FIRST(&ptr->ptr_wblocked)) != NULL) {
    516  1.35       uwe 			_DIAGASSERT(((uintptr_t)thread & RW_FLAGMASK) == 0);
    517  1.27        ad 			new = (uintptr_t)thread | RW_WRITE_LOCKED;
    518  1.27        ad 
    519  1.27        ad 			if (PTQ_NEXT(thread, pt_sleep) != NULL)
    520  1.27        ad 				new |= RW_HAS_WAITERS | RW_WRITE_WANTED;
    521  1.27        ad 			else if (ptr->ptr_nreaders != 0)
    522  1.27        ad 				new |= RW_HAS_WAITERS;
    523  1.27        ad 
    524  1.27        ad 			/*
    525  1.27        ad 			 * Set in the new value.  The lock becomes owned
    526  1.27        ad 			 * by the writer that we are about to wake.
    527  1.27        ad 			 */
    528  1.27        ad 			(void)atomic_swap_ptr(&ptr->ptr_owner, (void *)new);
    529  1.27        ad 
    530  1.27        ad 			/* Wake the writer. */
    531  1.27        ad 			thread->pt_rwlocked = _RW_LOCKED;
    532  1.30        ad 			pthread__unpark(&ptr->ptr_wblocked, self,
    533  1.30        ad 			    interlock);
    534  1.27        ad 		} else {
    535  1.27        ad 			new = 0;
    536  1.27        ad 			PTQ_FOREACH(thread, &ptr->ptr_rblocked, pt_sleep) {
    537  1.27        ad 				/*
    538  1.27        ad 				 * May have already been handed the lock,
    539  1.27        ad 				 * since pthread__unpark_all() can release
    540  1.27        ad 				 * our interlock before awakening all
    541  1.27        ad 				 * threads.
    542  1.27        ad 				 */
    543  1.27        ad 				if (thread->pt_sleepobj == NULL)
    544  1.27        ad 					continue;
    545  1.27        ad 				new += RW_READ_INCR;
    546  1.27        ad 				thread->pt_rwlocked = _RW_LOCKED;
    547  1.27        ad 			}
    548  1.27        ad 
    549  1.27        ad 			/*
    550  1.27        ad 			 * Set in the new value.  The lock becomes owned
    551  1.27        ad 			 * by the readers that we are about to wake.
    552  1.27        ad 			 */
    553  1.27        ad 			(void)atomic_swap_ptr(&ptr->ptr_owner, (void *)new);
    554  1.27        ad 
    555  1.27        ad 			/* Wake up all sleeping readers. */
    556  1.27        ad 			ptr->ptr_nreaders = 0;
    557  1.30        ad 			pthread__unpark_all(&ptr->ptr_rblocked, self,
    558  1.30        ad 			    interlock);
    559   1.2   thorpej 		}
    560  1.30        ad 		pthread_mutex_unlock(interlock);
    561  1.27        ad 
    562  1.27        ad 		return 0;
    563   1.2   thorpej 	}
    564  1.27        ad }
    565  1.27        ad 
    566  1.27        ad /*
    567  1.27        ad  * Called when a timedlock awakens early to adjust the waiter bits.
    568  1.27        ad  * The rwlock's interlock is held on entry, and the caller has been
    569  1.27        ad  * removed from the waiters lists.
    570  1.27        ad  */
    571  1.27        ad static void
    572  1.41        ad pthread__rwlock_early(pthread_t self, pthread_rwlock_t *ptr,
    573  1.41        ad     pthread_mutex_t *interlock)
    574  1.27        ad {
    575  1.41        ad 	uintptr_t owner, set, newval, next;
    576  1.41        ad 	pthread_queue_t *queue;
    577   1.2   thorpej 
    578  1.41        ad 	pthread_mutex_lock(interlock);
    579  1.41        ad 	if ((queue = self->pt_sleepobj) == NULL) {
    580  1.41        ad 		pthread_mutex_unlock(interlock);
    581  1.41        ad 		return;
    582  1.27        ad 	}
    583  1.41        ad 	PTQ_REMOVE(queue, self, pt_sleep);
    584  1.41        ad 	self->pt_sleepobj = NULL;
    585  1.27        ad 	owner = (uintptr_t)ptr->ptr_owner;
    586  1.27        ad 
    587  1.27        ad 	if ((owner & RW_THREAD) == 0) {
    588  1.27        ad 		pthread__errorfunc(__FILE__, __LINE__, __func__,
    589  1.27        ad 		    "lock not held");
    590  1.27        ad 	}
    591  1.27        ad 
    592  1.27        ad 	if (!PTQ_EMPTY(&ptr->ptr_wblocked))
    593  1.27        ad 		set = RW_HAS_WAITERS | RW_WRITE_WANTED;
    594  1.27        ad 	else if (ptr->ptr_nreaders != 0)
    595  1.27        ad 		set = RW_HAS_WAITERS;
    596  1.14        ad 	else
    597  1.27        ad 		set = 0;
    598   1.6        cl 
    599  1.27        ad 	for (;; owner = next) {
    600  1.41        ad 		newval = (owner & ~(RW_HAS_WAITERS | RW_WRITE_WANTED)) | set;
    601  1.41        ad 		next = rw_cas(ptr, owner, newval);
    602  1.27        ad 		if (owner == next)
    603  1.27        ad 			break;
    604  1.27        ad 	}
    605  1.41        ad 	pthread_mutex_unlock(interlock);
    606   1.2   thorpej }
    607   1.2   thorpej 
    608   1.2   thorpej int
    609  1.27        ad _pthread_rwlock_held_np(pthread_rwlock_t *ptr)
    610   1.2   thorpej {
    611  1.27        ad 	uintptr_t owner = (uintptr_t)ptr->ptr_owner;
    612   1.2   thorpej 
    613  1.28        ad 	if ((owner & RW_WRITE_LOCKED) != 0)
    614  1.28        ad 		return (owner & RW_THREAD) == (uintptr_t)pthread__self();
    615  1.27        ad 	return (owner & RW_THREAD) != 0;
    616   1.2   thorpej }
    617   1.2   thorpej 
    618   1.2   thorpej int
    619  1.27        ad _pthread_rwlock_rdheld_np(pthread_rwlock_t *ptr)
    620   1.2   thorpej {
    621  1.27        ad 	uintptr_t owner = (uintptr_t)ptr->ptr_owner;
    622   1.2   thorpej 
    623  1.27        ad 	return (owner & RW_THREAD) != 0 && (owner & RW_WRITE_LOCKED) == 0;
    624   1.2   thorpej }
    625  1.21        ad 
    626  1.23        ad int
    627  1.27        ad _pthread_rwlock_wrheld_np(pthread_rwlock_t *ptr)
    628  1.23        ad {
    629  1.27        ad 	uintptr_t owner = (uintptr_t)ptr->ptr_owner;
    630  1.23        ad 
    631  1.27        ad 	return (owner & (RW_THREAD | RW_WRITE_LOCKED)) ==
    632  1.27        ad 	    ((uintptr_t)pthread__self() | RW_WRITE_LOCKED);
    633  1.23        ad }
    634  1.23        ad 
    635  1.34  christos #ifdef _PTHREAD_PSHARED
    636  1.34  christos int
    637  1.34  christos pthread_rwlockattr_getpshared(const pthread_rwlockattr_t * __restrict attr,
    638  1.34  christos     int * __restrict pshared)
    639  1.34  christos {
    640  1.38     kamil 
    641  1.38     kamil 	pthread__error(EINVAL, "Invalid rwlock attribute",
    642  1.38     kamil 	    ptr->ptra_magic == _PT_RWLOCKATTR_MAGIC);
    643  1.38     kamil 
    644  1.34  christos 	*pshared = PTHREAD_PROCESS_PRIVATE;
    645  1.34  christos 	return 0;
    646  1.34  christos }
    647  1.34  christos 
    648  1.34  christos int
    649  1.34  christos pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
    650  1.34  christos {
    651  1.34  christos 
    652  1.38     kamil 	pthread__error(EINVAL, "Invalid rwlock attribute",
    653  1.38     kamil 	    ptr->ptra_magic == _PT_RWLOCKATTR_MAGIC);
    654  1.38     kamil 
    655  1.34  christos 	switch(pshared) {
    656  1.34  christos 	case PTHREAD_PROCESS_PRIVATE:
    657  1.34  christos 		return 0;
    658  1.34  christos 	case PTHREAD_PROCESS_SHARED:
    659  1.34  christos 		return ENOSYS;
    660  1.34  christos 	}
    661  1.34  christos 	return EINVAL;
    662  1.34  christos }
    663  1.34  christos #endif
    664  1.34  christos 
    665  1.23        ad int
    666  1.27        ad pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
    667  1.23        ad {
    668  1.23        ad 
    669  1.27        ad 	if (attr == NULL)
    670  1.27        ad 		return EINVAL;
    671  1.27        ad 	attr->ptra_magic = _PT_RWLOCKATTR_MAGIC;
    672  1.27        ad 
    673  1.27        ad 	return 0;
    674  1.23        ad }
    675  1.23        ad 
    676  1.27        ad 
    677  1.23        ad int
    678  1.27        ad pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
    679  1.23        ad {
    680  1.23        ad 
    681  1.38     kamil 	pthread__error(EINVAL, "Invalid rwlock attribute",
    682  1.38     kamil 	    attr->ptra_magic == _PT_RWLOCKATTR_MAGIC);
    683  1.38     kamil 
    684  1.27        ad 	attr->ptra_magic = _PT_RWLOCKATTR_DEAD;
    685  1.27        ad 
    686  1.27        ad 	return 0;
    687  1.23        ad }
    688