Example #1
0
    // returns whether we should produce more idle events
    virtual bool Action()
    {
        switch (m_count)
        {
        case 0:
            CPPUNIT_ASSERT(Init());
            break;
        case 1:
            GenerateEvent();
            break;
        case 2:
            // actual test
            CheckResult();
            Exit();
            break;

            // TODO a mechanism that will break the loop in case we
            // don't receive a file system event
            // this below doesn't quite work, so all tests must pass :-)
#if 0
        case 2:
            m_loop.Yield();
            m_loop.WakeUp();
            CPPUNIT_ASSERT(KeepWaiting());
            m_loop.Yield();
            break;
        case 3:
            break;
        case 4:
            CPPUNIT_ASSERT(AfterWait());
            break;
#endif
        } // switch (m_count)

        return m_count <= 0;
    }
Example #2
0
int LLBC_ConditionVariable::TimedWait(LLBC_ILock &lock, int milliSeconds)
{
    if (UNLIKELY(milliSeconds < 0 && milliSeconds != static_cast<int>(LLBC_INFINITE)))
    {
        LLBC_SetLastError(LLBC_ERROR_ARG);
        return LLBC_FAILED;
    }

#if LLBC_TARGET_PLATFORM_NON_WIN32
    pthread_mutex_t *mtx = reinterpret_cast<pthread_mutex_t *>(lock.Handle());
    ASSERT(mtx);

    if (milliSeconds == static_cast<int>(LLBC_INFINITE))
    {
        pthread_cond_wait(&m_handle, mtx);
        return LLBC_OK;
    }

    struct timeval tvStart, tvEnd;
    struct timespec ts;

    ::gettimeofday(&tvStart, NULL);
    tvEnd = tvStart;
    tvEnd.tv_sec += milliSeconds / 1000;
    tvEnd.tv_usec += (milliSeconds % 1000) * 1000;
    tvEnd.tv_sec += tvEnd.tv_usec / (1000 * 1000);
    tvEnd.tv_usec = tvEnd.tv_usec % (1000 * 1000);

    TIMEVAL_TO_TIMESPEC(&tvEnd, &ts);

    int errNo = pthread_cond_timedwait(&m_handle, mtx, &ts);
    if (errNo != 0)
    {
        errno = errNo;
        if (errNo == ETIMEDOUT)
        {
            LLBC_SetLastError(LLBC_ERROR_TIMEOUT);
        }
        else
        {
            LLBC_SetLastError(LLBC_ERROR_CLIB);
        }

        return LLBC_FAILED;
    }

    return LLBC_OK;
#else // LLBC_TARGET_PLATFORM_WIN32
    ::WaitForSingleObject(_cond.blockLock, INFINITE);
    ++ _cond.waitersBlocked;
    ::ReleaseSemaphore(_cond.blockLock, 1, NULL);

 #ifdef _MSC_VER
  #pragma inline_depth(0)
 #endif

    lock.Unlock();
    DWORD waitTimes = (milliSeconds == LLBC_INFINITE) ? (INFINITE) : milliSeconds;
    DWORD waitRet = ::WaitForSingleObject(_cond.blockQueue, waitTimes);
    ASSERT((waitRet == WAIT_OBJECT_0 || waitRet == WAIT_TIMEOUT) && "LLBC_CriticalVariable::TimedWait() error!");

    bool successed = (waitRet == WAIT_OBJECT_0) ? true : false;
    if (!successed)
    {
        LLBC_SetLastError(LLBC_ERROR_TIMEOUT);
    }

    AfterWait(lock);

 #ifdef _MSC_VER
  #pragma inline_depth()
 #endif

    return successed ? LLBC_OK : LLBC_FAILED;
#endif // LLBC_TARGET_PLATFORM_NON_WIN32
}