[Box Backup-commit] COMMIT r2845 - box/trunk/lib/common

subversion at boxbackup.org subversion at boxbackup.org
Wed Jan 12 00:09:16 GMT 2011


Author: chris
Date: 2011-01-12 00:09:16 +0000 (Wed, 12 Jan 2011)
New Revision: 2845

Modified:
   box/trunk/lib/common/BoxTime.cpp
   box/trunk/lib/common/BoxTime.h
   box/trunk/lib/common/Test.cpp
Log:
Move accurate sleep code from Test.cpp to BoxTime, allow requesting times
in microseconds with ShortSleep(), make safe_sleep() use it.

Rename MILLI_SEC_IN_NANO_SEC to MILLI_SEC_IN_SEC which is what it actually is.


Modified: box/trunk/lib/common/BoxTime.cpp
===================================================================
--- box/trunk/lib/common/BoxTime.cpp	2011-01-12 00:06:50 UTC (rev 2844)
+++ box/trunk/lib/common/BoxTime.cpp	2011-01-12 00:09:16 UTC (rev 2845)
@@ -94,3 +94,53 @@
 	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
+}
+

Modified: box/trunk/lib/common/BoxTime.h
===================================================================
--- box/trunk/lib/common/BoxTime.h	2011-01-12 00:06:50 UTC (rev 2844)
+++ box/trunk/lib/common/BoxTime.h	2011-01-12 00:09:16 UTC (rev 2845)
@@ -18,8 +18,8 @@
 #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 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 @@
 std::string FormatTime(box_time_t time, bool includeDate,
 	bool showMicros = false);
 
+void ShortSleep(box_time_t duration, bool logDuration);
+
 #endif // BOXTIME__H

Modified: box/trunk/lib/common/Test.cpp
===================================================================
--- box/trunk/lib/common/Test.cpp	2011-01-12 00:06:50 UTC (rev 2844)
+++ box/trunk/lib/common/Test.cpp	2011-01-12 00:09:16 UTC (rev 2845)
@@ -21,6 +21,7 @@
 	#include <unistd.h>
 #endif
 
+#include "BoxTime.h"
 #include "Test.h"
 
 bool TestFileExists(const char *Filename)
@@ -451,36 +452,6 @@
 
 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);
 }
 




More information about the Boxbackup-commit mailing list