1 /*	$OpenBSD: rthread_mutexattr.c,v 1.3 2012/04/13 13:50:37 kurt Exp $ */
2 /*
3  * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org>
4  * Copyright (c) 2011 Philip Guenther <guenther@openbsd.org>
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 /*
20  * Mutex attributes
21  */
22 
23 
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <errno.h>
27 
28 #include <pthread.h>
29 #include <pthread_np.h>
30 
31 #include "rthread.h"
32 
33 int
pthread_mutexattr_init(pthread_mutexattr_t * attrp)34 pthread_mutexattr_init(pthread_mutexattr_t *attrp)
35 {
36 	pthread_mutexattr_t attr;
37 
38 	attr = calloc(1, sizeof(*attr));
39 	if (!attr)
40 		return (errno);
41 	attr->ma_type = PTHREAD_MUTEX_DEFAULT;
42 	*attrp = attr;
43 
44 	return (0);
45 }
46 
47 int
pthread_mutexattr_destroy(pthread_mutexattr_t * attrp)48 pthread_mutexattr_destroy(pthread_mutexattr_t *attrp)
49 {
50 	free(*attrp);
51 	*attrp = NULL;
52 
53 	return (0);
54 }
55 
56 int
pthread_mutexattr_settype(pthread_mutexattr_t * attrp,int type)57 pthread_mutexattr_settype(pthread_mutexattr_t *attrp, int type)
58 {
59 	if (type < PTHREAD_MUTEX_ERRORCHECK || type >= PTHREAD_MUTEX_TYPE_MAX)
60 		return (EINVAL);
61 	(*attrp)->ma_type = type;
62 	return (0);
63 }
64 
65 int
pthread_mutexattr_gettype(pthread_mutexattr_t * attrp,int * type)66 pthread_mutexattr_gettype(pthread_mutexattr_t *attrp, int *type)
67 {
68 	*type = (*attrp)->ma_type;
69 	return (0);
70 }
71 
72 int
pthread_mutexattr_setprotocol(pthread_mutexattr_t * attrp,int protocol)73 pthread_mutexattr_setprotocol(pthread_mutexattr_t *attrp, int protocol)
74 {
75 	if (protocol < PTHREAD_PRIO_NONE || protocol > PTHREAD_PRIO_PROTECT)
76 		return (EINVAL);
77 	(*attrp)->ma_protocol = protocol;
78 	return (0);
79 }
80 
81 int
pthread_mutexattr_getprotocol(pthread_mutexattr_t * attrp,int * protocol)82 pthread_mutexattr_getprotocol(pthread_mutexattr_t *attrp, int *protocol)
83 {
84 	*protocol = (*attrp)->ma_protocol;
85 	return (0);
86 }
87 
88 int
pthread_mutexattr_setprioceiling(pthread_mutexattr_t * attrp,int prioceiling)89 pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attrp, int prioceiling)
90 {
91 	if (prioceiling < PTHREAD_MIN_PRIORITY ||
92 	    prioceiling > PTHREAD_MAX_PRIORITY)
93 		return (EINVAL);
94 	(*attrp)->ma_prioceiling = prioceiling;
95 	return (0);
96 }
97 
98 int
pthread_mutexattr_getprioceiling(pthread_mutexattr_t * attrp,int * prioceiling)99 pthread_mutexattr_getprioceiling(pthread_mutexattr_t *attrp, int *prioceiling)
100 {
101 	*prioceiling = (*attrp)->ma_prioceiling;
102 	return (0);
103 }
104 
105 int
pthread_mutexattr_getkind_np(pthread_mutexattr_t attrp)106 pthread_mutexattr_getkind_np(pthread_mutexattr_t attrp)
107 {
108 	int	ret;
109 
110 	if (attrp == NULL)
111 		ret = EINVAL;
112 	else
113 		ret = attrp->ma_type;
114 
115         return(ret);
116 }
117 
118 int
pthread_mutexattr_setkind_np(pthread_mutexattr_t * attrp,int kind)119 pthread_mutexattr_setkind_np(pthread_mutexattr_t *attrp, int kind)
120 {
121 	int	ret;
122 
123 	if (attrp == NULL || *attrp == NULL ||
124 	    kind < PTHREAD_MUTEX_ERRORCHECK || kind >= PTHREAD_MUTEX_TYPE_MAX)
125 		ret = EINVAL;
126 	else {
127 		(*attrp)->ma_type = kind;
128 		ret = 0;
129 	}
130 
131 	return (ret);
132 }
133