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
51 #include "pthread_internals.h"
52 #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 __semwait_signal(int, int, int, int, int64_t, int32_t);
62 extern int _pthread_cond_init(pthread_cond_t
*, const pthread_condattr_t
*, int);
63 extern int __unix_conforming
;
64 extern int usenew_mtximpl
;
67 /* 5243343 - temporary hack to detect if we are running the conformance test */
68 extern int PR_5243343_flag
;
69 #endif /* PR_5243343 */
71 __private_extern__
int _pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
, int isRelative
, int isconforming
);
72 #ifndef BUILDING_VARIANT
73 static void cond_cleanup(void *arg
);
74 static void cond_dropwait(npthread_cond_t
* cond
, int error
, uint32_t updateval
);
75 static void __pthread_cond_set_signature(npthread_cond_t
* cond
);
76 static int _pthread_cond_destroy_locked(pthread_cond_t
*cond
);
80 #define COND_GETSEQ_ADDR(cond, c_lseqcnt, c_useqcnt, c_sseqcnt) \
82 if (cond->misalign != 0) { \
83 c_lseqcnt = &cond->c_seq[1]; \
84 c_sseqcnt = &cond->c_seq[2]; \
85 c_useqcnt = &cond->c_seq[0]; \
88 c_lseqcnt = &cond->c_seq[0]; \
89 c_sseqcnt = &cond->c_seq[1]; \
90 c_useqcnt = &cond->c_seq[2]; \
94 #define COND_GETSEQ_ADDR(cond, c_lseqcnt, c_useqcnt, c_sseqcnt) \
96 if (cond->misalign != 0) { \
97 c_lseqcnt = &cond->c_seq[1]; \
98 c_sseqcnt = &cond->c_seq[2]; \
99 c_useqcnt = &cond->c_seq[0]; \
102 c_lseqcnt = &cond->c_seq[0]; \
103 c_sseqcnt = &cond->c_seq[1]; \
104 c_useqcnt = &cond->c_seq[2]; \
107 #endif /* __LP64__ */
110 #define _KSYN_TRACE_ 0
113 /* The Function qualifiers */
114 #define DBG_FUNC_START 1
115 #define DBG_FUNC_END 2
116 #define DBG_FUNC_NONE 0
118 int __kdebug_trace(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
120 #define _KSYN_TRACE_UM_LOCK 0x9000060
121 #define _KSYN_TRACE_UM_UNLOCK 0x9000064
122 #define _KSYN_TRACE_UM_MHOLD 0x9000068
123 #define _KSYN_TRACE_UM_MDROP 0x900006c
124 #define _KSYN_TRACE_UM_CVWAIT 0x9000070
125 #define _KSYN_TRACE_UM_CVSIG 0x9000074
126 #define _KSYN_TRACE_UM_CVBRD 0x9000078
127 #define _KSYN_TRACE_UM_CDROPWT 0x90000a0
128 #define _KSYN_TRACE_UM_CVCLRPRE 0x90000a4
130 #endif /* _KSYN_TRACE_ */
133 #ifndef BUILDING_VARIANT /* [ */
136 pthread_condattr_init(pthread_condattr_t
*attr
)
138 attr
->sig
= _PTHREAD_COND_ATTR_SIG
;
139 attr
->pshared
= _PTHREAD_DEFAULT_PSHARED
;
144 pthread_condattr_destroy(pthread_condattr_t
*attr
)
146 attr
->sig
= _PTHREAD_NO_SIG
; /* Uninitialized */
151 pthread_condattr_getpshared(const pthread_condattr_t
*attr
,
154 if (attr
->sig
== _PTHREAD_COND_ATTR_SIG
)
156 *pshared
= (int)attr
->pshared
;
160 return (EINVAL
); /* Not an initialized 'attribute' structure */
167 /* temp home till pshared is fixed correctly */
169 pthread_condattr_setpshared(pthread_condattr_t
* attr
, int pshared
)
172 if (attr
->sig
== _PTHREAD_COND_ATTR_SIG
)
175 if (( pshared
== PTHREAD_PROCESS_PRIVATE
) || (pshared
== PTHREAD_PROCESS_SHARED
))
176 #else /* __DARWIN_UNIX03 */
177 if ( pshared
== PTHREAD_PROCESS_PRIVATE
)
178 #endif /* __DARWIN_UNIX03 */
180 attr
->pshared
= pshared
;
183 return (EINVAL
); /* Invalid parameter */
187 return (EINVAL
); /* Not an initialized 'attribute' structure */
192 __private_extern__
int
193 _pthread_cond_init(pthread_cond_t
*ocond
,
194 const pthread_condattr_t
*attr
,
197 npthread_cond_t
* cond
= (npthread_cond_t
*)ocond
;
199 cond
->busy
= (npthread_mutex_t
*)NULL
;
205 if (((uintptr_t)cond
& 0x07) != 0) {
207 cond
->c_seq
[2] = PTH_RWS_CV_CBIT
;
210 cond
->c_seq
[1] = PTH_RWS_CV_CBIT
; /* set Sword to 0c */
214 cond
->pshared
= attr
->pshared
;
216 cond
->pshared
= _PTHREAD_DEFAULT_PSHARED
;
218 cond
->pshared
= _PTHREAD_DEFAULT_PSHARED
;
220 * For the new style mutex, interlocks are not held all the time.
221 * We needed the signature to be set in the end. And we need
222 * to protect against the code getting reorganized by compiler.
223 * cond->sig = _PTHREAD_COND_SIG;
225 __pthread_cond_set_signature(cond
);
230 pthread_cond_destroy(pthread_cond_t
* ocond
)
232 npthread_cond_t
*cond
= (npthread_cond_t
*)ocond
;
235 /* to provide backwards compat for apps using united condtn vars */
236 if((cond
->sig
!= _PTHREAD_COND_SIG
) && (cond
->sig
!= _PTHREAD_COND_SIG_init
))
240 ret
= _pthread_cond_destroy_locked(ocond
);
247 _pthread_cond_destroy_locked(pthread_cond_t
* ocond
)
249 npthread_cond_t
*cond
= (npthread_cond_t
*)ocond
;
251 volatile uint32_t * c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
252 uint32_t lcntval
, ucntval
, scntval
;
253 uint64_t oldval64
, newval64
;
256 if (cond
->sig
== _PTHREAD_COND_SIG
)
258 COND_GETSEQ_ADDR(cond
, c_lseqcnt
, c_useqcnt
, c_sseqcnt
);
259 lcntval
= *c_lseqcnt
;
260 ucntval
= *c_useqcnt
;
261 scntval
= *c_sseqcnt
;
263 if ((lcntval
& PTHRW_COUNT_MASK
) == (scntval
& PTHRW_COUNT_MASK
)) {
264 /* validate it is not busy */
265 oldval64
= (((uint64_t)scntval
) << 32);
269 if (OSAtomicCompareAndSwap64(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
)
271 cond
->sig
= _PTHREAD_NO_SIG
;
275 } else if (cond
->sig
== _PTHREAD_COND_SIG_init
) {
276 cond
->sig
= _PTHREAD_NO_SIG
;
279 ret
= EINVAL
; /* Not an initialized condition variable structure */
284 * Signal a condition variable, waking up all threads waiting for it.
287 pthread_cond_broadcast(pthread_cond_t
*ocond
)
289 npthread_cond_t
* cond
= (npthread_cond_t
*)ocond
;
291 uint32_t flags
, updateval
;
292 uint32_t lcntval
, ucntval
, scntval
;
293 uint64_t oldval64
, newval64
, mugen
, cvlsgen
, cvudgen
, mtid
=0;
294 int diffgen
, error
= 0;
295 volatile uint32_t * c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
296 uint32_t * pmtx
= NULL
;
297 uint32_t nlval
, ulval
;
298 int needclearpre
= 0, retry_count
= 0;
300 /* to provide backwards compat for apps using united condtn vars */
301 if((sig
!= _PTHREAD_COND_SIG
) && (sig
!= _PTHREAD_COND_SIG_init
))
304 if (sig
!= _PTHREAD_COND_SIG
)
307 if (cond
->sig
== _PTHREAD_COND_SIG_init
)
309 _pthread_cond_init(ocond
, NULL
, 0);
310 /* just inited nothing to post */
313 } else if (cond
->sig
!= _PTHREAD_COND_SIG
) {
314 /* Not a condition variable */
322 (void)__kdebug_trace(_KSYN_TRACE_UM_CVBRD
| DBG_FUNC_START
, (uint32_t)cond
, 0, 0, 0, 0);
325 COND_GETSEQ_ADDR(cond
, c_lseqcnt
, c_useqcnt
, c_sseqcnt
);
327 lcntval
= *c_lseqcnt
;
328 ucntval
= *c_useqcnt
;
329 scntval
= *c_sseqcnt
;
332 (void)__kdebug_trace(_KSYN_TRACE_UM_CVBRD
| DBG_FUNC_NONE
, (uint32_t)cond
, lcntval
, ucntval
, scntval
, 0);
335 if (((lcntval
& PTHRW_COUNT_MASK
) == (scntval
& PTHRW_COUNT_MASK
)) ||
336 ((lcntval
& PTHRW_COUNT_MASK
) == (ucntval
& PTHRW_COUNT_MASK
))) {
337 /* validate it is spurious and return */
338 oldval64
= (((uint64_t)scntval
) << 32);
342 if (OSAtomicCompareAndSwap64(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
)
345 (void)__kdebug_trace(_KSYN_TRACE_UM_CVBRD
| DBG_FUNC_NONE
, (uint32_t)cond
, lcntval
, ucntval
, 0xf1f1f1f1, 0);
346 (void)__kdebug_trace(_KSYN_TRACE_UM_CVBRD
| DBG_FUNC_END
, (uint32_t)cond
, scntval
, 0, 0xf1f1f1f1, 0);
351 if (is_seqhigher((ucntval
& PTHRW_COUNT_MASK
), (lcntval
& PTHRW_COUNT_MASK
)) || is_seqhigher((scntval
& PTHRW_COUNT_MASK
), (lcntval
& PTHRW_COUNT_MASK
))) {
352 /* since ucntval may be newer, just redo */
354 if (retry_count
> 8192) {
362 if (is_seqlower(ucntval
& PTHRW_COUNT_MASK
, scntval
& PTHRW_COUNT_MASK
) != 0) {
363 /* If U < S, set U = S+diff due to intr's TO, etc */
364 ulval
= (scntval
& PTHRW_COUNT_MASK
);
366 /* If U >= S, set U = U+diff due to intr's TO, etc */
367 ulval
= (ucntval
& PTHRW_COUNT_MASK
);
370 (void)__kdebug_trace(_KSYN_TRACE_UM_CVBRD
| DBG_FUNC_NONE
, lcntval
, ucntval
, scntval
, diffgen
, 0);
373 diffgen
= diff_genseq((lcntval
& PTHRW_COUNT_MASK
), (ulval
& PTHRW_COUNT_MASK
));
376 ulval
= (lcntval
& PTHRW_COUNT_MASK
);
377 if (OSAtomicCompareAndSwap32(ucntval
, ulval
, (volatile int32_t *)c_useqcnt
) != TRUE
) {
382 if (cond
->pshared
== PTHREAD_PROCESS_SHARED
)
383 flags
|= _PTHREAD_MTX_OPT_PSHARED
;
387 (void)__kdebug_trace(_KSYN_TRACE_UM_CVBRD
| DBG_FUNC_NONE
, (uint32_t)cond
, 3, diffgen
, flags
, 0);
391 /* pass old u val so kernel will know the diffgen */
393 cvlsgen
= ((uint64_t)scntval
<< 32) | nlval
;
394 cvudgen
= ((uint64_t)ucntval
<< 32) | diffgen
;
396 updateval
= __psynch_cvbroad(ocond
, cvlsgen
, cvudgen
, flags
, (pthread_mutex_t
*)pmtx
, mugen
, mtid
);
398 if (updateval
!= (uint32_t)-1) {
400 /* if kernel granted woke some threads, updatwe S for them as they will not access cv on their way out */
401 /* Were any threads woken or bits to be set? */
402 if (updateval
!= 0) {
405 lcntval
= *c_lseqcnt
;
406 ucntval
= *c_useqcnt
;
407 scntval
= *c_sseqcnt
;
408 /* update scntval with number of expected returns and bits */
409 nlval
= (scntval
& PTHRW_COUNT_MASK
) + (updateval
& PTHRW_COUNT_MASK
);
411 nlval
|= ((scntval
& PTH_RWS_CV_BITSALL
) | (updateval
& PTH_RWS_CV_BITSALL
));
414 (void)__kdebug_trace(_KSYN_TRACE_UM_CVBRD
| DBG_FUNC_NONE
, 0x25, lcntval
, scntval
, updateval
, 0);
416 /* if L==S and c&p bits are set, needs clearpre */
417 if (((nlval
& PTHRW_COUNT_MASK
) == (lcntval
& PTHRW_COUNT_MASK
))
418 && ((nlval
& PTH_RWS_CV_BITSALL
) == PTH_RWS_CV_BITSALL
)) {
419 /* reset p bit but retain c bit on the sword */
420 nlval
&= PTH_RWS_CV_RESET_PBIT
;
424 oldval64
= (((uint64_t)scntval
) << 32);
426 newval64
= (((uint64_t)nlval
) << 32);
430 (void)__kdebug_trace(_KSYN_TRACE_UM_CVBRD
| DBG_FUNC_NONE
, 0x25, nlval
, scntval
, updateval
, 0);
433 if (OSAtomicCompareAndSwap64(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
)
436 /* if L == S, then reset associated mutex */
437 if ((nlval
& PTHRW_COUNT_MASK
) == (lcntval
& PTHRW_COUNT_MASK
)) {
438 cond
->busy
= (npthread_mutex_t
*)NULL
;
441 if (needclearpre
!= 0) {
442 (void)__psynch_cvclrprepost(ocond
, lcntval
, ucntval
, nlval
, 0, lcntval
, flags
);
450 (void)__kdebug_trace(_KSYN_TRACE_UM_CVBRD
| DBG_FUNC_END
, (uint32_t)cond
, 0, error
, 0, 0);
457 * Signal a condition variable, waking a specified thread.
461 pthread_cond_signal_thread_np(pthread_cond_t
*ocond
, pthread_t thread
)
463 npthread_cond_t
* cond
= (npthread_cond_t
*)ocond
;
465 uint32_t flags
, updateval
;
466 uint32_t lcntval
, ucntval
, scntval
;
467 uint32_t nlval
, ulval
=0;
468 volatile uint32_t * c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
469 uint64_t oldval64
, newval64
, mugen
, cvlsgen
, mtid
= 0;
470 int needclearpre
= 0, retry_count
= 0;
473 /* to provide backwards compat for apps using united condtn vars */
475 if((sig
!= _PTHREAD_COND_SIG
) && (sig
!= _PTHREAD_COND_SIG_init
))
478 if (cond
->sig
!= _PTHREAD_COND_SIG
) {
480 if (cond
->sig
!= _PTHREAD_COND_SIG
) {
481 if (cond
->sig
== _PTHREAD_COND_SIG_init
) {
482 _pthread_cond_init(ocond
, NULL
, 0);
483 /* just inited, nothing to post yet */
495 (void)__kdebug_trace(_KSYN_TRACE_UM_CVSIG
| DBG_FUNC_START
, (uint32_t)cond
, 0, 0, 0, 0);
497 COND_GETSEQ_ADDR(cond
, c_lseqcnt
, c_useqcnt
, c_sseqcnt
);
499 lcntval
= *c_lseqcnt
;
500 ucntval
= *c_useqcnt
;
501 scntval
= *c_sseqcnt
;
503 (void)__kdebug_trace(_KSYN_TRACE_UM_CVSIG
| DBG_FUNC_NONE
, (uint32_t)cond
, lcntval
, ucntval
, scntval
, 0);
506 if (((lcntval
& PTHRW_COUNT_MASK
) == (scntval
& PTHRW_COUNT_MASK
)) ||
507 ((thread
== 0) && ((lcntval
& PTHRW_COUNT_MASK
) == (ucntval
& PTHRW_COUNT_MASK
)))) {
508 /* If L <= S+U, it is spurious broadcasr */
509 /* validate it is spurious and return */
510 oldval64
= (((uint64_t)scntval
) << 32);
514 if (OSAtomicCompareAndSwap64(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
)
517 (void)__kdebug_trace(_KSYN_TRACE_UM_CVSIG
| DBG_FUNC_NONE
, (uint32_t)cond
, lcntval
, ucntval
, 0xf1f1f1f1, 0);
518 (void)__kdebug_trace(_KSYN_TRACE_UM_CVSIG
| DBG_FUNC_END
, (uint32_t)cond
, scntval
, 0, 0xf1f1f1f1, 0);
523 if (((thread
== 0) && (is_seqhigher((ucntval
& PTHRW_COUNT_MASK
), (lcntval
& PTHRW_COUNT_MASK
)))) || is_seqhigher((scntval
& PTHRW_COUNT_MASK
), (lcntval
& PTHRW_COUNT_MASK
))) {
524 /* since ucntval may be newer, just redo */
526 if (retry_count
> 8192) {
536 * skip manipulating U count as ESRCH from kernel cannot be handled properly.
537 * S count will cover the imbalance and next signal without thread or broadcast
538 * will correct it. But we need to send the right U to kernel so it will use
539 * that to look for the appropriate sequenc. So the ulval is computed anyway.
542 if (is_seqlower(ucntval
& PTHRW_COUNT_MASK
, scntval
& PTHRW_COUNT_MASK
) != 0) {
543 /* If U < S, set U = S+1 due to intr's TO, etc */
544 ulval
= (scntval
& PTHRW_COUNT_MASK
) + PTHRW_INC
;
546 /* If U >= S, set U = U+1 due to intr's TO, etc */
547 ulval
= (ucntval
& PTHRW_COUNT_MASK
) + PTHRW_INC
;
550 if (OSAtomicCompareAndSwap32(ucntval
, ulval
, (volatile int32_t *)c_useqcnt
) != TRUE
) {
556 if (cond
->pshared
== PTHREAD_PROCESS_SHARED
)
557 flags
|= _PTHREAD_MTX_OPT_PSHARED
;
560 (void)__kdebug_trace(_KSYN_TRACE_UM_CVSIG
| DBG_FUNC_NONE
, (uint32_t)cond
, 3, nlval
, ulval
, 0);
563 /* pass old u val so kernel will know the diffgen */
565 cvlsgen
= ((uint64_t)scntval
<< 32) | nlval
;
567 updateval
= __psynch_cvsignal(ocond
, cvlsgen
, ucntval
, pthread_mach_thread_np(thread
), (pthread_mutex_t
*)0, mugen
, mtid
, flags
);
570 if (updateval
!= (uint32_t)-1) {
572 /* if kernel granted woke some threads, updatwe S for them as they will not access cv on their way out */
573 /* Were any threads woken or bits to be set? */
574 if (updateval
!= 0) {
576 lcntval
= *c_lseqcnt
;
577 ucntval
= *c_useqcnt
;
578 scntval
= *c_sseqcnt
;
579 /* update scntval with number of expected returns and bits */
580 nlval
= (scntval
& PTHRW_COUNT_MASK
) + (updateval
& PTHRW_COUNT_MASK
);
582 nlval
|= ((scntval
& PTH_RWS_CV_BITSALL
) | (updateval
& PTH_RWS_CV_BITSALL
));
585 (void)__kdebug_trace(_KSYN_TRACE_UM_CVSIG
| DBG_FUNC_NONE
, 0x25, 0, 0, updateval
, 0);
587 /* if L==S and c&p bits are set, needs clearpre */
588 if (((nlval
& PTHRW_COUNT_MASK
) == (lcntval
& PTHRW_COUNT_MASK
))
589 && ((nlval
& PTH_RWS_CV_BITSALL
) == PTH_RWS_CV_BITSALL
)) {
590 /* reset p bit but retain c bit on the sword */
591 nlval
&= PTH_RWS_CV_RESET_PBIT
;
596 oldval64
= (((uint64_t)scntval
) << 32);
598 newval64
= (((uint64_t)nlval
) << 32);
602 (void)__kdebug_trace(_KSYN_TRACE_UM_CVSIG
| DBG_FUNC_NONE
, 0x25, nlval
, ulval
, updateval
, 0);
605 if (OSAtomicCompareAndSwap64(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
)
608 /* if L == S, then reset associated mutex */
609 if ((nlval
& PTHRW_COUNT_MASK
) == (lcntval
& PTHRW_COUNT_MASK
)) {
610 cond
->busy
= (npthread_mutex_t
*)NULL
;
613 if (needclearpre
!= 0) {
614 (void)__psynch_cvclrprepost(ocond
, lcntval
, ucntval
, nlval
, 0, lcntval
, flags
);
622 (void)__kdebug_trace(_KSYN_TRACE_UM_CVSIG
| DBG_FUNC_END
, (uint32_t)cond
, 0, 0, 0, 0);
628 * Signal a condition variable, waking only one thread.
631 pthread_cond_signal(pthread_cond_t
*cond
)
633 return pthread_cond_signal_thread_np(cond
, NULL
);
637 * Manage a list of condition variables associated with a mutex
642 * Suspend waiting for a condition variable.
643 * Note: we have to keep a list of condition variables which are using
644 * this same mutex variable so we can detect invalid 'destroy' sequences.
645 * If isconforming < 0, we skip the _pthread_testcancel(), but keep the
646 * remaining conforming behavior..
648 __private_extern__
int
649 _pthread_cond_wait(pthread_cond_t
*ocond
,
650 pthread_mutex_t
*omutex
,
651 const struct timespec
*abstime
,
656 npthread_cond_t
* cond
= (npthread_cond_t
*)ocond
;
657 npthread_mutex_t
* mutex
= (npthread_mutex_t
* )omutex
;
658 mach_timespec_t then
= {0,0};
659 struct timespec cthen
= {0,0};
661 int msig
= mutex
->sig
;
662 npthread_mutex_t
* pmtx
;
663 uint32_t mtxgen
, mtxugen
, flags
=0, updateval
;
664 uint32_t lcntval
, ucntval
, scntval
;
665 uint32_t nlval
, ulval
, savebits
;
666 volatile uint32_t * c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
667 uint64_t oldval64
, newval64
, mugen
, cvlsgen
;
668 uint32_t * npmtx
= NULL
;
669 int error
, local_error
;
671 extern void _pthread_testcancel(pthread_t thread
, int isconforming
);
673 /* to provide backwards compat for apps using united condtn vars */
674 if((sig
!= _PTHREAD_COND_SIG
) && (sig
!= _PTHREAD_COND_SIG_init
))
678 if((msig
!= _PTHREAD_MUTEX_SIG
) && ((msig
& _PTHREAD_MUTEX_SIG_init_MASK
) != _PTHREAD_MUTEX_SIG_CMP
))
680 if (isconforming
> 0)
681 _pthread_testcancel(pthread_self(), 1);
684 if (cond
->sig
!= _PTHREAD_COND_SIG
)
687 if (cond
->sig
!= _PTHREAD_COND_SIG
) {
688 if (cond
->sig
== _PTHREAD_COND_SIG_init
) {
689 _pthread_cond_init(ocond
, NULL
, 0);
699 (void)__kdebug_trace(_KSYN_TRACE_UM_CVWAIT
| DBG_FUNC_START
, (uint32_t)cond
, isRelative
, 0, (uint32_t)abstime
, 0);
701 COND_GETSEQ_ADDR(cond
, c_lseqcnt
, c_useqcnt
, c_sseqcnt
);
703 /* send relative time to kernel */
706 (void)__kdebug_trace(_KSYN_TRACE_UM_CVWAIT
| DBG_FUNC_START
, 0x11111111, abstime
->tv_nsec
, abstime
->tv_sec
, 0, 0);
708 if (isRelative
== 0) {
711 gettimeofday(&tv
, NULL
);
712 TIMEVAL_TO_TIMESPEC(&tv
, &now
);
714 /* Compute relative time to sleep */
715 then
.tv_nsec
= abstime
->tv_nsec
- now
.tv_nsec
;
716 then
.tv_sec
= abstime
->tv_sec
- now
.tv_sec
;
717 if (then
.tv_nsec
< 0)
719 then
.tv_nsec
+= NSEC_PER_SEC
;
722 if (((int)then
.tv_sec
< 0) ||
723 ((then
.tv_sec
== 0) && (then
.tv_nsec
== 0)))
727 if (isconforming
!= 0) {
728 cthen
.tv_sec
= abstime
->tv_sec
;
729 cthen
.tv_nsec
= abstime
->tv_nsec
;
730 if ((cthen
.tv_sec
< 0) || (cthen
.tv_nsec
< 0)) {
733 if (cthen
.tv_nsec
>= NSEC_PER_SEC
) {
738 then
.tv_sec
= abstime
->tv_sec
;
739 then
.tv_nsec
= abstime
->tv_nsec
;
740 if ((then
.tv_sec
== 0) && (then
.tv_nsec
== 0)) {
744 if(isconforming
&& ((then
.tv_sec
< 0) || (then
.tv_nsec
< 0))) {
747 if (then
.tv_nsec
>= NSEC_PER_SEC
) {
752 if ((cond
->busy
!= (npthread_mutex_t
*)NULL
) && (cond
->busy
!= mutex
))
757 lcntval
= *c_lseqcnt
;
758 ucntval
= *c_useqcnt
;
759 scntval
= *c_sseqcnt
;
761 oldval64
= (((uint64_t)scntval
) << 32);
764 /* remove c and p bits on S word */
765 savebits
= scntval
& PTH_RWS_CV_BITSALL
;
766 ulval
= (scntval
& PTHRW_COUNT_MASK
);
767 nlval
= lcntval
+ PTHRW_INC
;
768 newval64
= (((uint64_t)ulval
) << 32);
771 if (OSAtomicCompareAndSwap64(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
)
777 (void)__kdebug_trace(_KSYN_TRACE_UM_CVWAIT
| DBG_FUNC_NONE
, (uint32_t)cond
, lcntval
, ucntval
, scntval
, 0);
779 retval
= __mtx_droplock(pmtx
, PTHRW_INC
, &flags
, &npmtx
, &mtxgen
, &mtxugen
);
781 /* TBD: cases are for normal (non owner for recursive mutex; error checking)*/
784 if ((flags
& _PTHREAD_MTX_OPT_NOTIFY
) == 0) {
788 mugen
= ((uint64_t)mtxugen
<< 32) | mtxgen
;
789 flags
&= ~_PTHREAD_MTX_OPT_MUTEX
; /* reset the mutex bit as this is cvar */
792 (void)__kdebug_trace(_KSYN_TRACE_UM_CVWAIT
| DBG_FUNC_NONE
, (uint32_t)cond
, 3, (uint32_t)mutex
, flags
, 0);
796 cvlsgen
= ((uint64_t)(ulval
| savebits
)<< 32) | nlval
;
799 pthread_cleanup_push(cond_cleanup
, (void *)cond
);
800 updateval
= __psynch_cvwait(ocond
, cvlsgen
, ucntval
, (pthread_mutex_t
*)npmtx
, mugen
, flags
, (int64_t)then
.tv_sec
, (int32_t)then
.tv_nsec
);
801 pthread_cleanup_pop(0);
803 updateval
= __psynch_cvwait(ocond
, cvlsgen
, ucntval
, (pthread_mutex_t
*)npmtx
, mugen
, flags
, (int64_t)then
.tv_sec
, (int32_t)then
.tv_nsec
);
809 if (updateval
== (uint32_t)-1) {
811 error
= local_error
& 0xff;
812 if (error
== ETIMEDOUT
) {
814 } else if (error
== EINTR
) {
816 ** EINTR can be treated as a spurious wakeup unless we were canceled.
822 // (void)__kdebug_trace(0x9000070 | 0, (uint32_t)cond, 0xf1f1f2f2, local_error, error, 0);
825 /* add unlock ref to show one less waiter */
826 cond_dropwait(cond
, local_error
, 0);
829 // (void)__kdebug_trace(0x9000070 | 0, (uint32_t)cond, 0xf3f3f4f4, updateval, 0, 0);
832 if (updateval
!= 0) {
833 /* the return due to prepost and might have bit states */
834 /* update S and return for prepo if needed */
835 cond_dropwait(cond
, 0, updateval
);
840 (void)__kdebug_trace(_KSYN_TRACE_UM_CVWAIT
| DBG_FUNC_NONE
, (uint32_t)cond
, 4, retval
, 0, 0);
842 pthread_mutex_lock(omutex
);
845 (void)__kdebug_trace(_KSYN_TRACE_UM_CVWAIT
| DBG_FUNC_END
, (uint32_t)cond
, 0, 0, retval
, 0);
851 * For the new style mutex, interlocks are not held all the time.
852 * We needed the signature to be set in the end. And we need
853 * to protect against the code getting reorganized by compiler.
856 __pthread_cond_set_signature(npthread_cond_t
* cond
)
858 cond
->sig
= _PTHREAD_COND_SIG
;
863 cond_cleanup(void *arg
)
865 npthread_cond_t
*cond
= (npthread_cond_t
*)arg
;
866 pthread_mutex_t
*mutex
;
869 pthread_t thread
= pthread_self();
873 thcanceled
= (thread
->detached
& _PTHREAD_WASCANCEL
);
874 UNLOCK(thread
->lock
);
880 mutex
= (pthread_mutex_t
*) cond
->busy
;
882 /* add unlock ref to show one less waiter */
883 cond_dropwait(cond
, thread
->cancel_error
, 0);
886 ** Can't do anything if this fails -- we're on the way out
889 (void)pthread_mutex_lock(mutex
);
892 #define ECVCERORR 256
893 #define ECVPERORR 512
896 cond_dropwait(npthread_cond_t
* cond
, int error
, uint32_t updateval
)
899 pthread_cond_t
* ocond
= (pthread_cond_t
*)cond
;
900 int needclearpre
= 0;
901 uint32_t diffgen
, nlval
, ulval
, flags
;
902 uint32_t lcntval
, ucntval
, scntval
, lval
;
903 volatile uint32_t * c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
904 uint64_t oldval64
, newval64
;
906 /* to provide backwards compat for apps using united condtn vars */
908 if (sig
!= _PTHREAD_COND_SIG
)
911 COND_GETSEQ_ADDR(cond
, c_lseqcnt
, c_useqcnt
, c_sseqcnt
);
915 if ((error
& ECVCERORR
) != 0)
916 lval
|= PTH_RWS_CV_CBIT
;
917 if ((error
& ECVPERORR
) != 0)
918 lval
|= PTH_RWS_CV_PBIT
;
923 (void)__kdebug_trace(_KSYN_TRACE_UM_CDROPWT
| DBG_FUNC_START
, (uint32_t)cond
, error
, updateval
, 0xee, 0);
926 lcntval
= *c_lseqcnt
;
927 ucntval
= *c_useqcnt
;
928 scntval
= *c_sseqcnt
;
930 diffgen
= diff_genseq((lcntval
& PTHRW_COUNT_MASK
), (scntval
& PTHRW_COUNT_MASK
)); /* pendig waiters */
932 (void)__kdebug_trace(_KSYN_TRACE_UM_CDROPWT
| DBG_FUNC_NONE
, (uint32_t)cond
, lcntval
, scntval
, diffgen
, 0);
935 /* TBD: Assert, should not be the case */
936 /* validate it is spurious and return */
937 oldval64
= (((uint64_t)scntval
) << 32);
940 if (OSAtomicCompareAndSwap64(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
)
943 (void)__kdebug_trace(_KSYN_TRACE_UM_CDROPWT
| DBG_FUNC_END
, (uint32_t)cond
, 0, 0, 0, 0);
948 /* update S by one */
949 oldval64
= (((uint64_t)scntval
) << 32);
952 /* update scntval with number of expected returns and bits */
953 ulval
= (scntval
& PTHRW_COUNT_MASK
) + (lval
& PTHRW_COUNT_MASK
);
955 ulval
|= ((scntval
& PTH_RWS_CV_BITSALL
) | (lval
& PTH_RWS_CV_BITSALL
));
961 /* If L==S, need to return to kernel */
962 if ((nlval
& PTHRW_COUNT_MASK
) == (ulval
& PTHRW_COUNT_MASK
)) {
963 if ((ulval
& PTH_RWS_CV_BITSALL
) == PTH_RWS_CV_BITSALL
) {
964 /* reset p bit but retain c bit on the sword */
966 ulval
&= PTH_RWS_CV_RESET_PBIT
;
970 newval64
= (((uint64_t)ulval
) << 32);
974 (void)__kdebug_trace(_KSYN_TRACE_UM_CDROPWT
| DBG_FUNC_NONE
, (uint32_t)cond
, 0xffff, nlval
, ulval
, 0);
976 if (OSAtomicCompareAndSwap64(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
)
980 (void)__kdebug_trace(_KSYN_TRACE_UM_CDROPWT
| DBG_FUNC_NONE
, (uint32_t)cond
, 2, 0, 0xee, 0);
982 if ((nlval
& PTHRW_COUNT_MASK
) == (ulval
& PTHRW_COUNT_MASK
)) {
983 /* last usage remove the mutex */
988 (void)__kdebug_trace(_KSYN_TRACE_UM_CDROPWT
| DBG_FUNC_NONE
, nlval
, ucntval
, ulval
, PTHRW_INC
, 0);
990 if (needclearpre
!= 0) {
992 if (cond
->pshared
== PTHREAD_PROCESS_SHARED
)
993 flags
|= _PTHREAD_MTX_OPT_PSHARED
;
995 (void)__psynch_cvclrprepost(ocond
, nlval
, ucntval
, ulval
, 0, nlval
, flags
);
998 (void)__kdebug_trace(_KSYN_TRACE_UM_CDROPWT
| DBG_FUNC_END
, nlval
, ucntval
, ulval
, PTHRW_INC
, 0);
1005 pthread_cond_timedwait_relative_np(pthread_cond_t
*cond
,
1006 pthread_mutex_t
*mutex
,
1007 const struct timespec
*abstime
)
1009 return (_pthread_cond_wait(cond
, mutex
, abstime
, 1, 0));
1014 #else /* !BUILDING_VARIANT */
1016 extern int _pthread_cond_wait(pthread_cond_t
*cond
,
1017 pthread_mutex_t
*mutex
,
1018 const struct timespec
*abstime
,
1022 #endif /* !BUILDING_VARIANT ] */
1024 * Initialize a condition variable. Note: 'attr' is ignored.
1028 * Initialize a condition variable. This is the public interface.
1029 * We can't trust the lock, so initialize it first before taking
1033 pthread_cond_init(pthread_cond_t
*cond
,
1034 const pthread_condattr_t
*attr
)
1040 #else /* __DARWIN_UNIX03 */
1042 #endif /* __DARWIN_UNIX03 */
1044 /* lock is same offset in both structures */
1045 LOCK_INIT(cond
->lock
);
1047 return (_pthread_cond_init(cond
, attr
, conforming
));
1052 pthread_cond_wait(pthread_cond_t *cond,
1053 pthread_mutex_t *mutex)
1056 pthread_cond_timedwait(pthread_cond_t *cond,
1057 pthread_mutex_t *mutex,
1058 const struct timespec *abstime)
1060 moved to pthread_cancelable.c */