thr_mutexattr.c revision 297706
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: head/lib/libthr/thread/thr_mutexattr.c 297706 2016-04-08 11:15:26Z kib $");
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
86int
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
104int
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
118int
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
131int
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
144int
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
159int
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
173int
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
184int
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
196int
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
209int
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
224int
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
239int
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