diff options
Diffstat (limited to 'lib/common/BoxTime.cpp')
-rw-r--r-- | lib/common/BoxTime.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/common/BoxTime.cpp b/lib/common/BoxTime.cpp index d05c0a6c..ead3410b 100644 --- a/lib/common/BoxTime.cpp +++ b/lib/common/BoxTime.cpp @@ -94,3 +94,55 @@ std::string FormatTime(box_time_t time, bool includeDate, bool showMicros) return buf.str(); } +<<<<<<< HEAD +======= +// -------------------------------------------------------------------------- +// +// Function +// Name: ShortSleep(box_time_t duration) +// Purpose: Sleeps for the specified duration as accurately +// and efficiently as possible. +// Created: 2011/01/11 +// +// -------------------------------------------------------------------------- + +void ShortSleep(box_time_t duration, bool logDuration) +{ + if(logDuration) + { + BOX_TRACE("Sleeping for " << BoxTimeToMicroSeconds(duration) << + " microseconds"); + } + +#ifdef WIN32 + Sleep(BoxTimeToMilliSeconds(duration)); +#else + struct timespec ts; + memset(&ts, 0, sizeof(ts)); + ts.tv_sec = duration / MICRO_SEC_IN_SEC; + ts.tv_nsec = duration % MICRO_SEC_IN_SEC; + + while (nanosleep(&ts, &ts) == -1 && errno == EINTR) + { + // FIXME evil hack for OSX, where ts.tv_sec contains + // a negative number interpreted as unsigned 32-bit + // when nanosleep() returns later than expected. + + int32_t secs = (int32_t) ts.tv_sec; + int64_t remain_ns = ((int64_t)secs * 1000000000) + ts.tv_nsec; + + if (remain_ns < 0) + { + BOX_WARNING("nanosleep interrupted " << + ((float)(0 - remain_ns) / 1000000000) << + " secs late"); + return; + } + + BOX_TRACE("nanosleep interrupted with " << remain_ns << + " nanosecs remaining, sleeping again"); + } +#endif +} + +>>>>>>> 0.12 |