summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/time.c23
-rw-r--r--src/utils/time.h9
2 files changed, 32 insertions, 0 deletions
diff --git a/src/utils/time.c b/src/utils/time.c
new file mode 100644
index 0000000..b6bc960
--- /dev/null
+++ b/src/utils/time.c
@@ -0,0 +1,23 @@
+#include "time.h"
+
+#include <time.h>
+
+int64_t nano_sleep(uint64_t nano_seconds) {
+ struct timespec remaining,
+ request = {nano_seconds / NANO_TO_SECOND, nano_seconds % NANO_TO_SECOND};
+ nanosleep(&request, &remaining);
+ int64_t ret = remaining.tv_sec * NANO_TO_SECOND + remaining.tv_nsec;
+ if (ret < 0 || ret > NANO_TO_SECOND) { // TODO: fix later
+ return 0;
+ } else {
+ return ret;
+ }
+}
+
+int64_t nano_time() {
+ struct timespec t = {0, 0};
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ return t.tv_sec * NANO_TO_SECOND + t.tv_nsec;
+}
+
+
diff --git a/src/utils/time.h b/src/utils/time.h
new file mode 100644
index 0000000..f7e9361
--- /dev/null
+++ b/src/utils/time.h
@@ -0,0 +1,9 @@
+#pragma once
+
+#include <stdint.h>
+
+constexpr int64_t NANO_TO_SECOND = 1000000000;
+
+int64_t nano_sleep(uint64_t nano_seconds);
+
+int64_t nano_time();