1 /* timers.c - simple timer routines
3 ** Copyright © 1995,1998,2000,2014 by Jef Poskanzer <jef@mail.acme.com>.
4 ** All rights reserved.
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions
9 ** 1. Redistributions of source code must retain the above copyright
10 ** notice, this list of conditions and the following disclaimer.
11 ** 2. Redistributions in binary form must reproduce the above copyright
12 ** notice, this list of conditions and the following disclaimer in the
13 ** documentation and/or other materials provided with the distribution.
15 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/types.h>
38 static Timer
* timers
[HASH_SIZE
];
39 static Timer
* free_timers
;
40 static int alloc_count
, active_count
, free_count
;
42 ClientData JunkClientData
;
49 /* We can hash on the trigger time, even though it can change over
50 ** the life of a timer via either the periodic bit or the tmr_reset()
51 ** call. This is because both of those guys call l_resort(), which
52 ** recomputes the hash and moves the timer to the appropriate list.
55 (unsigned int) t
->time
.tv_sec
^
56 (unsigned int) t
->time
.tv_usec
) % HASH_SIZE
;
68 if ( t2
== (Timer
*) 0 )
70 /* The list is empty. */
72 t
->prev
= t
->next
= (Timer
*) 0;
76 if ( t
->time
.tv_sec
< t2
->time
.tv_sec
||
77 ( t
->time
.tv_sec
== t2
->time
.tv_sec
&&
78 t
->time
.tv_usec
<= t2
->time
.tv_usec
) )
80 /* The new timer goes at the head of the list. */
88 /* Walk the list to find the insertion point. */
89 for ( t2prev
= t2
, t2
= t2
->next
; t2
!= (Timer
*) 0;
90 t2prev
= t2
, t2
= t2
->next
)
92 if ( t
->time
.tv_sec
< t2
->time
.tv_sec
||
93 ( t
->time
.tv_sec
== t2
->time
.tv_sec
&&
94 t
->time
.tv_usec
<= t2
->time
.tv_usec
) )
104 /* Oops, got to the end of the list. Add to tail. */
107 t
->next
= (Timer
*) 0;
118 if ( t
->prev
== (Timer
*) 0 )
121 t
->prev
->next
= t
->next
;
122 if ( t
->next
!= (Timer
*) 0 )
123 t
->next
->prev
= t
->prev
;
130 /* Remove the timer from its old list. */
132 /* Recompute the hash. */
134 /* And add it back in to its new list, sorted correctly. */
144 for ( h
= 0; h
< HASH_SIZE
; ++h
)
145 timers
[h
] = (Timer
*) 0;
146 free_timers
= (Timer
*) 0;
147 alloc_count
= active_count
= free_count
= 0;
153 struct timeval
* nowP
, TimerProc
* timer_proc
, ClientData client_data
,
154 long msecs
, int periodic
)
158 if ( free_timers
!= (Timer
*) 0 )
161 free_timers
= t
->next
;
166 t
= (Timer
*) malloc( sizeof(Timer
) );
167 if ( t
== (Timer
*) 0 )
172 t
->timer_proc
= timer_proc
;
173 t
->client_data
= client_data
;
175 t
->periodic
= periodic
;
176 if ( nowP
!= (struct timeval
*) 0 )
179 (void) gettimeofday( &t
->time
, (struct timezone
*) 0 );
180 t
->time
.tv_sec
+= msecs
/ 1000L;
181 t
->time
.tv_usec
+= ( msecs
% 1000L ) * 1000L;
182 if ( t
->time
.tv_usec
>= 1000000L )
184 t
->time
.tv_sec
+= t
->time
.tv_usec
/ 1000000L;
185 t
->time
.tv_usec
%= 1000000L;
188 /* Add the new timer to the proper active list. */
197 tmr_timeout( struct timeval
* nowP
)
200 static struct timeval timeout
;
202 msecs
= tmr_mstimeout( nowP
);
203 if ( msecs
== INFTIM
)
204 return (struct timeval
*) 0;
205 timeout
.tv_sec
= msecs
/ 1000L;
206 timeout
.tv_usec
= ( msecs
% 1000L ) * 1000L;
212 tmr_mstimeout( struct timeval
* nowP
)
220 msecs
= 0; /* make lint happy */
221 /* Since the lists are sorted, we only need to look at the
222 ** first timer on each one.
224 for ( h
= 0; h
< HASH_SIZE
; ++h
)
227 if ( t
!= (Timer
*) 0 )
229 m
= ( t
->time
.tv_sec
- nowP
->tv_sec
) * 1000L +
230 ( t
->time
.tv_usec
- nowP
->tv_usec
) / 1000L;
236 else if ( m
< msecs
)
249 tmr_run( struct timeval
* nowP
)
255 for ( h
= 0; h
< HASH_SIZE
; ++h
)
256 for ( t
= timers
[h
]; t
!= (Timer
*) 0; t
= next
)
259 /* Since the lists are sorted, as soon as we find a timer
260 ** that isn't ready yet, we can go on to the next list.
262 if ( t
->time
.tv_sec
> nowP
->tv_sec
||
263 ( t
->time
.tv_sec
== nowP
->tv_sec
&&
264 t
->time
.tv_usec
> nowP
->tv_usec
) )
266 (t
->timer_proc
)( t
->client_data
, nowP
);
270 t
->time
.tv_sec
+= t
->msecs
/ 1000L;
271 t
->time
.tv_usec
+= ( t
->msecs
% 1000L ) * 1000L;
272 if ( t
->time
.tv_usec
>= 1000000L )
274 t
->time
.tv_sec
+= t
->time
.tv_usec
/ 1000000L;
275 t
->time
.tv_usec
%= 1000000L;
286 tmr_reset( struct timeval
* nowP
, Timer
* t
)
289 t
->time
.tv_sec
+= t
->msecs
/ 1000L;
290 t
->time
.tv_usec
+= ( t
->msecs
% 1000L ) * 1000L;
291 if ( t
->time
.tv_usec
>= 1000000L )
293 t
->time
.tv_sec
+= t
->time
.tv_usec
/ 1000000L;
294 t
->time
.tv_usec
%= 1000000L;
301 tmr_cancel( Timer
* t
)
303 /* Remove it from its active list. */
306 /* And put it on the free list. */
307 t
->next
= free_timers
;
310 t
->prev
= (Timer
*) 0;
319 while ( free_timers
!= (Timer
*) 0 )
322 free_timers
= t
->next
;
335 for ( h
= 0; h
< HASH_SIZE
; ++h
)
336 while ( timers
[h
] != (Timer
*) 0 )
337 tmr_cancel( timers
[h
] );
342 /* Generate debugging statistics syslog message. */
344 tmr_logstats( long secs
)
347 LOG_NOTICE
, " timers - %d allocated, %d active, %d free",
348 alloc_count
, active_count
, free_count
);
349 if ( active_count
+ free_count
!= alloc_count
)
350 syslog( LOG_ERR
, "timer counts don't add up!" );