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