1998-09-07 19:01:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Alex Nash
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
* $FreeBSD$
|
1998-09-07 19:01:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
#include "namespace.h"
|
1998-09-07 19:01:43 +00:00
|
|
|
#include <pthread.h>
|
2003-04-18 05:04:16 +00:00
|
|
|
#include "un-namespace.h"
|
2002-09-16 08:45:36 +00:00
|
|
|
#include "thr_private.h"
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
/* maximum number of times a read lock may be obtained */
|
|
|
|
#define MAX_READ_LOCKS (INT_MAX - 1)
|
|
|
|
|
2001-04-10 04:19:21 +00:00
|
|
|
__weak_reference(_pthread_rwlock_destroy, pthread_rwlock_destroy);
|
|
|
|
__weak_reference(_pthread_rwlock_init, pthread_rwlock_init);
|
|
|
|
__weak_reference(_pthread_rwlock_rdlock, pthread_rwlock_rdlock);
|
|
|
|
__weak_reference(_pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
|
|
|
|
__weak_reference(_pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
|
|
|
|
__weak_reference(_pthread_rwlock_unlock, pthread_rwlock_unlock);
|
|
|
|
__weak_reference(_pthread_rwlock_wrlock, pthread_rwlock_wrlock);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
1998-09-07 19:01:43 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
|
|
|
* Prototypes
|
|
|
|
*/
|
|
|
|
static int init_static(pthread_rwlock_t *rwlock);
|
|
|
|
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
static int
|
2003-04-18 05:04:16 +00:00
|
|
|
init_static(pthread_rwlock_t *rwlock)
|
1998-09-07 19:01:43 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread *thread = _get_curthread();
|
1998-09-07 19:01:43 +00:00
|
|
|
int ret;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_ACQUIRE(thread, &_rwlock_static_lock);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (*rwlock == NULL)
|
2003-04-18 05:04:16 +00:00
|
|
|
ret = _pthread_rwlock_init(rwlock, NULL);
|
1998-09-07 19:01:43 +00:00
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_RELEASE(thread, &_rwlock_static_lock);
|
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
_pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
|
1998-09-07 19:01:43 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (rwlock == NULL)
|
|
|
|
ret = EINVAL;
|
|
|
|
else {
|
|
|
|
pthread_rwlock_t prwlock;
|
|
|
|
|
|
|
|
prwlock = *rwlock;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_destroy(&prwlock->lock);
|
|
|
|
_pthread_cond_destroy(&prwlock->read_signal);
|
|
|
|
_pthread_cond_destroy(&prwlock->write_signal);
|
1998-09-07 19:01:43 +00:00
|
|
|
free(prwlock);
|
|
|
|
|
|
|
|
*rwlock = NULL;
|
1998-09-07 19:23:55 +00:00
|
|
|
|
|
|
|
ret = 0;
|
1998-09-07 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
_pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
|
1998-09-07 19:01:43 +00:00
|
|
|
{
|
|
|
|
pthread_rwlock_t prwlock;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* allocate rwlock object */
|
|
|
|
prwlock = (pthread_rwlock_t)malloc(sizeof(struct pthread_rwlock));
|
|
|
|
|
|
|
|
if (prwlock == NULL)
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ENOMEM);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
/* initialize the lock */
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((ret = _pthread_mutex_init(&prwlock->lock, NULL)) != 0)
|
1998-09-07 19:01:43 +00:00
|
|
|
free(prwlock);
|
|
|
|
else {
|
|
|
|
/* initialize the read condition signal */
|
2003-04-18 05:04:16 +00:00
|
|
|
ret = _pthread_cond_init(&prwlock->read_signal, NULL);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (ret != 0) {
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_destroy(&prwlock->lock);
|
1998-09-07 19:01:43 +00:00
|
|
|
free(prwlock);
|
|
|
|
} else {
|
|
|
|
/* initialize the write condition signal */
|
2003-04-18 05:04:16 +00:00
|
|
|
ret = _pthread_cond_init(&prwlock->write_signal, NULL);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (ret != 0) {
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_cond_destroy(&prwlock->read_signal);
|
|
|
|
_pthread_mutex_destroy(&prwlock->lock);
|
1998-09-07 19:01:43 +00:00
|
|
|
free(prwlock);
|
|
|
|
} else {
|
|
|
|
/* success */
|
|
|
|
prwlock->state = 0;
|
|
|
|
prwlock->blocked_writers = 0;
|
|
|
|
|
|
|
|
*rwlock = prwlock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
_pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
|
1998-09-07 19:01:43 +00:00
|
|
|
{
|
|
|
|
pthread_rwlock_t prwlock;
|
1998-09-07 21:07:59 +00:00
|
|
|
int ret;
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (rwlock == NULL)
|
2003-04-18 05:04:16 +00:00
|
|
|
return (EINVAL);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
prwlock = *rwlock;
|
|
|
|
|
|
|
|
/* check for static initialization */
|
|
|
|
if (prwlock == NULL) {
|
|
|
|
if ((ret = init_static(rwlock)) != 0)
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
prwlock = *rwlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* grab the monitor lock */
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
|
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
/* give writers priority over readers */
|
2003-03-15 04:45:42 +00:00
|
|
|
while (prwlock->blocked_writers || prwlock->state < 0) {
|
2003-04-18 05:04:16 +00:00
|
|
|
ret = _pthread_cond_wait(&prwlock->read_signal, &prwlock->lock);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
/* can't do a whole lot if this fails */
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_unlock(&prwlock->lock);
|
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check lock count */
|
|
|
|
if (prwlock->state == MAX_READ_LOCKS)
|
|
|
|
ret = EAGAIN;
|
|
|
|
else
|
|
|
|
++prwlock->state; /* indicate we are locked for reading */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Something is really wrong if this call fails. Returning
|
|
|
|
* error won't do because we've already obtained the read
|
|
|
|
* lock. Decrementing 'state' is no good because we probably
|
|
|
|
* don't have the monitor lock.
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_unlock(&prwlock->lock);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
_pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
|
1998-09-07 19:01:43 +00:00
|
|
|
{
|
|
|
|
pthread_rwlock_t prwlock;
|
1998-09-07 21:07:59 +00:00
|
|
|
int ret;
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (rwlock == NULL)
|
2003-04-18 05:04:16 +00:00
|
|
|
return (EINVAL);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
prwlock = *rwlock;
|
|
|
|
|
|
|
|
/* check for static initialization */
|
|
|
|
if (prwlock == NULL) {
|
|
|
|
if ((ret = init_static(rwlock)) != 0)
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
prwlock = *rwlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* grab the monitor lock */
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
|
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
/* give writers priority over readers */
|
|
|
|
if (prwlock->blocked_writers || prwlock->state < 0)
|
2002-03-15 18:27:58 +00:00
|
|
|
ret = EBUSY;
|
1998-09-07 19:01:43 +00:00
|
|
|
else if (prwlock->state == MAX_READ_LOCKS)
|
|
|
|
ret = EAGAIN; /* too many read locks acquired */
|
|
|
|
else
|
|
|
|
++prwlock->state; /* indicate we are locked for reading */
|
|
|
|
|
|
|
|
/* see the comment on this in pthread_rwlock_rdlock */
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_unlock(&prwlock->lock);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
_pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
|
1998-09-07 19:01:43 +00:00
|
|
|
{
|
|
|
|
pthread_rwlock_t prwlock;
|
1998-09-07 21:07:59 +00:00
|
|
|
int ret;
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (rwlock == NULL)
|
2003-04-18 05:04:16 +00:00
|
|
|
return (EINVAL);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
prwlock = *rwlock;
|
|
|
|
|
|
|
|
/* check for static initialization */
|
|
|
|
if (prwlock == NULL) {
|
|
|
|
if ((ret = init_static(rwlock)) != 0)
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
prwlock = *rwlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* grab the monitor lock */
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
|
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (prwlock->state != 0)
|
2002-03-15 18:27:58 +00:00
|
|
|
ret = EBUSY;
|
1998-09-07 19:01:43 +00:00
|
|
|
else
|
|
|
|
/* indicate we are locked for writing */
|
|
|
|
prwlock->state = -1;
|
|
|
|
|
|
|
|
/* see the comment on this in pthread_rwlock_rdlock */
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_unlock(&prwlock->lock);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
_pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
|
1998-09-07 19:01:43 +00:00
|
|
|
{
|
|
|
|
pthread_rwlock_t prwlock;
|
1998-09-07 21:07:59 +00:00
|
|
|
int ret;
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (rwlock == NULL)
|
2003-04-18 05:04:16 +00:00
|
|
|
return (EINVAL);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
prwlock = *rwlock;
|
|
|
|
|
|
|
|
if (prwlock == NULL)
|
2003-04-18 05:04:16 +00:00
|
|
|
return (EINVAL);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
/* grab the monitor lock */
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
|
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (prwlock->state > 0) {
|
|
|
|
if (--prwlock->state == 0 && prwlock->blocked_writers)
|
2003-04-18 05:04:16 +00:00
|
|
|
ret = _pthread_cond_signal(&prwlock->write_signal);
|
1998-09-07 19:01:43 +00:00
|
|
|
} else if (prwlock->state < 0) {
|
|
|
|
prwlock->state = 0;
|
|
|
|
|
|
|
|
if (prwlock->blocked_writers)
|
2003-04-18 05:04:16 +00:00
|
|
|
ret = _pthread_cond_signal(&prwlock->write_signal);
|
1998-09-07 19:01:43 +00:00
|
|
|
else
|
2003-04-18 05:04:16 +00:00
|
|
|
ret = _pthread_cond_broadcast(&prwlock->read_signal);
|
1998-09-07 19:01:43 +00:00
|
|
|
} else
|
|
|
|
ret = EINVAL;
|
|
|
|
|
|
|
|
/* see the comment on this in pthread_rwlock_rdlock */
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_unlock(&prwlock->lock);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
_pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
|
1998-09-07 19:01:43 +00:00
|
|
|
{
|
|
|
|
pthread_rwlock_t prwlock;
|
1998-09-07 21:07:59 +00:00
|
|
|
int ret;
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (rwlock == NULL)
|
2003-04-18 05:04:16 +00:00
|
|
|
return (EINVAL);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
prwlock = *rwlock;
|
|
|
|
|
|
|
|
/* check for static initialization */
|
|
|
|
if (prwlock == NULL) {
|
|
|
|
if ((ret = init_static(rwlock)) != 0)
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
prwlock = *rwlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* grab the monitor lock */
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
|
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
while (prwlock->state != 0) {
|
|
|
|
++prwlock->blocked_writers;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
ret = _pthread_cond_wait(&prwlock->write_signal, &prwlock->lock);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
--prwlock->blocked_writers;
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_unlock(&prwlock->lock);
|
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
--prwlock->blocked_writers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* indicate we are locked for writing */
|
|
|
|
prwlock->state = -1;
|
|
|
|
|
|
|
|
/* see the comment on this in pthread_rwlock_rdlock */
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_unlock(&prwlock->lock);
|
1998-09-07 19:01:43 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-09-07 19:01:43 +00:00
|
|
|
}
|