1 /* $OpenBSD: timer.c,v 1.14 2004/06/14 09:55:42 ho Exp $ */
2 /* $EOM: timer.c,v 1.13 2000/02/20 19:58:42 niklas Exp $ */
5 * Copyright (c) 1998, 1999 Niklas Hallqvist. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * This code was written under funding by Ericsson Radio Systems.
32 #include <sys/queue.h>
41 static TAILQ_HEAD(event_list
, event
) events
;
50 timer_next_event(struct timeval
**timeout
)
54 if (TAILQ_FIRST(&events
)) {
55 gettimeofday(&now
, 0);
56 if (timercmp(&now
, &TAILQ_FIRST(&events
)->expiration
, >=))
59 timersub(&TAILQ_FIRST(&events
)->expiration
, &now
,
66 timer_handle_expirations(void)
71 gettimeofday(&now
, 0);
72 for (n
= TAILQ_FIRST(&events
); n
&& timercmp(&now
, &n
->expiration
, >=);
73 n
= TAILQ_FIRST(&events
)) {
74 LOG_DBG((LOG_TIMER
, 10,
75 "timer_handle_expirations: event %s(%p)", n
->name
,
77 TAILQ_REMOVE(&events
, n
, link
);
84 timer_add_event(char *name
, void (*func
)(void *), void *arg
,
85 struct timeval
*expiration
)
87 struct event
*ev
= (struct event
*) malloc(sizeof *ev
);
96 gettimeofday(&now
, 0);
97 memcpy(&ev
->expiration
, expiration
, sizeof *expiration
);
98 for (n
= TAILQ_FIRST(&events
);
99 n
&& timercmp(expiration
, &n
->expiration
, >=);
100 n
= TAILQ_NEXT(n
, link
))
103 LOG_DBG((LOG_TIMER
, 10,
104 "timer_add_event: event %s(%p) added before %s(%p), "
105 "expiration in %lds", name
,
106 arg
, n
->name
, n
->arg
, expiration
->tv_sec
- now
.tv_sec
));
107 TAILQ_INSERT_BEFORE(n
, ev
, link
);
109 LOG_DBG((LOG_TIMER
, 10, "timer_add_event: event %s(%p) added "
110 "last, expiration in %lds", name
, arg
,
111 expiration
->tv_sec
- now
.tv_sec
));
112 TAILQ_INSERT_TAIL(&events
, ev
, link
);
118 timer_remove_event(struct event
*ev
)
120 LOG_DBG((LOG_TIMER
, 10, "timer_remove_event: removing event %s(%p)",
122 TAILQ_REMOVE(&events
, ev
, link
);
132 gettimeofday(&now
, 0);
134 for (ev
= TAILQ_FIRST(&events
); ev
; ev
= TAILQ_NEXT(ev
, link
))
135 LOG_DBG((LOG_REPORT
, 0,
136 "timer_report: event %s(%p) scheduled in %d seconds",
137 (ev
->name
? ev
->name
: "<unknown>"), ev
,
138 (int) (ev
->expiration
.tv_sec
- now
.tv_sec
)));