xref: /freebsd/lib/libthr/thread/thr_mutexattr.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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(_thr_mutexattr_init, pthread_mutexattr_init);
76 __weak_reference(_thr_mutexattr_init, _pthread_mutexattr_init);
77 __weak_reference(_pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
78 __weak_reference(_pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
79 __weak_reference(_pthread_mutexattr_gettype, pthread_mutexattr_gettype);
80 __weak_reference(_thr_mutexattr_settype, pthread_mutexattr_settype);
81 __weak_reference(_thr_mutexattr_settype, _pthread_mutexattr_settype);
82 __weak_reference(_thr_mutexattr_destroy, pthread_mutexattr_destroy);
83 __weak_reference(_thr_mutexattr_destroy, _pthread_mutexattr_destroy);
84 __weak_reference(_pthread_mutexattr_getpshared, pthread_mutexattr_getpshared);
85 __weak_reference(_pthread_mutexattr_setpshared, pthread_mutexattr_setpshared);
86 __weak_reference(_pthread_mutexattr_getprotocol, pthread_mutexattr_getprotocol);
87 __weak_reference(_pthread_mutexattr_setprotocol, pthread_mutexattr_setprotocol);
88 __weak_reference(_pthread_mutexattr_getprioceiling,
89     pthread_mutexattr_getprioceiling);
90 __weak_reference(_pthread_mutexattr_setprioceiling,
91     pthread_mutexattr_setprioceiling);
92 __weak_reference(_thr_mutexattr_getrobust, pthread_mutexattr_getrobust);
93 __weak_reference(_thr_mutexattr_getrobust, _pthread_mutexattr_getrobust);
94 __weak_reference(_thr_mutexattr_setrobust, pthread_mutexattr_setrobust);
95 __weak_reference(_thr_mutexattr_setrobust, _pthread_mutexattr_setrobust);
96 
97 int
98 _thr_mutexattr_init(pthread_mutexattr_t *attr)
99 {
100 	int ret;
101 	pthread_mutexattr_t pattr;
102 
103 	if ((pattr = (pthread_mutexattr_t)
104 	    malloc(sizeof(struct pthread_mutex_attr))) == NULL) {
105 		ret = ENOMEM;
106 	} else {
107 		memcpy(pattr, &_pthread_mutexattr_default,
108 		    sizeof(struct pthread_mutex_attr));
109 		*attr = pattr;
110 		ret = 0;
111 	}
112 	return (ret);
113 }
114 
115 int
116 _pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
117 {
118 	int	ret;
119 	if (attr == NULL || *attr == NULL) {
120 		errno = EINVAL;
121 		ret = -1;
122 	} else {
123 		(*attr)->m_type = kind;
124 		ret = 0;
125 	}
126 	return(ret);
127 }
128 
129 int
130 _pthread_mutexattr_getkind_np(pthread_mutexattr_t attr)
131 {
132 	int	ret;
133 
134 	if (attr == NULL) {
135 		errno = EINVAL;
136 		ret = -1;
137 	} else {
138 		ret = attr->m_type;
139 	}
140 	return (ret);
141 }
142 
143 int
144 _thr_mutexattr_settype(pthread_mutexattr_t *attr, int type)
145 {
146 	int	ret;
147 
148 	if (attr == NULL || *attr == NULL || type >= PTHREAD_MUTEX_TYPE_MAX) {
149 		ret = EINVAL;
150 	} else {
151 		(*attr)->m_type = type;
152 		ret = 0;
153 	}
154 	return (ret);
155 }
156 
157 int
158 _pthread_mutexattr_gettype(const pthread_mutexattr_t * __restrict attr,
159     int * __restrict type)
160 {
161 	int	ret;
162 
163 	if (attr == NULL || *attr == NULL || (*attr)->m_type >=
164 	    PTHREAD_MUTEX_TYPE_MAX) {
165 		ret = EINVAL;
166 	} else {
167 		*type = (*attr)->m_type;
168 		ret = 0;
169 	}
170 	return (ret);
171 }
172 
173 int
174 _thr_mutexattr_destroy(pthread_mutexattr_t *attr)
175 {
176 	int	ret;
177 	if (attr == NULL || *attr == NULL) {
178 		ret = EINVAL;
179 	} else {
180 		free(*attr);
181 		*attr = NULL;
182 		ret = 0;
183 	}
184 	return (ret);
185 }
186 
187 int
188 _pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr,
189 	int *pshared)
190 {
191 
192 	if (attr == NULL || *attr == NULL)
193 		return (EINVAL);
194 	*pshared = (*attr)->m_pshared;
195 	return (0);
196 }
197 
198 int
199 _pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
200 {
201 
202 	if (attr == NULL || *attr == NULL ||
203 	    (pshared != PTHREAD_PROCESS_PRIVATE &&
204 	    pshared != PTHREAD_PROCESS_SHARED))
205 		return (EINVAL);
206 	(*attr)->m_pshared = pshared;
207 	return (0);
208 }
209 
210 int
211 _pthread_mutexattr_getprotocol(const pthread_mutexattr_t * __restrict mattr,
212     int * __restrict protocol)
213 {
214 	int ret = 0;
215 
216 	if (mattr == NULL || *mattr == NULL)
217 		ret = EINVAL;
218 	else
219 		*protocol = (*mattr)->m_protocol;
220 
221 	return (ret);
222 }
223 
224 int
225 _pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol)
226 {
227 	int ret = 0;
228 
229 	if (mattr == NULL || *mattr == NULL ||
230 	    protocol < PTHREAD_PRIO_NONE || protocol > PTHREAD_PRIO_PROTECT)
231 		ret = EINVAL;
232 	else {
233 		(*mattr)->m_protocol = protocol;
234 		(*mattr)->m_ceiling = THR_MAX_RR_PRIORITY;
235 	}
236 	return (ret);
237 }
238 
239 int
240 _pthread_mutexattr_getprioceiling(const pthread_mutexattr_t * __restrict mattr,
241     int * __restrict prioceiling)
242 {
243 	int ret = 0;
244 
245 	if (mattr == NULL || *mattr == NULL)
246 		ret = EINVAL;
247 	else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
248 		ret = EINVAL;
249 	else
250 		*prioceiling = (*mattr)->m_ceiling;
251 
252 	return (ret);
253 }
254 
255 int
256 _pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling)
257 {
258 	int ret = 0;
259 
260 	if (mattr == NULL || *mattr == NULL)
261 		ret = EINVAL;
262 	else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
263 		ret = EINVAL;
264 	else
265 		(*mattr)->m_ceiling = prioceiling;
266 
267 	return (ret);
268 }
269 
270 int
271 _thr_mutexattr_getrobust(pthread_mutexattr_t *mattr, int *robust)
272 {
273 	int ret;
274 
275 	if (mattr == NULL || *mattr == NULL) {
276 		ret = EINVAL;
277 	} else {
278 		ret = 0;
279 		*robust = (*mattr)->m_robust;
280 	}
281 	return (ret);
282 }
283 
284 int
285 _thr_mutexattr_setrobust(pthread_mutexattr_t *mattr, int robust)
286 {
287 	int ret;
288 
289 	if (mattr == NULL || *mattr == NULL) {
290 		ret = EINVAL;
291 	} else if (robust != PTHREAD_MUTEX_STALLED &&
292 	    robust != PTHREAD_MUTEX_ROBUST) {
293 		ret = EINVAL;
294 	} else {
295 		ret = 0;
296 		(*mattr)->m_robust = robust;
297 	}
298 	return (ret);
299 }
300 
301