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