summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/common/BoxTime.cpp50
-rw-r--r--lib/common/BoxTime.h8
-rw-r--r--lib/common/Test.cpp33
3 files changed, 57 insertions, 34 deletions
diff --git a/lib/common/BoxTime.cpp b/lib/common/BoxTime.cpp
index d05c0a6c..298c3910 100644
--- a/lib/common/BoxTime.cpp
+++ b/lib/common/BoxTime.cpp
@@ -94,3 +94,53 @@ std::string FormatTime(box_time_t time, bool includeDate, bool showMicros)
return buf.str();
}
+// --------------------------------------------------------------------------
+//
+// 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 = (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 / 1000000000) << " secs remaining, "
+ "sleeping again");
+ }
+#endif
+}
+
diff --git a/lib/common/BoxTime.h b/lib/common/BoxTime.h
index 6681bbbd..a56f39f1 100644
--- a/lib/common/BoxTime.h
+++ b/lib/common/BoxTime.h
@@ -18,8 +18,8 @@ typedef uint64_t box_time_t;
#define NANO_SEC_IN_USEC_LL (1000LL)
#define MICRO_SEC_IN_SEC (1000000)
#define MICRO_SEC_IN_SEC_LL (1000000LL)
-#define MILLI_SEC_IN_NANO_SEC (1000)
-#define MILLI_SEC_IN_NANO_SEC_LL (1000LL)
+#define MILLI_SEC_IN_SEC (1000)
+#define MILLI_SEC_IN_SEC_LL (1000LL)
box_time_t GetCurrentBoxTime();
@@ -33,7 +33,7 @@ inline time_t BoxTimeToSeconds(box_time_t Time)
}
inline uint64_t BoxTimeToMilliSeconds(box_time_t Time)
{
- return Time / MILLI_SEC_IN_NANO_SEC_LL;
+ return Time / MILLI_SEC_IN_SEC_LL;
}
inline uint64_t BoxTimeToMicroSeconds(box_time_t Time)
{
@@ -43,4 +43,6 @@ inline uint64_t BoxTimeToMicroSeconds(box_time_t Time)
std::string FormatTime(box_time_t time, bool includeDate,
bool showMicros = false);
+void ShortSleep(box_time_t duration, bool logDuration);
+
#endif // BOXTIME__H
diff --git a/lib/common/Test.cpp b/lib/common/Test.cpp
index 6d685918..de87c465 100644
--- a/lib/common/Test.cpp
+++ b/lib/common/Test.cpp
@@ -21,6 +21,7 @@
#include <unistd.h>
#endif
+#include "BoxTime.h"
#include "Test.h"
bool TestFileExists(const char *Filename)
@@ -451,36 +452,6 @@ void wait_for_operation(int seconds, const char* message)
void safe_sleep(int seconds)
{
- BOX_TRACE("sleeping for " << seconds << " seconds");
-
-#ifdef WIN32
- Sleep(seconds * 1000);
-#else
- struct timespec ts;
- memset(&ts, 0, sizeof(ts));
- ts.tv_sec = seconds;
- ts.tv_nsec = 0;
- 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 = (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 / 1000000000) << " secs remaining, "
- "sleeping again");
- }
-#endif
+ ShortSleep(SecondsToBoxTime(seconds), true);
}