Created
November 19, 2008 18:22
-
-
Save kevinw/26616 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool ThreadCondition::timedWait(Mutex& mutex, double secondsToWait) | |
{ | |
if (secondsToWait < 0.0) { | |
wait(mutex); | |
return true; | |
} | |
int intervalSeconds = static_cast<int>(secondsToWait); | |
int intervalMicroseconds = static_cast<int>((secondsToWait - intervalSeconds) * 1000000.0); | |
#if !COMPILER(MSVC) | |
// Current time comes in sec/microsec | |
timeval currentTime; | |
gettimeofday(¤tTime, NULL); | |
// Target time comes in sec/nanosec | |
timespec targetTime; | |
targetTime.tv_sec = currentTime.tv_sec + intervalSeconds; | |
targetTime.tv_nsec = (currentTime.tv_usec + intervalMicroseconds) * 1000; | |
#else | |
// Windows lacks gettimeofday | |
double currentTime = getCurrentUTCTimeWithMicroseconds(); | |
time_t currentTimeSeconds = static_cast<time_t>(currentTime); | |
long currentTimeNanoseconds = static_cast<long>((currentTime - currentTimeSeconds) * 1000000000.0); | |
targetTime.tv_sec = currentTimeSeconds + intervalSeconds; | |
targetTime.tv_nsec = currentTimeNanoseconds + intervalMicroseconds * 1000.0; | |
#endif | |
if (targetTime.tv_nsec > 1000000000) { | |
targetTime.tv_nsec -= 1000000000; | |
targetTime.tv_sec++; | |
} | |
return pthread_cond_timedwait(&m_condition, &mutex.impl(), &targetTime) == 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment