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