1 /* SPDX-License-Identifier: GPL-3.0-or-later
2  * Copyright © 2016-2020 The TokTok team.
3  * Copyright © 2014 Tox project.
4  */
5 #ifndef _XOPEN_SOURCE
6 #define _XOPEN_SOURCE 600
7 #endif
8 
9 #if !defined(OS_WIN32) && (defined(_WIN32) || defined(__WIN32__) || defined(WIN32))
10 #define OS_WIN32
11 #define WIN32_LEAN_AND_MEAN
12 #include <windows.h>
13 #endif
14 
15 #ifdef __APPLE__
16 #include <mach/clock.h>
17 #include <mach/mach.h>
18 #endif
19 
20 #ifndef OS_WIN32
21 #include <sys/time.h>
22 #endif
23 
24 #include "mono_time.h"
25 
26 #include <pthread.h>
27 #include <stdlib.h>
28 #include <time.h>
29 
30 #include "ccompat.h"
31 
32 /* don't call into system billions of times for no reason */
33 struct Mono_Time {
34     uint64_t time;
35     uint64_t base_time;
36 #ifdef OS_WIN32
37     /* protect `last_clock_update` and `last_clock_mono` from concurrent access */
38     pthread_mutex_t last_clock_lock;
39     uint32_t last_clock_mono;
40     bool last_clock_update;
41 #endif
42 
43     /* protect `time` from concurrent access */
44     pthread_rwlock_t *time_update_lock;
45 
46     mono_time_current_time_cb *current_time_callback;
47     void *user_data;
48 };
49 
current_time_monotonic_default(Mono_Time * mono_time,void * user_data)50 static uint64_t current_time_monotonic_default(Mono_Time *mono_time, void *user_data)
51 {
52     uint64_t time = 0;
53 #ifdef OS_WIN32
54     /* Must hold mono_time->last_clock_lock here */
55 
56     /* GetTickCount provides only a 32 bit counter, but we can't use
57      * GetTickCount64 for backwards compatibility, so we handle wraparound
58      * ourselves.
59      */
60     uint32_t ticks = GetTickCount();
61 
62     /* the higher 32 bits count the number of wrap arounds */
63     uint64_t old_ovf = mono_time->time & ~((uint64_t)UINT32_MAX);
64 
65     /* Check if time has decreased because of 32 bit wrap from GetTickCount() */
66     if (ticks < mono_time->last_clock_mono) {
67         /* account for overflow */
68         old_ovf += UINT32_MAX + UINT64_C(1);
69     }
70 
71     if (mono_time->last_clock_update) {
72         mono_time->last_clock_mono = ticks;
73         mono_time->last_clock_update = false;
74     }
75 
76     /* splice the low and high bits back together */
77     time = old_ovf + ticks;
78 #else
79     struct timespec clock_mono;
80 #if defined(__APPLE__)
81     clock_serv_t muhclock;
82     mach_timespec_t machtime;
83 
84     host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &muhclock);
85     clock_get_time(muhclock, &machtime);
86     mach_port_deallocate(mach_task_self(), muhclock);
87 
88     clock_mono.tv_sec = machtime.tv_sec;
89     clock_mono.tv_nsec = machtime.tv_nsec;
90 #else
91     clock_gettime(CLOCK_MONOTONIC, &clock_mono);
92 #endif
93     time = 1000ULL * clock_mono.tv_sec + (clock_mono.tv_nsec / 1000000ULL);
94 #endif
95     return time;
96 }
97 
mono_time_new(void)98 Mono_Time *mono_time_new(void)
99 {
100     Mono_Time *mono_time = (Mono_Time *)malloc(sizeof(Mono_Time));
101 
102     if (mono_time == nullptr) {
103         return nullptr;
104     }
105 
106     mono_time->time_update_lock = (pthread_rwlock_t *)malloc(sizeof(pthread_rwlock_t));
107 
108     if (mono_time->time_update_lock == nullptr) {
109         free(mono_time);
110         return nullptr;
111     }
112 
113     if (pthread_rwlock_init(mono_time->time_update_lock, nullptr) < 0) {
114         free(mono_time->time_update_lock);
115         free(mono_time);
116         return nullptr;
117     }
118 
119     mono_time->current_time_callback = current_time_monotonic_default;
120     mono_time->user_data = nullptr;
121 
122 #ifdef OS_WIN32
123 
124     mono_time->last_clock_mono = 0;
125     mono_time->last_clock_update = false;
126 
127     if (pthread_mutex_init(&mono_time->last_clock_lock, nullptr) < 0) {
128         free(mono_time->time_update_lock);
129         free(mono_time);
130         return nullptr;
131     }
132 
133 #endif
134 
135     mono_time->time = 0;
136     mono_time->base_time = (uint64_t)time(nullptr) - (current_time_monotonic(mono_time) / 1000ULL);
137 
138     mono_time_update(mono_time);
139 
140     return mono_time;
141 }
142 
mono_time_free(Mono_Time * mono_time)143 void mono_time_free(Mono_Time *mono_time)
144 {
145 #ifdef OS_WIN32
146     pthread_mutex_destroy(&mono_time->last_clock_lock);
147 #endif
148     pthread_rwlock_destroy(mono_time->time_update_lock);
149     free(mono_time->time_update_lock);
150     free(mono_time);
151 }
152 
mono_time_update(Mono_Time * mono_time)153 void mono_time_update(Mono_Time *mono_time)
154 {
155     uint64_t time = 0;
156 #ifdef OS_WIN32
157     /* we actually want to update the overflow state of mono_time here */
158     pthread_mutex_lock(&mono_time->last_clock_lock);
159     mono_time->last_clock_update = true;
160 #endif
161     time = mono_time->current_time_callback(mono_time, mono_time->user_data) / 1000ULL;
162     time += mono_time->base_time;
163 #ifdef OS_WIN32
164     pthread_mutex_unlock(&mono_time->last_clock_lock);
165 #endif
166 
167     pthread_rwlock_wrlock(mono_time->time_update_lock);
168     mono_time->time = time;
169     pthread_rwlock_unlock(mono_time->time_update_lock);
170 }
171 
mono_time_get(const Mono_Time * mono_time)172 uint64_t mono_time_get(const Mono_Time *mono_time)
173 {
174     uint64_t time = 0;
175     pthread_rwlock_rdlock(mono_time->time_update_lock);
176     time = mono_time->time;
177     pthread_rwlock_unlock(mono_time->time_update_lock);
178     return time;
179 }
180 
mono_time_is_timeout(const Mono_Time * mono_time,uint64_t timestamp,uint64_t timeout)181 bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64_t timeout)
182 {
183     return timestamp + timeout <= mono_time_get(mono_time);
184 }
185 
mono_time_set_current_time_callback(Mono_Time * mono_time,mono_time_current_time_cb * current_time_callback,void * user_data)186 void mono_time_set_current_time_callback(Mono_Time *mono_time,
187         mono_time_current_time_cb *current_time_callback, void *user_data)
188 {
189     if (current_time_callback == nullptr) {
190         mono_time->current_time_callback = current_time_monotonic_default;
191         mono_time->user_data = nullptr;
192     } else {
193         mono_time->current_time_callback = current_time_callback;
194         mono_time->user_data = user_data;
195     }
196 }
197 
198 /* return current monotonic time in milliseconds (ms). */
current_time_monotonic(Mono_Time * mono_time)199 uint64_t current_time_monotonic(Mono_Time *mono_time)
200 {
201     /* For WIN32 we don't want to change overflow state of mono_time here */
202 #ifdef OS_WIN32
203     /* We don't want to update the overflow state of mono_time here,
204      * but must protect against other threads */
205     pthread_mutex_lock(&mono_time->last_clock_lock);
206 #endif
207     uint64_t time = mono_time->current_time_callback(mono_time, mono_time->user_data);
208 #ifdef OS_WIN32
209     pthread_mutex_unlock(&mono_time->last_clock_lock);
210 #endif
211     return time;
212 }
213