diff options
author | 3gg <3gg@shellblade.net> | 2025-07-08 08:39:27 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2025-07-08 08:39:27 -0700 |
commit | cc945cad053cd36d67ee5daf50a71e212ce2cfe3 (patch) | |
tree | 1ffa2aa11d43528bfe439be0113777fd67d0317d | |
parent | f99204184c8b96f499f6e7efbffb8b6b4ea8c93f (diff) |
-rw-r--r-- | timer/include/timer.h | 5 | ||||
-rw-r--r-- | timer/src/timer.c | 15 |
2 files changed, 17 insertions, 3 deletions
diff --git a/timer/include/timer.h b/timer/include/timer.h index 94781d6..6dc87d9 100644 --- a/timer/include/timer.h +++ b/timer/include/timer.h | |||
@@ -20,7 +20,7 @@ typedef struct timespec time_point; | |||
20 | typedef uint64_t time_delta; | 20 | typedef uint64_t time_delta; |
21 | 21 | ||
22 | /// A high resolution timer. | 22 | /// A high resolution timer. |
23 | typedef struct { | 23 | typedef struct Timer { |
24 | time_point start_time; // The instant the timer was last started. | 24 | time_point start_time; // The instant the timer was last started. |
25 | time_point last_tick; // The instant the timer was last ticked. | 25 | time_point last_tick; // The instant the timer was last ticked. |
26 | time_delta running_time; // Time elapsed since the timer was last started. | 26 | time_delta running_time; // Time elapsed since the timer was last started. |
@@ -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 | time_point time_add(time_point, time_delta); | ||
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 | ||
diff --git a/timer/src/timer.c b/timer/src/timer.c index 340cd98..d886f59 100644 --- a/timer/src/timer.c +++ b/timer/src/timer.c | |||
@@ -78,7 +78,7 @@ time_delta sec_to_time_delta(double seconds) { | |||
78 | #ifdef _WIN32 | 78 | #ifdef _WIN32 |
79 | return (time_delta)(seconds / seconds_per_count); | 79 | return (time_delta)(seconds / seconds_per_count); |
80 | #else | 80 | #else |
81 | return (time_delta)(seconds * nanoseconds); | 81 | return (time_delta)(seconds * 1.0e9); |
82 | #endif | 82 | #endif |
83 | } | 83 | } |
84 | 84 | ||
@@ -90,9 +90,20 @@ uint64_t time_point_to_ns(time_point t) { | |||
90 | #endif | 90 | #endif |
91 | } | 91 | } |
92 | 92 | ||
93 | time_point time_add(time_point t, time_delta dt) { | ||
94 | time_point out; | ||
95 | #ifdef _WIN32 | ||
96 | out = t + dt; | ||
97 | #else | ||
98 | out.tv_sec = t.tv_sec + (__time_t)(dt / nanoseconds); | ||
99 | out.tv_nsec = t.tv_nsec + (__time_t)(dt % nanoseconds); | ||
100 | #endif | ||
101 | return out; | ||
102 | } | ||
103 | |||
93 | void time_sleep(time_delta dt) { | 104 | void time_sleep(time_delta dt) { |
94 | #ifdef _WIN32 | 105 | #ifdef _WIN32 |
95 | const int64_t ms = dt / microseconds; | 106 | const uint64_t ms = dt / microseconds; |
96 | Sleep((DWORD)(ms)); | 107 | Sleep((DWORD)(ms)); |
97 | #else | 108 | #else |
98 | const uint64_t sec = dt / nanoseconds; | 109 | const uint64_t sec = dt / nanoseconds; |