2 * Copyright (c) 2000-2003, 2007, 2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
27 * Permission to use, copy, modify, and distribute this software and
28 * its documentation for any purpose and without fee is hereby granted,
29 * provided that the above copyright notice appears in all copies and
30 * that both the copyright notice and this permission notice appear in
31 * supporting documentation.
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
37 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
38 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
39 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
40 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
41 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49 * POSIX Pthread Library
50 * -- Mutex variable support
55 #include "kern/kern_trace.h"
56 #include <sys/syscall.h>
58 #include "os/atomic.h"
61 #include "plockstat.h"
62 #else /* !PLOCKSTAT */
63 #define PLOCKSTAT_MUTEX_SPIN(x)
64 #define PLOCKSTAT_MUTEX_SPUN(x, y, z)
65 #define PLOCKSTAT_MUTEX_ERROR(x, y)
66 #define PLOCKSTAT_MUTEX_BLOCK(x)
67 #define PLOCKSTAT_MUTEX_BLOCKED(x, y)
68 #define PLOCKSTAT_MUTEX_ACQUIRE(x, y, z)
69 #define PLOCKSTAT_MUTEX_RELEASE(x, y)
70 #endif /* PLOCKSTAT */
72 extern int __unix_conforming
;
74 #ifndef BUILDING_VARIANT
76 PTHREAD_NOEXPORT PTHREAD_WEAK
// prevent inlining of return value into callers
78 _pthread_mutex_unlock_slow(pthread_mutex_t
*omutex
);
80 PTHREAD_NOEXPORT PTHREAD_WEAK
// prevent inlining of return value into callers
82 _pthread_mutex_lock_slow(pthread_mutex_t
*omutex
, bool trylock
);
84 PTHREAD_NOEXPORT PTHREAD_WEAK
// prevent inlining of return value into _pthread_mutex_lock
86 _pthread_mutex_lock_wait(pthread_mutex_t
*omutex
, uint64_t newval64
, uint64_t oldtid
);
88 #endif /* BUILDING_VARIANT */
90 #define DEBUG_TRACE_POINTS 0
92 #if DEBUG_TRACE_POINTS
93 extern int __syscall(int number
, ...);
94 #define DEBUG_TRACE(x, a, b, c, d) __syscall(SYS_kdebug_trace, TRACE_##x, a, b, c, d)
96 #define DEBUG_TRACE(x, a, b, c, d) do { } while(0)
99 #include <machine/cpu_capabilities.h>
101 static inline int _pthread_mutex_init(_pthread_mutex
*mutex
, const pthread_mutexattr_t
*attr
, uint32_t static_type
);
103 #if !__LITTLE_ENDIAN__
104 #error MUTEX_GETSEQ_ADDR assumes little endian layout of 2 32-bit sequence words
107 PTHREAD_ALWAYS_INLINE
109 MUTEX_GETSEQ_ADDR(_pthread_mutex
*mutex
,
110 volatile uint64_t **seqaddr
)
112 // addr of m_seq[1] for misaligned, m_seq[0] for aligned mutex struct
113 *seqaddr
= (volatile uint64_t *)(((uintptr_t)&mutex
->m_seq
[1]) & ~0x7ul
);
116 PTHREAD_ALWAYS_INLINE
118 MUTEX_GETTID_ADDR(_pthread_mutex
*mutex
,
119 volatile uint64_t **tidaddr
)
121 // addr of m_tid[1] for misaligned, m_tid[0] for aligned mutex struct
122 *tidaddr
= (volatile uint64_t *)(((uintptr_t)&mutex
->m_tid
[1]) & ~0x7ul
);
125 #ifndef BUILDING_VARIANT /* [ */
126 #ifndef OS_UP_VARIANT_ONLY
128 #define BLOCK_FAIL_PLOCKSTAT 0
129 #define BLOCK_SUCCESS_PLOCKSTAT 1
132 /* This function is never called and exists to provide never-fired dtrace
133 * probes so that user d scripts don't get errors.
135 PTHREAD_NOEXPORT PTHREAD_USED
137 _plockstat_never_fired(void)
139 PLOCKSTAT_MUTEX_SPIN(NULL
);
140 PLOCKSTAT_MUTEX_SPUN(NULL
, 0, 0);
145 * Initialize a mutex variable, possibly with additional attributes.
146 * Public interface - so don't trust the lock - initialize it first.
149 pthread_mutex_init(pthread_mutex_t
*omutex
, const pthread_mutexattr_t
*attr
)
152 /* conformance tests depend on not having this behavior */
153 /* The test for this behavior is optional */
154 if (_pthread_mutex_check_signature(mutex
))
157 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
158 LOCK_INIT(mutex
->lock
);
159 return (_pthread_mutex_init(mutex
, attr
, 0x7));
163 pthread_mutex_getprioceiling(const pthread_mutex_t
*omutex
, int *prioceiling
)
166 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
167 if (_pthread_mutex_check_signature(mutex
)) {
169 *prioceiling
= mutex
->prioceiling
;
177 pthread_mutex_setprioceiling(pthread_mutex_t
*omutex
, int prioceiling
, int *old_prioceiling
)
180 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
181 if (_pthread_mutex_check_signature(mutex
)) {
183 if (prioceiling
>= -999 || prioceiling
<= 999) {
184 *old_prioceiling
= mutex
->prioceiling
;
185 mutex
->prioceiling
= prioceiling
;
194 pthread_mutexattr_getprioceiling(const pthread_mutexattr_t
*attr
, int *prioceiling
)
197 if (attr
->sig
== _PTHREAD_MUTEX_ATTR_SIG
) {
198 *prioceiling
= attr
->prioceiling
;
205 pthread_mutexattr_getprotocol(const pthread_mutexattr_t
*attr
, int *protocol
)
208 if (attr
->sig
== _PTHREAD_MUTEX_ATTR_SIG
) {
209 *protocol
= attr
->protocol
;
216 pthread_mutexattr_gettype(const pthread_mutexattr_t
*attr
, int *type
)
219 if (attr
->sig
== _PTHREAD_MUTEX_ATTR_SIG
) {
227 pthread_mutexattr_getpshared(const pthread_mutexattr_t
*attr
, int *pshared
)
230 if (attr
->sig
== _PTHREAD_MUTEX_ATTR_SIG
) {
231 *pshared
= (int)attr
->pshared
;
238 pthread_mutexattr_init(pthread_mutexattr_t
*attr
)
240 attr
->prioceiling
= _PTHREAD_DEFAULT_PRIOCEILING
;
241 attr
->protocol
= _PTHREAD_DEFAULT_PROTOCOL
;
242 attr
->policy
= _PTHREAD_MUTEX_POLICY_FAIRSHARE
;
243 attr
->type
= PTHREAD_MUTEX_DEFAULT
;
244 attr
->sig
= _PTHREAD_MUTEX_ATTR_SIG
;
245 attr
->pshared
= _PTHREAD_DEFAULT_PSHARED
;
250 pthread_mutexattr_setprioceiling(pthread_mutexattr_t
*attr
, int prioceiling
)
253 if (attr
->sig
== _PTHREAD_MUTEX_ATTR_SIG
) {
254 if (prioceiling
>= -999 || prioceiling
<= 999) {
255 attr
->prioceiling
= prioceiling
;
263 pthread_mutexattr_setprotocol(pthread_mutexattr_t
*attr
, int protocol
)
266 if (attr
->sig
== _PTHREAD_MUTEX_ATTR_SIG
) {
268 case PTHREAD_PRIO_NONE
:
269 case PTHREAD_PRIO_INHERIT
:
270 case PTHREAD_PRIO_PROTECT
:
271 attr
->protocol
= protocol
;
280 pthread_mutexattr_setpolicy_np(pthread_mutexattr_t
*attr
, int policy
)
283 if (attr
->sig
== _PTHREAD_MUTEX_ATTR_SIG
) {
285 case _PTHREAD_MUTEX_POLICY_FAIRSHARE
:
286 case _PTHREAD_MUTEX_POLICY_FIRSTFIT
:
287 attr
->policy
= policy
;
296 pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int type
)
299 if (attr
->sig
== _PTHREAD_MUTEX_ATTR_SIG
) {
301 case PTHREAD_MUTEX_NORMAL
:
302 case PTHREAD_MUTEX_ERRORCHECK
:
303 case PTHREAD_MUTEX_RECURSIVE
:
304 //case PTHREAD_MUTEX_DEFAULT:
321 pthread_yield_np(void)
328 * Temp: till pshared is fixed correctly
331 pthread_mutexattr_setpshared(pthread_mutexattr_t
*attr
, int pshared
)
335 if (__unix_conforming
== 0) {
336 __unix_conforming
= 1;
338 #endif /* __DARWIN_UNIX03 */
340 if (attr
->sig
== _PTHREAD_MUTEX_ATTR_SIG
) {
342 if (( pshared
== PTHREAD_PROCESS_PRIVATE
) || (pshared
== PTHREAD_PROCESS_SHARED
))
343 #else /* __DARWIN_UNIX03 */
344 if ( pshared
== PTHREAD_PROCESS_PRIVATE
)
345 #endif /* __DARWIN_UNIX03 */
347 attr
->pshared
= pshared
;
354 PTHREAD_NOEXPORT PTHREAD_WEAK
// prevent inlining of return value into callers
356 _pthread_mutex_corruption_abort(_pthread_mutex
*mutex
);
360 _pthread_mutex_corruption_abort(_pthread_mutex
*mutex
)
362 PTHREAD_ABORT("pthread_mutex corruption: mutex %p owner changed in the middle of lock/unlock");
363 return EINVAL
; // NOTREACHED
367 * Sequence numbers and TID:
369 * In steady (and uncontended) state, an unlocked mutex will
370 * look like A=[L4 U4 TID0]. When it is being locked, it transitions
371 * to B=[L5+KE U4 TID0] and then C=[L5+KE U4 TID940]. For an uncontended mutex,
372 * the unlock path will then transition to D=[L5 U4 TID0] and then finally
375 * If a contender comes in after B, the mutex will instead transition to E=[L6+KE U4 TID0]
376 * and then F=[L6+KE U4 TID940]. If a contender comes in after C, it will transition to
377 * F=[L6+KE U4 TID940] directly. In both cases, the contender will enter the kernel with either
378 * mutexwait(U4, TID0) or mutexwait(U4, TID940). The first owner will unlock the mutex
379 * by first updating the owner to G=[L6+KE U4 TID-1] and then doing the actual unlock to
380 * H=[L6+KE U5 TID=-1] before entering the kernel with mutexdrop(U5, -1) to signal the next waiter
381 * (potentially as a prepost). When the waiter comes out of the kernel, it will update the owner to
382 * I=[L6+KE U5 TID941]. An unlock at this point is simply J=[L6 U5 TID0] and then K=[L6 U6 TID0].
384 * At various points along these timelines, since the sequence words and TID are written independently,
385 * a thread may get preempted and another thread might see inconsistent data. In the worst case, another
386 * thread may see the TID in the SWITCHING (-1) state or unlocked (0) state for longer because the
387 * owning thread was preempted.
391 * Drop the mutex unlock references from cond_wait. or mutex_unlock.
393 PTHREAD_ALWAYS_INLINE
395 _pthread_mutex_unlock_updatebits(_pthread_mutex
*mutex
, uint32_t *flagsp
, uint32_t **pmtxp
, uint32_t *mgenp
, uint32_t *ugenp
)
397 bool firstfit
= (mutex
->mtxopts
.options
.policy
== _PTHREAD_MUTEX_POLICY_FIRSTFIT
);
398 uint32_t lgenval
, ugenval
, flags
;
399 uint64_t oldtid
, newtid
;
400 volatile uint64_t *tidaddr
;
401 MUTEX_GETTID_ADDR(mutex
, &tidaddr
);
403 flags
= mutex
->mtxopts
.value
;
404 flags
&= ~_PTHREAD_MTX_OPT_NOTIFY
; // no notification by default
406 if (mutex
->mtxopts
.options
.type
!= PTHREAD_MUTEX_NORMAL
) {
407 uint64_t selfid
= _pthread_selfid_direct();
409 if (*tidaddr
!= selfid
) {
410 //PTHREAD_ABORT("dropping recur or error mutex not owned by the thread");
411 PLOCKSTAT_MUTEX_ERROR((pthread_mutex_t
*)mutex
, EPERM
);
413 } else if (mutex
->mtxopts
.options
.type
== PTHREAD_MUTEX_RECURSIVE
&&
414 --mutex
->mtxopts
.options
.lock_count
) {
415 PLOCKSTAT_MUTEX_RELEASE((pthread_mutex_t
*)mutex
, 1);
416 if (flagsp
!= NULL
) {
423 uint64_t oldval64
, newval64
;
424 volatile uint64_t *seqaddr
;
425 MUTEX_GETSEQ_ADDR(mutex
, &seqaddr
);
427 bool clearprepost
, clearnotify
, spurious
;
431 lgenval
= (uint32_t)oldval64
;
432 ugenval
= (uint32_t)(oldval64
>> 32);
434 clearprepost
= false;
438 int numwaiters
= diff_genseq(lgenval
, ugenval
); // pending waiters
440 if (numwaiters
== 0) {
441 // spurious unlock; do not touch tid
444 ugenval
+= PTHRW_INC
;
446 if ((lgenval
& PTHRW_COUNT_MASK
) == (ugenval
& PTHRW_COUNT_MASK
)) {
447 // our unlock sequence matches to lock sequence, so if the CAS is successful, the mutex is unlocked
449 /* do not reset Ibit, just K&E */
450 lgenval
&= ~(PTH_RWL_KBIT
| PTH_RWL_EBIT
);
452 newtid
= 0; // clear owner
455 lgenval
&= ~PTH_RWL_EBIT
; // reset E bit so another can acquire meanwhile
458 newtid
= PTHREAD_MTX_TID_SWITCHING
;
460 // need to signal others waiting for mutex
461 flags
|= _PTHREAD_MTX_OPT_NOTIFY
;
464 if (newtid
!= oldtid
) {
465 // We're giving up the mutex one way or the other, so go ahead and update the owner to SWITCHING
466 // or 0 so that once the CAS below succeeds, there is no stale ownership information.
467 // If the CAS of the seqaddr fails, we may loop, but it's still valid for the owner
469 if (!os_atomic_cmpxchg(tidaddr
, oldtid
, newtid
, relaxed
)) {
470 // we own this mutex, nobody should be updating it except us
471 return _pthread_mutex_corruption_abort(mutex
);
476 if (clearnotify
|| spurious
) {
477 flags
&= ~_PTHREAD_MTX_OPT_NOTIFY
;
478 if (firstfit
&& ((lgenval
& PTH_RWL_PBIT
) != 0)) {
480 lgenval
&= ~PTH_RWL_PBIT
;
484 newval64
= (((uint64_t)ugenval
) << 32);
487 } while (!os_atomic_cmpxchg(seqaddr
, oldval64
, newval64
, release
));
490 __psynch_cvclrprepost(mutex
, lgenval
, ugenval
, 0, 0, lgenval
, (flags
| _PTHREAD_MTX_OPT_MUTEX
));
500 *pmtxp
= (uint32_t *)mutex
;
502 if (flagsp
!= NULL
) {
511 __mtx_droplock(_pthread_mutex
*mutex
, uint32_t *flagsp
, uint32_t **pmtxp
, uint32_t *mgenp
, uint32_t *ugenp
)
513 return _pthread_mutex_unlock_updatebits(mutex
, flagsp
, pmtxp
, mgenp
, ugenp
);
516 PTHREAD_ALWAYS_INLINE
518 _pthread_mutex_lock_updatebits(_pthread_mutex
*mutex
, uint64_t selfid
)
521 int firstfit
= (mutex
->mtxopts
.options
.policy
== _PTHREAD_MUTEX_POLICY_FIRSTFIT
);
524 uint32_t lgenval
, ugenval
;
525 uint64_t oldval64
, newval64
;
526 volatile uint64_t *seqaddr
;
527 MUTEX_GETSEQ_ADDR(mutex
, &seqaddr
);
529 volatile uint64_t *tidaddr
;
530 MUTEX_GETTID_ADDR(mutex
, &tidaddr
);
536 lgenval
= (uint32_t)oldval64
;
537 ugenval
= (uint32_t)(oldval64
>> 32);
539 // E bit was set on first pass through the loop but is no longer
540 // set. Apparently we spin until it arrives.
541 // XXX: verify this is desired behavior.
542 } while (isebit
&& (lgenval
& PTH_RWL_EBIT
) == 0);
545 // first fit mutex now has the E bit set. Return 1.
551 isebit
= (lgenval
& PTH_RWL_EBIT
) != 0;
552 } else if ((lgenval
& (PTH_RWL_KBIT
|PTH_RWL_EBIT
)) == (PTH_RWL_KBIT
|PTH_RWL_EBIT
)) {
553 // fairshare mutex and the bits are already set, just update tid
557 // either first fit or no E bit set
559 lgenval
|= PTH_RWL_KBIT
| PTH_RWL_EBIT
;
561 newval64
= (((uint64_t)ugenval
) << 32);
565 // Retry if CAS fails, or if it succeeds with firstfit and E bit already set
566 } while (!os_atomic_cmpxchg(seqaddr
, oldval64
, newval64
, acquire
) || (firstfit
&& isebit
));
569 if (!os_atomic_cmpxchg(tidaddr
, oldtid
, selfid
, relaxed
)) {
570 // we own this mutex, nobody should be updating it except us
571 return _pthread_mutex_corruption_abort(mutex
);
580 __mtx_markprepost(_pthread_mutex
*mutex
, uint32_t updateval
, int firstfit
)
583 uint32_t lgenval
, ugenval
;
584 uint64_t oldval64
, newval64
;
586 volatile uint64_t *seqaddr
;
587 MUTEX_GETSEQ_ADDR(mutex
, &seqaddr
);
589 if (firstfit
!= 0 && (updateval
& PTH_RWL_PBIT
) != 0) {
594 flags
= mutex
->mtxopts
.value
;
597 lgenval
= (uint32_t)oldval64
;
598 ugenval
= (uint32_t)(oldval64
>> 32);
600 /* update the bits */
601 if ((lgenval
& PTHRW_COUNT_MASK
) == (ugenval
& PTHRW_COUNT_MASK
)) {
603 lgenval
&= ~PTH_RWL_PBIT
;
605 lgenval
|= PTH_RWL_PBIT
;
607 newval64
= (((uint64_t)ugenval
) << 32);
609 } while (!os_atomic_cmpxchg(seqaddr
, oldval64
, newval64
, release
));
611 if (clearprepost
!= 0) {
612 __psynch_cvclrprepost(mutex
, lgenval
, ugenval
, 0, 0, lgenval
, (flags
| _PTHREAD_MTX_OPT_MUTEX
));
620 _pthread_mutex_check_init_slow(pthread_mutex_t
*omutex
)
623 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
625 if (_pthread_mutex_check_signature_init(mutex
)) {
627 if (_pthread_mutex_check_signature_init(mutex
)) {
628 // initialize a statically initialized mutex to provide
629 // compatibility for misbehaving applications.
630 // (unlock should not be the first operation on a mutex)
631 res
= _pthread_mutex_init(mutex
, NULL
, (mutex
->sig
& 0xf));
632 } else if (_pthread_mutex_check_signature(mutex
)) {
636 } else if (_pthread_mutex_check_signature(mutex
)) {
640 PLOCKSTAT_MUTEX_ERROR(omutex
, res
);
645 PTHREAD_ALWAYS_INLINE
647 _pthread_mutex_check_init(pthread_mutex_t
*omutex
)
650 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
652 if (!_pthread_mutex_check_signature(mutex
)) {
653 return _pthread_mutex_check_init_slow(omutex
);
660 _pthread_mutex_lock_wait(pthread_mutex_t
*omutex
, uint64_t newval64
, uint64_t oldtid
)
662 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
663 uint32_t lgenval
= (uint32_t)newval64
;
664 uint32_t ugenval
= (uint32_t)(newval64
>> 32);
666 volatile uint64_t *tidaddr
;
667 MUTEX_GETTID_ADDR(mutex
, &tidaddr
);
668 uint64_t selfid
= _pthread_selfid_direct();
670 PLOCKSTAT_MUTEX_BLOCK(omutex
);
674 updateval
= __psynch_mutexwait(omutex
, lgenval
, ugenval
, oldtid
, mutex
->mtxopts
.value
);
676 } while (updateval
== (uint32_t)-1);
678 // returns 0 on succesful update; in firstfit it may fail with 1
679 } while (_pthread_mutex_lock_updatebits(mutex
, selfid
) == 1);
680 PLOCKSTAT_MUTEX_BLOCKED(omutex
, BLOCK_SUCCESS_PLOCKSTAT
);
686 _pthread_mutex_lock_slow(pthread_mutex_t
*omutex
, bool trylock
)
689 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
691 res
= _pthread_mutex_check_init(omutex
);
697 volatile uint64_t *tidaddr
;
698 MUTEX_GETTID_ADDR(mutex
, &tidaddr
);
699 uint64_t selfid
= _pthread_selfid_direct();
701 if (mutex
->mtxopts
.options
.type
!= PTHREAD_MUTEX_NORMAL
) {
702 if (*tidaddr
== selfid
) {
703 if (mutex
->mtxopts
.options
.type
== PTHREAD_MUTEX_RECURSIVE
) {
704 if (mutex
->mtxopts
.options
.lock_count
< USHRT_MAX
) {
705 mutex
->mtxopts
.options
.lock_count
++;
706 PLOCKSTAT_MUTEX_ACQUIRE(omutex
, 1, 0);
710 PLOCKSTAT_MUTEX_ERROR(omutex
, res
);
712 } else if (trylock
) { /* PTHREAD_MUTEX_ERRORCHECK */
713 // <rdar://problem/16261552> as per OpenGroup, trylock cannot
714 // return EDEADLK on a deadlock, it should return EBUSY.
716 PLOCKSTAT_MUTEX_ERROR(omutex
, res
);
717 } else { /* PTHREAD_MUTEX_ERRORCHECK */
719 PLOCKSTAT_MUTEX_ERROR(omutex
, res
);
725 uint64_t oldval64
, newval64
;
726 volatile uint64_t *seqaddr
;
727 MUTEX_GETSEQ_ADDR(mutex
, &seqaddr
);
729 uint32_t lgenval
, ugenval
;
730 bool gotlock
= false;
735 lgenval
= (uint32_t)oldval64
;
736 ugenval
= (uint32_t)(oldval64
>> 32);
738 gotlock
= ((lgenval
& PTH_RWL_EBIT
) == 0);
740 if (trylock
&& !gotlock
) {
741 // A trylock on a held lock will fail immediately. But since
742 // we did not load the sequence words atomically, perform a
743 // no-op CAS64 to ensure that nobody has unlocked concurrently.
745 // Increment the lock sequence number and force the lock into E+K
746 // mode, whether "gotlock" is true or not.
747 lgenval
+= PTHRW_INC
;
748 lgenval
|= PTH_RWL_EBIT
| PTH_RWL_KBIT
;
751 newval64
= (((uint64_t)ugenval
) << 32);
755 } while (!os_atomic_cmpxchg(seqaddr
, oldval64
, newval64
, acquire
));
758 os_atomic_store(tidaddr
, selfid
, relaxed
);
760 DEBUG_TRACE(psynch_mutex_ulock
, omutex
, lgenval
, ugenval
, selfid
);
761 PLOCKSTAT_MUTEX_ACQUIRE(omutex
, 0, 0);
762 } else if (trylock
) {
764 DEBUG_TRACE(psynch_mutex_utrylock_failed
, omutex
, lgenval
, ugenval
, oldtid
);
765 PLOCKSTAT_MUTEX_ERROR(omutex
, res
);
767 res
= _pthread_mutex_lock_wait(omutex
, newval64
, oldtid
);
770 if (res
== 0 && mutex
->mtxopts
.options
.type
== PTHREAD_MUTEX_RECURSIVE
) {
771 mutex
->mtxopts
.options
.lock_count
= 1;
774 PLOCKSTAT_MUTEX_ACQUIRE(omutex
, 0, 0);
779 #endif // OS_UP_VARIANT_ONLY
781 PTHREAD_ALWAYS_INLINE
783 _pthread_mutex_lock(pthread_mutex_t
*omutex
, bool trylock
)
785 #if PLOCKSTAT || DEBUG_TRACE_POINTS
786 if (PLOCKSTAT_MUTEX_ACQUIRE_ENABLED() || PLOCKSTAT_MUTEX_ERROR_ENABLED() ||
787 DEBUG_TRACE_POINTS
) {
788 return _pthread_mutex_lock_slow(omutex
, trylock
);
791 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
792 if (!_pthread_mutex_check_signature_fast(mutex
)) {
793 return _pthread_mutex_lock_slow(omutex
, trylock
);
797 volatile uint64_t *tidaddr
;
798 MUTEX_GETTID_ADDR(mutex
, &tidaddr
);
799 uint64_t selfid
= _pthread_selfid_direct();
801 uint64_t oldval64
, newval64
;
802 volatile uint64_t *seqaddr
;
803 MUTEX_GETSEQ_ADDR(mutex
, &seqaddr
);
805 uint32_t lgenval
, ugenval
;
806 bool gotlock
= false;
811 lgenval
= (uint32_t)oldval64
;
812 ugenval
= (uint32_t)(oldval64
>> 32);
814 gotlock
= ((lgenval
& PTH_RWL_EBIT
) == 0);
816 if (trylock
&& !gotlock
) {
817 // A trylock on a held lock will fail immediately. But since
818 // we did not load the sequence words atomically, perform a
819 // no-op CAS64 to ensure that nobody has unlocked concurrently.
821 // Increment the lock sequence number and force the lock into E+K
822 // mode, whether "gotlock" is true or not.
823 lgenval
+= PTHRW_INC
;
824 lgenval
|= PTH_RWL_EBIT
| PTH_RWL_KBIT
;
827 newval64
= (((uint64_t)ugenval
) << 32);
831 } while (!os_atomic_cmpxchg(seqaddr
, oldval64
, newval64
, acquire
));
833 if (os_fastpath(gotlock
)) {
834 os_atomic_store(tidaddr
, selfid
, relaxed
);
836 } else if (trylock
) {
839 return _pthread_mutex_lock_wait(omutex
, newval64
, oldtid
);
843 PTHREAD_NOEXPORT_VARIANT
845 pthread_mutex_lock(pthread_mutex_t
*mutex
)
847 return _pthread_mutex_lock(mutex
, false);
850 PTHREAD_NOEXPORT_VARIANT
852 pthread_mutex_trylock(pthread_mutex_t
*mutex
)
854 return _pthread_mutex_lock(mutex
, true);
857 #ifndef OS_UP_VARIANT_ONLY
860 * TODO: Priority inheritance stuff
865 _pthread_mutex_unlock_drop(pthread_mutex_t
*omutex
, uint64_t newval64
, uint32_t flags
)
868 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
869 uint32_t lgenval
= (uint32_t)newval64
;
870 uint32_t ugenval
= (uint32_t)(newval64
>> 32);
873 int firstfit
= (mutex
->mtxopts
.options
.policy
== _PTHREAD_MUTEX_POLICY_FIRSTFIT
);
874 volatile uint64_t *tidaddr
;
875 MUTEX_GETTID_ADDR(mutex
, &tidaddr
);
877 updateval
= __psynch_mutexdrop(omutex
, lgenval
, ugenval
, *tidaddr
, flags
);
879 if (updateval
== (uint32_t)-1) {
886 PTHREAD_ABORT("__p_mutexdrop failed with error %d", res
);
889 } else if (firstfit
== 1) {
890 if ((updateval
& PTH_RWL_PBIT
) != 0) {
891 __mtx_markprepost(mutex
, updateval
, firstfit
);
899 _pthread_mutex_unlock_slow(pthread_mutex_t
*omutex
)
902 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
903 uint32_t mtxgen
, mtxugen
, flags
;
905 // Initialize static mutexes for compatibility with misbehaving
906 // applications (unlock should not be the first operation on a mutex).
907 res
= _pthread_mutex_check_init(omutex
);
912 res
= _pthread_mutex_unlock_updatebits(mutex
, &flags
, NULL
, &mtxgen
, &mtxugen
);
917 if ((flags
& _PTHREAD_MTX_OPT_NOTIFY
) != 0) {
919 newval64
= (((uint64_t)mtxugen
) << 32);
921 return _pthread_mutex_unlock_drop(omutex
, newval64
, flags
);
923 volatile uint64_t *tidaddr
;
924 MUTEX_GETTID_ADDR(mutex
, &tidaddr
);
925 DEBUG_TRACE(psynch_mutex_uunlock
, omutex
, mtxgen
, mtxugen
, *tidaddr
);
931 #endif // OS_UP_VARIANT_ONLY
933 PTHREAD_NOEXPORT_VARIANT
935 pthread_mutex_unlock(pthread_mutex_t
*omutex
)
937 #if PLOCKSTAT || DEBUG_TRACE_POINTS
938 if (PLOCKSTAT_MUTEX_RELEASE_ENABLED() || PLOCKSTAT_MUTEX_ERROR_ENABLED() ||
939 DEBUG_TRACE_POINTS
) {
940 return _pthread_mutex_unlock_slow(omutex
);
943 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
944 if (!_pthread_mutex_check_signature_fast(mutex
)) {
945 return _pthread_mutex_unlock_slow(omutex
);
948 volatile uint64_t *tidaddr
;
949 MUTEX_GETTID_ADDR(mutex
, &tidaddr
);
951 uint64_t oldval64
, newval64
;
952 volatile uint64_t *seqaddr
;
953 MUTEX_GETSEQ_ADDR(mutex
, &seqaddr
);
955 uint32_t lgenval
, ugenval
;
959 lgenval
= (uint32_t)oldval64
;
960 ugenval
= (uint32_t)(oldval64
>> 32);
962 int numwaiters
= diff_genseq(lgenval
, ugenval
); // pending waiters
964 if (numwaiters
== 0) {
965 // spurious unlock; do not touch tid
967 ugenval
+= PTHRW_INC
;
969 if ((lgenval
& PTHRW_COUNT_MASK
) == (ugenval
& PTHRW_COUNT_MASK
)) {
970 // our unlock sequence matches to lock sequence, so if the CAS is successful, the mutex is unlocked
972 /* do not reset Ibit, just K&E */
973 lgenval
&= ~(PTH_RWL_KBIT
| PTH_RWL_EBIT
);
975 return _pthread_mutex_unlock_slow(omutex
);
978 // We're giving up the mutex one way or the other, so go ahead and update the owner
979 // to 0 so that once the CAS below succeeds, there is no stale ownership information.
980 // If the CAS of the seqaddr fails, we may loop, but it's still valid for the owner
982 os_atomic_store(tidaddr
, 0, relaxed
);
985 newval64
= (((uint64_t)ugenval
) << 32);
988 } while (!os_atomic_cmpxchg(seqaddr
, oldval64
, newval64
, release
));
993 #ifndef OS_UP_VARIANT_ONLY
997 _pthread_mutex_init(_pthread_mutex
*mutex
, const pthread_mutexattr_t
*attr
, uint32_t static_type
)
1000 if (attr
->sig
!= _PTHREAD_MUTEX_ATTR_SIG
) {
1003 mutex
->prioceiling
= attr
->prioceiling
;
1004 mutex
->mtxopts
.options
.protocol
= attr
->protocol
;
1005 mutex
->mtxopts
.options
.policy
= attr
->policy
;
1006 mutex
->mtxopts
.options
.type
= attr
->type
;
1007 mutex
->mtxopts
.options
.pshared
= attr
->pshared
;
1009 switch (static_type
) {
1011 mutex
->mtxopts
.options
.type
= PTHREAD_MUTEX_ERRORCHECK
;
1014 mutex
->mtxopts
.options
.type
= PTHREAD_MUTEX_RECURSIVE
;
1017 /* firstfit fall thru */
1019 mutex
->mtxopts
.options
.type
= PTHREAD_MUTEX_DEFAULT
;
1025 mutex
->prioceiling
= _PTHREAD_DEFAULT_PRIOCEILING
;
1026 mutex
->mtxopts
.options
.protocol
= _PTHREAD_DEFAULT_PROTOCOL
;
1027 if (static_type
!= 3) {
1028 mutex
->mtxopts
.options
.policy
= _PTHREAD_MUTEX_POLICY_FAIRSHARE
;
1030 mutex
->mtxopts
.options
.policy
= _PTHREAD_MUTEX_POLICY_FIRSTFIT
;
1032 mutex
->mtxopts
.options
.pshared
= _PTHREAD_DEFAULT_PSHARED
;
1035 mutex
->mtxopts
.options
.notify
= 0;
1036 mutex
->mtxopts
.options
.unused
= 0;
1037 mutex
->mtxopts
.options
.hold
= 0;
1038 mutex
->mtxopts
.options
.mutex
= 1;
1039 mutex
->mtxopts
.options
.lock_count
= 0;
1041 mutex
->m_tid
[0] = 0;
1042 mutex
->m_tid
[1] = 0;
1043 mutex
->m_seq
[0] = 0;
1044 mutex
->m_seq
[1] = 0;
1045 mutex
->m_seq
[2] = 0;
1046 mutex
->prioceiling
= 0;
1047 mutex
->priority
= 0;
1049 mutex
->mtxopts
.options
.misalign
= (((uintptr_t)&mutex
->m_seq
[0]) & 0x7ul
) != 0;
1050 if (mutex
->mtxopts
.options
.misalign
) {
1051 mutex
->m_tid
[0] = ~0u;
1053 mutex
->m_seq
[2] = ~0u;
1056 long sig
= _PTHREAD_MUTEX_SIG
;
1057 if (mutex
->mtxopts
.options
.type
== PTHREAD_MUTEX_NORMAL
&&
1058 mutex
->mtxopts
.options
.policy
== _PTHREAD_MUTEX_POLICY_FAIRSHARE
) {
1059 // rdar://18148854 _pthread_mutex_lock & pthread_mutex_unlock fastpath
1060 sig
= _PTHREAD_MUTEX_SIG_fast
;
1063 // unused, purely for detecting copied mutexes and smashes during debugging:
1064 mutex
->reserved2
[0] = ~(uintptr_t)mutex
; // use ~ to hide from leaks
1065 mutex
->reserved2
[1] = (uintptr_t)sig
;
1067 // Ensure all contents are properly set before setting signature.
1068 #if defined(__LP64__)
1069 // For binary compatibility reasons we cannot require natural alignment of
1070 // the 64bit 'sig' long value in the struct. rdar://problem/21610439
1071 uint32_t *sig32_ptr
= (uint32_t*)&mutex
->sig
;
1072 uint32_t *sig32_val
= (uint32_t*)&sig
;
1073 *(sig32_ptr
+1) = *(sig32_val
+1);
1074 os_atomic_store(sig32_ptr
, *sig32_val
, release
);
1076 os_atomic_store2o(mutex
, sig
, sig
, release
);
1083 pthread_mutex_destroy(pthread_mutex_t
*omutex
)
1085 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
1090 if (_pthread_mutex_check_signature(mutex
)) {
1091 uint32_t lgenval
, ugenval
;
1093 volatile uint64_t *seqaddr
;
1094 MUTEX_GETSEQ_ADDR(mutex
, &seqaddr
);
1095 volatile uint64_t *tidaddr
;
1096 MUTEX_GETTID_ADDR(mutex
, &tidaddr
);
1098 oldval64
= *seqaddr
;
1099 lgenval
= (uint32_t)oldval64
;
1100 ugenval
= (uint32_t)(oldval64
>> 32);
1101 if ((*tidaddr
== (uint64_t)0) &&
1102 ((lgenval
& PTHRW_COUNT_MASK
) == (ugenval
& PTHRW_COUNT_MASK
))) {
1103 mutex
->sig
= _PTHREAD_NO_SIG
;
1108 } else if (_pthread_mutex_check_signature_init(mutex
)) {
1109 mutex
->sig
= _PTHREAD_NO_SIG
;
1112 UNLOCK(mutex
->lock
);
1117 #endif // OS_UP_VARIANT_ONLY
1119 #endif /* !BUILDING_VARIANT ] */
1121 #ifndef OS_UP_VARIANT_ONLY
1123 * Destroy a mutex attribute structure.
1126 pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
)
1129 if (__unix_conforming
== 0) {
1130 __unix_conforming
= 1;
1132 if (attr
->sig
!= _PTHREAD_MUTEX_ATTR_SIG
) {
1135 #endif /* __DARWIN_UNIX03 */
1137 attr
->sig
= _PTHREAD_NO_SIG
;
1141 #endif // OS_UP_VARIANT_ONLY