diff options
author | rachytski <siarhei.rachytski@gmail.com> | 2013-01-08 21:48:04 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:48:21 +0300 |
commit | 7a942ac0fd5b492db0976642e7eb01be1ba0de32 (patch) | |
tree | bc0123e85e1ee746034764b1cd103b22e82b8c27 | |
parent | f9523d9e3944314437719a87b515843a15e00589 (diff) |
added ability to wait on condition variable for the specified amount of time.
-rw-r--r-- | base/condition.cpp | 4 | ||||
-rw-r--r-- | base/condition.hpp | 4 | ||||
-rw-r--r-- | base/condition_posix.cpp | 23 | ||||
-rw-r--r-- | base/condition_windows_native.cpp | 7 |
4 files changed, 30 insertions, 8 deletions
diff --git a/base/condition.cpp b/base/condition.cpp index 7af813e99c..c55c8c926f 100644 --- a/base/condition.cpp +++ b/base/condition.cpp @@ -21,9 +21,9 @@ namespace threads m_Condition.Unlock(); } - void ConditionGuard::Wait() + void ConditionGuard::Wait(unsigned ms) { - m_Condition.Wait(); + m_Condition.Wait(ms); } void ConditionGuard::Signal(bool broadcast) diff --git a/base/condition.hpp b/base/condition.hpp index 87d576cc00..384652453c 100644 --- a/base/condition.hpp +++ b/base/condition.hpp @@ -20,7 +20,7 @@ namespace threads ~Condition(); void Signal(bool broadcast = false); - void Wait(); + void Wait(unsigned ms = -1); void Lock(); void Unlock(); }; @@ -33,7 +33,7 @@ namespace threads public: ConditionGuard(Condition & condition); ~ConditionGuard(); - void Wait(); + void Wait(unsigned ms = -1); void Signal(bool broadcast = false); }; } diff --git a/base/condition_posix.cpp b/base/condition_posix.cpp index 64183a4bdb..d2cfa97d1e 100644 --- a/base/condition_posix.cpp +++ b/base/condition_posix.cpp @@ -5,6 +5,10 @@ #include "condition.hpp" #include "mutex.hpp" +#include "../std/stdint.hpp" +#include <sys/time.h> +#include <sys/errno.h> + #include <pthread.h> namespace threads @@ -38,9 +42,24 @@ namespace threads ::pthread_cond_signal(&m_pImpl->m_Condition); } - void Condition::Wait() + void Condition::Wait(unsigned ms) { - ::pthread_cond_wait(&m_pImpl->m_Condition, &m_pImpl->m_Mutex.m_Mutex); + if (ms == -1) + ::pthread_cond_wait(&m_pImpl->m_Condition, &m_pImpl->m_Mutex.m_Mutex); + else + { + ::timeval curtv; + ::gettimeofday(&curtv, 0); + + ::timespec ts; + + uint64_t deltaNanoSec = curtv.tv_usec * 1000 + ms * 1000000; + + ts.tv_sec = curtv.tv_sec + deltaNanoSec / 1000000000; + ts.tv_nsec = deltaNanoSec % 1000000000; + + ::pthread_cond_timedwait(&m_pImpl->m_Condition, &m_pImpl->m_Mutex.m_Mutex, &ts); + } } void Condition::Lock() diff --git a/base/condition_windows_native.cpp b/base/condition_windows_native.cpp index 143c8e868d..abd467cea7 100644 --- a/base/condition_windows_native.cpp +++ b/base/condition_windows_native.cpp @@ -53,6 +53,7 @@ namespace threads void Wait() { + /// TODO: Use MS parameter. m_pSleep(&m_Condition, &m_mutex.m_Mutex, INFINITE); } @@ -159,6 +160,8 @@ namespace threads void Wait() { + // TODO : Use MS parameter. + // Avoid race conditions ::EnterCriticalSection(&waiters_count_lock_); ++waiters_count_; @@ -228,9 +231,9 @@ namespace threads m_pImpl->Signal(broadcast); } - void Condition::Wait() + void Condition::Wait(unsigned ms) { - m_pImpl->Wait(); + m_pImpl->Wait(ms); } void Condition::Lock() |