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