/*******************************************************************************
**
** Function:        wait
**
** Description:     Block the caller and wait for a condition.
**                  millisec: Timeout in milliseconds.
**
** Returns:         True if wait is successful; false if timeout occurs.
**
*******************************************************************************/
bool CondVar::wait (Mutex& mutex, long millisec)
{
    bool retVal = false;
    struct timespec absoluteTime;

    if (clock_gettime (CLOCK_MONOTONIC, &absoluteTime) == -1)
    {
        ALOGE ("CondVar::wait: fail get time; errno=0x%X", errno);
    }
    else
    {
        absoluteTime.tv_sec += millisec / 1000;
        long ns = absoluteTime.tv_nsec + ((millisec % 1000) * 1000000);
        if (ns > 1000000000)
        {
            absoluteTime.tv_sec++;
            absoluteTime.tv_nsec = ns - 1000000000;
        }
        else
            absoluteTime.tv_nsec = ns;
    }

    //pthread_cond_timedwait_monotonic_np() is an Android-specific function
    //declared in /development/ndk/platforms/android-9/include/pthread.h;
    //it uses monotonic clock.
    //the standard pthread_cond_timedwait() uses realtime clock.
    int waitResult = pthread_cond_timedwait_monotonic_np (&mCondition, mutex.nativeHandle(), &absoluteTime);
    if ((waitResult != 0) && (waitResult != ETIMEDOUT))
        ALOGE ("CondVar::wait: fail timed wait; error=0x%X", waitResult);
    retVal = (waitResult == 0); //waited successfully
    return retVal;
}
Beispiel #2
0
int uv_cond_timedwait(uv_cond_t* cond, uv_mutex_t* mutex, uint64_t timeout) {
  int r;
  struct timespec ts;

#if defined(__APPLE__) && defined(__MACH__)
  ts.tv_sec = timeout / NANOSEC;
  ts.tv_nsec = timeout % NANOSEC;
  r = pthread_cond_timedwait_relative_np(cond, mutex, &ts);
#else
  timeout += uv__hrtime(UV_CLOCK_PRECISE);
  ts.tv_sec = timeout / NANOSEC;
  ts.tv_nsec = timeout % NANOSEC;
#if defined(__ANDROID__) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
  /*
   * The bionic pthread implementation doesn't support CLOCK_MONOTONIC,
   * but has this alternative function instead.
   */
  r = pthread_cond_timedwait_monotonic_np(cond, mutex, &ts);
#else
  r = pthread_cond_timedwait(cond, mutex, &ts);
#endif /* __ANDROID__ */
#endif


  if (r == 0)
    return 0;

  if (r == ETIMEDOUT)
    return -ETIMEDOUT;

  abort();
  return -EINVAL;  /* Satisfy the compiler. */
}
Beispiel #3
0
int uv_cond_timedwait(uv_cond_t* cond, uv_mutex_t* mutex, uint64_t timeout) {
    int r;
    struct timespec ts;

#if defined(__APPLE__) && defined(__MACH__)
    ts.tv_sec = timeout / NANOSEC;
    ts.tv_nsec = timeout % NANOSEC;
    r = pthread_cond_timedwait_relative_np(cond, mutex, &ts);
#else
    timeout += uv__hrtime();
    ts.tv_sec = timeout / NANOSEC;
    ts.tv_nsec = timeout % NANOSEC;
#if defined(__ANDROID__) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
    r = pthread_cond_timedwait_monotonic_np(cond, mutex, &ts);
#else
    r = pthread_cond_timedwait(cond, mutex, &ts);
#endif /* __ANDROID__ */
#endif

    if (r == 0) return 0;

    if (r == ETIMEDOUT) return -1;

    JXABORT("19");
    return -1; /* Satisfy the compiler. */
}
Beispiel #4
0
int uv_cond_timedwait(uv_cond_t* cond, uv_mutex_t* mutex, uint64_t timeout) {
  int r;
  struct timespec ts;
#if defined(__MVS__)
  struct timeval tv;
#endif

#if defined(__APPLE__) && defined(__MACH__)
  ts.tv_sec = timeout / NANOSEC;
  ts.tv_nsec = timeout % NANOSEC;
  r = pthread_cond_timedwait_relative_np(cond, mutex, &ts);
#else
#if defined(__MVS__)
  if (gettimeofday(&tv, NULL))
    abort();
  timeout += tv.tv_sec * NANOSEC + tv.tv_usec * 1e3;
#else
  timeout += uv__hrtime(UV_CLOCK_PRECISE);
#endif
  ts.tv_sec = timeout / NANOSEC;
  ts.tv_nsec = timeout % NANOSEC;
#if defined(__ANDROID_API__) && __ANDROID_API__ < 21

  /*
   * The bionic pthread implementation doesn't support CLOCK_MONOTONIC,
   * but has this alternative function instead.
   */
  r = pthread_cond_timedwait_monotonic_np(cond, mutex, &ts);
#else
  r = pthread_cond_timedwait(cond, mutex, &ts);
#endif /* __ANDROID_API__ */
#endif


  if (r == 0)
    return 0;

  if (r == ETIMEDOUT)
    return UV_ETIMEDOUT;

  abort();
  return UV_EINVAL;  /* Satisfy the compiler. */
}
Beispiel #5
0
// TODO: this exists only for backward binary compatibility on 32 bit platforms.
extern "C" int pthread_cond_timedwait_monotonic(pthread_cond_t* cond_interface,
                                                pthread_mutex_t* mutex,
                                                const timespec* abs_timeout) {
  return pthread_cond_timedwait_monotonic_np(cond_interface, mutex, abs_timeout);
}
Beispiel #6
0
CL_BOOL
CL_ThreadUnix::Wait(DWORD msec)
{
    INT ret = 0;

    pthread_mutex_lock(&m_mutex);

    if (m_bSignalFlg == CL_FALSE)
    {
        if (INFINITE != msec)
        {
#ifdef _FOR_APPLE_
            struct timespec nptime;
            nptime.tv_sec = msec/ 1000;
            nptime.tv_nsec = msec% 1000*1000000;

            ret = pthread_cond_timedwait_relative_np(&m_cond, &m_mutex, &nptime);

#elif defined(_FOR_ANDROID_)
            struct timespec nptime;
            gettimespec(&nptime, msec);

            ret = pthread_cond_timedwait_monotonic_np(&m_cond, &m_mutex, &nptime);

#else // _LINUX
            struct timespec nptime;
            gettimespec(&nptime, msec);

            ret = pthread_cond_timedwait(&m_cond, &m_mutex, &nptime);
#endif
        }
        else
        {
            ret = pthread_cond_wait(&m_cond, &m_mutex);
        }
    }

    m_bSignalFlg = CL_FALSE;

    pthread_mutex_unlock(&m_mutex);

    // DWORD *pAddr = NULL; // [0] timer id; [1] timer pointer.
    while (1)
    {
        m_cSyncMSg.SyncStart();
        intptr_t* pAddr = (intptr_t*)m_cMsgQue.Pop();
        m_cSyncMSg.SyncEnd();
        if (pAddr != NULL)
        {
            if (CL_Timer::IsValid(*pAddr) == TRUE)
            {
                reinterpret_cast<CL_Timer*>(*(pAddr+1))->DoAction();
            }
            delete[] pAddr;
        }
        else
        {
            break;
        }
    }

    // ETIMEDOUT
    if (0 == ret)
    {
        return CL_TRUE;
    }
    else
    {
        return CL_FALSE;
    }
}