pthread_mutex.c revision 1.49 1 /* $NetBSD: pthread_mutex.c,v 1.49 2008/05/25 12:29:59 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2003, 2006, 2007, 2008 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * To track threads waiting for mutexes to be released, we use lockless
34 * lists built on atomic operations and memory barriers.
35 *
36 * A simple spinlock would be faster and make the code easier to
37 * follow, but spinlocks are problematic in userspace. If a thread is
38 * preempted by the kernel while holding a spinlock, any other thread
39 * attempting to acquire that spinlock will needlessly busy wait.
40 *
41 * There is no good way to know that the holding thread is no longer
42 * running, nor to request a wake-up once it has begun running again.
43 * Of more concern, threads in the SCHED_FIFO class do not have a
44 * limited time quantum and so could spin forever, preventing the
45 * thread holding the spinlock from getting CPU time: it would never
46 * be released.
47 */
48
49 #include <sys/cdefs.h>
50 __RCSID("$NetBSD: pthread_mutex.c,v 1.49 2008/05/25 12:29:59 ad Exp $");
51
52 #include <sys/types.h>
53 #include <sys/lwpctl.h>
54
55 #include <errno.h>
56 #include <limits.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <stdio.h>
60
61 #include "pthread.h"
62 #include "pthread_int.h"
63
64 #define pt_nextwaiter pt_sleep.ptqe_next
65
66 #define MUTEX_WAITERS_BIT ((uintptr_t)0x01)
67 #define MUTEX_RECURSIVE_BIT ((uintptr_t)0x02)
68 #define MUTEX_DEFERRED_BIT ((uintptr_t)0x04)
69 #define MUTEX_THREAD ((uintptr_t)-16L)
70
71 #define MUTEX_HAS_WAITERS(x) ((uintptr_t)(x) & MUTEX_WAITERS_BIT)
72 #define MUTEX_RECURSIVE(x) ((uintptr_t)(x) & MUTEX_RECURSIVE_BIT)
73 #define MUTEX_OWNER(x) ((uintptr_t)(x) & MUTEX_THREAD)
74
75 #if __GNUC_PREREQ__(3, 0)
76 #define NOINLINE __attribute ((noinline))
77 #else
78 #define NOINLINE /* nothing */
79 #endif
80
81 static void pthread__mutex_wakeup(pthread_t, pthread_mutex_t *);
82 static int pthread__mutex_lock_slow(pthread_mutex_t *);
83 static int pthread__mutex_unlock_slow(pthread_mutex_t *);
84 static void pthread__mutex_pause(void);
85
86 int _pthread_mutex_held_np(pthread_mutex_t *);
87 pthread_t _pthread_mutex_owner_np(pthread_mutex_t *);
88
89 __weak_alias(pthread_mutex_held_np,_pthread_mutex_held_np)
90 __weak_alias(pthread_mutex_owner_np,_pthread_mutex_owner_np)
91
92 __strong_alias(__libc_mutex_init,pthread_mutex_init)
93 __strong_alias(__libc_mutex_lock,pthread_mutex_lock)
94 __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock)
95 __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock)
96 __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy)
97
98 __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init)
99 __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy)
100 __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype)
101
102 __strong_alias(__libc_thr_once,pthread_once)
103
104 int
105 pthread_mutex_init(pthread_mutex_t *ptm, const pthread_mutexattr_t *attr)
106 {
107 intptr_t type;
108
109 if (attr == NULL)
110 type = PTHREAD_MUTEX_NORMAL;
111 else
112 type = (intptr_t)attr->ptma_private;
113
114 switch (type) {
115 case PTHREAD_MUTEX_ERRORCHECK:
116 ptm->ptm_errorcheck = 1;
117 ptm->ptm_owner = NULL;
118 break;
119 case PTHREAD_MUTEX_RECURSIVE:
120 ptm->ptm_errorcheck = 0;
121 ptm->ptm_owner = (void *)MUTEX_RECURSIVE_BIT;
122 break;
123 default:
124 ptm->ptm_errorcheck = 0;
125 ptm->ptm_owner = NULL;
126 break;
127 }
128
129 ptm->ptm_magic = _PT_MUTEX_MAGIC;
130 ptm->ptm_waiters = NULL;
131 ptm->ptm_recursed = 0;
132
133 return 0;
134 }
135
136
137 int
138 pthread_mutex_destroy(pthread_mutex_t *ptm)
139 {
140
141 pthread__error(EINVAL, "Invalid mutex",
142 ptm->ptm_magic == _PT_MUTEX_MAGIC);
143 pthread__error(EBUSY, "Destroying locked mutex",
144 MUTEX_OWNER(ptm->ptm_owner) == 0);
145
146 ptm->ptm_magic = _PT_MUTEX_DEAD;
147 return 0;
148 }
149
150 int
151 pthread_mutex_lock(pthread_mutex_t *ptm)
152 {
153 pthread_t self;
154 void *val;
155
156 self = pthread__self();
157 val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
158 if (__predict_true(val == NULL)) {
159 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
160 membar_enter();
161 #endif
162 return 0;
163 }
164 return pthread__mutex_lock_slow(ptm);
165 }
166
167 /* We want function call overhead. */
168 NOINLINE static void
169 pthread__mutex_pause(void)
170 {
171
172 pthread__smt_pause();
173 }
174
175 /*
176 * Spin while the holder is running. 'lwpctl' gives us the true
177 * status of the thread. pt_blocking is set by libpthread in order
178 * to cut out system call and kernel spinlock overhead on remote CPUs
179 * (could represent many thousands of clock cycles). pt_blocking also
180 * makes this thread yield if the target is calling sched_yield().
181 */
182 NOINLINE static void *
183 pthread__mutex_spin(pthread_mutex_t *ptm, pthread_t owner)
184 {
185 pthread_t thread;
186 unsigned int count, i;
187
188 for (count = 2;; owner = ptm->ptm_owner) {
189 thread = (pthread_t)MUTEX_OWNER(owner);
190 if (thread == NULL)
191 break;
192 if (thread->pt_lwpctl->lc_curcpu == LWPCTL_CPU_NONE ||
193 thread->pt_blocking)
194 break;
195 if (count < 128)
196 count += count;
197 for (i = count; i != 0; i--)
198 pthread__mutex_pause();
199 }
200
201 return owner;
202 }
203
204 NOINLINE static int
205 pthread__mutex_lock_slow(pthread_mutex_t *ptm)
206 {
207 void *waiters, *new, *owner, *next;
208 pthread_t self;
209
210 pthread__error(EINVAL, "Invalid mutex",
211 ptm->ptm_magic == _PT_MUTEX_MAGIC);
212
213 owner = ptm->ptm_owner;
214 self = pthread__self();
215
216 /* Recursive or errorcheck? */
217 if (MUTEX_OWNER(owner) == (uintptr_t)self) {
218 if (MUTEX_RECURSIVE(owner)) {
219 if (ptm->ptm_recursed == INT_MAX)
220 return EAGAIN;
221 ptm->ptm_recursed++;
222 return 0;
223 }
224 if (ptm->ptm_errorcheck)
225 return EDEADLK;
226 }
227
228 for (;; owner = ptm->ptm_owner) {
229 /* Spin while the owner is running. */
230 owner = pthread__mutex_spin(ptm, owner);
231
232 /* If it has become free, try to acquire it again. */
233 if (MUTEX_OWNER(owner) == 0) {
234 do {
235 new = (void *)
236 ((uintptr_t)self | (uintptr_t)owner);
237 next = atomic_cas_ptr(&ptm->ptm_owner, owner,
238 new);
239 if (next == owner) {
240 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
241 membar_enter();
242 #endif
243 return 0;
244 }
245 owner = next;
246 } while (MUTEX_OWNER(owner) == 0);
247 /*
248 * We have lost the race to acquire the mutex.
249 * The new owner could be running on another
250 * CPU, in which case we should spin and avoid
251 * the overhead of blocking.
252 */
253 continue;
254 }
255
256 /*
257 * Nope, still held. Add thread to the list of waiters.
258 * Issue a memory barrier to ensure sleeponq/nextwaiter
259 * are visible before we enter the waiters list.
260 */
261 self->pt_sleeponq = 1;
262 for (waiters = ptm->ptm_waiters;; waiters = next) {
263 self->pt_nextwaiter = waiters;
264 membar_producer();
265 next = atomic_cas_ptr(&ptm->ptm_waiters, waiters, self);
266 if (next == waiters)
267 break;
268 }
269
270 /*
271 * Set the waiters bit and block.
272 *
273 * Note that the mutex can become unlocked before we set
274 * the waiters bit. If that happens it's not safe to sleep
275 * as we may never be awoken: we must remove the current
276 * thread from the waiters list and try again.
277 *
278 * Because we are doing this atomically, we can't remove
279 * one waiter: we must remove all waiters and awken them,
280 * then sleep in _lwp_park() until we have been awoken.
281 *
282 * Issue a memory barrier to ensure that we are reading
283 * the value of ptm_owner/pt_sleeponq after we have entered
284 * the waiters list (the CAS itself must be atomic).
285 */
286 membar_consumer();
287 for (owner = ptm->ptm_owner;; owner = next) {
288 if (MUTEX_HAS_WAITERS(owner))
289 break;
290 if (MUTEX_OWNER(owner) == 0) {
291 pthread__mutex_wakeup(self, ptm);
292 break;
293 }
294 new = (void *)((uintptr_t)owner | MUTEX_WAITERS_BIT);
295 next = atomic_cas_ptr(&ptm->ptm_owner, owner, new);
296 if (next == owner) {
297 /*
298 * pthread_mutex_unlock() can do a
299 * non-interlocked CAS. We cannot
300 * know if our attempt to set the
301 * waiters bit has succeeded while
302 * the holding thread is running.
303 * There are many assumptions; see
304 * sys/kern/kern_mutex.c for details.
305 * In short, we must spin if we see
306 * that the holder is running again.
307 */
308 membar_sync();
309 next = pthread__mutex_spin(ptm, owner);
310 }
311 }
312
313 /*
314 * We may have been awoken by the current thread above,
315 * or will be awoken by the current holder of the mutex.
316 * The key requirement is that we must not proceed until
317 * told that we are no longer waiting (via pt_sleeponq
318 * being set to zero). Otherwise it is unsafe to re-enter
319 * the thread onto the waiters list.
320 */
321 while (self->pt_sleeponq) {
322 self->pt_blocking++;
323 (void)_lwp_park(NULL, 0,
324 __UNVOLATILE(&ptm->ptm_waiters), NULL);
325 self->pt_blocking--;
326 membar_sync();
327 }
328 }
329 }
330
331 int
332 pthread_mutex_trylock(pthread_mutex_t *ptm)
333 {
334 pthread_t self;
335 void *val, *new, *next;
336
337 self = pthread__self();
338 val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
339 if (__predict_true(val == NULL)) {
340 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
341 membar_enter();
342 #endif
343 return 0;
344 }
345
346 if (MUTEX_RECURSIVE(val)) {
347 if (MUTEX_OWNER(val) == 0) {
348 new = (void *)((uintptr_t)self | (uintptr_t)val);
349 next = atomic_cas_ptr(&ptm->ptm_owner, val, new);
350 if (__predict_true(next == val)) {
351 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
352 membar_enter();
353 #endif
354 return 0;
355 }
356 }
357 if (MUTEX_OWNER(val) == (uintptr_t)self) {
358 if (ptm->ptm_recursed == INT_MAX)
359 return EAGAIN;
360 ptm->ptm_recursed++;
361 return 0;
362 }
363 }
364
365 return EBUSY;
366 }
367
368 int
369 pthread_mutex_unlock(pthread_mutex_t *ptm)
370 {
371 pthread_t self;
372 void *value;
373
374 /*
375 * Note this may be a non-interlocked CAS. See lock_slow()
376 * above and sys/kern/kern_mutex.c for details.
377 */
378 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
379 membar_exit();
380 #endif
381 self = pthread__self();
382 value = atomic_cas_ptr_ni(&ptm->ptm_owner, self, NULL);
383 if (__predict_true(value == self))
384 return 0;
385 return pthread__mutex_unlock_slow(ptm);
386 }
387
388 NOINLINE static int
389 pthread__mutex_unlock_slow(pthread_mutex_t *ptm)
390 {
391 pthread_t self, owner, new;
392 int weown, error, deferred;
393
394 pthread__error(EINVAL, "Invalid mutex",
395 ptm->ptm_magic == _PT_MUTEX_MAGIC);
396
397 self = pthread__self();
398 owner = ptm->ptm_owner;
399 weown = (MUTEX_OWNER(owner) == (uintptr_t)self);
400 deferred = (int)((uintptr_t)owner & MUTEX_DEFERRED_BIT);
401 error = 0;
402
403 if (ptm->ptm_errorcheck) {
404 if (!weown) {
405 error = EPERM;
406 new = owner;
407 } else {
408 new = NULL;
409 }
410 } else if (MUTEX_RECURSIVE(owner)) {
411 if (!weown) {
412 error = EPERM;
413 new = owner;
414 } else if (ptm->ptm_recursed) {
415 ptm->ptm_recursed--;
416 new = owner;
417 } else {
418 new = (pthread_t)MUTEX_RECURSIVE_BIT;
419 }
420 } else {
421 pthread__error(EPERM,
422 "Unlocking unlocked mutex", (owner != NULL));
423 pthread__error(EPERM,
424 "Unlocking mutex owned by another thread", weown);
425 new = NULL;
426 }
427
428 /*
429 * Release the mutex. If there appear to be waiters, then
430 * wake them up.
431 */
432 if (new != owner) {
433 owner = atomic_swap_ptr(&ptm->ptm_owner, new);
434 if (MUTEX_HAS_WAITERS(owner) != 0) {
435 pthread__mutex_wakeup(self, ptm);
436 return 0;
437 }
438 }
439
440 /*
441 * There were no waiters, but we may have deferred waking
442 * other threads until mutex unlock - we must wake them now.
443 */
444 if (!deferred)
445 return error;
446
447 if (self->pt_nwaiters == 1) {
448 /*
449 * If the calling thread is about to block, defer
450 * unparking the target until _lwp_park() is called.
451 */
452 if (self->pt_willpark && self->pt_unpark == 0) {
453 self->pt_unpark = self->pt_waiters[0];
454 self->pt_unparkhint =
455 __UNVOLATILE(&ptm->ptm_waiters);
456 } else {
457 (void)_lwp_unpark(self->pt_waiters[0],
458 __UNVOLATILE(&ptm->ptm_waiters));
459 }
460 } else {
461 (void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters,
462 __UNVOLATILE(&ptm->ptm_waiters));
463 }
464 self->pt_nwaiters = 0;
465
466 return error;
467 }
468
469 static void
470 pthread__mutex_wakeup(pthread_t self, pthread_mutex_t *ptm)
471 {
472 pthread_t thread, next;
473 ssize_t n, rv;
474
475 /*
476 * Take ownership of the current set of waiters. No
477 * need for a memory barrier following this, all loads
478 * are dependent upon 'thread'.
479 */
480 thread = atomic_swap_ptr(&ptm->ptm_waiters, NULL);
481
482 for (;;) {
483 /*
484 * Pull waiters from the queue and add to our list.
485 * Use a memory barrier to ensure that we safely
486 * read the value of pt_nextwaiter before 'thread'
487 * sees pt_sleeponq being cleared.
488 */
489 for (n = self->pt_nwaiters, self->pt_nwaiters = 0;
490 n < pthread__unpark_max && thread != NULL;
491 thread = next) {
492 next = thread->pt_nextwaiter;
493 if (thread != self) {
494 self->pt_waiters[n++] = thread->pt_lid;
495 membar_sync();
496 }
497 thread->pt_sleeponq = 0;
498 /* No longer safe to touch 'thread' */
499 }
500
501 switch (n) {
502 case 0:
503 return;
504 case 1:
505 /*
506 * If the calling thread is about to block,
507 * defer unparking the target until _lwp_park()
508 * is called.
509 */
510 if (self->pt_willpark && self->pt_unpark == 0) {
511 self->pt_unpark = self->pt_waiters[0];
512 self->pt_unparkhint =
513 __UNVOLATILE(&ptm->ptm_waiters);
514 return;
515 }
516 rv = (ssize_t)_lwp_unpark(self->pt_waiters[0],
517 __UNVOLATILE(&ptm->ptm_waiters));
518 if (rv != 0 && errno != EALREADY && errno != EINTR &&
519 errno != ESRCH) {
520 pthread__errorfunc(__FILE__, __LINE__,
521 __func__, "_lwp_unpark failed");
522 }
523 return;
524 default:
525 rv = _lwp_unpark_all(self->pt_waiters, (size_t)n,
526 __UNVOLATILE(&ptm->ptm_waiters));
527 if (rv != 0 && errno != EINTR) {
528 pthread__errorfunc(__FILE__, __LINE__,
529 __func__, "_lwp_unpark_all failed");
530 }
531 break;
532 }
533 }
534 }
535 int
536 pthread_mutexattr_init(pthread_mutexattr_t *attr)
537 {
538
539 attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
540 attr->ptma_private = (void *)PTHREAD_MUTEX_DEFAULT;
541 return 0;
542 }
543
544 int
545 pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
546 {
547
548 pthread__error(EINVAL, "Invalid mutex attribute",
549 attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
550
551 return 0;
552 }
553
554
555 int
556 pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
557 {
558
559 pthread__error(EINVAL, "Invalid mutex attribute",
560 attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
561
562 *typep = (int)(intptr_t)attr->ptma_private;
563 return 0;
564 }
565
566
567 int
568 pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
569 {
570
571 pthread__error(EINVAL, "Invalid mutex attribute",
572 attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
573
574 switch (type) {
575 case PTHREAD_MUTEX_NORMAL:
576 case PTHREAD_MUTEX_ERRORCHECK:
577 case PTHREAD_MUTEX_RECURSIVE:
578 attr->ptma_private = (void *)(intptr_t)type;
579 return 0;
580 default:
581 return EINVAL;
582 }
583 }
584
585
586 static void
587 once_cleanup(void *closure)
588 {
589
590 pthread_mutex_unlock((pthread_mutex_t *)closure);
591 }
592
593
594 int
595 pthread_once(pthread_once_t *once_control, void (*routine)(void))
596 {
597
598 if (once_control->pto_done == 0) {
599 pthread_mutex_lock(&once_control->pto_mutex);
600 pthread_cleanup_push(&once_cleanup, &once_control->pto_mutex);
601 if (once_control->pto_done == 0) {
602 routine();
603 once_control->pto_done = 1;
604 }
605 pthread_cleanup_pop(1);
606 }
607
608 return 0;
609 }
610
611 int
612 pthread__mutex_deferwake(pthread_t thread, pthread_mutex_t *ptm)
613 {
614
615 if (MUTEX_OWNER(ptm->ptm_owner) != (uintptr_t)thread)
616 return 0;
617 atomic_or_ulong((volatile unsigned long *)
618 (uintptr_t)&ptm->ptm_owner,
619 (unsigned long)MUTEX_DEFERRED_BIT);
620 return 1;
621 }
622
623 int
624 _pthread_mutex_held_np(pthread_mutex_t *ptm)
625 {
626
627 return MUTEX_OWNER(ptm->ptm_owner) == (uintptr_t)pthread__self();
628 }
629
630 pthread_t
631 _pthread_mutex_owner_np(pthread_mutex_t *ptm)
632 {
633
634 return (pthread_t)MUTEX_OWNER(ptm->ptm_owner);
635 }
636