diff options
author | Philip Guenthe <guenther@cvs.openbsd.org> | 2011-12-21 00:49:48 +0000 |
---|---|---|
committer | Philip Guenthe <guenther@cvs.openbsd.org> | 2011-12-21 00:49:48 +0000 |
commit | 5211a46a412e049874e314958d8dfff695fa9be3 (patch) | |
tree | 2a95756b339ce47011ebaa4aaca7cb10e01b17c9 | |
parent | 9af67f50df595a14e65ab45bb8e90a58b1915b6d (diff) |
Split out the pthread_mutexattr_* functions from rthread_sync.c to
new file rthread_mutexattr.c. Add basic implementations of
pthread_mutexattr_{set,get}{protocol,prioceiling}
Requested by aja
-rw-r--r-- | lib/librthread/Makefile | 4 | ||||
-rw-r--r-- | lib/librthread/rthread.h | 7 | ||||
-rw-r--r-- | lib/librthread/rthread_mutexattr.c | 103 | ||||
-rw-r--r-- | lib/librthread/rthread_sync.c | 53 |
4 files changed, 119 insertions, 48 deletions
diff --git a/lib/librthread/Makefile b/lib/librthread/Makefile index 5716a110a63..3e5bfd8e64a 100644 --- a/lib/librthread/Makefile +++ b/lib/librthread/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.19 2011/12/05 04:02:03 guenther Exp $ +# $OpenBSD: Makefile,v 1.20 2011/12/21 00:49:47 guenther Exp $ LIB=rthread WANTLINT= @@ -14,7 +14,7 @@ LDADD = -Wl,-znodelete,-zinitfirst SRCS= rthread.c rthread_attr.c rthread_sched.c rthread_sync.c rthread_tls.c \ rthread_sig.c rthread_np.c rthread_debug.c rthread_stack.c \ rthread_libc.c rthread_fork.c rthread_file.c sched_prio.c \ - rthread_cancel.c + rthread_cancel.c rthread_mutexattr.c OBJS+= _atomic_lock.o rfork_thread.o cerror.o diff --git a/lib/librthread/rthread.h b/lib/librthread/rthread.h index 66576c11b00..e20bf11d734 100644 --- a/lib/librthread/rthread.h +++ b/lib/librthread/rthread.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rthread.h,v 1.29 2011/12/05 04:02:03 guenther Exp $ */ +/* $OpenBSD: rthread.h,v 1.30 2011/12/21 00:49:47 guenther Exp $ */ /* * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org> * All Rights Reserved. @@ -58,10 +58,13 @@ struct pthread_mutex { int type; pthread_t owner; int count; + int prioceiling; }; struct pthread_mutex_attr { - int type; + int ma_type; + int ma_protocol; + int ma_prioceiling; }; struct pthread_cond { diff --git a/lib/librthread/rthread_mutexattr.c b/lib/librthread/rthread_mutexattr.c new file mode 100644 index 00000000000..6a7d780e7ac --- /dev/null +++ b/lib/librthread/rthread_mutexattr.c @@ -0,0 +1,103 @@ +/* $OpenBSD: rthread_mutexattr.c,v 1.1 2011/12/21 00:49:47 guenther Exp $ */ +/* + * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org> + * Copyright (c) 2011 Philip Guenther <guenther@openbsd.org> + * All Rights Reserved. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +/* + * Mutex attributes + */ + + +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> + +#include <pthread.h> + +#include "rthread.h" + +int +pthread_mutexattr_init(pthread_mutexattr_t *attrp) +{ + pthread_mutexattr_t attr; + + attr = calloc(1, sizeof(*attr)); + if (!attr) + return (errno); + attr->ma_type = PTHREAD_MUTEX_ERRORCHECK; + *attrp = attr; + + return (0); +} + +int +pthread_mutexattr_destroy(pthread_mutexattr_t *attrp) +{ + free(*attrp); + *attrp = NULL; + + return (0); +} + +int +pthread_mutexattr_settype(pthread_mutexattr_t *attrp, int type) +{ + if (type < PTHREAD_MUTEX_ERRORCHECK || type >= PTHREAD_MUTEX_TYPE_MAX) + return (EINVAL); + (*attrp)->ma_type = type; + return (0); +} + +int +pthread_mutexattr_gettype(pthread_mutexattr_t *attrp, int *type) +{ + *type = (*attrp)->ma_type; + return (0); +} + +int +pthread_mutexattr_setprotocol(pthread_mutexattr_t *attrp, int protocol) +{ + if (protocol < PTHREAD_PRIO_NONE || protocol > PTHREAD_PRIO_PROTECT) + return (EINVAL); + (*attrp)->ma_protocol = protocol; + return (0); +} + +int +pthread_mutexattr_getprotocol(pthread_mutexattr_t *attrp, int *protocol) +{ + *protocol = (*attrp)->ma_protocol; + return (0); +} + +int +pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attrp, int prioceiling) +{ + if (prioceiling < PTHREAD_MIN_PRIORITY || + prioceiling > PTHREAD_MAX_PRIORITY) + return (EINVAL); + (*attrp)->ma_prioceiling = prioceiling; + return (0); +} + +int +pthread_mutexattr_getprioceiling(pthread_mutexattr_t *attrp, int *prioceiling) +{ + *prioceiling = (*attrp)->ma_prioceiling; + return (0); +} + diff --git a/lib/librthread/rthread_sync.c b/lib/librthread/rthread_sync.c index b619843504e..502ad04fe9e 100644 --- a/lib/librthread/rthread_sync.c +++ b/lib/librthread/rthread_sync.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rthread_sync.c,v 1.25 2011/11/06 11:48:59 guenther Exp $ */ +/* $OpenBSD: rthread_sync.c,v 1.26 2011/12/21 00:49:47 guenther Exp $ */ /* * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org> * All Rights Reserved. @@ -219,7 +219,14 @@ pthread_mutex_init(pthread_mutex_t *mutexp, const pthread_mutexattr_t *attr) return (errno); mutex->sem.lock = _SPINLOCK_UNLOCKED; mutex->sem.value = 1; /* unlocked */ - mutex->type = attr ? (*attr)->type : PTHREAD_MUTEX_ERRORCHECK; + if (attr == NULL) { + mutex->type = PTHREAD_MUTEX_ERRORCHECK; + mutex->prioceiling = PTHREAD_PRIO_NONE; + } else { + mutex->type = (*attr)->ma_type; + mutex->prioceiling = (*attr)->ma_protocol == + PTHREAD_PRIO_PROTECT ? (*attr)->ma_prioceiling : -1; + } *mutexp = mutex; return (0); @@ -308,48 +315,6 @@ pthread_mutex_unlock(pthread_mutex_t *mutexp) } /* - * mutexen attributes - */ -int -pthread_mutexattr_init(pthread_mutexattr_t *attrp) -{ - pthread_mutexattr_t attr; - - attr = calloc(1, sizeof(*attr)); - if (!attr) - return (errno); - attr->type = PTHREAD_MUTEX_ERRORCHECK; - *attrp = attr; - - return (0); -} - -int -pthread_mutexattr_destroy(pthread_mutexattr_t *attrp) -{ - free(*attrp); - *attrp = NULL; - - return (0); -} - -int -pthread_mutexattr_settype(pthread_mutexattr_t *attrp, int type) -{ - (*attrp)->type = type; - - return (0); -} - -int -pthread_mutexattr_gettype(pthread_mutexattr_t *attrp, int *type) -{ - *type = (*attrp)->type; - - return (0); -} - -/* * condition variables */ int |