aboutsummaryrefslogtreecommitdiff
path: root/timer/include/timer.h
diff options
context:
space:
mode:
Diffstat (limited to 'timer/include/timer.h')
-rw-r--r--timer/include/timer.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/timer/include/timer.h b/timer/include/timer.h
new file mode 100644
index 0000000..0274c69
--- /dev/null
+++ b/timer/include/timer.h
@@ -0,0 +1,55 @@
1#pragma once
2
3#include <stdint.h>
4
5/// A particular point in time.
6#ifdef _WIN32
7typedef uint64_t time_point;
8#else
9#include <time.h>
10typedef struct timespec time_point;
11#endif
12
13/// Time elapsed between two time points.
14typedef uint64_t time_delta;
15
16/// A high resolution timer.
17typedef struct {
18 time_point start_time; // The instant the timer was last started.
19 time_point last_tick; // The instant the timer was last ticked.
20 time_delta running_time; // Time elapsed since the timer was last started.
21 time_delta delta_time; // Time elapsed since the last tick.
22} Timer;
23
24/// Construct a new timer.
25Timer timer_make(void);
26
27/// Start the timer.
28/// This sets the time point from which time deltas are measured.
29/// Calling this multilple times resets the timer.
30void timer_start(Timer*);
31
32/// Update the timer's running and delta times.
33void timer_tick(Timer*);
34
35/// Get the current time.
36time_point time_now(void);
37
38/// Return the time elapsed between two timestamps.
39time_delta time_diff(time_point start, time_point end);
40
41/// Return the time elapsed in seconds.
42double time_delta_to_sec(time_delta dt);
43
44/// Convert the time elapsed in seconds to a time delta.
45time_delta sec_to_time_delta(double seconds);
46
47/// Put the caller thread to sleep for the given amount of time.
48void time_sleep(time_delta dt);
49
50/// The time point 0.
51#ifdef _WIN32
52static const time_point time_zero = 0;
53#else
54static const time_point time_zero = {0, 0};
55#endif