xref: /freebsd/lib/libthr/thread/thr_mutexattr.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
1 /*
2  * Copyright (c) 1996 Jeffrey Hsu <hsu@freebsd.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. Neither the name of the author nor the names of any co-contributors
43  *    may be used to endorse or promote products derived from this software
44  *    without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  *
58  */
59 
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
62 
63 #include "namespace.h"
64 #include <string.h>
65 #include <stdlib.h>
66 #include <errno.h>
67 #include <pthread.h>
68 #include <pthread_np.h>
69 #include "un-namespace.h"
70 
71 #include "thr_private.h"
72 
73 __weak_reference(_pthread_mutexattr_init, pthread_mutexattr_init);
74 __weak_reference(_pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
75 __weak_reference(_pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
76 __weak_reference(_pthread_mutexattr_gettype, pthread_mutexattr_gettype);
77 __weak_reference(_pthread_mutexattr_settype, pthread_mutexattr_settype);
78 __weak_reference(_pthread_mutexattr_destroy, pthread_mutexattr_destroy);
79 __weak_reference(_pthread_mutexattr_getpshared, pthread_mutexattr_getpshared);
80 __weak_reference(_pthread_mutexattr_setpshared, pthread_mutexattr_setpshared);
81 __weak_reference(_pthread_mutexattr_getprotocol, pthread_mutexattr_getprotocol);
82 __weak_reference(_pthread_mutexattr_setprotocol, pthread_mutexattr_setprotocol);
83 __weak_reference(_pthread_mutexattr_getprioceiling, pthread_mutexattr_getprioceiling);
84 __weak_reference(_pthread_mutexattr_setprioceiling, pthread_mutexattr_setprioceiling);
85 
86 int
87 _pthread_mutexattr_init(pthread_mutexattr_t *attr)
88 {
89 	int ret;
90 	pthread_mutexattr_t pattr;
91 
92 	if ((pattr = (pthread_mutexattr_t)
93 	    malloc(sizeof(struct pthread_mutex_attr))) == NULL) {
94 		ret = ENOMEM;
95 	} else {
96 		memcpy(pattr, &_pthread_mutexattr_default,
97 		    sizeof(struct pthread_mutex_attr));
98 		*attr = pattr;
99 		ret = 0;
100 	}
101 	return (ret);
102 }
103 
104 int
105 _pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
106 {
107 	int	ret;
108 	if (attr == NULL || *attr == NULL) {
109 		errno = EINVAL;
110 		ret = -1;
111 	} else {
112 		(*attr)->m_type = kind;
113 		ret = 0;
114 	}
115 	return(ret);
116 }
117 
118 int
119 _pthread_mutexattr_getkind_np(pthread_mutexattr_t attr)
120 {
121 	int	ret;
122 	if (attr == NULL) {
123 		errno = EINVAL;
124 		ret = -1;
125 	} else {
126 		ret = attr->m_type;
127 	}
128 	return(ret);
129 }
130 
131 int
132 _pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
133 {
134 	int	ret;
135 	if (attr == NULL || *attr == NULL || type >= PTHREAD_MUTEX_TYPE_MAX) {
136 		ret = EINVAL;
137 	} else {
138 		(*attr)->m_type = type;
139 		ret = 0;
140 	}
141 	return(ret);
142 }
143 
144 int
145 _pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type)
146 {
147 	int	ret;
148 
149 	if (attr == NULL || *attr == NULL || (*attr)->m_type >=
150 	    PTHREAD_MUTEX_TYPE_MAX) {
151 		ret = EINVAL;
152 	} else {
153 		*type = (*attr)->m_type;
154 		ret = 0;
155 	}
156 	return ret;
157 }
158 
159 int
160 _pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
161 {
162 	int	ret;
163 	if (attr == NULL || *attr == NULL) {
164 		ret = EINVAL;
165 	} else {
166 		free(*attr);
167 		*attr = NULL;
168 		ret = 0;
169 	}
170 	return(ret);
171 }
172 
173 int
174 _pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr,
175 	int *pshared)
176 {
177 
178 	if (attr == NULL || *attr == NULL)
179 		return (EINVAL);
180 	*pshared = (*attr)->m_pshared;
181 	return (0);
182 }
183 
184 int
185 _pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
186 {
187 
188 	if (attr == NULL || *attr == NULL ||
189 	    (pshared != PTHREAD_PROCESS_PRIVATE &&
190 	    pshared != PTHREAD_PROCESS_SHARED))
191 		return (EINVAL);
192 	(*attr)->m_pshared = pshared;
193 	return (0);
194 }
195 
196 int
197 _pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol)
198 {
199 	int ret = 0;
200 
201 	if ((mattr == NULL) || (*mattr == NULL))
202 		ret = EINVAL;
203 	else
204 		*protocol = (*mattr)->m_protocol;
205 
206 	return(ret);
207 }
208 
209 int
210 _pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol)
211 {
212 	int ret = 0;
213 
214 	if ((mattr == NULL) || (*mattr == NULL) ||
215 	    (protocol < PTHREAD_PRIO_NONE) || (protocol > PTHREAD_PRIO_PROTECT))
216 		ret = EINVAL;
217 	else {
218 		(*mattr)->m_protocol = protocol;
219 		(*mattr)->m_ceiling = THR_MAX_RR_PRIORITY;
220 	}
221 	return(ret);
222 }
223 
224 int
225 _pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling)
226 {
227 	int ret = 0;
228 
229 	if ((mattr == NULL) || (*mattr == NULL))
230 		ret = EINVAL;
231 	else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
232 		ret = EINVAL;
233 	else
234 		*prioceiling = (*mattr)->m_ceiling;
235 
236 	return(ret);
237 }
238 
239 int
240 _pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling)
241 {
242 	int ret = 0;
243 
244 	if ((mattr == NULL) || (*mattr == NULL))
245 		ret = EINVAL;
246 	else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
247 		ret = EINVAL;
248 	else
249 		(*mattr)->m_ceiling = prioceiling;
250 
251 	return(ret);
252 }
253 
254