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