Selaa lähdekoodia

Fixed sleeping function usage on Windows.

Sascha Zelzer 12 vuotta sitten
vanhempi
commit
bf1d4584a2

+ 20 - 2
Libs/CommandLineModules/Backend/FunctionPointer/ctkCmdLineModuleBackendFunctionPointer.cpp

@@ -34,6 +34,7 @@
 #include <QHash>
 #include <QUrl>
 
+
 namespace ctk {
 namespace CmdLineModuleBackendFunctionPointer {
 
@@ -55,12 +56,29 @@ CTK_CMDLINEMODULEBACKENDFP_EXPORT QString GetParameterTypeName<QList<int> >()
 }
 
 //----------------------------------------------------------------------------
+#ifdef Q_OS_WIN
+#include <windows.h>
+void sleep_secs(int secs)
+{
+  Sleep(secs*1000);
+}
+#else
+#include <time.h>
+void sleep_secs(int secs)
+{
+  struct timespec nanostep;
+  nanostep.tv_sec = secs;
+  nanostep.tv_nsec = 0;
+  nanosleep(&nanostep, NULL);
+}
+#endif
+
 void CalculateFibonacciNumbers(int level) //, QList<int>* result)
 {
   qDebug() << "Number: 0";
   if (level > 0)
   {
-    sleep(1);
+    sleep_secs(1);
     qDebug() << "Number: 1";
     if (level == 1) return;
   }
@@ -72,7 +90,7 @@ void CalculateFibonacciNumbers(int level) //, QList<int>* result)
     int tmp = first;
     first = second;
     second = first + tmp;
-    sleep(1);
+    sleep_secs(1);
     qDebug() << "Number:" << second;
   }
 }

+ 22 - 3
Libs/CommandLineModules/Testing/Cpp/ctkCmdLineModuleFutureTest.cpp

@@ -40,6 +40,24 @@
 #include <cstdlib>
 
 
+#ifdef Q_OS_WIN
+#include <windows.h>
+#else
+#include <time.h>
+#endif
+
+void sleep_ms(int ms)
+{
+#ifdef Q_OS_WIN
+  Sleep(ms);
+#else
+  struct timespec nanostep;
+  nanostep.tv_sec = ms / 1000;
+  nanostep.tv_nsec = ((ms % 1000) * 1000.0 * 1000.0);
+  nanosleep(&nanostep, NULL);
+#endif
+}
+
 class ctkCmdLineModuleFrontendMockupFactory : public ctkCmdLineModuleFrontendFactory
 {
 public:
@@ -189,17 +207,18 @@ bool futureTestPauseAndCancel(ctkCmdLineModuleManager* manager, ctkCmdLineModule
     expectedSignals.push_back("module.resumed");
     expectedSignals.push_back("module.paused");
   }
+
   if (future.canCancel())
   {
     expectedSignals.push_back("module.canceled");
   }
   expectedSignals.push_back("module.finished");
 
-  sleep(1);
+  sleep_ms(500);
 
   QCoreApplication::processEvents();
   future.pause();
-  sleep(1);
+  sleep_ms(500);
   QCoreApplication::processEvents();
 
   if (future.canPause())
@@ -218,7 +237,7 @@ bool futureTestPauseAndCancel(ctkCmdLineModuleManager* manager, ctkCmdLineModule
   future.togglePaused();
   QCoreApplication::processEvents();
 
-  sleep(1);
+  sleep_ms(500);
 
   if (future.isPaused() && future.isRunning())
   {

+ 19 - 7
Libs/CommandLineModules/Testing/Modules/TestBed/ctkCmdLineModuleTestBed.cpp

@@ -28,7 +28,24 @@
 #include <QTime>
 
 #include <cstdlib>
+
+#ifdef Q_OS_WIN
+#include <windows.h>
+#else
 #include <time.h>
+#endif
+
+void sleep_ms(int ms)
+{
+#ifdef Q_OS_WIN
+  Sleep(ms);
+#else
+  struct timespec nanostep;
+  nanostep.tv_sec = static_cast<time_t>(ms / 1000);
+  nanostep.tv_nsec = ((ms % 1000) * 1000.0 * 1000.0);
+  nanosleep(&nanostep, NULL);
+#endif
+}
 
 int main(int argc, char* argv[])
 {
@@ -98,8 +115,6 @@ int main(int argc, char* argv[])
   QTime time;
   time.start();
 
-  struct timespec nanostep;
-
   if (!outputs.empty())
   {
     out << "<filter-start>\n";
@@ -132,10 +147,7 @@ int main(int argc, char* argv[])
     }
 
     // simulate some work
-    nanostep.tv_sec = static_cast<time_t>(stepTime);
-    double millisecs = (stepTime - static_cast<time_t>(stepTime)) * 1000.0;
-    nanostep.tv_nsec = static_cast<long>(millisecs * 1000.0 * 1000.0);
-    nanosleep(&nanostep, NULL);
+    sleep_ms(stepTime*1000);
 
     // print the first output
     if (output != "dummy")
@@ -147,7 +159,7 @@ int main(int argc, char* argv[])
   }
 
   // sleep 1 second to avoid squashing the last progress event with the finished event
-  sleep(1);
+  sleep_ms(1000);
 
   if (exitCrash)
   {