-
Notifications
You must be signed in to change notification settings - Fork 203
/
timer.c
158 lines (140 loc) · 5.45 KB
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright (c) 2015 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <stdint.h>
#include <sys/time.h>
#include <time.h>
#if defined __APPLE__
#include <mach/mach_time.h>
static mach_timebase_info_data_t mill_mtid = {0};
#endif
#include "libmill.h"
#include "timer.h"
#include "utils.h"
/* 1 millisecond expressed in CPU ticks. The value is chosen is such a way that
it works reasonably well for CPU frequencies above 500MHz. On significanly
slower machines you may wish to reconsider. */
#define MILL_CLOCK_PRECISION 1000000
/* Returns current time by querying the operating system. */
static int64_t mill_os_time(void) {
#if defined __APPLE__
if (mill_slow(!mill_mtid.denom))
mach_timebase_info(&mill_mtid);
uint64_t ticks = mach_absolute_time();
return (int64_t)(ticks * mill_mtid.numer / mill_mtid.denom / 1000000);
#elif defined CLOCK_MONOTONIC
struct timespec ts;
int rc = clock_gettime(CLOCK_MONOTONIC, &ts);
mill_assert (rc == 0);
return ((int64_t)ts.tv_sec) * 1000 + (((int64_t)ts.tv_nsec) / 1000000);
#else
struct timeval tv;
int rc = gettimeofday(&tv, NULL);
assert(rc == 0);
return ((int64_t)tv.tv_sec) * 1000 + (((int64_t)tv.tv_usec) / 1000);
#endif
}
int64_t mill_now_(void) {
#if (defined __GNUC__ || defined __clang__) && \
(defined __i386__ || defined __x86_64__)
/* Get the timestamp counter. This is time since startup, expressed in CPU
cycles. Unlike gettimeofday() or similar function, it's extremely fast -
it takes only few CPU cycles to evaluate. */
uint32_t low;
uint32_t high;
__asm__ volatile("rdtsc" : "=a" (low), "=d" (high));
int64_t tsc = (int64_t)((uint64_t)high << 32 | low);
/* These global variables are used to hold the last seen timestamp counter
and last seen time measurement. We'll initilise them the first time
this function is called. */
static int64_t last_tsc = -1;
static int64_t last_now = -1;
if(mill_slow(last_tsc < 0)) {
last_tsc = tsc;
last_now = mill_os_time();
}
/* If TSC haven't jumped back or progressed more than 1/2 ms, we can use
the cached time value. */
if(mill_fast(tsc - last_tsc <= (MILL_CLOCK_PRECISION / 2) &&
tsc >= last_tsc))
return last_now;
/* It's more than 1/2 ms since we've last measured the time.
We'll do a new measurement now. */
last_tsc = tsc;
last_now = mill_os_time();
return last_now;
#else
return mill_os_time();
#endif
}
/* Global linked list of all timers. The list is ordered.
First timer to be resume comes first and so on. */
static struct mill_list mill_timers = {0};
void mill_timer_add(struct mill_timer *timer, int64_t deadline,
mill_timer_callback callback) {
mill_assert(deadline >= 0);
timer->expiry = deadline;
timer->callback = callback;
/* Move the timer into the right place in the ordered list
of existing timers. TODO: This is an O(n) operation! */
struct mill_list_item *it = mill_list_begin(&mill_timers);
while(it) {
struct mill_timer *tm = mill_cont(it, struct mill_timer, item);
/* If multiple timers expire at the same momemt they will be fired
in the order they were created in (> rather than >=). */
if(tm->expiry > timer->expiry)
break;
it = mill_list_next(it);
}
mill_list_insert(&mill_timers, &timer->item, it);
}
void mill_timer_rm(struct mill_timer *timer) {
mill_assert(timer->expiry >= 0);
mill_list_erase(&mill_timers, &timer->item);
timer->expiry = -1;
}
int mill_timer_next(void) {
if(mill_list_empty(&mill_timers))
return -1;
int64_t nw = now();
int64_t expiry = mill_cont(mill_list_begin(&mill_timers),
struct mill_timer, item)->expiry;
return (int) (nw >= expiry ? 0 : expiry - nw);
}
int mill_timer_fire(void) {
/* Avoid getting current time if there are no timers anyway. */
if(mill_list_empty(&mill_timers))
return 0;
int64_t nw = now();
int fired = 0;
while(!mill_list_empty(&mill_timers)) {
struct mill_timer *tm = mill_cont(
mill_list_begin(&mill_timers), struct mill_timer, item);
if(tm->expiry > nw)
break;
mill_list_erase(&mill_timers, mill_list_begin(&mill_timers));
tm->expiry = -1;
if(tm->callback)
tm->callback(tm);
fired = 1;
}
return fired;
}
void mill_timer_postfork(void) {
mill_list_init(&mill_timers);
}