Guest User

Untitled

a guest
Feb 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. bool ThreadCondition::timedWait(Mutex& mutex, double secondsToWait)
  2. {
  3. if (secondsToWait < 0.0) {
  4. wait(mutex);
  5. return true;
  6. }
  7.  
  8. int intervalSeconds = static_cast<int>(secondsToWait);
  9. int intervalMicroseconds = static_cast<int>((secondsToWait - intervalSeconds) * 1000000.0);
  10.  
  11. #if !COMPILER(MSVC)
  12. // Current time comes in sec/microsec
  13. timeval currentTime;
  14. gettimeofday(&currentTime, NULL);
  15.  
  16. // Target time comes in sec/nanosec
  17. timespec targetTime;
  18. targetTime.tv_sec = currentTime.tv_sec + intervalSeconds;
  19. targetTime.tv_nsec = (currentTime.tv_usec + intervalMicroseconds) * 1000;
  20. #else
  21. // Windows lacks gettimeofday
  22. double currentTime = getCurrentUTCTimeWithMicroseconds();
  23.  
  24. time_t currentTimeSeconds = static_cast<time_t>(currentTime);
  25. long currentTimeNanoseconds = static_cast<long>((currentTime - currentTimeSeconds) * 1000000000.0);
  26.  
  27. targetTime.tv_sec = currentTimeSeconds + intervalSeconds;
  28. targetTime.tv_nsec = currentTimeNanoseconds + intervalMicroseconds * 1000.0;
  29. #endif
  30.  
  31. if (targetTime.tv_nsec > 1000000000) {
  32. targetTime.tv_nsec -= 1000000000;
  33. targetTime.tv_sec++;
  34. }
  35.  
  36. return pthread_cond_timedwait(&m_condition, &mutex.impl(), &targetTime) == 0;
  37. }
Add Comment
Please, Sign In to add comment