diff options
Diffstat (limited to 'Spear/Sys/Timer')
| -rw-r--r-- | Spear/Sys/Timer/timer.c | 19 | ||||
| -rw-r--r-- | Spear/Sys/Timer/timer.h | 3 |
2 files changed, 17 insertions, 5 deletions
diff --git a/Spear/Sys/Timer/timer.c b/Spear/Sys/Timer/timer.c index 8487f48..55e64b5 100644 --- a/Spear/Sys/Timer/timer.c +++ b/Spear/Sys/Timer/timer.c | |||
| @@ -48,7 +48,7 @@ void time_now(time_point* t) { | |||
| 48 | #ifdef _WIN32 | 48 | #ifdef _WIN32 |
| 49 | QueryPerformanceCounter((LARGE_INTEGER*)t); | 49 | QueryPerformanceCounter((LARGE_INTEGER*)t); |
| 50 | #else | 50 | #else |
| 51 | clock_gettime(CLOCK_REALTIME, t); | 51 | clock_gettime(CLOCK_MONOTONIC_RAW, t); |
| 52 | #endif | 52 | #endif |
| 53 | } | 53 | } |
| 54 | 54 | ||
| @@ -59,7 +59,7 @@ time_delta time_diff(time_point* start, time_point* end) { | |||
| 59 | // another processor, then the delta time can be negative. | 59 | // another processor, then the delta time can be negative. |
| 60 | return std::max(0, *end - *start); | 60 | return std::max(0, *end - *start); |
| 61 | #else | 61 | #else |
| 62 | return (end->tv_sec - start->tv_sec) * 1e9 + (end->tv_nsec - start->tv_nsec); | 62 | return (end->tv_sec - start->tv_sec) * nanoseconds + (end->tv_nsec - start->tv_nsec); |
| 63 | #endif | 63 | #endif |
| 64 | } | 64 | } |
| 65 | 65 | ||
| @@ -83,16 +83,25 @@ uint64_t time_point_to_ns(time_point* t) { | |||
| 83 | #ifdef _WIN32 | 83 | #ifdef _WIN32 |
| 84 | return (uint64_t)((double)*t * seconds_per_count * 1.0e+9); | 84 | return (uint64_t)((double)*t * seconds_per_count * 1.0e+9); |
| 85 | #else | 85 | #else |
| 86 | return (uint64_t)t->tv_sec * 1e+9 + (uint64_t)t->tv_nsec; | 86 | return (uint64_t)t->tv_sec * nanoseconds + (uint64_t)t->tv_nsec; |
| 87 | #endif | ||
| 88 | } | ||
| 89 | |||
| 90 | void time_add(const time_point* t, time_delta dt, time_point* out) { | ||
| 91 | #ifdef _WIN32 | ||
| 92 | *out = *t + dt; | ||
| 93 | #else | ||
| 94 | out->tv_sec = t->tv_sec + (__time_t)(dt / nanoseconds); | ||
| 95 | out->tv_nsec = t->tv_nsec + (__time_t)(dt % nanoseconds); | ||
| 87 | #endif | 96 | #endif |
| 88 | } | 97 | } |
| 89 | 98 | ||
| 90 | void time_sleep(time_delta dt) { | 99 | void time_sleep(time_delta dt) { |
| 91 | #ifdef _WIN32 | 100 | #ifdef _WIN32 |
| 92 | const int64_t ms = dt / microseconds; | 101 | const uint64_t ms = dt / microseconds; |
| 93 | Sleep((DWORD)(ms)); | 102 | Sleep((DWORD)(ms)); |
| 94 | #else | 103 | #else |
| 95 | const int64_t sec = dt / nanoseconds; | 104 | const uint64_t sec = dt / nanoseconds; |
| 96 | struct timespec ts; | 105 | struct timespec ts; |
| 97 | ts.tv_sec = (long)sec; | 106 | ts.tv_sec = (long)sec; |
| 98 | ts.tv_nsec = (long)(dt % nanoseconds); | 107 | ts.tv_nsec = (long)(dt % nanoseconds); |
diff --git a/Spear/Sys/Timer/timer.h b/Spear/Sys/Timer/timer.h index e426135..da4e7c7 100644 --- a/Spear/Sys/Timer/timer.h +++ b/Spear/Sys/Timer/timer.h | |||
| @@ -53,6 +53,9 @@ time_delta sec_to_time_delta(double seconds); | |||
| 53 | /// Convert the time point to nanoseconds. | 53 | /// Convert the time point to nanoseconds. |
| 54 | uint64_t time_point_to_ns(time_point*); | 54 | uint64_t time_point_to_ns(time_point*); |
| 55 | 55 | ||
| 56 | /// Add a time delta to a timestamp. | ||
| 57 | void time_add(const time_point*, time_delta, time_point* out); | ||
| 58 | |||
| 56 | /// Put the caller thread to sleep for the given amount of time. | 59 | /// Put the caller thread to sleep for the given amount of time. |
| 57 | void time_sleep(time_delta dt); | 60 | void time_sleep(time_delta dt); |
| 58 | 61 | ||
