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