-
Notifications
You must be signed in to change notification settings - Fork 81
/
timers.c
351 lines (305 loc) · 7.75 KB
/
timers.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* timers.c - simple timer routines
**
** Copyright © 1995,1998,2000,2014 by Jef Poskanzer <jef@mail.acme.com>.
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
*/
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include "timers.h"
#define HASH_SIZE 67
static Timer* timers[HASH_SIZE];
static Timer* free_timers;
static int alloc_count, active_count, free_count;
ClientData JunkClientData;
static unsigned int
hash( Timer* t )
{
/* We can hash on the trigger time, even though it can change over
** the life of a timer via either the periodic bit or the tmr_reset()
** call. This is because both of those guys call l_resort(), which
** recomputes the hash and moves the timer to the appropriate list.
*/
return (
(unsigned int) t->time.tv_sec ^
(unsigned int) t->time.tv_usec ) % HASH_SIZE;
}
static void
l_add( Timer* t )
{
int h = t->hash;
Timer* t2;
Timer* t2prev;
t2 = timers[h];
if ( t2 == (Timer*) 0 )
{
/* The list is empty. */
timers[h] = t;
t->prev = t->next = (Timer*) 0;
}
else
{
if ( t->time.tv_sec < t2->time.tv_sec ||
( t->time.tv_sec == t2->time.tv_sec &&
t->time.tv_usec <= t2->time.tv_usec ) )
{
/* The new timer goes at the head of the list. */
timers[h] = t;
t->prev = (Timer*) 0;
t->next = t2;
t2->prev = t;
}
else
{
/* Walk the list to find the insertion point. */
for ( t2prev = t2, t2 = t2->next; t2 != (Timer*) 0;
t2prev = t2, t2 = t2->next )
{
if ( t->time.tv_sec < t2->time.tv_sec ||
( t->time.tv_sec == t2->time.tv_sec &&
t->time.tv_usec <= t2->time.tv_usec ) )
{
/* Found it. */
t2prev->next = t;
t->prev = t2prev;
t->next = t2;
t2->prev = t;
return;
}
}
/* Oops, got to the end of the list. Add to tail. */
t2prev->next = t;
t->prev = t2prev;
t->next = (Timer*) 0;
}
}
}
static void
l_remove( Timer* t )
{
int h = t->hash;
if ( t->prev == (Timer*) 0 )
timers[h] = t->next;
else
t->prev->next = t->next;
if ( t->next != (Timer*) 0 )
t->next->prev = t->prev;
}
static void
l_resort( Timer* t )
{
/* Remove the timer from its old list. */
l_remove( t );
/* Recompute the hash. */
t->hash = hash( t );
/* And add it back in to its new list, sorted correctly. */
l_add( t );
}
void
tmr_init( void )
{
int h;
for ( h = 0; h < HASH_SIZE; ++h )
timers[h] = (Timer*) 0;
free_timers = (Timer*) 0;
alloc_count = active_count = free_count = 0;
}
Timer*
tmr_create(
struct timeval* nowP, TimerProc* timer_proc, ClientData client_data,
long msecs, int periodic )
{
Timer* t;
if ( free_timers != (Timer*) 0 )
{
t = free_timers;
free_timers = t->next;
--free_count;
}
else
{
t = (Timer*) malloc( sizeof(Timer) );
if ( t == (Timer*) 0 )
return (Timer*) 0;
++alloc_count;
}
t->timer_proc = timer_proc;
t->client_data = client_data;
t->msecs = msecs;
t->periodic = periodic;
if ( nowP != (struct timeval*) 0 )
t->time = *nowP;
else
(void) gettimeofday( &t->time, (struct timezone*) 0 );
t->time.tv_sec += msecs / 1000L;
t->time.tv_usec += ( msecs % 1000L ) * 1000L;
if ( t->time.tv_usec >= 1000000L )
{
t->time.tv_sec += t->time.tv_usec / 1000000L;
t->time.tv_usec %= 1000000L;
}
t->hash = hash( t );
/* Add the new timer to the proper active list. */
l_add( t );
++active_count;
return t;
}
struct timeval*
tmr_timeout( struct timeval* nowP )
{
long msecs;
static struct timeval timeout;
msecs = tmr_mstimeout( nowP );
if ( msecs == INFTIM )
return (struct timeval*) 0;
timeout.tv_sec = msecs / 1000L;
timeout.tv_usec = ( msecs % 1000L ) * 1000L;
return &timeout;
}
long
tmr_mstimeout( struct timeval* nowP )
{
int h;
int gotone;
long msecs, m;
Timer* t;
gotone = 0;
msecs = 0; /* make lint happy */
/* Since the lists are sorted, we only need to look at the
** first timer on each one.
*/
for ( h = 0; h < HASH_SIZE; ++h )
{
t = timers[h];
if ( t != (Timer*) 0 )
{
m = ( t->time.tv_sec - nowP->tv_sec ) * 1000L +
( t->time.tv_usec - nowP->tv_usec ) / 1000L;
if ( ! gotone )
{
msecs = m;
gotone = 1;
}
else if ( m < msecs )
msecs = m;
}
}
if ( ! gotone )
return INFTIM;
if ( msecs <= 0 )
msecs = 0;
return msecs;
}
void
tmr_run( struct timeval* nowP )
{
int h;
Timer* t;
Timer* next;
for ( h = 0; h < HASH_SIZE; ++h )
for ( t = timers[h]; t != (Timer*) 0; t = next )
{
next = t->next;
/* Since the lists are sorted, as soon as we find a timer
** that isn't ready yet, we can go on to the next list.
*/
if ( t->time.tv_sec > nowP->tv_sec ||
( t->time.tv_sec == nowP->tv_sec &&
t->time.tv_usec > nowP->tv_usec ) )
break;
(t->timer_proc)( t->client_data, nowP );
if ( t->periodic )
{
/* Reschedule. */
t->time.tv_sec += t->msecs / 1000L;
t->time.tv_usec += ( t->msecs % 1000L ) * 1000L;
if ( t->time.tv_usec >= 1000000L )
{
t->time.tv_sec += t->time.tv_usec / 1000000L;
t->time.tv_usec %= 1000000L;
}
l_resort( t );
}
else
tmr_cancel( t );
}
}
void
tmr_reset( struct timeval* nowP, Timer* t )
{
t->time = *nowP;
t->time.tv_sec += t->msecs / 1000L;
t->time.tv_usec += ( t->msecs % 1000L ) * 1000L;
if ( t->time.tv_usec >= 1000000L )
{
t->time.tv_sec += t->time.tv_usec / 1000000L;
t->time.tv_usec %= 1000000L;
}
l_resort( t );
}
void
tmr_cancel( Timer* t )
{
/* Remove it from its active list. */
l_remove( t );
--active_count;
/* And put it on the free list. */
t->next = free_timers;
free_timers = t;
++free_count;
t->prev = (Timer*) 0;
}
void
tmr_cleanup( void )
{
Timer* t;
while ( free_timers != (Timer*) 0 )
{
t = free_timers;
free_timers = t->next;
--free_count;
free( (void*) t );
--alloc_count;
}
}
void
tmr_term( void )
{
int h;
for ( h = 0; h < HASH_SIZE; ++h )
while ( timers[h] != (Timer*) 0 )
tmr_cancel( timers[h] );
tmr_cleanup();
}
/* Generate debugging statistics syslog message. */
void
tmr_logstats( long secs )
{
syslog(
LOG_NOTICE, " timers - %d allocated, %d active, %d free",
alloc_count, active_count, free_count );
if ( active_count + free_count != alloc_count )
syslog( LOG_ERR, "timer counts don't add up!" );
}