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