2007-09-14 03:51:47 +04:00
|
|
|
/* $NetBSD: pthread_cond.c,v 1.37 2007/09/13 23:51:47 ad Exp $ */
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
/*-
|
2007-03-02 21:53:51 +03:00
|
|
|
* Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
|
2003-01-18 13:32:11 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2007-03-06 02:55:54 +03:00
|
|
|
* by Nathan J. Williams and Andrew Doran.
|
2003-01-18 13:32:11 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2007-09-14 03:51:47 +04:00
|
|
|
__RCSID("$NetBSD: pthread_cond.c,v 1.37 2007/09/13 23:51:47 ad Exp $");
|
2003-03-08 11:03:34 +03:00
|
|
|
|
|
|
|
#include <errno.h>
|
2003-02-01 03:57:31 +03:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
#include "pthread.h"
|
|
|
|
#include "pthread_int.h"
|
|
|
|
|
2005-01-06 20:33:36 +03:00
|
|
|
int _sys_nanosleep(const struct timespec *, struct timespec *);
|
2003-02-01 03:57:31 +03:00
|
|
|
|
|
|
|
extern int pthread__started;
|
|
|
|
|
|
|
|
static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
|
|
|
|
const struct timespec *);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
__strong_alias(__libc_cond_init,pthread_cond_init)
|
|
|
|
__strong_alias(__libc_cond_signal,pthread_cond_signal)
|
|
|
|
__strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
|
|
|
|
__strong_alias(__libc_cond_wait,pthread_cond_wait)
|
|
|
|
__strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
|
|
|
|
__strong_alias(__libc_cond_destroy,pthread_cond_destroy)
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
|
|
|
|
{
|
|
|
|
|
2003-04-23 23:36:12 +04:00
|
|
|
pthread__error(EINVAL, "Invalid condition variable attribute",
|
|
|
|
(attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
cond->ptc_magic = _PT_COND_MAGIC;
|
|
|
|
pthread_lockinit(&cond->ptc_lock);
|
|
|
|
PTQ_INIT(&cond->ptc_waiters);
|
|
|
|
cond->ptc_mutex = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_cond_destroy(pthread_cond_t *cond)
|
|
|
|
{
|
|
|
|
|
2003-04-23 23:36:12 +04:00
|
|
|
pthread__error(EINVAL, "Invalid condition variable",
|
|
|
|
cond->ptc_magic == _PT_COND_MAGIC);
|
|
|
|
pthread__error(EBUSY, "Destroying condition variable in use",
|
|
|
|
cond->ptc_mutex == NULL);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
cond->ptc_magic = _PT_COND_DEAD;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
pthread_t self;
|
2003-04-19 01:36:38 +04:00
|
|
|
|
2003-04-23 23:36:12 +04:00
|
|
|
pthread__error(EINVAL, "Invalid condition variable",
|
|
|
|
cond->ptc_magic == _PT_COND_MAGIC);
|
|
|
|
pthread__error(EINVAL, "Invalid mutex",
|
|
|
|
mutex->ptm_magic == _PT_MUTEX_MAGIC);
|
|
|
|
pthread__error(EPERM, "Mutex not locked in condition wait",
|
2007-09-07 18:09:27 +04:00
|
|
|
mutex->ptm_owner != NULL);
|
2003-04-19 01:36:38 +04:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
self = pthread__self();
|
2003-01-28 00:01:00 +03:00
|
|
|
PTHREADD_ADD(PTHREADD_COND_WAIT);
|
2003-02-01 03:57:31 +03:00
|
|
|
|
|
|
|
/* Just hang out for a while if threads aren't running yet. */
|
|
|
|
if (__predict_false(pthread__started == 0))
|
|
|
|
return pthread_cond_wait_nothread(self, mutex, NULL);
|
|
|
|
|
2007-03-06 01:25:27 +03:00
|
|
|
if (__predict_false(self->pt_cancel))
|
2007-03-02 20:47:40 +03:00
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
2007-08-04 17:37:48 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Note this thread as waiting on the CV. To ensure good
|
|
|
|
* performance it's critical that the spinlock is held for
|
|
|
|
* as short a time as possible - that means no system calls.
|
|
|
|
*/
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinlock(&cond->ptc_lock);
|
2007-09-09 02:49:50 +04:00
|
|
|
#ifdef ERRORCHECK
|
2007-03-02 20:47:40 +03:00
|
|
|
if (cond->ptc_mutex == NULL)
|
|
|
|
cond->ptc_mutex = mutex;
|
2007-03-21 02:49:58 +03:00
|
|
|
else {
|
2007-03-02 20:47:40 +03:00
|
|
|
pthread__error(EINVAL,
|
|
|
|
"Multiple mutexes used for condition wait",
|
|
|
|
cond->ptc_mutex == mutex);
|
2007-03-21 02:49:58 +03:00
|
|
|
}
|
2007-09-09 02:49:50 +04:00
|
|
|
#else
|
|
|
|
cond->ptc_mutex = mutex;
|
|
|
|
#endif
|
2007-03-02 22:56:47 +03:00
|
|
|
PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
|
2007-04-13 01:36:06 +04:00
|
|
|
self->pt_signalled = 0;
|
2007-03-02 22:56:47 +03:00
|
|
|
self->pt_sleeponq = 1;
|
2007-03-06 02:55:54 +03:00
|
|
|
self->pt_sleepobj = &cond->ptc_waiters;
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinunlock(&cond->ptc_lock);
|
2007-08-04 17:37:48 +04:00
|
|
|
|
2007-08-07 23:04:21 +04:00
|
|
|
/*
|
|
|
|
* Before releasing the mutex, note that this thread is
|
|
|
|
* about to block by setting the willpark flag. If there
|
|
|
|
* is a single waiter on the mutex, setting the flag will
|
|
|
|
* defer restarting it until calling into the kernel to
|
|
|
|
* park, saving a syscall & involuntary context switch.
|
|
|
|
*/
|
|
|
|
self->pt_willpark = 1;
|
2006-12-23 08:14:46 +03:00
|
|
|
pthread_mutex_unlock(mutex);
|
2007-03-06 02:55:54 +03:00
|
|
|
(void)pthread__park(self, &cond->ptc_lock, &cond->ptc_waiters,
|
2007-03-24 21:51:59 +03:00
|
|
|
NULL, 1, &mutex->ptm_blocked);
|
2007-09-07 18:09:27 +04:00
|
|
|
pthread_mutex_lock(mutex);
|
2007-03-02 22:56:47 +03:00
|
|
|
|
2007-08-04 17:37:48 +04:00
|
|
|
/*
|
|
|
|
* If we awoke abnormally the waiters list will have been
|
|
|
|
* made empty by the current thread (in pthread__park()),
|
|
|
|
* so we can check the value safely without locking.
|
|
|
|
*
|
|
|
|
* Otherwise, it will have been updated by whichever thread
|
|
|
|
* last issued a wakeup.
|
|
|
|
*/
|
|
|
|
if (PTQ_EMPTY(&cond->ptc_waiters) && cond->ptc_mutex != NULL) {
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinlock(&cond->ptc_lock);
|
2007-08-04 17:37:48 +04:00
|
|
|
if (PTQ_EMPTY(&cond->ptc_waiters))
|
|
|
|
cond->ptc_mutex = NULL;
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinunlock(&cond->ptc_lock);
|
2007-08-04 17:37:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-09-07 18:09:27 +04:00
|
|
|
* If we have cancelled then exit. POSIX dictates that the
|
|
|
|
* mutex must be held when we action the cancellation.
|
2007-08-04 17:37:48 +04:00
|
|
|
*/
|
2007-04-13 01:36:06 +04:00
|
|
|
if (__predict_false(self->pt_cancel)) {
|
|
|
|
if (self->pt_signalled)
|
|
|
|
pthread_cond_signal(cond);
|
2003-11-22 01:08:00 +03:00
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
2007-04-13 01:36:06 +04:00
|
|
|
}
|
2003-11-22 01:08:00 +03:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
pthread_t self;
|
|
|
|
int retval;
|
|
|
|
|
2003-04-23 23:36:12 +04:00
|
|
|
pthread__error(EINVAL, "Invalid condition variable",
|
|
|
|
cond->ptc_magic == _PT_COND_MAGIC);
|
|
|
|
pthread__error(EINVAL, "Invalid mutex",
|
|
|
|
mutex->ptm_magic == _PT_MUTEX_MAGIC);
|
|
|
|
pthread__error(EPERM, "Mutex not locked in condition wait",
|
2007-09-07 18:09:27 +04:00
|
|
|
mutex->ptm_owner != NULL);
|
2003-04-23 23:36:12 +04:00
|
|
|
pthread__error(EINVAL, "Invalid wait time",
|
|
|
|
(abstime->tv_sec >= 0) &&
|
2003-04-19 01:36:38 +04:00
|
|
|
(abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
self = pthread__self();
|
2003-02-01 03:57:31 +03:00
|
|
|
PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
|
|
|
|
|
|
|
|
/* Just hang out for a while if threads aren't running yet. */
|
|
|
|
if (__predict_false(pthread__started == 0))
|
|
|
|
return pthread_cond_wait_nothread(self, mutex, abstime);
|
|
|
|
|
2007-03-06 01:25:27 +03:00
|
|
|
if (__predict_false(self->pt_cancel))
|
2007-03-02 20:47:40 +03:00
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
2007-08-04 17:37:48 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Note this thread as waiting on the CV. To ensure good
|
|
|
|
* performance it's critical that the spinlock is held for
|
|
|
|
* as short a time as possible - that means no system calls.
|
|
|
|
*/
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinlock(&cond->ptc_lock);
|
2007-09-09 02:49:50 +04:00
|
|
|
#ifdef ERRORCHECK
|
2007-03-02 20:47:40 +03:00
|
|
|
if (cond->ptc_mutex == NULL)
|
|
|
|
cond->ptc_mutex = mutex;
|
2007-03-21 02:49:58 +03:00
|
|
|
else {
|
2007-03-02 20:47:40 +03:00
|
|
|
pthread__error(EINVAL,
|
|
|
|
"Multiple mutexes used for condition wait",
|
|
|
|
cond->ptc_mutex == mutex);
|
2007-03-21 02:49:58 +03:00
|
|
|
}
|
2007-09-09 02:49:50 +04:00
|
|
|
#else
|
|
|
|
cond->ptc_mutex = mutex;
|
|
|
|
#endif
|
2007-03-02 22:56:47 +03:00
|
|
|
PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
|
2007-04-13 01:36:06 +04:00
|
|
|
self->pt_signalled = 0;
|
2007-03-02 22:56:47 +03:00
|
|
|
self->pt_sleeponq = 1;
|
2007-03-06 02:55:54 +03:00
|
|
|
self->pt_sleepobj = &cond->ptc_waiters;
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinunlock(&cond->ptc_lock);
|
2007-08-04 17:37:48 +04:00
|
|
|
|
2007-08-07 23:04:21 +04:00
|
|
|
/*
|
|
|
|
* Before releasing the mutex, note that this thread is
|
|
|
|
* about to block by setting the willpark flag. If there
|
|
|
|
* is a single waiter on the mutex, setting the flag will
|
|
|
|
* defer restarting it until calling into the kernel to
|
|
|
|
* park, saving a syscall & involuntary context switch.
|
|
|
|
*/
|
|
|
|
self->pt_willpark = 1;
|
2006-12-24 21:39:45 +03:00
|
|
|
pthread_mutex_unlock(mutex);
|
2007-03-06 02:55:54 +03:00
|
|
|
retval = pthread__park(self, &cond->ptc_lock, &cond->ptc_waiters,
|
2007-03-24 21:51:59 +03:00
|
|
|
abstime, 1, &mutex->ptm_blocked);
|
2007-09-07 18:09:27 +04:00
|
|
|
pthread_mutex_lock(mutex);
|
2006-12-23 08:14:46 +03:00
|
|
|
|
2007-08-04 17:37:48 +04:00
|
|
|
/*
|
|
|
|
* If we awoke abnormally the waiters list will have been
|
|
|
|
* made empty by the current thread (in pthread__park()),
|
|
|
|
* so we can check the value safely without locking.
|
|
|
|
*
|
|
|
|
* Otherwise, it will have been updated by whichever thread
|
|
|
|
* last issued a wakeup.
|
|
|
|
*/
|
|
|
|
if (PTQ_EMPTY(&cond->ptc_waiters) && cond->ptc_mutex != NULL) {
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinlock(&cond->ptc_lock);
|
2007-08-04 17:37:48 +04:00
|
|
|
if (PTQ_EMPTY(&cond->ptc_waiters))
|
|
|
|
cond->ptc_mutex = NULL;
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinunlock(&cond->ptc_lock);
|
2007-08-04 17:37:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-09-07 18:09:27 +04:00
|
|
|
* If we have cancelled then exit. POSIX dictates that the
|
|
|
|
* mutex must be held when we action the cancellation.
|
2007-08-04 17:37:48 +04:00
|
|
|
*/
|
2007-04-13 01:36:06 +04:00
|
|
|
if (__predict_false(self->pt_cancel | retval)) {
|
|
|
|
if (self->pt_signalled)
|
|
|
|
pthread_cond_signal(cond);
|
|
|
|
if (self->pt_cancel)
|
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
}
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_cond_signal(pthread_cond_t *cond)
|
|
|
|
{
|
|
|
|
pthread_t self, signaled;
|
2007-03-21 02:49:58 +03:00
|
|
|
pthread_mutex_t *mutex;
|
2003-04-19 01:36:38 +04:00
|
|
|
|
2003-04-23 23:36:12 +04:00
|
|
|
pthread__error(EINVAL, "Invalid condition variable",
|
|
|
|
cond->ptc_magic == _PT_COND_MAGIC);
|
2003-01-28 00:01:00 +03:00
|
|
|
PTHREADD_ADD(PTHREADD_COND_SIGNAL);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2007-03-15 02:34:48 +03:00
|
|
|
if (PTQ_EMPTY(&cond->ptc_waiters))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
self = pthread__self();
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinlock(&cond->ptc_lock);
|
2007-03-21 02:49:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a thread that is still blocked (no pending wakeup).
|
|
|
|
* A wakeup can be pending if we have interrupted unpark_all
|
|
|
|
* as it releases the interlock.
|
|
|
|
*/
|
|
|
|
PTQ_FOREACH(signaled, &cond->ptc_waiters, pt_sleep) {
|
|
|
|
if (signaled->pt_sleepobj != NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (__predict_false(signaled == NULL)) {
|
|
|
|
cond->ptc_mutex = NULL;
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinunlock(&cond->ptc_lock);
|
2007-03-21 02:49:58 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-04-13 01:36:06 +04:00
|
|
|
* Pull the thread off the queue, and set pt_signalled.
|
|
|
|
*
|
|
|
|
* After resuming execution, the thread must check to see if it
|
|
|
|
* has been restarted as a result of pthread_cond_signal(). If it
|
2007-08-04 17:37:48 +04:00
|
|
|
* has, but cannot take the wakeup (because of eg a timeout) then
|
|
|
|
* try to ensure that another thread sees it. This is necessary
|
|
|
|
* because there may be multiple waiters, and at least one should
|
|
|
|
* take the wakeup if possible.
|
2007-03-21 02:49:58 +03:00
|
|
|
*/
|
|
|
|
PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
|
|
|
|
mutex = cond->ptc_mutex;
|
|
|
|
if (PTQ_EMPTY(&cond->ptc_waiters))
|
|
|
|
cond->ptc_mutex = NULL;
|
2007-04-13 01:36:06 +04:00
|
|
|
signaled->pt_signalled = 1;
|
2007-03-21 02:49:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For all valid uses of pthread_cond_signal(), the caller will
|
|
|
|
* hold the mutex that the target is using to synchronize with.
|
|
|
|
* To avoid the target awakening and immediatley blocking on the
|
2007-09-07 18:09:27 +04:00
|
|
|
* mutex, transfer the thread to be awoken to the current thread's
|
|
|
|
* deferred wakeup list. The waiter will be set running when the
|
|
|
|
* caller (this thread) releases the mutex.
|
2007-03-21 02:49:58 +03:00
|
|
|
*/
|
2007-09-14 03:51:47 +04:00
|
|
|
if (mutex != NULL && self->pt_nwaiters < pthread__unpark_max &&
|
|
|
|
pthread__mutex_deferwake(self, mutex)) {
|
2007-09-07 18:09:27 +04:00
|
|
|
signaled->pt_sleepobj = NULL;
|
|
|
|
signaled->pt_sleeponq = 0;
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinunlock(&cond->ptc_lock);
|
2007-09-07 18:09:27 +04:00
|
|
|
self->pt_waiters[self->pt_nwaiters++] = signaled->pt_lid;
|
2007-03-21 02:49:58 +03:00
|
|
|
} else {
|
2007-03-15 02:34:48 +03:00
|
|
|
pthread__unpark(self, &cond->ptc_lock,
|
|
|
|
&cond->ptc_waiters, signaled);
|
2003-01-31 07:26:50 +03:00
|
|
|
}
|
2007-03-21 22:08:18 +03:00
|
|
|
PTHREADD_ADD(PTHREADD_COND_WOKEUP);
|
2007-03-02 21:53:51 +03:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_cond_broadcast(pthread_cond_t *cond)
|
|
|
|
{
|
2007-03-21 02:49:58 +03:00
|
|
|
pthread_t self, signaled, next;
|
|
|
|
pthread_mutex_t *mutex;
|
2003-04-19 01:36:38 +04:00
|
|
|
|
2007-03-21 02:49:58 +03:00
|
|
|
pthread__error(EINVAL, "Invalid condition variable",
|
|
|
|
cond->ptc_magic == _PT_COND_MAGIC);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2003-01-28 00:01:00 +03:00
|
|
|
PTHREADD_ADD(PTHREADD_COND_BROADCAST);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2007-03-15 02:34:48 +03:00
|
|
|
if (PTQ_EMPTY(&cond->ptc_waiters))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
self = pthread__self();
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_spinlock(&cond->ptc_lock);
|
2007-03-21 02:49:58 +03:00
|
|
|
mutex = cond->ptc_mutex;
|
2007-03-15 02:34:48 +03:00
|
|
|
cond->ptc_mutex = NULL;
|
2007-03-21 02:49:58 +03:00
|
|
|
|
|
|
|
/*
|
2007-09-07 18:09:27 +04:00
|
|
|
* Try to defer waking threads (see pthread_cond_signal()).
|
|
|
|
* Only transfer waiters for which there is no pending wakeup.
|
2007-03-21 02:49:58 +03:00
|
|
|
*/
|
2007-09-14 03:51:47 +04:00
|
|
|
if (mutex != NULL && pthread__mutex_deferwake(self, mutex)) {
|
2007-03-21 02:49:58 +03:00
|
|
|
for (signaled = PTQ_FIRST(&cond->ptc_waiters);
|
|
|
|
signaled != NULL;
|
|
|
|
signaled = next) {
|
|
|
|
next = PTQ_NEXT(signaled, pt_sleep);
|
|
|
|
if (__predict_false(signaled->pt_sleepobj == NULL))
|
|
|
|
continue;
|
2007-09-07 18:09:27 +04:00
|
|
|
if (self->pt_nwaiters == pthread__unpark_max) {
|
|
|
|
/* Overflow, take the slow path. */
|
|
|
|
break;
|
|
|
|
}
|
2007-03-21 02:49:58 +03:00
|
|
|
PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
|
2007-09-07 18:09:27 +04:00
|
|
|
signaled->pt_sleepobj = NULL;
|
|
|
|
signaled->pt_sleeponq = 0;
|
|
|
|
self->pt_waiters[self->pt_nwaiters++] =
|
|
|
|
signaled->pt_lid;
|
2007-03-21 02:49:58 +03:00
|
|
|
}
|
2007-09-07 18:09:27 +04:00
|
|
|
if (signaled == NULL) {
|
|
|
|
/* Anything more to do? */
|
|
|
|
pthread_spinunlock(&cond->ptc_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread__unpark_all(self, &cond->ptc_lock, &cond->ptc_waiters);
|
2007-03-21 02:49:58 +03:00
|
|
|
|
2007-03-15 02:34:48 +03:00
|
|
|
PTHREADD_ADD(PTHREADD_COND_WOKEUP);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_condattr_init(pthread_condattr_t *attr)
|
|
|
|
{
|
|
|
|
|
|
|
|
attr->ptca_magic = _PT_CONDATTR_MAGIC;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_condattr_destroy(pthread_condattr_t *attr)
|
|
|
|
{
|
|
|
|
|
2003-04-23 23:36:12 +04:00
|
|
|
pthread__error(EINVAL, "Invalid condition variable attribute",
|
|
|
|
attr->ptca_magic == _PT_CONDATTR_MAGIC);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
attr->ptca_magic = _PT_CONDATTR_DEAD;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2003-02-01 03:57:31 +03:00
|
|
|
|
|
|
|
/* Utility routine to hang out for a while if threads haven't started yet. */
|
|
|
|
static int
|
|
|
|
pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
|
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
2005-01-06 20:33:36 +03:00
|
|
|
struct timespec now, diff;
|
2003-02-01 03:57:31 +03:00
|
|
|
int retval;
|
|
|
|
|
2005-01-06 20:33:36 +03:00
|
|
|
if (abstime == NULL) {
|
|
|
|
diff.tv_sec = 99999999;
|
|
|
|
diff.tv_nsec = 0;
|
|
|
|
} else {
|
|
|
|
clock_gettime(CLOCK_REALTIME, &now);
|
|
|
|
if (timespeccmp(abstime, &now, <))
|
|
|
|
timespecclear(&diff);
|
2004-12-10 20:11:53 +03:00
|
|
|
else
|
2005-01-06 20:33:36 +03:00
|
|
|
timespecsub(abstime, &now, &diff);
|
2003-02-01 03:57:31 +03:00
|
|
|
}
|
|
|
|
|
2005-01-06 20:33:36 +03:00
|
|
|
do {
|
|
|
|
pthread__testcancel(self);
|
|
|
|
pthread_mutex_unlock(mutex);
|
|
|
|
retval = _sys_nanosleep(&diff, NULL);
|
|
|
|
pthread_mutex_lock(mutex);
|
|
|
|
} while (abstime == NULL && retval == 0);
|
2003-02-01 03:57:31 +03:00
|
|
|
pthread__testcancel(self);
|
|
|
|
|
|
|
|
if (retval == 0)
|
|
|
|
return ETIMEDOUT;
|
|
|
|
else
|
2004-05-03 13:13:34 +04:00
|
|
|
/* spurious wakeup */
|
|
|
|
return 0;
|
2003-02-01 03:57:31 +03:00
|
|
|
}
|