summaryrefslogtreecommitdiff
path: root/src/Fl_sleep.cxx
blob: fed36fdccdf2084d83b308d8f0a2909257381048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// "$Id$"
//
// Multi-platform sleep functions for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2014 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$".
//