lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [5811] Add a cross-platform sleep function


From: Greg Chicares
Subject: [lmi-commits] [5811] Add a cross-platform sleep function
Date: Wed, 30 Oct 2013 13:08:14 +0000

Revision: 5811
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=5811
Author:   chicares
Date:     2013-10-30 13:08:14 +0000 (Wed, 30 Oct 2013)
Log Message:
-----------
Add a cross-platform sleep function

Modified Paths:
--------------
    lmi/trunk/timer.cpp
    lmi/trunk/timer.hpp
    lmi/trunk/timer_test.cpp

Modified: lmi/trunk/timer.cpp
===================================================================
--- lmi/trunk/timer.cpp 2013-10-29 16:56:22 UTC (rev 5810)
+++ lmi/trunk/timer.cpp 2013-10-30 13:08:14 UTC (rev 5811)
@@ -51,6 +51,20 @@
 #   endif // LMI_MS_HEADER_INCLUDED
 #endif // LMI_MSW
 
+/// Suspend execution for a given number of seconds.
+
+#if defined LMI_POSIX
+#   include <unistd.h>                  // sleep()
+void lmi_sleep(unsigned int seconds) {sleep(seconds);}
+#elif defined LMI_MSW
+#   if !defined LMI_MS_HEADER_INCLUDED
+extern "C" void __stdcall Sleep(unsigned int);
+#   endif // !defined LMI_MS_HEADER_INCLUDED
+void lmi_sleep(unsigned int seconds) {Sleep(1000 * seconds);}
+#else // Unknown platform.
+#   error Unknown platform.
+#endif // Unknown platform.
+
 /// Create, calibrate, and start a timer.
 ///
 /// Postcondition: 0 < frequency_. Throws if a positive frequency_

Modified: lmi/trunk/timer.hpp
===================================================================
--- lmi/trunk/timer.hpp 2013-10-29 16:56:22 UTC (rev 5810)
+++ lmi/trunk/timer.hpp 2013-10-30 13:08:14 UTC (rev 5811)
@@ -30,7 +30,7 @@
 #include "uncopyable_lmi.hpp"
 
 #if defined LMI_POSIX
-#   include <sys/time.h> // gettimeofday()
+#   include <sys/time.h>                // gettimeofday()
     typedef double elapsed_t;
 #elif defined LMI_MSW
     // Compilers for this platform use various types for its high-
@@ -57,6 +57,8 @@
 #include <stdexcept>
 #include <string>
 
+void lmi_sleep(unsigned int seconds);
+
 /// Why another timer class?
 ///
 /// Boost provides a timer class, but they deliberately chose to use

Modified: lmi/trunk/timer_test.cpp
===================================================================
--- lmi/trunk/timer_test.cpp    2013-10-29 16:56:22 UTC (rev 5810)
+++ lmi/trunk/timer_test.cpp    2013-10-30 13:08:14 UTC (rev 5811)
@@ -67,6 +67,7 @@
 struct TimerTest
 {
     static void WaitTenMsec();
+    static void SleepOneSec();
     static void TestResolution();
     static void TestExceptions();
     static void TestAliquotTimer();
@@ -79,6 +80,13 @@
     for(;timer.inspect() - timer.time_when_started_ <= limit;) {}
 }
 
+/// Test lmi_sleep().
+
+void TimerTest::SleepOneSec()
+{
+    lmi_sleep(1);
+}
+
 /// Roughly validate accuracy of high-resolution timer.
 ///
 /// Time an interval of about one second with both std::clock() and
@@ -170,6 +178,8 @@
     BOOST_TEST_THROW(TimeAnAliquot(WaitTenMsec,  1.0e-100), 
std::invalid_argument, "");
     BOOST_TEST_THROW(TimeAnAliquot(WaitTenMsec,  0.0     ), 
std::invalid_argument, "");
     BOOST_TEST_THROW(TimeAnAliquot(WaitTenMsec, -1.0     ), 
std::invalid_argument, "");
+
+    std::cout << "  " << TimeAnAliquot(SleepOneSec, 2.000) << '\n';
 }
 
 int test_main(int, char*[])




reply via email to

[Prev in Thread] Current Thread [Next in Thread]