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