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