示例#1
0
//----------------------------------------------------------------------
// The Wait() function atomically blocks the current thread
// waiting on the owned condition variable, and unblocks the mutex
// specified by "mutex".  The waiting thread unblocks only after
// another thread calls Signal(), or Broadcast() with the same
// condition variable, or if "abstime" is valid (non-NULL) this
// function will return when the system time reaches the time
// specified in "abstime". If "abstime" is NULL this function will
// wait for an infinite amount of time for the condition variable
// to be signaled or broadcasted.
//
// The current thread re-acquires the lock on "mutex".
//----------------------------------------------------------------------
int
Condition::Wait (Mutex &mutex, const TimeValue *abstime, bool *timed_out)
{
    int err = 0;
    do
    {
        if (abstime && abstime->IsValid())
        {
            struct timespec abstime_ts = abstime->GetAsTimeSpec();
            err = ::pthread_cond_timedwait (&m_condition, mutex.GetMutex(), &abstime_ts);
        }
        else
            err = ::pthread_cond_wait (&m_condition, mutex.GetMutex());
    } while (err == EINTR);

    if (timed_out != NULL)
    {
        if (err == ETIMEDOUT)
            *timed_out = true;
        else
            *timed_out = false;
    }


    return err;
}
示例#2
0
int
Condition::Wait (Mutex &mutex, const TimeValue *abstime, bool *timed_out)
{
#ifdef _WIN32
    DWORD wait = INFINITE;
    if (abstime != NULL)
        wait = tv2ms(abstime->GetAsTimeVal());

    int err = SleepConditionVariableCS(&m_condition, (PCRITICAL_SECTION)&mutex,
        wait);

    if (timed_out != NULL)
    {
        if ((err == 0) && GetLastError() == ERROR_TIMEOUT)
            *timed_out = true;
        else
            *timed_out = false;
    }

    return err != 0;
#else
    int err = 0;
    do
    {
        if (abstime && abstime->IsValid())
        {
            struct timespec abstime_ts = abstime->GetAsTimeSpec();
            err = ::pthread_cond_timedwait (&m_condition, mutex.GetMutex(), &abstime_ts);
        }
        else
            err = ::pthread_cond_wait (&m_condition, mutex.GetMutex());
    } while (err == EINTR);

    if (timed_out != NULL)
    {
        if (err == ETIMEDOUT)
            *timed_out = true;
        else
            *timed_out = false;
    }

    return err;
#endif
}
示例#3
0
 void Condition::Wait(Mutex& mutex)
 {
     pthread_cond_wait(&cond, mutex.GetMutex());
 }