int WaitCondition::Wait(pthread_mutex_t *lock, uint32_t msTimeout) { int ret; if (!m_initDone) { LogAssertFalse("waiting on uninitialized signal"); return -1; } if (msTimeout) { struct timespec waitTime; clock_gettime(CLOCK_REALTIME, &waitTime); timespecAddMs(waitTime, msTimeout); ret = pthread_cond_timedwait(&m_condition, lock, &waitTime); } else ret = pthread_cond_wait(&m_condition, lock); if (ret == 0) return 0; if (ret == ETIMEDOUT) return 1; else { LogVerifyFalse("pthread_cond_wait failed"); return -1; } }
void WaitCondition::Signal() { if (!m_initDone) { LogAssertFalse("signaling on uninitialized signal"); return; } LogVerify(!pthread_cond_signal(&m_condition)); }
bool GetRealTime(struct timespec &now) { if (0 == clock_gettime( CLOCK_REALTIME, &now)) return true; LogAssertFalse("clock_gettime failed"); gLog.Optional(Log::Critical, "clock_gettime failed.%s", strerror(errno)); now.tv_sec = 0; now.tv_nsec = 0; return false; }
bool GetMonolithicTime(struct timespec &now) { if (0 == clock_gettime(CLOCK_MONOTONIC, &now)) return true; LogAssertFalse("clock_gettime failed"); gLog.Optional(Log::Critical, "clock_gettime failed.%s", ErrnoToString()); now.tv_sec = 0; now.tv_nsec = 0; return false; }
TimeSpec TimeSpec::RealNow() { struct TimeSpec now; if (0 == clock_gettime(CLOCK_REALTIME, &now)) return now; gLog.Optional(Log::Critical, "clock_gettime(CLOCK_REALTIME) failed.%s", ErrnoToString()); LogAssertFalse("clock_gettime(CLOCK_REALTIME) failed"); return TimeSpec(); }
TimeSpec TimeSpec::MonoNow() { struct TimeSpec now; if (0 == clock_gettime(CLOCK_MONOTONIC, &now)) return now; gLog.Optional(Log::Critical, "clock_gettime(CLOCK_MONOTONIC) failed.%s", ErrnoToString()); LogAssertFalse("clock_gettime(CLOCK_MONOTONIC) failed"); return TimeSpec(); }
bool QuickLock::Create() { if (!LogVerify(!m_initialized)) return false; if (pthread_mutex_init(&m_Lock, NULL)) { LogAssertFalse("pthread_mutex_init failed"); return false; } m_initialized = true; return true; }
bool ReadWriteLock::Create() { if (!LogVerify(!m_initialized)) return false; if (pthread_rwlock_init(&m_Lock, NULL)) { LogAssertFalse("pthread_rwlock_init failed"); return false; } m_initialized = true; return true; }
bool WaitCondition::Init() { if (m_initDone) { LogAssertFalse("WaitCondition::Init called more than once."); return true; } if (LogVerify(!pthread_cond_init(&m_condition, NULL))) m_initDone = true; return m_initDone; }