]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libthr/thread/thr_mutexattr.c
Update libc++ to 3.8.0. Excerpted list of fixes (with upstream revision
[FreeBSD/FreeBSD.git] / lib / libthr / thread / thr_mutexattr.c
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$");
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
90 int
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
108 int
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
122 int
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
136 int
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
150 int
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
165 int
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
179 int
180 _pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr,
181         int *pshared)
182 {
183
184         if (attr == NULL || *attr == NULL)
185                 return (EINVAL);
186         *pshared = (*attr)->m_pshared;
187         return (0);
188 }
189
190 int
191 _pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
192 {
193
194         if (attr == NULL || *attr == NULL ||
195             (pshared != PTHREAD_PROCESS_PRIVATE &&
196             pshared != PTHREAD_PROCESS_SHARED))
197                 return (EINVAL);
198         (*attr)->m_pshared = pshared;
199         return (0);
200 }
201
202 int
203 _pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol)
204 {
205         int ret = 0;
206
207         if (mattr == NULL || *mattr == NULL)
208                 ret = EINVAL;
209         else
210                 *protocol = (*mattr)->m_protocol;
211
212         return (ret);
213 }
214
215 int
216 _pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol)
217 {
218         int ret = 0;
219
220         if (mattr == NULL || *mattr == NULL ||
221             protocol < PTHREAD_PRIO_NONE || protocol > PTHREAD_PRIO_PROTECT)
222                 ret = EINVAL;
223         else {
224                 (*mattr)->m_protocol = protocol;
225                 (*mattr)->m_ceiling = THR_MAX_RR_PRIORITY;
226         }
227         return (ret);
228 }
229
230 int
231 _pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling)
232 {
233         int ret = 0;
234
235         if (mattr == NULL || *mattr == NULL)
236                 ret = EINVAL;
237         else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
238                 ret = EINVAL;
239         else
240                 *prioceiling = (*mattr)->m_ceiling;
241
242         return (ret);
243 }
244
245 int
246 _pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling)
247 {
248         int ret = 0;
249
250         if (mattr == NULL || *mattr == NULL)
251                 ret = EINVAL;
252         else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
253                 ret = EINVAL;
254         else
255                 (*mattr)->m_ceiling = prioceiling;
256
257         return (ret);
258 }
259
260 int
261 _pthread_mutexattr_getrobust(pthread_mutexattr_t *mattr, int *robust)
262 {
263         int ret;
264
265         if (mattr == NULL || *mattr == NULL) {
266                 ret = EINVAL;
267         } else {
268                 ret = 0;
269                 *robust = (*mattr)->m_robust;
270         }
271         return (ret);
272 }
273
274 int
275 _pthread_mutexattr_setrobust(pthread_mutexattr_t *mattr, int robust)
276 {
277         int ret;
278
279         if (mattr == NULL || *mattr == NULL) {
280                 ret = EINVAL;
281         } else if (robust != PTHREAD_MUTEX_STALLED &&
282             robust != PTHREAD_MUTEX_ROBUST) {
283                 ret = EINVAL;
284         } else {
285                 ret = 0;
286                 (*mattr)->m_robust = robust;
287         }
288         return (ret);
289 }
290