-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
196 lines (152 loc) · 3.69 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*===-- timer.c ----------------------------------------*- SAMPLE -*- C -*-===*/
/**
*** @file
*** @brief Sample Utility
***
*** This is the implementation of the portable accurate timer routines, in the
*** timer.h header. It may change wildly depending on the underlying system.
**/
/*===----------------------------------------------------------------------===*/
#include "utils/timer.h"
#if defined(_WIN32) /* On Windows, use system functions. */
#include <windows.h>
#include <signal.h>
#include <stdio.h>
static HANDLE timer_id;
static volatile sig_atomic_t timer_elapsed;
void CALLBACK timer_handler(void *aux, BOOLEAN unused)
{
timer_elapsed = 1;
}
void timer_init(double seconds)
{
timer_elapsed = 0;
if (!CreateTimerQueueTimer(&timer_id, 0, timer_handler, 0,
(DWORD)(seconds * 1000), 0,
WT_EXECUTEONLYONCE))
{
printf("CreateTimerQueueTimer failed.\n");
exit(EXIT_FAILURE);
}
}
int timer_has_elapsed(void)
{
return timer_elapsed;
}
double timer_now(void)
{
static LARGE_INTEGER freq = {0};
LARGE_INTEGER counter;
if (!freq.QuadPart)
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&counter);
return (double)counter.QuadPart / freq.QuadPart;
}
void timer_free(void)
{
DeleteTimerQueueTimer(0, timer_id, 0);
}
#elif defined(__OpenBSD__)
#include <time.h>
static double timer_delta, timer_start;
void timer_init(double seconds)
{
timer_start = timer_now();
timer_delta = seconds;
}
int timer_has_elapsed(void)
{
return (timer_now() - timer_start) >= timer_delta;
}
double timer_now(void)
{
struct timespec tv;
clock_gettime(CLOCK_MONOTONIC, &tv);
return tv.tv_sec + tv.tv_nsec / 1000000000.0;
}
void timer_free(void)
{
return;
}
#elif defined(__APPLE__)
#include <sys/time.h>
static double timer_delta, timer_start;
void timer_init(double seconds)
{
timer_start = timer_now();
timer_delta = seconds;
}
int timer_has_elapsed(void)
{
return (timer_now() - timer_start) >= timer_delta;
}
double timer_now(void)
{
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + tv.tv_usec / 1000000.0;
}
void timer_free(void)
{
return;
}
#else /* Assume we are on a POSIX 1993 compliant system. */
#define _POSIX_C_SOURCE 1993109L
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
static timer_t timer_id;
static struct sigaction timer_old;
static volatile sig_atomic_t timer_elapsed;
static void timer_handler(int unused)
{
timer_elapsed = 1;
}
void timer_init(double seconds)
{
struct sigevent evp = {0};
struct sigaction sig;
struct itimerspec tm;
tm.it_interval.tv_nsec = (long)(seconds - (long)seconds) * 1000000000;
tm.it_interval.tv_sec = (time_t)seconds;
tm.it_value = tm.it_interval;
timer_elapsed = 0;
sig.sa_handler = timer_handler;
sigemptyset(&sig.sa_mask);
sig.sa_flags = 0;
evp.sigev_value.sival_ptr = &timer_id;
evp.sigev_notify = SIGEV_SIGNAL;
evp.sigev_signo = SIGALRM;
if (timer_create(CLOCK_MONOTONIC, &evp, &timer_id))
{
perror("timer_create");
exit(EXIT_FAILURE);
}
if (timer_settime(timer_id, 0, &tm, 0))
{
perror("timer_settime");
exit(EXIT_FAILURE);
}
if (sigaction(SIGALRM, &sig, &timer_old))
{
perror("sigaction");
exit(EXIT_FAILURE);
}
}
int timer_has_elapsed(void)
{
return timer_elapsed;
}
double timer_now(void)
{
struct timespec tv;
clock_gettime(CLOCK_MONOTONIC, &tv);
return tv.tv_sec + tv.tv_nsec / 1000000000.0;
}
void timer_free(void)
{
sigaction(SIGALRM, &timer_old, 0);
timer_delete(timer_id);
}
#endif