]>
git.saurik.com Git - apple/libpthread.git/blob - src/pthread_cond.c
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
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 __private_extern__
int _pthread_cond_init(_pthread_cond
*, const pthread_condattr_t
*, int);
62 __private_extern__
int _pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
, int isRelative
, int isconforming
);
64 extern int __gettimeofday(struct timeval
*, struct timezone
*);
66 #ifndef BUILDING_VARIANT
67 static void _pthread_cond_cleanup(void *arg
);
68 static void _pthread_cond_updateval(_pthread_cond
* cond
, int error
, uint32_t updateval
);
72 COND_GETSEQ_ADDR(_pthread_cond
*cond
,
73 volatile uint32_t **c_lseqcnt
,
74 volatile uint32_t **c_useqcnt
,
75 volatile uint32_t **c_sseqcnt
)
78 *c_lseqcnt
= &cond
->c_seq
[1];
79 *c_sseqcnt
= &cond
->c_seq
[2];
80 *c_useqcnt
= &cond
->c_seq
[0];
82 *c_lseqcnt
= &cond
->c_seq
[0];
83 *c_sseqcnt
= &cond
->c_seq
[1];
84 *c_useqcnt
= &cond
->c_seq
[2];
88 #ifndef BUILDING_VARIANT /* [ */
91 pthread_condattr_init(pthread_condattr_t
*attr
)
93 attr
->sig
= _PTHREAD_COND_ATTR_SIG
;
94 attr
->pshared
= _PTHREAD_DEFAULT_PSHARED
;
99 pthread_condattr_destroy(pthread_condattr_t
*attr
)
101 attr
->sig
= _PTHREAD_NO_SIG
;
106 pthread_condattr_getpshared(const pthread_condattr_t
*attr
, int *pshared
)
109 if (attr
->sig
== _PTHREAD_COND_ATTR_SIG
) {
110 *pshared
= (int)attr
->pshared
;
117 pthread_condattr_setpshared(pthread_condattr_t
*attr
, int pshared
)
120 if (attr
->sig
== _PTHREAD_COND_ATTR_SIG
) {
122 if (pshared
== PTHREAD_PROCESS_PRIVATE
|| pshared
== PTHREAD_PROCESS_SHARED
)
123 #else /* __DARWIN_UNIX03 */
124 if (pshared
== PTHREAD_PROCESS_PRIVATE
)
125 #endif /* __DARWIN_UNIX03 */
127 attr
->pshared
= pshared
;
134 __private_extern__
int
135 _pthread_cond_init(_pthread_cond
*cond
, const pthread_condattr_t
*attr
, int conforming
)
137 volatile uint32_t *c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
145 cond
->misalign
= (((uintptr_t)&cond
->c_seq
[0]) & 0x7) != 0;
146 COND_GETSEQ_ADDR(cond
, &c_lseqcnt
, &c_useqcnt
, &c_sseqcnt
);
147 *c_sseqcnt
= PTH_RWS_CV_CBIT
; // set Sword to 0c
151 cond
->pshared
= attr
->pshared
;
153 cond
->pshared
= _PTHREAD_DEFAULT_PSHARED
;
156 cond
->pshared
= _PTHREAD_DEFAULT_PSHARED
;
159 // Ensure all contents are properly set before setting signature.
161 cond
->sig
= _PTHREAD_COND_SIG
;
168 _pthread_cond_check_init_slow(_pthread_cond
*cond
, bool *inited
)
171 if (cond
->sig
== _PTHREAD_COND_SIG_init
) {
172 _PTHREAD_LOCK(cond
->lock
);
173 if (cond
->sig
== _PTHREAD_COND_SIG_init
) {
174 res
= _pthread_cond_init(cond
, NULL
, 0);
178 } else if (cond
->sig
== _PTHREAD_COND_SIG
) {
181 _PTHREAD_UNLOCK(cond
->lock
);
182 } else if (cond
->sig
== _PTHREAD_COND_SIG
) {
189 _pthread_cond_check_init(_pthread_cond
*cond
, bool *inited
)
192 if (cond
->sig
!= _PTHREAD_COND_SIG
) {
193 return _pthread_cond_check_init_slow(cond
, inited
);
199 pthread_cond_destroy(pthread_cond_t
*ocond
)
201 _pthread_cond
*cond
= (_pthread_cond
*)ocond
;
203 if (cond
->sig
== _PTHREAD_COND_SIG
) {
204 _PTHREAD_LOCK(cond
->lock
);
206 uint64_t oldval64
, newval64
;
207 uint32_t lcntval
, ucntval
, scntval
;
208 volatile uint32_t *c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
210 COND_GETSEQ_ADDR(cond
, &c_lseqcnt
, &c_useqcnt
, &c_sseqcnt
);
213 lcntval
= *c_lseqcnt
;
214 ucntval
= *c_useqcnt
;
215 scntval
= *c_sseqcnt
;
217 // validate it is not busy
218 if ((lcntval
& PTHRW_COUNT_MASK
) != (scntval
& PTHRW_COUNT_MASK
)) {
222 oldval64
= (((uint64_t)scntval
) << 32);
225 } while (OSAtomicCompareAndSwap64Barrier(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
);
227 // <rdar://problem/13782056> Need to clear preposts.
229 bool needclearpre
= ((scntval
& PTH_RWS_CV_PBIT
) != 0);
230 if (needclearpre
&& cond
->pshared
== PTHREAD_PROCESS_SHARED
) {
231 flags
|= _PTHREAD_MTX_OPT_PSHARED
;
234 cond
->sig
= _PTHREAD_NO_SIG
;
237 _PTHREAD_UNLOCK(cond
->lock
);
240 (void)__psynch_cvclrprepost(cond
, lcntval
, ucntval
, scntval
, 0, lcntval
, flags
);
242 } else if (cond
->sig
== _PTHREAD_COND_SIG_init
) {
243 // Compatibility for misbehaving applications that attempt to
244 // destroy a statically initialized condition variable.
245 cond
->sig
= _PTHREAD_NO_SIG
;
252 _pthread_cond_signal(pthread_cond_t
*ocond
, bool broadcast
, mach_port_t thread
)
255 _pthread_cond
*cond
= (_pthread_cond
*)ocond
;
261 uint64_t oldval64
, newval64
;
262 uint32_t lcntval
, ucntval
, scntval
;
263 volatile uint32_t *c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
265 int retry_count
= 0, uretry_count
= 0;
269 res
= _pthread_cond_check_init(cond
, &inited
);
270 if (res
!= 0 || inited
== true) {
274 COND_GETSEQ_ADDR(cond
, &c_lseqcnt
, &c_useqcnt
, &c_sseqcnt
);
280 lcntval
= *c_lseqcnt
;
281 ucntval
= *c_useqcnt
;
282 scntval
= *c_sseqcnt
;
284 if (((lcntval
& PTHRW_COUNT_MASK
) == (scntval
& PTHRW_COUNT_MASK
)) ||
285 (thread
== MACH_PORT_NULL
&& ((lcntval
& PTHRW_COUNT_MASK
) == (ucntval
& PTHRW_COUNT_MASK
)))) {
286 /* validate it is spurious and return */
287 oldval64
= (((uint64_t)scntval
) << 32);
291 if (OSAtomicCompareAndSwap64Barrier(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
) {
303 /* validate to eliminate spurious values, race snapshots */
304 if (is_seqhigher((scntval
& PTHRW_COUNT_MASK
), (lcntval
& PTHRW_COUNT_MASK
))) {
305 /* since ucntval may be newer, just redo */
307 if (retry_count
> 8192) {
314 } else if (is_seqhigher((ucntval
& PTHRW_COUNT_MASK
), (lcntval
& PTHRW_COUNT_MASK
))) {
315 /* since ucntval may be newer, just redo */
317 if (uretry_count
> 8192) {
319 * U value if not used for a while can go out of sync
320 * set this to S value and try one more time.
322 if (ucountreset
!= 0) {
324 } else if (OSAtomicCompareAndSwap32Barrier(ucntval
, (scntval
& PTHRW_COUNT_MASK
), (volatile int32_t *)c_useqcnt
) == TRUE
) {
325 /* now the U is reset to S value */
335 if (is_seqlower(ucntval
& PTHRW_COUNT_MASK
, scntval
& PTHRW_COUNT_MASK
) != 0) {
336 /* If U < S, set U = S+diff due to intr's TO, etc */
337 ulval
= (scntval
& PTHRW_COUNT_MASK
);
339 /* If U >= S, set U = U+diff due to intr's TO, etc */
340 ulval
= (ucntval
& PTHRW_COUNT_MASK
);
344 diffgen
= diff_genseq(lcntval
, ulval
);
346 ulval
= (lcntval
& PTHRW_COUNT_MASK
);
351 } while (retry
|| OSAtomicCompareAndSwap32Barrier(ucntval
, ulval
, (volatile int32_t *)c_useqcnt
) != TRUE
);
354 if (cond
->pshared
== PTHREAD_PROCESS_SHARED
) {
355 flags
|= _PTHREAD_MTX_OPT_PSHARED
;
358 uint64_t cvlsgen
= ((uint64_t)scntval
<< 32) | lcntval
;
361 // pass old U val so kernel will know the diffgen
362 uint64_t cvudgen
= ((uint64_t)ucntval
<< 32) | diffgen
;
363 updateval
= __psynch_cvbroad(ocond
, cvlsgen
, cvudgen
, flags
, NULL
, 0, 0);
365 updateval
= __psynch_cvsignal(ocond
, cvlsgen
, ucntval
, thread
, NULL
, 0, 0, flags
);
368 if (updateval
!= (uint32_t)-1 && updateval
!= 0) {
369 _pthread_cond_updateval(cond
, 0, updateval
);
377 * Signal a condition variable, waking up all threads waiting for it.
380 pthread_cond_broadcast(pthread_cond_t
*ocond
)
382 return _pthread_cond_signal(ocond
, true, MACH_PORT_NULL
);
386 * Signal a condition variable, waking a specified thread.
389 pthread_cond_signal_thread_np(pthread_cond_t
*ocond
, pthread_t thread
)
391 mach_port_t mp
= MACH_PORT_NULL
;
393 mp
= pthread_mach_thread_np(thread
);
395 return _pthread_cond_signal(ocond
, false, mp
);
399 * Signal a condition variable, waking only one thread.
402 pthread_cond_signal(pthread_cond_t
*cond
)
404 return pthread_cond_signal_thread_np(cond
, NULL
);
408 * Manage a list of condition variables associated with a mutex
413 * Suspend waiting for a condition variable.
414 * Note: we have to keep a list of condition variables which are using
415 * this same mutex variable so we can detect invalid 'destroy' sequences.
416 * If isconforming < 0, we skip the _pthread_testcancel(), but keep the
417 * remaining conforming behavior..
419 __private_extern__
int
420 _pthread_cond_wait(pthread_cond_t
*ocond
,
421 pthread_mutex_t
*omutex
,
422 const struct timespec
*abstime
,
427 _pthread_cond
*cond
= (_pthread_cond
*)ocond
;
428 _pthread_mutex
*mutex
= (_pthread_mutex
*)omutex
;
429 struct timespec then
= { 0, 0 };
430 uint32_t mtxgen
, mtxugen
, flags
=0, updateval
;
431 uint32_t lcntval
, ucntval
, scntval
;
432 uint32_t nlval
, ulval
, savebits
;
433 volatile uint32_t *c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
434 uint64_t oldval64
, newval64
, mugen
, cvlsgen
;
435 uint32_t *npmtx
= NULL
;
437 extern void _pthread_testcancel(pthread_t thread
, int isconforming
);
439 res
= _pthread_cond_check_init(cond
, NULL
);
445 if (!_pthread_mutex_check_signature(mutex
) &&
446 !_pthread_mutex_check_signature_init(mutex
)) {
449 if (isconforming
> 0) {
450 _pthread_testcancel(pthread_self(), 1);
454 /* send relative time to kernel */
456 if (isRelative
== 0) {
459 __gettimeofday(&tv
, NULL
);
460 TIMEVAL_TO_TIMESPEC(&tv
, &now
);
462 /* Compute relative time to sleep */
463 then
.tv_nsec
= abstime
->tv_nsec
- now
.tv_nsec
;
464 then
.tv_sec
= abstime
->tv_sec
- now
.tv_sec
;
465 if (then
.tv_nsec
< 0) {
466 then
.tv_nsec
+= NSEC_PER_SEC
;
469 if (then
.tv_sec
< 0 || (then
.tv_sec
== 0 && then
.tv_nsec
== 0)) {
473 (abstime
->tv_sec
< 0 ||
474 abstime
->tv_nsec
< 0 ||
475 abstime
->tv_nsec
>= NSEC_PER_SEC
)) {
479 then
.tv_sec
= abstime
->tv_sec
;
480 then
.tv_nsec
= abstime
->tv_nsec
;
481 if ((then
.tv_sec
== 0) && (then
.tv_nsec
== 0)) {
485 if (isconforming
&& (then
.tv_sec
< 0 || then
.tv_nsec
< 0)) {
488 if (then
.tv_nsec
>= NSEC_PER_SEC
) {
493 if (cond
->busy
!= NULL
&& cond
->busy
!= mutex
) {
497 COND_GETSEQ_ADDR(cond
, &c_lseqcnt
, &c_useqcnt
, &c_sseqcnt
);
500 lcntval
= *c_lseqcnt
;
501 ucntval
= *c_useqcnt
;
502 scntval
= *c_sseqcnt
;
504 oldval64
= (((uint64_t)scntval
) << 32);
507 /* remove c and p bits on S word */
508 savebits
= scntval
& PTH_RWS_CV_BITSALL
;
509 ulval
= (scntval
& PTHRW_COUNT_MASK
);
510 nlval
= lcntval
+ PTHRW_INC
;
511 newval64
= (((uint64_t)ulval
) << 32);
513 } while (OSAtomicCompareAndSwap64Barrier(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
);
517 res
= __mtx_droplock(mutex
, &flags
, &npmtx
, &mtxgen
, &mtxugen
);
519 /* TBD: cases are for normal (non owner for recursive mutex; error checking)*/
523 if ((flags
& _PTHREAD_MTX_OPT_NOTIFY
) == 0) {
527 mugen
= ((uint64_t)mtxugen
<< 32) | mtxgen
;
529 flags
&= ~_PTHREAD_MTX_OPT_MUTEX
; /* reset the mutex bit as this is cvar */
531 cvlsgen
= ((uint64_t)(ulval
| savebits
)<< 32) | nlval
;
533 // SUSv3 requires pthread_cond_wait to be a cancellation point
535 pthread_cleanup_push(_pthread_cond_cleanup
, (void *)cond
);
536 updateval
= __psynch_cvwait(ocond
, cvlsgen
, ucntval
, (pthread_mutex_t
*)npmtx
, mugen
, flags
, (int64_t)then
.tv_sec
, (int32_t)then
.tv_nsec
);
537 _pthread_testcancel(pthread_self(), isconforming
);
538 pthread_cleanup_pop(0);
540 updateval
= __psynch_cvwait(ocond
, cvlsgen
, ucntval
, (pthread_mutex_t
*)npmtx
, mugen
, flags
, (int64_t)then
.tv_sec
, (int32_t)then
.tv_nsec
);
543 if (updateval
== (uint32_t)-1) {
545 switch (err
& 0xff) {
550 // spurious wakeup (unless canceled)
558 // add unlock ref to show one less waiter
559 _pthread_cond_updateval(cond
, err
, 0);
560 } else if (updateval
!= 0) {
562 // The return due to prepost and might have bit states
563 // update S and return for prepo if needed
564 _pthread_cond_updateval(cond
, 0, updateval
);
567 pthread_mutex_lock(omutex
);
573 _pthread_cond_cleanup(void *arg
)
575 _pthread_cond
*cond
= (_pthread_cond
*)arg
;
576 pthread_mutex_t
*mutex
;
579 pthread_t thread
= pthread_self();
582 _PTHREAD_LOCK(thread
->lock
);
583 thcanceled
= (thread
->detached
& _PTHREAD_WASCANCEL
);
584 _PTHREAD_UNLOCK(thread
->lock
);
586 if (thcanceled
== 0) {
591 mutex
= (pthread_mutex_t
*)cond
->busy
;
593 // add unlock ref to show one less waiter
594 _pthread_cond_updateval(cond
, thread
->cancel_error
, 0);
597 ** Can't do anything if this fails -- we're on the way out
600 (void)pthread_mutex_lock(mutex
);
604 #define ECVCERORR 256
605 #define ECVPERORR 512
608 _pthread_cond_updateval(_pthread_cond
*cond
, int error
, uint32_t updateval
)
612 uint32_t diffgen
, nsval
;
613 uint64_t oldval64
, newval64
;
614 uint32_t lcntval
, ucntval
, scntval
;
615 volatile uint32_t *c_lseqcnt
, *c_useqcnt
, *c_sseqcnt
;
618 updateval
= PTHRW_INC
;
619 if ((error
& ECVCERORR
) != 0) {
620 updateval
|= PTH_RWS_CV_CBIT
;
622 if ((error
& ECVPERORR
) != 0) {
623 updateval
|= PTH_RWS_CV_PBIT
;
627 COND_GETSEQ_ADDR(cond
, &c_lseqcnt
, &c_useqcnt
, &c_sseqcnt
);
630 lcntval
= *c_lseqcnt
;
631 ucntval
= *c_useqcnt
;
632 scntval
= *c_sseqcnt
;
634 diffgen
= diff_genseq(lcntval
, scntval
); // pending waiters
636 oldval64
= (((uint64_t)scntval
) << 32);
640 /* TBD: Assert, should not be the case */
641 /* validate it is spurious and return */
646 // update scntval with number of expected returns and bits
647 nsval
= (scntval
& PTHRW_COUNT_MASK
) + (updateval
& PTHRW_COUNT_MASK
);
649 nsval
|= ((scntval
& PTH_RWS_CV_BITSALL
) | (updateval
& PTH_RWS_CV_BITSALL
));
651 // if L==S and c&p bits are set, needs clearpre
652 if (((nsval
& PTHRW_COUNT_MASK
) == (lcntval
& PTHRW_COUNT_MASK
)) &&
653 ((nsval
& PTH_RWS_CV_BITSALL
) == PTH_RWS_CV_BITSALL
)) {
654 // reset p bit but retain c bit on the sword
655 nsval
&= PTH_RWS_CV_RESET_PBIT
;
661 newval64
= (((uint64_t)nsval
) << 32);
664 } while (OSAtomicCompareAndSwap64Barrier(oldval64
, newval64
, (volatile int64_t *)c_lseqcnt
) != TRUE
);
667 // if L == S, then reset associated mutex
668 if ((nsval
& PTHRW_COUNT_MASK
) == (lcntval
& PTHRW_COUNT_MASK
)) {
672 if (needclearpre
!= 0) {
674 if (cond
->pshared
== PTHREAD_PROCESS_SHARED
) {
675 flags
|= _PTHREAD_MTX_OPT_PSHARED
;
677 (void)__psynch_cvclrprepost(cond
, lcntval
, ucntval
, nsval
, 0, lcntval
, flags
);
684 pthread_cond_timedwait_relative_np(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
)
686 return _pthread_cond_wait(cond
, mutex
, abstime
, 1, 0);
689 #endif /* !BUILDING_VARIANT ] */
692 pthread_cond_init(pthread_cond_t
*ocond
, const pthread_condattr_t
*attr
)
698 #else /* __DARWIN_UNIX03 */
700 #endif /* __DARWIN_UNIX03 */
702 _pthread_cond
*cond
= (_pthread_cond
*)ocond
;
703 _PTHREAD_LOCK_INIT(cond
->lock
);
704 return _pthread_cond_init(cond
, attr
, conforming
);