1#include <Foundation/FoundationInternal.h>
2PL_FOUNDATION_INTERNAL_HEADER
4#include <Foundation/Threading/ConditionVariable.h>
5#include <Foundation/Time/Time.h>
11plConditionVariable::plConditionVariable()
13 pthread_cond_init(&m_Data.m_ConditionVariable,
nullptr);
16plConditionVariable::~plConditionVariable()
18 PL_ASSERT_DEV(m_iLockCount == 0,
"Thread-signal must be unlocked during destruction.");
20 pthread_cond_destroy(&m_Data.m_ConditionVariable);
25 pthread_cond_signal(&m_Data.m_ConditionVariable);
30 pthread_cond_broadcast(&m_Data.m_ConditionVariable);
35 PL_ASSERT_DEV(m_iLockCount > 0,
"plConditionVariable must be locked when calling UnlockWaitForSignalAndLock.");
37 pthread_cond_wait(&m_Data.m_ConditionVariable, &m_Mutex.GetMutexHandle());
42 PL_ASSERT_DEV(m_iLockCount > 0,
"plConditionVariable must be locked when calling UnlockWaitForSignalAndLock.");
48 gettimeofday(&now,
nullptr);
51 struct timespec timeToWait;
53 const plInt64 iNanoSecondsPerSecond = 1000000000LL;
54 const plInt64 iMicroSecondsPerNanoSecond = 1000LL;
56 plInt64 endTime = now.tv_sec * iNanoSecondsPerSecond + now.tv_usec * iMicroSecondsPerNanoSecond +
static_cast<plInt64
>(timeout.
GetNanoseconds());
58 timeToWait.tv_sec = endTime / iNanoSecondsPerSecond;
59 timeToWait.tv_nsec = endTime % iNanoSecondsPerSecond;
61 if (pthread_cond_timedwait(&m_Data.m_ConditionVariable, &m_Mutex.GetMutexHandle(), &timeToWait) == ETIMEDOUT)
65 return WaitResult::Timeout;
70 return WaitResult::Signaled;
void SignalAll()
Wakes up all the threads that are currently waiting for the variable.
Definition ConditionVariable_Posix.h:28
void UnlockWaitForSignalAndLock() const
Puts the calling thread to sleep and waits for the variable to get signaled.
Definition ConditionVariable_Posix.h:33
void SignalOne()
Wakes up one of the threads that are currently waiting for the variable.
Definition ConditionVariable_Posix.h:23
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
constexpr double GetNanoseconds() const
Returns the nanoseconds value.
Definition Time_inl.h:15