2001-10-06 14:13:28 +00:00
|
|
|
#include "CCondVar.h"
|
|
|
|
#include "CStopwatch.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// CCondVarBase
|
|
|
|
//
|
|
|
|
|
|
|
|
CCondVarBase::CCondVarBase(CMutex* mutex) :
|
2002-06-10 22:06:45 +00:00
|
|
|
m_mutex(mutex)
|
2001-10-06 14:13:28 +00:00
|
|
|
#if defined(CONFIG_PLATFORM_WIN32)
|
2002-06-10 22:06:45 +00:00
|
|
|
, m_waitCountMutex()
|
2001-10-06 14:13:28 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
assert(m_mutex != NULL);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCondVarBase::~CCondVarBase()
|
|
|
|
{
|
|
|
|
fini();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CCondVarBase::lock() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
m_mutex->lock();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CCondVarBase::unlock() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
m_mutex->unlock();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
|
|
|
CCondVarBase::wait(double timeout) const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
CStopwatch timer(true);
|
|
|
|
return wait(timer, timeout);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CMutex*
|
|
|
|
CCondVarBase::getMutex() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return m_mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONFIG_PTHREADS)
|
|
|
|
|
|
|
|
#include "CThread.h"
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sys/time.h>
|
2002-06-10 22:06:45 +00:00
|
|
|
#include <cerrno>
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CCondVarBase::init()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
pthread_cond_t* cond = new pthread_cond_t;
|
|
|
|
int status = pthread_cond_init(cond, NULL);
|
|
|
|
assert(status == 0);
|
|
|
|
m_cond = reinterpret_cast<pthread_cond_t*>(cond);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CCondVarBase::fini()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(m_cond);
|
|
|
|
int status = pthread_cond_destroy(cond);
|
|
|
|
assert(status == 0);
|
|
|
|
delete cond;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CCondVarBase::signal()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(m_cond);
|
|
|
|
int status = pthread_cond_signal(cond);
|
|
|
|
assert(status == 0);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CCondVarBase::broadcast()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(m_cond);
|
|
|
|
int status = pthread_cond_broadcast(cond);
|
|
|
|
assert(status == 0);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
CCondVarBase::wait(CStopwatch& timer, double timeout) const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// check timeout against timer
|
|
|
|
if (timeout >= 0.0) {
|
|
|
|
timeout -= timer.getTime();
|
|
|
|
if (timeout < 0.0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get condition variable and mutex
|
|
|
|
pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(m_cond);
|
|
|
|
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(m_mutex->m_mutex);
|
|
|
|
|
|
|
|
// get final time
|
|
|
|
struct timeval now;
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
struct timespec finalTime;
|
|
|
|
finalTime.tv_sec = now.tv_sec;
|
|
|
|
finalTime.tv_nsec = now.tv_usec * 1000;
|
|
|
|
if (timeout >= 0.0) {
|
|
|
|
const long timeout_sec = (long)timeout;
|
|
|
|
const long timeout_nsec = (long)(1000000000.0 * (timeout - timeout_sec));
|
|
|
|
finalTime.tv_sec += timeout_sec;
|
|
|
|
finalTime.tv_nsec += timeout_nsec;
|
|
|
|
if (finalTime.tv_nsec >= 1000000000) {
|
|
|
|
finalTime.tv_nsec -= 1000000000;
|
|
|
|
finalTime.tv_sec += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// repeat until we reach the final time
|
|
|
|
int status;
|
|
|
|
for (;;) {
|
|
|
|
// compute the next timeout
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
struct timespec endTime;
|
|
|
|
endTime.tv_sec = now.tv_sec;
|
|
|
|
endTime.tv_nsec = now.tv_usec * 1000 + 50000000;
|
|
|
|
if (endTime.tv_nsec >= 1000000000) {
|
|
|
|
endTime.tv_nsec -= 1000000000;
|
|
|
|
endTime.tv_sec += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// see if we should cancel this thread
|
|
|
|
CThread::testCancel();
|
|
|
|
|
|
|
|
// done if past final timeout
|
|
|
|
if (timeout >= 0.0) {
|
|
|
|
if (endTime.tv_sec > finalTime.tv_sec ||
|
|
|
|
(endTime.tv_sec == finalTime.tv_sec &&
|
|
|
|
endTime.tv_nsec >= finalTime.tv_nsec)) {
|
|
|
|
status = ETIMEDOUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait
|
|
|
|
status = pthread_cond_timedwait(cond, mutex, &endTime);
|
|
|
|
|
|
|
|
// check for cancel again
|
|
|
|
CThread::testCancel();
|
|
|
|
|
|
|
|
// check wait status
|
2002-06-10 22:06:45 +00:00
|
|
|
if (status != ETIMEDOUT && status != EINTR) {
|
2001-10-06 14:13:28 +00:00
|
|
|
break;
|
2002-06-10 22:06:45 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (status) {
|
2002-04-29 14:40:01 +00:00
|
|
|
case 0:
|
2001-10-06 14:13:28 +00:00
|
|
|
// success
|
|
|
|
return true;
|
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
case ETIMEDOUT:
|
2001-10-06 14:13:28 +00:00
|
|
|
return false;
|
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
default:
|
2001-10-06 14:13:28 +00:00
|
|
|
assert(0 && "condition variable wait error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_PTHREADS
|
|
|
|
|
|
|
|
#if defined(CONFIG_PLATFORM_WIN32)
|
|
|
|
|
|
|
|
#include "CLock.h"
|
|
|
|
#include "CThreadRep.h"
|
2002-06-10 16:49:46 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2001-10-06 14:13:28 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
//
|
|
|
|
// note -- implementation taken from
|
|
|
|
// http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
|
|
|
|
// titled "Strategies for Implementing POSIX Condition Variables
|
|
|
|
// on Win32." it also provides an implementation that doesn't
|
|
|
|
// suffer from the incorrectness problem described in our
|
|
|
|
// corresponding header but it is slower, still unfair, and
|
|
|
|
// can cause busy waiting.
|
|
|
|
//
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CCondVarBase::init()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// prepare events
|
|
|
|
HANDLE* events = new HANDLE[2];
|
2002-04-29 14:40:01 +00:00
|
|
|
events[kSignal] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
events[kBroadcast] = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// prepare members
|
|
|
|
m_cond = reinterpret_cast<void*>(events);
|
|
|
|
m_waitCount = 0;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CCondVarBase::fini()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
HANDLE* events = reinterpret_cast<HANDLE*>(m_cond);
|
2002-04-29 14:40:01 +00:00
|
|
|
CloseHandle(events[kSignal]);
|
|
|
|
CloseHandle(events[kBroadcast]);
|
2001-10-06 14:13:28 +00:00
|
|
|
delete[] events;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CCondVarBase::signal()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// is anybody waiting?
|
|
|
|
bool hasWaiter;
|
|
|
|
{
|
|
|
|
CLock lock(&m_waitCountMutex);
|
|
|
|
hasWaiter = (m_waitCount > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// wake one thread if anybody is waiting
|
2002-06-10 22:06:45 +00:00
|
|
|
if (hasWaiter) {
|
2002-04-29 14:40:01 +00:00
|
|
|
SetEvent(reinterpret_cast<HANDLE*>(m_cond)[kSignal]);
|
2002-06-10 22:06:45 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CCondVarBase::broadcast()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// is anybody waiting?
|
|
|
|
bool hasWaiter;
|
|
|
|
{
|
|
|
|
CLock lock(&m_waitCountMutex);
|
|
|
|
hasWaiter = (m_waitCount > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// wake all threads if anybody is waiting
|
2002-06-10 22:06:45 +00:00
|
|
|
if (hasWaiter) {
|
2002-04-29 14:40:01 +00:00
|
|
|
SetEvent(reinterpret_cast<HANDLE*>(m_cond)[kBroadcast]);
|
2002-06-10 22:06:45 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
CCondVarBase::wait(CStopwatch& timer, double timeout) const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// check timeout against timer
|
|
|
|
if (timeout >= 0.0) {
|
|
|
|
timeout -= timer.getTime();
|
2002-06-10 22:06:45 +00:00
|
|
|
if (timeout < 0.0) {
|
2001-10-06 14:13:28 +00:00
|
|
|
return false;
|
2002-06-10 22:06:45 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepare to wait
|
2001-11-19 00:33:36 +00:00
|
|
|
CThreadPtr currentRep = CThreadRep::getCurrentThreadRep();
|
2001-10-06 14:13:28 +00:00
|
|
|
const DWORD winTimeout = (timeout < 0.0) ? INFINITE :
|
|
|
|
static_cast<DWORD>(1000.0 * timeout);
|
|
|
|
HANDLE* events = reinterpret_cast<HANDLE*>(m_cond);
|
|
|
|
HANDLE handles[3];
|
|
|
|
handles[0] = events[kSignal];
|
|
|
|
handles[1] = events[kBroadcast];
|
|
|
|
handles[2] = currentRep->getCancelEvent();
|
|
|
|
const DWORD n = currentRep->isCancellable() ? 3 : 2;
|
|
|
|
|
|
|
|
// update waiter count
|
|
|
|
{
|
|
|
|
CLock lock(&m_waitCountMutex);
|
|
|
|
++m_waitCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// release mutex. this should be atomic with the wait so that it's
|
|
|
|
// impossible for another thread to signal us between the unlock and
|
|
|
|
// the wait, which would lead to a lost signal on broadcasts.
|
|
|
|
// however, we're using a manual reset event for broadcasts which
|
|
|
|
// stays set until we reset it, so we don't lose the broadcast.
|
|
|
|
m_mutex->unlock();
|
|
|
|
|
|
|
|
// wait for a signal or broadcast
|
2001-10-25 22:17:17 +00:00
|
|
|
DWORD result = WaitForMultipleObjects(n, handles, FALSE, winTimeout);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// cancel takes priority
|
|
|
|
if (n == 3 && result != WAIT_OBJECT_0 + 2 &&
|
2002-06-10 22:06:45 +00:00
|
|
|
WaitForSingleObject(handles[2], 0) == WAIT_OBJECT_0) {
|
2001-10-06 14:13:28 +00:00
|
|
|
result = WAIT_OBJECT_0 + 2;
|
2002-06-10 22:06:45 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// update the waiter count and check if we're the last waiter
|
|
|
|
bool last;
|
|
|
|
{
|
|
|
|
CLock lock(&m_waitCountMutex);
|
|
|
|
--m_waitCount;
|
|
|
|
last = (result == WAIT_OBJECT_0 + 1 && m_waitCount == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset the broadcast event if we're the last waiter
|
2002-06-10 22:06:45 +00:00
|
|
|
if (last) {
|
2001-10-06 14:13:28 +00:00
|
|
|
ResetEvent(events[kBroadcast]);
|
2002-06-10 22:06:45 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// reacquire the mutex
|
|
|
|
m_mutex->lock();
|
|
|
|
|
|
|
|
// cancel thread if necessary
|
2002-06-10 22:06:45 +00:00
|
|
|
if (result == WAIT_OBJECT_0 + 2) {
|
2001-10-06 14:13:28 +00:00
|
|
|
currentRep->testCancel();
|
2002-06-10 22:06:45 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// return success or failure
|
|
|
|
return (result == WAIT_OBJECT_0 + 0 ||
|
|
|
|
result == WAIT_OBJECT_0 + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_PLATFORM_WIN32
|