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.
48 * POSIX Pthread Library
53 #include <sys/time.h> /* For struct timespec and getclock(). */
56 #include "plockstat.h"
57 #else /* !PLOCKSTAT */
58 #define PLOCKSTAT_MUTEX_RELEASE(x, y)
59 #endif /* PLOCKSTAT */
61 extern int __gettimeofday(struct timeval
*, struct timezone
*);
62 extern void _pthread_testcancel(pthread_t thread
, int isconforming
);
65 int _pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
,
66 const struct timespec
*abstime
, int isRelative
, int isconforming
);
70 COND_GETSEQ_ADDR(_pthread_cond
*cond
,
71 volatile uint64_t **c_lsseqaddr
,
72 volatile uint32_t **c_lseqcnt
,
73 volatile uint32_t **c_useqcnt
,
74 volatile uint32_t **c_sseqcnt
)
77 *c_lseqcnt
= &cond
->c_seq
[1];
78 *c_sseqcnt
= &cond
->c_seq
[2];
79 *c_useqcnt
= &cond
->c_seq
[0];
81 *c_lseqcnt
= &cond
->c_seq
[0];
82 *c_sseqcnt
= &cond
->c_seq
[1];
83 *c_useqcnt
= &cond
->c_seq
[2];
85 *c_lsseqaddr
= (volatile uint64_t *)*c_lseqcnt
;
88 #ifndef BUILDING_VARIANT /* [ */
90 static void _pthread_cond_cleanup(void *arg
);
91 static void _pthread_cond_updateval(_pthread_cond
* cond
, int error
,
96 pthread_condattr_init(pthread_condattr_t
*attr
)
98 attr
->sig
= _PTHREAD_COND_ATTR_SIG
;
99 attr
->pshared
= _PTHREAD_DEFAULT_PSHARED
;
104 pthread_condattr_destroy(pthread_condattr_t
*attr
)
106 attr
->sig
= _PTHREAD_NO_SIG
;
111 pthread_condattr_getpshared(const pthread_condattr_t
*attr
, int *pshared
)
114 if (attr
->sig
== _PTHREAD_COND_ATTR_SIG
) {
115 *pshared
= (int)attr
->pshared
;
122 pthread_condattr_setpshared(pthread_condattr_t
*attr
, int pshared
)
125 if (attr
->sig
== _PTHREAD_COND_ATTR_SIG
) {
127 if (pshared
== PTHREAD_PROCESS_PRIVATE
|| pshared
== PTHREAD_PROCESS_SHARED
)
128 #else /* __DARWIN_UNIX03 */
129 if (pshared
== PTHREAD_PROCESS_PRIVATE
)
130 #endif /* __DARWIN_UNIX03 */
132 attr
->pshared
= pshared
;
140 pthread_cond_timedwait_relative_np(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
,
141 const struct timespec
*abstime
)
143 return _pthread_cond_wait(cond
, mutex
, abstime
, 1, 0);
146 #endif /* !BUILDING_VARIANT ] */
148 PTHREAD_ALWAYS_INLINE
150 _pthread_cond_init(_pthread_cond
*cond
, const pthread_condattr_t
*attr
, int conforming
)
152 volatile uint64_t *c_lsseqaddr
;
153 volatile uint32_t *c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
161 cond
->misalign
= (((uintptr_t)&cond
->c_seq
[0]) & 0x7) != 0;
162 COND_GETSEQ_ADDR(cond
, &c_lsseqaddr
, &c_lseqcnt
, &c_useqcnt
, &c_sseqcnt
);
163 *c_sseqcnt
= PTH_RWS_CV_CBIT
; // set Sword to 0c
167 cond
->pshared
= attr
->pshared
;
169 cond
->pshared
= _PTHREAD_DEFAULT_PSHARED
;
172 cond
->pshared
= _PTHREAD_DEFAULT_PSHARED
;
175 long sig
= _PTHREAD_COND_SIG
;
177 // Ensure all contents are properly set before setting signature.
178 #if defined(__LP64__)
179 // For binary compatibility reasons we cannot require natural alignment of
180 // the 64bit 'sig' long value in the struct. rdar://problem/21610439
181 uint32_t *sig32_ptr
= (uint32_t*)&cond
->sig
;
182 uint32_t *sig32_val
= (uint32_t*)&sig
;
183 *(sig32_ptr
+ 1) = *(sig32_val
+ 1);
184 os_atomic_store(sig32_ptr
, *sig32_val
, release
);
186 os_atomic_store2o(cond
, sig
, sig
, release
);
192 #ifndef BUILDING_VARIANT /* [ */
196 _pthread_cond_check_init_slow(_pthread_cond
*cond
, bool *inited
)
199 if (cond
->sig
== _PTHREAD_COND_SIG_init
) {
200 _PTHREAD_LOCK(cond
->lock
);
201 if (cond
->sig
== _PTHREAD_COND_SIG_init
) {
202 res
= _pthread_cond_init(cond
, NULL
, 0);
206 } else if (cond
->sig
== _PTHREAD_COND_SIG
) {
209 _PTHREAD_UNLOCK(cond
->lock
);
210 } else if (cond
->sig
== _PTHREAD_COND_SIG
) {
216 PTHREAD_ALWAYS_INLINE
218 _pthread_cond_check_init(_pthread_cond
*cond
, bool *inited
)
221 if (cond
->sig
!= _PTHREAD_COND_SIG
) {
222 return _pthread_cond_check_init_slow(cond
, inited
);
227 PTHREAD_NOEXPORT_VARIANT
229 pthread_cond_destroy(pthread_cond_t
*ocond
)
231 _pthread_cond
*cond
= (_pthread_cond
*)ocond
;
233 if (cond
->sig
== _PTHREAD_COND_SIG
) {
234 _PTHREAD_LOCK(cond
->lock
);
236 uint64_t oldval64
, newval64
;
237 uint32_t lcntval
, ucntval
, scntval
;
238 volatile uint64_t *c_lsseqaddr
;
239 volatile uint32_t *c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
241 COND_GETSEQ_ADDR(cond
, &c_lsseqaddr
, &c_lseqcnt
, &c_useqcnt
, &c_sseqcnt
);
244 lcntval
= *c_lseqcnt
;
245 ucntval
= *c_useqcnt
;
246 scntval
= *c_sseqcnt
;
248 // validate it is not busy
249 if ((lcntval
& PTHRW_COUNT_MASK
) != (scntval
& PTHRW_COUNT_MASK
)) {
253 oldval64
= (((uint64_t)scntval
) << 32);
256 } while (!os_atomic_cmpxchg(c_lsseqaddr
, oldval64
, newval64
, seq_cst
));
258 // <rdar://problem/13782056> Need to clear preposts.
260 bool needclearpre
= ((scntval
& PTH_RWS_CV_PBIT
) != 0);
261 if (needclearpre
&& cond
->pshared
== PTHREAD_PROCESS_SHARED
) {
262 flags
|= _PTHREAD_MTX_OPT_PSHARED
;
265 cond
->sig
= _PTHREAD_NO_SIG
;
268 _PTHREAD_UNLOCK(cond
->lock
);
271 (void)__psynch_cvclrprepost(cond
, lcntval
, ucntval
, scntval
, 0, lcntval
, flags
);
273 } else if (cond
->sig
== _PTHREAD_COND_SIG_init
) {
274 // Compatibility for misbehaving applications that attempt to
275 // destroy a statically initialized condition variable.
276 cond
->sig
= _PTHREAD_NO_SIG
;
282 PTHREAD_ALWAYS_INLINE
284 _pthread_cond_signal(pthread_cond_t
*ocond
, bool broadcast
, mach_port_t thread
)
287 _pthread_cond
*cond
= (_pthread_cond
*)ocond
;
293 uint64_t oldval64
, newval64
;
294 uint32_t lcntval
, ucntval
, scntval
;
295 volatile uint64_t *c_lsseqaddr
;
296 volatile uint32_t *c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
298 int retry_count
= 0, uretry_count
= 0;
302 res
= _pthread_cond_check_init(cond
, &inited
);
303 if (res
!= 0 || inited
== true) {
307 COND_GETSEQ_ADDR(cond
, &c_lsseqaddr
, &c_lseqcnt
, &c_useqcnt
, &c_sseqcnt
);
313 lcntval
= *c_lseqcnt
;
314 ucntval
= *c_useqcnt
;
315 scntval
= *c_sseqcnt
;
319 if (((lcntval
& PTHRW_COUNT_MASK
) == (scntval
& PTHRW_COUNT_MASK
)) ||
320 (thread
== MACH_PORT_NULL
&& ((lcntval
& PTHRW_COUNT_MASK
) == (ucntval
& PTHRW_COUNT_MASK
)))) {
321 /* validate it is spurious and return */
322 oldval64
= (((uint64_t)scntval
) << 32);
326 if (!os_atomic_cmpxchg(c_lsseqaddr
, oldval64
, newval64
, seq_cst
)) {
338 /* validate to eliminate spurious values, race snapshots */
339 if (is_seqhigher((scntval
& PTHRW_COUNT_MASK
), (lcntval
& PTHRW_COUNT_MASK
))) {
340 /* since ucntval may be newer, just redo */
342 if (retry_count
> 8192) {
349 } else if (is_seqhigher((ucntval
& PTHRW_COUNT_MASK
), (lcntval
& PTHRW_COUNT_MASK
))) {
350 /* since ucntval may be newer, just redo */
352 if (uretry_count
> 8192) {
354 * U value if not used for a while can go out of sync
355 * set this to S value and try one more time.
357 if (ucountreset
!= 0) {
359 } else if (os_atomic_cmpxchg(c_useqcnt
, ucntval
, (scntval
& PTHRW_COUNT_MASK
), seq_cst
)) {
360 /* now the U is reset to S value */
370 if (is_seqlower(ucntval
& PTHRW_COUNT_MASK
, scntval
& PTHRW_COUNT_MASK
) != 0) {
371 /* If U < S, set U = S+diff due to intr's TO, etc */
372 ulval
= (scntval
& PTHRW_COUNT_MASK
);
374 /* If U >= S, set U = U+diff due to intr's TO, etc */
375 ulval
= (ucntval
& PTHRW_COUNT_MASK
);
379 diffgen
= diff_genseq(lcntval
, ulval
);
381 ulval
= (lcntval
& PTHRW_COUNT_MASK
);
386 } while (retry
|| !os_atomic_cmpxchg(c_useqcnt
, ucntval
, ulval
, seq_cst
));
389 if (cond
->pshared
== PTHREAD_PROCESS_SHARED
) {
390 flags
|= _PTHREAD_MTX_OPT_PSHARED
;
393 uint64_t cvlsgen
= ((uint64_t)scntval
<< 32) | lcntval
;
396 // pass old U val so kernel will know the diffgen
397 uint64_t cvudgen
= ((uint64_t)ucntval
<< 32) | diffgen
;
398 updateval
= __psynch_cvbroad(ocond
, cvlsgen
, cvudgen
, flags
, NULL
, 0, 0);
400 updateval
= __psynch_cvsignal(ocond
, cvlsgen
, ucntval
, thread
, NULL
, 0, 0, flags
);
403 if (updateval
!= (uint32_t)-1 && updateval
!= 0) {
404 _pthread_cond_updateval(cond
, 0, updateval
);
411 * Signal a condition variable, waking up all threads waiting for it.
413 PTHREAD_NOEXPORT_VARIANT
415 pthread_cond_broadcast(pthread_cond_t
*ocond
)
417 return _pthread_cond_signal(ocond
, true, MACH_PORT_NULL
);
421 * Signal a condition variable, waking a specified thread.
423 PTHREAD_NOEXPORT_VARIANT
425 pthread_cond_signal_thread_np(pthread_cond_t
*ocond
, pthread_t thread
)
427 mach_port_t mp
= MACH_PORT_NULL
;
429 mp
= pthread_mach_thread_np((_Nonnull pthread_t
)thread
);
431 return _pthread_cond_signal(ocond
, false, mp
);
435 * Signal a condition variable, waking only one thread.
437 PTHREAD_NOEXPORT_VARIANT
439 pthread_cond_signal(pthread_cond_t
*ocond
)
441 return _pthread_cond_signal(ocond
, false, MACH_PORT_NULL
);
445 * Manage a list of condition variables associated with a mutex
449 * Suspend waiting for a condition variable.
450 * Note: we have to keep a list of condition variables which are using
451 * this same mutex variable so we can detect invalid 'destroy' sequences.
452 * If isconforming < 0, we skip the _pthread_testcancel(), but keep the
453 * remaining conforming behavior..
455 PTHREAD_NOEXPORT PTHREAD_NOINLINE
457 _pthread_cond_wait(pthread_cond_t
*ocond
,
458 pthread_mutex_t
*omutex
,
459 const struct timespec
*abstime
,
464 _pthread_cond
*cond
= (_pthread_cond
*)ocond
;
465 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
466 struct timespec then
= { 0, 0 };
467 uint32_t mtxgen
, mtxugen
, flags
=0, updateval
;
468 uint32_t lcntval
, ucntval
, scntval
;
469 uint32_t nlval
, ulval
, savebits
;
470 volatile uint64_t *c_lsseqaddr
;
471 volatile uint32_t *c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
472 uint64_t oldval64
, newval64
, mugen
, cvlsgen
;
473 uint32_t *npmtx
= NULL
;
475 res
= _pthread_cond_check_init(cond
, NULL
);
481 if (!_pthread_mutex_check_signature(mutex
) &&
482 !_pthread_mutex_check_signature_init(mutex
)) {
485 if (isconforming
> 0) {
486 _pthread_testcancel(pthread_self(), 1);
490 /* send relative time to kernel */
492 if (isRelative
== 0) {
495 __gettimeofday(&tv
, NULL
);
496 TIMEVAL_TO_TIMESPEC(&tv
, &now
);
498 /* Compute relative time to sleep */
499 then
.tv_nsec
= abstime
->tv_nsec
- now
.tv_nsec
;
500 then
.tv_sec
= abstime
->tv_sec
- now
.tv_sec
;
501 if (then
.tv_nsec
< 0) {
502 then
.tv_nsec
+= NSEC_PER_SEC
;
505 if (then
.tv_sec
< 0 || (then
.tv_sec
== 0 && then
.tv_nsec
== 0)) {
509 (abstime
->tv_sec
< 0 ||
510 abstime
->tv_nsec
< 0 ||
511 abstime
->tv_nsec
>= NSEC_PER_SEC
)) {
515 then
.tv_sec
= abstime
->tv_sec
;
516 then
.tv_nsec
= abstime
->tv_nsec
;
517 if ((then
.tv_sec
== 0) && (then
.tv_nsec
== 0)) {
521 if (isconforming
&& (then
.tv_sec
< 0 || then
.tv_nsec
< 0)) {
524 if (then
.tv_nsec
>= NSEC_PER_SEC
) {
529 if (cond
->busy
!= NULL
&& cond
->busy
!= mutex
) {
533 COND_GETSEQ_ADDR(cond
, &c_lsseqaddr
, &c_lseqcnt
, &c_useqcnt
, &c_sseqcnt
);
536 lcntval
= *c_lseqcnt
;
537 ucntval
= *c_useqcnt
;
538 scntval
= *c_sseqcnt
;
540 oldval64
= (((uint64_t)scntval
) << 32);
543 /* remove c and p bits on S word */
544 savebits
= scntval
& PTH_RWS_CV_BITSALL
;
545 ulval
= (scntval
& PTHRW_COUNT_MASK
);
546 nlval
= lcntval
+ PTHRW_INC
;
547 newval64
= (((uint64_t)ulval
) << 32);
549 } while (!os_atomic_cmpxchg(c_lsseqaddr
, oldval64
, newval64
, seq_cst
));
553 res
= _pthread_mutex_droplock(mutex
, &flags
, &npmtx
, &mtxgen
, &mtxugen
);
555 /* TBD: cases are for normal (non owner for recursive mutex; error checking)*/
559 if ((flags
& _PTHREAD_MTX_OPT_NOTIFY
) == 0) {
563 mugen
= ((uint64_t)mtxugen
<< 32) | mtxgen
;
565 flags
&= ~_PTHREAD_MTX_OPT_MUTEX
; /* reset the mutex bit as this is cvar */
567 cvlsgen
= ((uint64_t)(ulval
| savebits
)<< 32) | nlval
;
569 // SUSv3 requires pthread_cond_wait to be a cancellation point
571 pthread_cleanup_push(_pthread_cond_cleanup
, (void *)cond
);
572 updateval
= __psynch_cvwait(ocond
, cvlsgen
, ucntval
, (pthread_mutex_t
*)npmtx
, mugen
, flags
, (int64_t)then
.tv_sec
, (int32_t)then
.tv_nsec
);
573 _pthread_testcancel(pthread_self(), isconforming
);
574 pthread_cleanup_pop(0);
576 updateval
= __psynch_cvwait(ocond
, cvlsgen
, ucntval
, (pthread_mutex_t
*)npmtx
, mugen
, flags
, (int64_t)then
.tv_sec
, (int32_t)then
.tv_nsec
);
579 if (updateval
== (uint32_t)-1) {
581 switch (err
& 0xff) {
586 // spurious wakeup (unless canceled)
594 // add unlock ref to show one less waiter
595 _pthread_cond_updateval(cond
, err
, 0);
596 } else if (updateval
!= 0) {
598 // The return due to prepost and might have bit states
599 // update S and return for prepo if needed
600 _pthread_cond_updateval(cond
, 0, updateval
);
603 pthread_mutex_lock(omutex
);
609 _pthread_cond_cleanup(void *arg
)
611 _pthread_cond
*cond
= (_pthread_cond
*)arg
;
612 pthread_mutex_t
*mutex
;
615 pthread_t thread
= pthread_self();
618 _PTHREAD_LOCK(thread
->lock
);
619 thcanceled
= (thread
->detached
& _PTHREAD_WASCANCEL
);
620 _PTHREAD_UNLOCK(thread
->lock
);
622 if (thcanceled
== 0) {
627 mutex
= (pthread_mutex_t
*)cond
->busy
;
629 // add unlock ref to show one less waiter
630 _pthread_cond_updateval(cond
, thread
->cancel_error
, 0);
633 ** Can't do anything if this fails -- we're on the way out
636 (void)pthread_mutex_lock(mutex
);
640 #define ECVCERORR 256
641 #define ECVPERORR 512
644 _pthread_cond_updateval(_pthread_cond
*cond
, int error
, uint32_t updateval
)
648 uint32_t diffgen
, nsval
;
649 uint64_t oldval64
, newval64
;
650 uint32_t lcntval
, ucntval
, scntval
;
651 volatile uint64_t *c_lsseqaddr
;
652 volatile uint32_t *c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
655 updateval
= PTHRW_INC
;
656 if ((error
& ECVCERORR
) != 0) {
657 updateval
|= PTH_RWS_CV_CBIT
;
659 if ((error
& ECVPERORR
) != 0) {
660 updateval
|= PTH_RWS_CV_PBIT
;
664 COND_GETSEQ_ADDR(cond
, &c_lsseqaddr
, &c_lseqcnt
, &c_useqcnt
, &c_sseqcnt
);
667 lcntval
= *c_lseqcnt
;
668 ucntval
= *c_useqcnt
;
669 scntval
= *c_sseqcnt
;
673 diffgen
= diff_genseq(lcntval
, scntval
); // pending waiters
675 oldval64
= (((uint64_t)scntval
) << 32);
679 /* TBD: Assert, should not be the case */
680 /* validate it is spurious and return */
685 // update scntval with number of expected returns and bits
686 nsval
= (scntval
& PTHRW_COUNT_MASK
) + (updateval
& PTHRW_COUNT_MASK
);
688 nsval
|= ((scntval
& PTH_RWS_CV_BITSALL
) | (updateval
& PTH_RWS_CV_BITSALL
));
690 // if L==S and c&p bits are set, needs clearpre
691 if (((nsval
& PTHRW_COUNT_MASK
) == (lcntval
& PTHRW_COUNT_MASK
)) &&
692 ((nsval
& PTH_RWS_CV_BITSALL
) == PTH_RWS_CV_BITSALL
)) {
693 // reset p bit but retain c bit on the sword
694 nsval
&= PTH_RWS_CV_RESET_PBIT
;
698 newval64
= (((uint64_t)nsval
) << 32);
701 } while (!os_atomic_cmpxchg(c_lsseqaddr
, oldval64
, newval64
, seq_cst
));
704 // if L == S, then reset associated mutex
705 if ((nsval
& PTHRW_COUNT_MASK
) == (lcntval
& PTHRW_COUNT_MASK
)) {
709 if (needclearpre
!= 0) {
711 if (cond
->pshared
== PTHREAD_PROCESS_SHARED
) {
712 flags
|= _PTHREAD_MTX_OPT_PSHARED
;
714 (void)__psynch_cvclrprepost(cond
, lcntval
, ucntval
, nsval
, 0, lcntval
, flags
);
719 #endif /* !BUILDING_VARIANT ] */
721 PTHREAD_NOEXPORT_VARIANT
723 pthread_cond_init(pthread_cond_t
*ocond
, const pthread_condattr_t
*attr
)
729 #else /* __DARWIN_UNIX03 */
731 #endif /* __DARWIN_UNIX03 */
733 _pthread_cond
*cond
= (_pthread_cond
*)ocond
;
734 _PTHREAD_LOCK_INIT(cond
->lock
);
735 return _pthread_cond_init(cond
, attr
, conforming
);