]>
Commit | Line | Data |
---|---|---|
ec9c7112 | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
3 | // | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // utilities requiring Posix library functions | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #if !defined(_WIN32) | |
12 | #define _POSIX_C_SOURCE 199309L // need nanosleep() | |
13 | #else | |
14 | #include <windows.h> | |
15 | #endif | |
16 | ||
17 | #include "util_posix.h" | |
18 | #include <stdint.h> | |
19 | #include <time.h> | |
20 | ||
21 | ||
22 | // Timer functions | |
23 | #if !defined (_WIN32) | |
24 | #include <errno.h> | |
25 | ||
26 | static void nsleep(uint64_t n) { | |
27 | struct timespec timeout; | |
28 | timeout.tv_sec = n/1000000000; | |
29 | timeout.tv_nsec = n%1000000000; | |
30 | while (nanosleep(&timeout, &timeout) && errno == EINTR); | |
31 | } | |
32 | ||
33 | void msleep(uint32_t n) { | |
34 | nsleep(1000000 * n); | |
35 | } | |
36 | #endif // _WIN32 | |
37 | ||
38 | // a milliseconds timer for performance measurement | |
39 | uint64_t msclock() { | |
40 | #if defined(_WIN32) | |
41 | #include <sys/types.h> | |
42 | ||
43 | // WORKAROUND FOR MinGW (some versions - use if normal code does not compile) | |
44 | // It has no _ftime_s and needs explicit inclusion of timeb.h | |
45 | #include <sys/timeb.h> | |
46 | struct _timeb t; | |
47 | _ftime(&t); | |
48 | return 1000 * t.time + t.millitm; | |
49 | ||
50 | // NORMAL CODE (use _ftime_s) | |
51 | //struct _timeb t; | |
52 | //if (_ftime_s(&t)) { | |
53 | // return 0; | |
54 | //} else { | |
55 | // return 1000 * t.time + t.millitm; | |
56 | //} | |
57 | #else | |
58 | struct timespec t; | |
59 | clock_gettime(CLOCK_MONOTONIC, &t); | |
60 | return (t.tv_sec * 1000 + t.tv_nsec / 1000000); | |
61 | #endif | |
62 | } | |
63 |