pthread_mutex.c revision 1.29.2.2 1 1.29.2.2 ad /* $NetBSD: pthread_mutex.c,v 1.29.2.2 2007/08/04 13:37:50 ad Exp $ */
2 1.29.2.2 ad
3 1.29.2.2 ad /*-
4 1.29.2.2 ad * Copyright (c) 2001, 2003, 2006, 2007 The NetBSD Foundation, Inc.
5 1.29.2.2 ad * All rights reserved.
6 1.29.2.2 ad *
7 1.29.2.2 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.29.2.2 ad * by Nathan J. Williams, by Jason R. Thorpe, and by Andrew Doran.
9 1.29.2.2 ad *
10 1.29.2.2 ad * Redistribution and use in source and binary forms, with or without
11 1.29.2.2 ad * modification, are permitted provided that the following conditions
12 1.29.2.2 ad * are met:
13 1.29.2.2 ad * 1. Redistributions of source code must retain the above copyright
14 1.29.2.2 ad * notice, this list of conditions and the following disclaimer.
15 1.29.2.2 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.29.2.2 ad * notice, this list of conditions and the following disclaimer in the
17 1.29.2.2 ad * documentation and/or other materials provided with the distribution.
18 1.29.2.2 ad * 3. All advertising materials mentioning features or use of this software
19 1.29.2.2 ad * must display the following acknowledgement:
20 1.29.2.2 ad * This product includes software developed by the NetBSD
21 1.29.2.2 ad * Foundation, Inc. and its contributors.
22 1.29.2.2 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.29.2.2 ad * contributors may be used to endorse or promote products derived
24 1.29.2.2 ad * from this software without specific prior written permission.
25 1.29.2.2 ad *
26 1.29.2.2 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.29.2.2 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.29.2.2 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.29.2.2 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.29.2.2 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.29.2.2 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.29.2.2 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.29.2.2 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.29.2.2 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.29.2.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.29.2.2 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.29.2.2 ad */
38 1.29.2.2 ad
39 1.29.2.2 ad #include <sys/cdefs.h>
40 1.29.2.2 ad __RCSID("$NetBSD: pthread_mutex.c,v 1.29.2.2 2007/08/04 13:37:50 ad Exp $");
41 1.29.2.2 ad
42 1.29.2.2 ad #include <errno.h>
43 1.29.2.2 ad #include <limits.h>
44 1.29.2.2 ad #include <stdlib.h>
45 1.29.2.2 ad #include <string.h>
46 1.29.2.2 ad
47 1.29.2.2 ad #include "pthread.h"
48 1.29.2.2 ad #include "pthread_int.h"
49 1.29.2.2 ad
50 1.29.2.2 ad static int pthread_mutex_lock_slow(pthread_t, pthread_mutex_t *);
51 1.29.2.2 ad
52 1.29.2.2 ad __strong_alias(__libc_mutex_init,pthread_mutex_init)
53 1.29.2.2 ad __strong_alias(__libc_mutex_lock,pthread_mutex_lock)
54 1.29.2.2 ad __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock)
55 1.29.2.2 ad __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock)
56 1.29.2.2 ad __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy)
57 1.29.2.2 ad
58 1.29.2.2 ad __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init)
59 1.29.2.2 ad __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy)
60 1.29.2.2 ad __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype)
61 1.29.2.2 ad
62 1.29.2.2 ad __strong_alias(__libc_thr_once,pthread_once)
63 1.29.2.2 ad
64 1.29.2.2 ad struct mutex_private {
65 1.29.2.2 ad int type;
66 1.29.2.2 ad int recursecount;
67 1.29.2.2 ad };
68 1.29.2.2 ad
69 1.29.2.2 ad static const struct mutex_private mutex_private_default = {
70 1.29.2.2 ad PTHREAD_MUTEX_DEFAULT,
71 1.29.2.2 ad 0,
72 1.29.2.2 ad };
73 1.29.2.2 ad
74 1.29.2.2 ad struct mutexattr_private {
75 1.29.2.2 ad int type;
76 1.29.2.2 ad };
77 1.29.2.2 ad
78 1.29.2.2 ad static const struct mutexattr_private mutexattr_private_default = {
79 1.29.2.2 ad PTHREAD_MUTEX_DEFAULT,
80 1.29.2.2 ad };
81 1.29.2.2 ad
82 1.29.2.2 ad /*
83 1.29.2.2 ad * If the mutex does not already have private data (i.e. was statically
84 1.29.2.2 ad * initialized), then give it the default.
85 1.29.2.2 ad */
86 1.29.2.2 ad #define GET_MUTEX_PRIVATE(mutex, mp) \
87 1.29.2.2 ad do { \
88 1.29.2.2 ad if (__predict_false((mp = (mutex)->ptm_private) == NULL)) { \
89 1.29.2.2 ad /* LINTED cast away const */ \
90 1.29.2.2 ad mp = ((mutex)->ptm_private = \
91 1.29.2.2 ad (void *)&mutex_private_default); \
92 1.29.2.2 ad } \
93 1.29.2.2 ad } while (/*CONSTCOND*/0)
94 1.29.2.2 ad
95 1.29.2.2 ad int
96 1.29.2.2 ad pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
97 1.29.2.2 ad {
98 1.29.2.2 ad struct mutexattr_private *map;
99 1.29.2.2 ad struct mutex_private *mp;
100 1.29.2.2 ad
101 1.29.2.2 ad pthread__error(EINVAL, "Invalid mutex attribute",
102 1.29.2.2 ad (attr == NULL) || (attr->ptma_magic == _PT_MUTEXATTR_MAGIC));
103 1.29.2.2 ad
104 1.29.2.2 ad if (attr != NULL && (map = attr->ptma_private) != NULL &&
105 1.29.2.2 ad memcmp(map, &mutexattr_private_default, sizeof(*map)) != 0) {
106 1.29.2.2 ad mp = malloc(sizeof(*mp));
107 1.29.2.2 ad if (mp == NULL)
108 1.29.2.2 ad return ENOMEM;
109 1.29.2.2 ad
110 1.29.2.2 ad mp->type = map->type;
111 1.29.2.2 ad mp->recursecount = 0;
112 1.29.2.2 ad } else {
113 1.29.2.2 ad /* LINTED cast away const */
114 1.29.2.2 ad mp = (struct mutex_private *) &mutex_private_default;
115 1.29.2.2 ad }
116 1.29.2.2 ad
117 1.29.2.2 ad mutex->ptm_magic = _PT_MUTEX_MAGIC;
118 1.29.2.2 ad mutex->ptm_owner = NULL;
119 1.29.2.2 ad pthread_lockinit(&mutex->ptm_lock);
120 1.29.2.2 ad pthread_lockinit(&mutex->ptm_interlock);
121 1.29.2.2 ad PTQ_INIT(&mutex->ptm_blocked);
122 1.29.2.2 ad mutex->ptm_private = mp;
123 1.29.2.2 ad
124 1.29.2.2 ad return 0;
125 1.29.2.2 ad }
126 1.29.2.2 ad
127 1.29.2.2 ad
128 1.29.2.2 ad int
129 1.29.2.2 ad pthread_mutex_destroy(pthread_mutex_t *mutex)
130 1.29.2.2 ad {
131 1.29.2.2 ad
132 1.29.2.2 ad pthread__error(EINVAL, "Invalid mutex",
133 1.29.2.2 ad mutex->ptm_magic == _PT_MUTEX_MAGIC);
134 1.29.2.2 ad pthread__error(EBUSY, "Destroying locked mutex",
135 1.29.2.2 ad mutex->ptm_lock == __SIMPLELOCK_UNLOCKED);
136 1.29.2.2 ad
137 1.29.2.2 ad mutex->ptm_magic = _PT_MUTEX_DEAD;
138 1.29.2.2 ad if (mutex->ptm_private != NULL &&
139 1.29.2.2 ad mutex->ptm_private != (const void *)&mutex_private_default)
140 1.29.2.2 ad free(mutex->ptm_private);
141 1.29.2.2 ad
142 1.29.2.2 ad return 0;
143 1.29.2.2 ad }
144 1.29.2.2 ad
145 1.29.2.2 ad
146 1.29.2.2 ad /*
147 1.29.2.2 ad * Note regarding memory visibility: Pthreads has rules about memory
148 1.29.2.2 ad * visibility and mutexes. Very roughly: Memory a thread can see when
149 1.29.2.2 ad * it unlocks a mutex can be seen by another thread that locks the
150 1.29.2.2 ad * same mutex.
151 1.29.2.2 ad *
152 1.29.2.2 ad * A memory barrier after a lock and before an unlock will provide
153 1.29.2.2 ad * this behavior. This code relies on pthread__simple_lock_try() to issue
154 1.29.2.2 ad * a barrier after obtaining a lock, and on pthread__simple_unlock() to
155 1.29.2.2 ad * issue a barrier before releasing a lock.
156 1.29.2.2 ad */
157 1.29.2.2 ad
158 1.29.2.2 ad int
159 1.29.2.2 ad pthread_mutex_lock(pthread_mutex_t *mutex)
160 1.29.2.2 ad {
161 1.29.2.2 ad pthread_t self;
162 1.29.2.2 ad int error;
163 1.29.2.2 ad
164 1.29.2.2 ad self = pthread__self();
165 1.29.2.2 ad
166 1.29.2.2 ad PTHREADD_ADD(PTHREADD_MUTEX_LOCK);
167 1.29.2.2 ad
168 1.29.2.2 ad /*
169 1.29.2.2 ad * Note that if we get the lock, we don't have to deal with any
170 1.29.2.2 ad * non-default lock type handling.
171 1.29.2.2 ad */
172 1.29.2.2 ad if (__predict_false(pthread__simple_lock_try(&mutex->ptm_lock) == 0)) {
173 1.29.2.2 ad error = pthread_mutex_lock_slow(self, mutex);
174 1.29.2.2 ad if (error)
175 1.29.2.2 ad return error;
176 1.29.2.2 ad }
177 1.29.2.2 ad
178 1.29.2.2 ad /*
179 1.29.2.2 ad * We have the lock!
180 1.29.2.2 ad */
181 1.29.2.2 ad self->pt_mutexhint = mutex;
182 1.29.2.2 ad mutex->ptm_owner = self;
183 1.29.2.2 ad
184 1.29.2.2 ad return 0;
185 1.29.2.2 ad }
186 1.29.2.2 ad
187 1.29.2.2 ad
188 1.29.2.2 ad static int
189 1.29.2.2 ad pthread_mutex_lock_slow(pthread_t self, pthread_mutex_t *mutex)
190 1.29.2.2 ad {
191 1.29.2.2 ad extern int pthread__started;
192 1.29.2.2 ad struct mutex_private *mp;
193 1.29.2.2 ad sigset_t ss;
194 1.29.2.2 ad int count;
195 1.29.2.2 ad
196 1.29.2.2 ad pthread__error(EINVAL, "Invalid mutex",
197 1.29.2.2 ad mutex->ptm_magic == _PT_MUTEX_MAGIC);
198 1.29.2.2 ad
199 1.29.2.2 ad PTHREADD_ADD(PTHREADD_MUTEX_LOCK_SLOW);
200 1.29.2.2 ad
201 1.29.2.2 ad for (;;) {
202 1.29.2.2 ad /* Spin for a while. */
203 1.29.2.2 ad count = pthread__nspins;
204 1.29.2.2 ad while (mutex->ptm_lock == __SIMPLELOCK_LOCKED && --count > 0)
205 1.29.2.2 ad pthread__smt_pause();
206 1.29.2.2 ad if (count > 0) {
207 1.29.2.2 ad if (pthread__simple_lock_try(&mutex->ptm_lock) != 0)
208 1.29.2.2 ad break;
209 1.29.2.2 ad continue;
210 1.29.2.2 ad }
211 1.29.2.2 ad
212 1.29.2.2 ad /* Okay, didn't look free. Get the interlock... */
213 1.29.2.2 ad pthread_spinlock(self, &mutex->ptm_interlock);
214 1.29.2.2 ad
215 1.29.2.2 ad /*
216 1.29.2.2 ad * The mutex_unlock routine will get the interlock
217 1.29.2.2 ad * before looking at the list of sleepers, so if the
218 1.29.2.2 ad * lock is held we can safely put ourselves on the
219 1.29.2.2 ad * sleep queue. If it's not held, we can try taking it
220 1.29.2.2 ad * again.
221 1.29.2.2 ad */
222 1.29.2.2 ad PTQ_INSERT_HEAD(&mutex->ptm_blocked, self, pt_sleep);
223 1.29.2.2 ad if (mutex->ptm_lock != __SIMPLELOCK_LOCKED) {
224 1.29.2.2 ad PTQ_REMOVE(&mutex->ptm_blocked, self, pt_sleep);
225 1.29.2.2 ad pthread_spinunlock(self, &mutex->ptm_interlock);
226 1.29.2.2 ad continue;
227 1.29.2.2 ad }
228 1.29.2.2 ad
229 1.29.2.2 ad GET_MUTEX_PRIVATE(mutex, mp);
230 1.29.2.2 ad
231 1.29.2.2 ad if (mutex->ptm_owner == self) {
232 1.29.2.2 ad switch (mp->type) {
233 1.29.2.2 ad case PTHREAD_MUTEX_ERRORCHECK:
234 1.29.2.2 ad PTQ_REMOVE(&mutex->ptm_blocked, self, pt_sleep);
235 1.29.2.2 ad pthread_spinunlock(self, &mutex->ptm_interlock);
236 1.29.2.2 ad return EDEADLK;
237 1.29.2.2 ad
238 1.29.2.2 ad case PTHREAD_MUTEX_RECURSIVE:
239 1.29.2.2 ad /*
240 1.29.2.2 ad * It's safe to do this without
241 1.29.2.2 ad * holding the interlock, because
242 1.29.2.2 ad * we only modify it if we know we
243 1.29.2.2 ad * own the mutex.
244 1.29.2.2 ad */
245 1.29.2.2 ad PTQ_REMOVE(&mutex->ptm_blocked, self, pt_sleep);
246 1.29.2.2 ad pthread_spinunlock(self, &mutex->ptm_interlock);
247 1.29.2.2 ad if (mp->recursecount == INT_MAX)
248 1.29.2.2 ad return EAGAIN;
249 1.29.2.2 ad mp->recursecount++;
250 1.29.2.2 ad return 0;
251 1.29.2.2 ad }
252 1.29.2.2 ad }
253 1.29.2.2 ad
254 1.29.2.2 ad if (pthread__started == 0) {
255 1.29.2.2 ad /* The spec says we must deadlock, so... */
256 1.29.2.2 ad pthread__assert(mp->type == PTHREAD_MUTEX_NORMAL);
257 1.29.2.2 ad (void) sigprocmask(SIG_SETMASK, NULL, &ss);
258 1.29.2.2 ad for (;;) {
259 1.29.2.2 ad sigsuspend(&ss);
260 1.29.2.2 ad }
261 1.29.2.2 ad /*NOTREACHED*/
262 1.29.2.2 ad }
263 1.29.2.2 ad
264 1.29.2.2 ad /*
265 1.29.2.2 ad * Locking a mutex is not a cancellation
266 1.29.2.2 ad * point, so we don't need to do the
267 1.29.2.2 ad * test-cancellation dance. We may get woken
268 1.29.2.2 ad * up spuriously by pthread_cancel or signals,
269 1.29.2.2 ad * but it's okay since we're just going to
270 1.29.2.2 ad * retry.
271 1.29.2.2 ad */
272 1.29.2.2 ad self->pt_sleeponq = 1;
273 1.29.2.2 ad self->pt_sleepobj = &mutex->ptm_blocked;
274 1.29.2.2 ad pthread_spinunlock(self, &mutex->ptm_interlock);
275 1.29.2.2 ad (void)pthread__park(self, &mutex->ptm_interlock,
276 1.29.2.2 ad &mutex->ptm_blocked, NULL, 0, &mutex->ptm_blocked);
277 1.29.2.2 ad }
278 1.29.2.2 ad
279 1.29.2.2 ad return 0;
280 1.29.2.2 ad }
281 1.29.2.2 ad
282 1.29.2.2 ad
283 1.29.2.2 ad int
284 1.29.2.2 ad pthread_mutex_trylock(pthread_mutex_t *mutex)
285 1.29.2.2 ad {
286 1.29.2.2 ad struct mutex_private *mp;
287 1.29.2.2 ad pthread_t self;
288 1.29.2.2 ad
289 1.29.2.2 ad pthread__error(EINVAL, "Invalid mutex",
290 1.29.2.2 ad mutex->ptm_magic == _PT_MUTEX_MAGIC);
291 1.29.2.2 ad
292 1.29.2.2 ad self = pthread__self();
293 1.29.2.2 ad
294 1.29.2.2 ad PTHREADD_ADD(PTHREADD_MUTEX_TRYLOCK);
295 1.29.2.2 ad if (pthread__simple_lock_try(&mutex->ptm_lock) == 0) {
296 1.29.2.2 ad /*
297 1.29.2.2 ad * These tests can be performed without holding the
298 1.29.2.2 ad * interlock because these fields are only modified
299 1.29.2.2 ad * if we know we own the mutex.
300 1.29.2.2 ad */
301 1.29.2.2 ad GET_MUTEX_PRIVATE(mutex, mp);
302 1.29.2.2 ad if (mp->type == PTHREAD_MUTEX_RECURSIVE &&
303 1.29.2.2 ad mutex->ptm_owner == self) {
304 1.29.2.2 ad if (mp->recursecount == INT_MAX)
305 1.29.2.2 ad return EAGAIN;
306 1.29.2.2 ad mp->recursecount++;
307 1.29.2.2 ad return 0;
308 1.29.2.2 ad }
309 1.29.2.2 ad
310 1.29.2.2 ad return EBUSY;
311 1.29.2.2 ad }
312 1.29.2.2 ad
313 1.29.2.2 ad mutex->ptm_owner = self;
314 1.29.2.2 ad self->pt_mutexhint = mutex;
315 1.29.2.2 ad
316 1.29.2.2 ad return 0;
317 1.29.2.2 ad }
318 1.29.2.2 ad
319 1.29.2.2 ad
320 1.29.2.2 ad int
321 1.29.2.2 ad pthread_mutex_unlock(pthread_mutex_t *mutex)
322 1.29.2.2 ad {
323 1.29.2.2 ad struct mutex_private *mp;
324 1.29.2.2 ad pthread_t self;
325 1.29.2.2 ad int weown;
326 1.29.2.2 ad
327 1.29.2.2 ad pthread__error(EINVAL, "Invalid mutex",
328 1.29.2.2 ad mutex->ptm_magic == _PT_MUTEX_MAGIC);
329 1.29.2.2 ad
330 1.29.2.2 ad PTHREADD_ADD(PTHREADD_MUTEX_UNLOCK);
331 1.29.2.2 ad
332 1.29.2.2 ad GET_MUTEX_PRIVATE(mutex, mp);
333 1.29.2.2 ad
334 1.29.2.2 ad self = pthread_self();
335 1.29.2.2 ad /*
336 1.29.2.2 ad * These tests can be performed without holding the
337 1.29.2.2 ad * interlock because these fields are only modified
338 1.29.2.2 ad * if we know we own the mutex.
339 1.29.2.2 ad */
340 1.29.2.2 ad weown = (mutex->ptm_owner == self);
341 1.29.2.2 ad switch (mp->type) {
342 1.29.2.2 ad case PTHREAD_MUTEX_RECURSIVE:
343 1.29.2.2 ad if (!weown)
344 1.29.2.2 ad return EPERM;
345 1.29.2.2 ad if (mp->recursecount != 0) {
346 1.29.2.2 ad mp->recursecount--;
347 1.29.2.2 ad return 0;
348 1.29.2.2 ad }
349 1.29.2.2 ad break;
350 1.29.2.2 ad case PTHREAD_MUTEX_ERRORCHECK:
351 1.29.2.2 ad if (!weown)
352 1.29.2.2 ad return EPERM;
353 1.29.2.2 ad /*FALLTHROUGH*/
354 1.29.2.2 ad default:
355 1.29.2.2 ad if (__predict_false(!weown)) {
356 1.29.2.2 ad pthread__error(EPERM, "Unlocking unlocked mutex",
357 1.29.2.2 ad (mutex->ptm_owner != 0));
358 1.29.2.2 ad pthread__error(EPERM,
359 1.29.2.2 ad "Unlocking mutex owned by another thread", weown);
360 1.29.2.2 ad }
361 1.29.2.2 ad break;
362 1.29.2.2 ad }
363 1.29.2.2 ad
364 1.29.2.2 ad mutex->ptm_owner = NULL;
365 1.29.2.2 ad pthread__simple_unlock(&mutex->ptm_lock);
366 1.29.2.2 ad
367 1.29.2.2 ad /*
368 1.29.2.2 ad * Do a double-checked locking dance to see if there are any
369 1.29.2.2 ad * waiters. If we don't see any waiters, we can exit, because
370 1.29.2.2 ad * we've already released the lock. If we do see waiters, they
371 1.29.2.2 ad * were probably waiting on us... there's a slight chance that
372 1.29.2.2 ad * they are waiting on a different thread's ownership of the
373 1.29.2.2 ad * lock that happened between the unlock above and this
374 1.29.2.2 ad * examination of the queue; if so, no harm is done, as the
375 1.29.2.2 ad * waiter will loop and see that the mutex is still locked.
376 1.29.2.2 ad *
377 1.29.2.2 ad * Note that waiters may have been transferred here from a
378 1.29.2.2 ad * condition variable.
379 1.29.2.2 ad */
380 1.29.2.2 ad if (self->pt_mutexhint == mutex)
381 1.29.2.2 ad self->pt_mutexhint = NULL;
382 1.29.2.2 ad
383 1.29.2.2 ad pthread_spinlock(self, &mutex->ptm_interlock);
384 1.29.2.2 ad pthread__unpark_all(self, &mutex->ptm_interlock, &mutex->ptm_blocked);
385 1.29.2.2 ad
386 1.29.2.2 ad return 0;
387 1.29.2.2 ad }
388 1.29.2.2 ad
389 1.29.2.2 ad int
390 1.29.2.2 ad pthread_mutexattr_init(pthread_mutexattr_t *attr)
391 1.29.2.2 ad {
392 1.29.2.2 ad struct mutexattr_private *map;
393 1.29.2.2 ad
394 1.29.2.2 ad map = malloc(sizeof(*map));
395 1.29.2.2 ad if (map == NULL)
396 1.29.2.2 ad return ENOMEM;
397 1.29.2.2 ad
398 1.29.2.2 ad *map = mutexattr_private_default;
399 1.29.2.2 ad
400 1.29.2.2 ad attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
401 1.29.2.2 ad attr->ptma_private = map;
402 1.29.2.2 ad
403 1.29.2.2 ad return 0;
404 1.29.2.2 ad }
405 1.29.2.2 ad
406 1.29.2.2 ad
407 1.29.2.2 ad int
408 1.29.2.2 ad pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
409 1.29.2.2 ad {
410 1.29.2.2 ad
411 1.29.2.2 ad pthread__error(EINVAL, "Invalid mutex attribute",
412 1.29.2.2 ad attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
413 1.29.2.2 ad
414 1.29.2.2 ad attr->ptma_magic = _PT_MUTEXATTR_DEAD;
415 1.29.2.2 ad if (attr->ptma_private != NULL)
416 1.29.2.2 ad free(attr->ptma_private);
417 1.29.2.2 ad
418 1.29.2.2 ad return 0;
419 1.29.2.2 ad }
420 1.29.2.2 ad
421 1.29.2.2 ad
422 1.29.2.2 ad int
423 1.29.2.2 ad pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
424 1.29.2.2 ad {
425 1.29.2.2 ad struct mutexattr_private *map;
426 1.29.2.2 ad
427 1.29.2.2 ad pthread__error(EINVAL, "Invalid mutex attribute",
428 1.29.2.2 ad attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
429 1.29.2.2 ad
430 1.29.2.2 ad map = attr->ptma_private;
431 1.29.2.2 ad
432 1.29.2.2 ad *typep = map->type;
433 1.29.2.2 ad
434 1.29.2.2 ad return 0;
435 1.29.2.2 ad }
436 1.29.2.2 ad
437 1.29.2.2 ad
438 1.29.2.2 ad int
439 1.29.2.2 ad pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
440 1.29.2.2 ad {
441 1.29.2.2 ad struct mutexattr_private *map;
442 1.29.2.2 ad
443 1.29.2.2 ad pthread__error(EINVAL, "Invalid mutex attribute",
444 1.29.2.2 ad attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
445 1.29.2.2 ad
446 1.29.2.2 ad map = attr->ptma_private;
447 1.29.2.2 ad
448 1.29.2.2 ad switch (type) {
449 1.29.2.2 ad case PTHREAD_MUTEX_NORMAL:
450 1.29.2.2 ad case PTHREAD_MUTEX_ERRORCHECK:
451 1.29.2.2 ad case PTHREAD_MUTEX_RECURSIVE:
452 1.29.2.2 ad map->type = type;
453 1.29.2.2 ad break;
454 1.29.2.2 ad
455 1.29.2.2 ad default:
456 1.29.2.2 ad return EINVAL;
457 1.29.2.2 ad }
458 1.29.2.2 ad
459 1.29.2.2 ad return 0;
460 1.29.2.2 ad }
461 1.29.2.2 ad
462 1.29.2.2 ad
463 1.29.2.2 ad static void
464 1.29.2.2 ad once_cleanup(void *closure)
465 1.29.2.2 ad {
466 1.29.2.2 ad
467 1.29.2.2 ad pthread_mutex_unlock((pthread_mutex_t *)closure);
468 1.29.2.2 ad }
469 1.29.2.2 ad
470 1.29.2.2 ad
471 1.29.2.2 ad int
472 1.29.2.2 ad pthread_once(pthread_once_t *once_control, void (*routine)(void))
473 1.29.2.2 ad {
474 1.29.2.2 ad
475 1.29.2.2 ad if (once_control->pto_done == 0) {
476 1.29.2.2 ad pthread_mutex_lock(&once_control->pto_mutex);
477 1.29.2.2 ad pthread_cleanup_push(&once_cleanup, &once_control->pto_mutex);
478 1.29.2.2 ad if (once_control->pto_done == 0) {
479 1.29.2.2 ad routine();
480 1.29.2.2 ad once_control->pto_done = 1;
481 1.29.2.2 ad }
482 1.29.2.2 ad pthread_cleanup_pop(1);
483 1.29.2.2 ad }
484 1.29.2.2 ad
485 1.29.2.2 ad return 0;
486 1.29.2.2 ad }
487