diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-07-08 08:43:42 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-07-09 21:04:50 +0000 |
commit | abc17b0f8997ab07ae66130edd5dc8c43e72c886 (patch) | |
tree | 88056839c808a9f7f8c58f55ebe273aa7b5facdb /toxcore/util.c | |
parent | 4e21c065517d6e125cb1d1b9a13e886b3046b0d8 (diff) |
Factor out time keeping code into its own module: mono_time.c.
It turns out, `unix_time` is also monotonic, and is used as such, so I've
renamed the new functions to `mono_time_*`.
2018-07-08:
```
00:01 <@irungentoo> the idea used to be that the unix_time() function
could go backward in time but I think I might have started using it like
if it could not after I changed it so that it would never go back in time
```
Diffstat (limited to 'toxcore/util.c')
-rw-r--r-- | toxcore/util.c | 63 |
1 files changed, 0 insertions, 63 deletions
diff --git a/toxcore/util.c b/toxcore/util.c index 0bc77c05..5ab092ce 100644 --- a/toxcore/util.c +++ b/toxcore/util.c | |||
@@ -34,75 +34,12 @@ | |||
34 | #include "util.h" | 34 | #include "util.h" |
35 | 35 | ||
36 | #include "crypto_core.h" /* for CRYPTO_PUBLIC_KEY_SIZE */ | 36 | #include "crypto_core.h" /* for CRYPTO_PUBLIC_KEY_SIZE */ |
37 | #include "network.h" /* for current_time_monotonic */ | ||
38 | 37 | ||
39 | #include <stdlib.h> | 38 | #include <stdlib.h> |
40 | #include <string.h> | 39 | #include <string.h> |
41 | #include <time.h> | 40 | #include <time.h> |
42 | 41 | ||
43 | 42 | ||
44 | /* don't call into system billions of times for no reason */ | ||
45 | struct Unix_Time { | ||
46 | uint64_t time; | ||
47 | uint64_t base_time; | ||
48 | }; | ||
49 | |||
50 | Unix_Time *unix_time_new(void) | ||
51 | { | ||
52 | Unix_Time *unixtime = (Unix_Time *)malloc(sizeof(Unix_Time)); | ||
53 | |||
54 | if (unixtime == nullptr) { | ||
55 | return nullptr; | ||
56 | } | ||
57 | |||
58 | unixtime->time = 0; | ||
59 | unixtime->base_time = 0; | ||
60 | |||
61 | return unixtime; | ||
62 | } | ||
63 | |||
64 | void unix_time_free(Unix_Time *unixtime) | ||
65 | { | ||
66 | free(unixtime); | ||
67 | } | ||
68 | |||
69 | void unix_time_update_r(Unix_Time *unixtime) | ||
70 | { | ||
71 | if (unixtime->base_time == 0) { | ||
72 | unixtime->base_time = ((uint64_t)time(nullptr) - (current_time_monotonic() / 1000ULL)); | ||
73 | } | ||
74 | |||
75 | unixtime->time = (current_time_monotonic() / 1000ULL) + unixtime->base_time; | ||
76 | } | ||
77 | |||
78 | uint64_t unix_time_get(const Unix_Time *unixtime) | ||
79 | { | ||
80 | return unixtime->time; | ||
81 | } | ||
82 | |||
83 | int unix_time_is_timeout(const Unix_Time *unixtime, uint64_t timestamp, uint64_t timeout) | ||
84 | { | ||
85 | return timestamp + timeout <= unix_time_get(unixtime); | ||
86 | } | ||
87 | |||
88 | static Unix_Time global_time; | ||
89 | |||
90 | /* XXX: note that this is not thread-safe; if multiple threads call unix_time_update() concurrently, the return value of | ||
91 | * unix_time() may fail to increase monotonically with increasing time */ | ||
92 | void unix_time_update(void) | ||
93 | { | ||
94 | unix_time_update_r(&global_time); | ||
95 | } | ||
96 | uint64_t unix_time(void) | ||
97 | { | ||
98 | return unix_time_get(&global_time); | ||
99 | } | ||
100 | int is_timeout(uint64_t timestamp, uint64_t timeout) | ||
101 | { | ||
102 | return unix_time_is_timeout(&global_time, timestamp, timeout); | ||
103 | } | ||
104 | |||
105 | |||
106 | /* id functions */ | 43 | /* id functions */ |
107 | bool id_equal(const uint8_t *dest, const uint8_t *src) | 44 | bool id_equal(const uint8_t *dest, const uint8_t *src) |
108 | { | 45 | { |