summaryrefslogtreecommitdiff
path: root/src/util/timer
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2023-10-10 20:59:07 -0400
committerAndrew Opalach <andrew@akon.city> 2023-10-10 20:59:07 -0400
commita723efcc67b6c14ff0dd1efd1ba88596e959daaa (patch)
tree45384ce34e9601533fdc5bfbbdaf1b63ea7f1310 /src/util/timer
downloadlibnaunet-a723efcc67b6c14ff0dd1efd1ba88596e959daaa.tar.gz
libnaunet-a723efcc67b6c14ff0dd1efd1ba88596e959daaa.tar.bz2
libnaunet-a723efcc67b6c14ff0dd1efd1ba88596e959daaa.zip
Add libakiyo
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/util/timer')
-rw-r--r--src/util/timer/timer.h6
-rw-r--r--src/util/timer/timer_linux.c17
-rw-r--r--src/util/timer/timer_windows.c39
3 files changed, 62 insertions, 0 deletions
diff --git a/src/util/timer/timer.h b/src/util/timer/timer.h
new file mode 100644
index 0000000..2bf6b95
--- /dev/null
+++ b/src/util/timer/timer.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#include <al/types.h>
+
+u64 aki_get_timestamp(void);
+f64 aki_get_tick(void);
diff --git a/src/util/timer/timer_linux.c b/src/util/timer/timer_linux.c
new file mode 100644
index 0000000..d93bfdc
--- /dev/null
+++ b/src/util/timer/timer_linux.c
@@ -0,0 +1,17 @@
+#include <time.h>
+
+#include "timer.h"
+
+u64 aki_get_timestamp(void)
+{
+ struct timespec spec;
+ clock_gettime(CLOCK_REALTIME, &spec);
+ return 1e6 * spec.tv_sec + spec.tv_nsec * 1e-3;
+}
+
+f64 aki_get_tick(void)
+{
+ struct timespec spec;
+ clock_gettime(CLOCK_MONOTONIC, &spec);
+ return spec.tv_sec + spec.tv_nsec * 1e-9;
+}
diff --git a/src/util/timer/timer_windows.c b/src/util/timer/timer_windows.c
new file mode 100644
index 0000000..37a8fd8
--- /dev/null
+++ b/src/util/timer/timer_windows.c
@@ -0,0 +1,39 @@
+#include <al/lib.h>
+
+#include "../../winwrap.h"
+
+#include "timer.h"
+
+f64 aki_get_tick(void)
+{
+ LARGE_INTEGER result;
+ QueryPerformanceCounter(&result);
+ return (result.QuadPart/(f64)performance_frequency.QuadPart);
+}
+
+static SYSTEMTIME unix_epoch = {
+ .wYear = 1970,
+ .wMonth = 1,
+ .wDayOfWeek = 4,
+ .wDay = 1,
+ .wHour = 0,
+ .wMinute = 0,
+ .wSecond = 0,
+ .wMilliseconds = 0
+};
+
+u64 aki_get_timestamp(void)
+{
+ FILETIME result0, result1;
+ SystemTimeToFileTime(&unix_epoch, &result0);
+ ULARGE_INTEGER li0 = {
+ .LowPart = result0.dwLowDateTime,
+ .HighPart = result0.dwHighDateTime
+ };
+ GetSystemTimePreciseAsFileTime(&result1);
+ ULARGE_INTEGER li1 = {
+ .LowPart = result1.dwLowDateTime,
+ .HighPart = result1.dwHighDateTime
+ };
+ return (li1.QuadPart - li0.QuadPart) / 10;
+}