Jonathan Peyton | 614c7ef | 2015-09-21 20:41:31 +0000 | [diff] [blame] | 1 | #ifndef MY_SLEEP_H |
| 2 | #define MY_SLEEP_H |
| 3 | |
| 4 | /*! Utility function to have a sleep function with better resolution and |
| 5 | * which only stops one thread. */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <errno.h> |
| 10 | #include <time.h> |
| 11 | |
| 12 | #if defined(_WIN32) |
| 13 | # include <windows.h> |
| 14 | // Windows version of my_sleep() function |
| 15 | static void my_sleep(double sleeptime) { |
| 16 | DWORD ms = (DWORD) (sleeptime * 1000.0); |
| 17 | Sleep(ms); |
| 18 | } |
| 19 | |
| 20 | |
| 21 | #else // _WIN32 |
| 22 | |
| 23 | // Unices version of my_sleep() function |
| 24 | static void my_sleep(double sleeptime) { |
| 25 | struct timespec ts; |
| 26 | ts.tv_sec = (time_t)sleeptime; |
| 27 | ts.tv_nsec = (long)((sleeptime - (double)ts.tv_sec) * 1E9); |
| 28 | nanosleep(&ts, NULL); |
| 29 | } |
| 30 | |
| 31 | #endif // _WIN32 |
| 32 | |
| 33 | #endif // MY_SLEEP_H |