summaryrefslogtreecommitdiff
path: root/src/Fl_sleep.cxx
diff options
context:
space:
mode:
authorFabien Costantini <fabien@onepost.net>2014-05-21 04:10:12 +0000
committerFabien Costantini <fabien@onepost.net>2014-05-21 04:10:12 +0000
commit54b0123eab05295c4f510692bef48c0ac7618607 (patch)
treebacc68bc8db89fba29363b8e8d0e8bfc57cea4ba /src/Fl_sleep.cxx
parent2c820fd53dad8a753721a9a4ff5d8daced7345ee (diff)
Added a new portable sleep API(ABI compatible). Now features a flexible multi-usage sleep() api accepting decimals, msleep() for millisecs only, usleep() for microsecs. Updated Makefiles, cmakefiles, vs2008, vs2010 with the new Fl_sleep add-on. Documented new API. still needs to be added in Xcode.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10150 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src/Fl_sleep.cxx')
-rw-r--r--src/Fl_sleep.cxx68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/Fl_sleep.cxx b/src/Fl_sleep.cxx
new file mode 100644
index 000000000..43fbec46c
--- /dev/null
+++ b/src/Fl_sleep.cxx
@@ -0,0 +1,68 @@
+//
+// "$Id: Fl_sleep.cxx $"
+//
+// Multi-threading support code for the Fast Light Tool Kit (FLTK).
+//
+// Copyright 1998-2010 by Bill Spitzak and others.
+//
+// This library is free software. Distribution and use rights are outlined in
+// the file "COPYING" which should have been included with this file. If this
+// file is missing or damaged, see the license at:
+//
+// http://www.fltk.org/COPYING.php
+//
+// Please report all bugs and problems on the following page:
+//
+// http://www.fltk.org/str.php
+//
+
+// Cross platform sleep API for FLTK, F. Costantini, May 20th, 2014
+
+#include <FL/Fl.H>
+#include <config.h>
+
+#include <stdlib.h>
+
+#ifdef WIN32
+#include <windows.h>
+#else
+#include <unistd.h>
+#endif
+
+/** Make the current thread sleep for n seconds, support decimals ... */
+void Fl::sleep(double seconds)
+{
+ Fl::usleep((unsigned long long) (seconds*1000000));
+}
+
+/** Make the current thread to sleep for n milliseconds */
+void Fl::msleep(unsigned long milliseconds)
+{
+#ifdef WIN32
+ ::Sleep( (DWORD) milliseconds);
+#else
+ ::usleep((useconds_t) (milliseconds*1000));
+#endif
+}
+
+/** Make the current thread to sleep for n microseconds */
+void Fl::usleep(unsigned long long microseconds)
+// unsigned long long more should be more portable than int64_t before c++ 2011 ...
+{
+#ifdef WIN32
+ HANDLE timer;
+ LARGE_INTEGER reltime;
+
+ reltime.QuadPart = (LONGLONG) -(10*microseconds); // Convert to 100 nanosecond relative time interval
+ timer = CreateWaitableTimer(NULL, TRUE, NULL);
+ SetWaitableTimer(timer, &reltime, 0, NULL, NULL, 0);
+ WaitForSingleObject(timer, INFINITE);
+ CloseHandle(timer);
+#else
+ ::usleep((useconds_t) microseconds);
+#endif
+}
+
+//
+// End of "$Id: $".
+//