thr_mutexattr.c revision 165967
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 * $FreeBSD: head/lib/libthr/thread/thr_mutexattr.c 165967 2007-01-12 07:26:21Z imp $
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 "namespace.h"
63#include <string.h>
64#include <stdlib.h>
65#include <errno.h>
66#include <pthread.h>
67#include <pthread_np.h>
68#include "un-namespace.h"
69
70#include "thr_private.h"
71
72__weak_reference(_pthread_mutexattr_init, pthread_mutexattr_init);
73__weak_reference(_pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
74__weak_reference(_pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
75__weak_reference(_pthread_mutexattr_gettype, pthread_mutexattr_gettype);
76__weak_reference(_pthread_mutexattr_settype, pthread_mutexattr_settype);
77__weak_reference(_pthread_mutexattr_destroy, pthread_mutexattr_destroy);
78__weak_reference(_pthread_mutexattr_getpshared, pthread_mutexattr_getpshared);
79__weak_reference(_pthread_mutexattr_setpshared, pthread_mutexattr_setpshared);
80__weak_reference(_pthread_mutexattr_getprotocol, pthread_mutexattr_getprotocol);
81__weak_reference(_pthread_mutexattr_setprotocol, pthread_mutexattr_setprotocol);
82__weak_reference(_pthread_mutexattr_getprioceiling, pthread_mutexattr_getprioceiling);
83__weak_reference(_pthread_mutexattr_setprioceiling, pthread_mutexattr_setprioceiling);
84
85int
86_pthread_mutexattr_init(pthread_mutexattr_t *attr)
87{
88	int ret;
89	pthread_mutexattr_t pattr;
90
91	if ((pattr = (pthread_mutexattr_t)
92	    malloc(sizeof(struct pthread_mutex_attr))) == NULL) {
93		ret = ENOMEM;
94	} else {
95		memcpy(pattr, &_pthread_mutexattr_default,
96		    sizeof(struct pthread_mutex_attr));
97		*attr = pattr;
98		ret = 0;
99	}
100	return (ret);
101}
102
103int
104_pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
105{
106	int	ret;
107	if (attr == NULL || *attr == NULL) {
108		errno = EINVAL;
109		ret = -1;
110	} else {
111		(*attr)->m_type = kind;
112		ret = 0;
113	}
114	return(ret);
115}
116
117int
118_pthread_mutexattr_getkind_np(pthread_mutexattr_t attr)
119{
120	int	ret;
121	if (attr == NULL) {
122		errno = EINVAL;
123		ret = -1;
124	} else {
125		ret = attr->m_type;
126	}
127	return(ret);
128}
129
130int
131_pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
132{
133	int	ret;
134	if (attr == NULL || *attr == NULL || type >= PTHREAD_MUTEX_TYPE_MAX) {
135		errno = EINVAL;
136		ret = -1;
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
181	*pshared = PTHREAD_PROCESS_PRIVATE;
182	return (0);
183}
184
185int
186_pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
187{
188
189	if (attr == NULL || *attr == NULL)
190		return (EINVAL);
191
192	/* Only PTHREAD_PROCESS_PRIVATE is supported. */
193	if (pshared != PTHREAD_PROCESS_PRIVATE)
194		return (EINVAL);
195
196	return (0);
197}
198
199int
200_pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol)
201{
202	int ret = 0;
203
204	if ((mattr == NULL) || (*mattr == NULL))
205		ret = EINVAL;
206	else
207		*protocol = (*mattr)->m_protocol;
208
209	return(ret);
210}
211
212int
213_pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol)
214{
215	int ret = 0;
216
217	if ((mattr == NULL) || (*mattr == NULL) ||
218	    (protocol < PTHREAD_PRIO_NONE) || (protocol > PTHREAD_PRIO_PROTECT))
219		ret = EINVAL;
220	else {
221		(*mattr)->m_protocol = protocol;
222		(*mattr)->m_ceiling = THR_MAX_RR_PRIORITY;
223	}
224	return(ret);
225}
226
227int
228_pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling)
229{
230	int ret = 0;
231
232	if ((mattr == NULL) || (*mattr == NULL))
233		ret = EINVAL;
234	else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
235		ret = EINVAL;
236	else
237		*prioceiling = (*mattr)->m_ceiling;
238
239	return(ret);
240}
241
242int
243_pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling)
244{
245	int ret = 0;
246
247	if ((mattr == NULL) || (*mattr == NULL))
248		ret = EINVAL;
249	else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
250		ret = EINVAL;
251	else
252		(*mattr)->m_ceiling = prioceiling;
253
254	return(ret);
255}
256
257