size_t/ssize_t - change to [unsigned] long
[dragonfly.git] / lib / libatm / timer.c
bloba5abc85425990c839d1e62fad179fe78febcf2fb
1 /*
3 * ===================================
4 * HARP | Host ATM Research Platform
5 * ===================================
8 * This Host ATM Research Platform ("HARP") file (the "Software") is
9 * made available by Network Computing Services, Inc. ("NetworkCS")
10 * "AS IS". NetworkCS does not provide maintenance, improvements or
11 * support of any kind.
13 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17 * In no event shall NetworkCS be responsible for any damages, including
18 * but not limited to consequential damages, arising from or relating to
19 * any use of the Software or related support.
21 * Copyright 1994-1998 Network Computing Services, Inc.
23 * Copies of this Software may be made, however, the above copyright
24 * notice must be reproduced on all copies.
26 * @(#) $FreeBSD: src/lib/libatm/timer.c,v 1.3.2.1 2001/09/28 16:52:10 dillon Exp $
27 * @(#) $DragonFly: src/lib/libatm/timer.c,v 1.4 2008/11/10 15:58:20 swildner Exp $
31 #include <sys/cdefs.h>
34 * User Space Library Functions
35 * ----------------------------
37 * Timer functions
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/socket.h>
44 #include <net/if.h>
45 #include <netinet/in.h>
46 #include <netatm/port.h>
47 #include <netatm/queue.h>
48 #include <netatm/atm.h>
49 #include <netatm/atm_if.h>
50 #include <netatm/atm_sap.h>
51 #include <netatm/atm_sys.h>
52 #include <netatm/atm_ioctl.h>
54 #include <errno.h>
55 #include <signal.h>
57 #include "libatm.h"
59 Harp_timer *harp_timer_head;
60 int harp_timer_exec;
64 * Process a HARP timer tick
66 * This function is called via the SIGALRM signal. It increments
67 * harp_timer_exec. The user should check this flag frequently and
68 * call timer_proc when it is set.
70 * Arguments:
71 * None
73 * Returns:
74 * None
77 static void
78 timer_tick(int signo __unused)
81 * Bump the timer flag
83 harp_timer_exec++;
88 * Process HARP timers
90 * This function is called after a SIGALRM signal is posted. It runs
91 * down the list of timer entries, calling the specified functions
92 * for any timers that have expired.
94 * Arguments:
95 * None
97 * Returns:
98 * None
101 void
102 timer_proc(void)
104 Harp_timer *htp;
105 void (*f)();
108 * Reset marks in all timers on the queue
110 for (htp = harp_timer_head; htp; htp = htp->ht_next) {
111 htp->ht_mark = -1;
115 * Run through timer chain decrementing each timer.
116 * If an expired timer is found, take the timer block
117 * off the chain and call the specified function. A
118 * timer's action can result in other timers being
119 * cancelled (taken off the queue), so every time we
120 * call a user function, we start over from the top of
121 * the list.
123 timer_cont:
124 for (htp = harp_timer_head; htp; htp = htp->ht_next) {
126 * Make sure we only process each entry once and
127 * don't process entries that are put on the queue
128 * by user functions we call for this tick
130 if (htp->ht_mark == -1) {
132 * Decrement the timer and mark that we've
133 * processed the entry
135 htp->ht_ticks -= harp_timer_exec;
136 htp->ht_mark = 1;
139 * Check whether the timer is expired
141 if (htp->ht_ticks <= 0) {
143 * Unlink the timer block and call
144 * the user function
146 f = htp->ht_func;
147 UNLINK(htp, Harp_timer, harp_timer_head,
148 ht_next);
149 f(htp);
152 * Start over
154 goto timer_cont;
160 * Reset the timer exec flag
162 harp_timer_exec = 0;
167 * Start the timer
169 * Set up the SIGALRM signal handler and set up the real-time
170 * timer to tick once per second.
172 * Arguments:
173 * None
175 * Returns:
176 * 0 success
177 * errno reason for failure
181 init_timer(void)
183 int rc = 0;
184 struct itimerval timeval;
187 * Clear the timer flag
189 harp_timer_exec = 0;
192 * Set up signal handler
194 if (signal(SIGALRM, timer_tick) == SIG_ERR) {
195 return(errno);
199 * Start timer
201 timeval.it_value.tv_sec = 1;
202 timeval.it_value.tv_usec = 0;
203 timeval.it_interval.tv_sec = 1;
204 timeval.it_interval.tv_usec = 0;
206 if (setitimer(ITIMER_REAL, &timeval,
207 NULL) == -1) {
208 rc = errno;
209 (void)signal(SIGALRM, SIG_DFL);
212 return(rc);
217 * Block timers from firing
219 * Block the SIGALRM signal.
221 * Arguments:
222 * None
224 * Returns:
225 * mask the previous blocked signal mask
229 block_timer(void)
232 * Block the SIGALRM signal
234 return(sigblock(sigmask(SIGALRM)));
239 * Re-enable timers
241 * Restore the signal mask (presumably one that was returned by
242 * block_timer).
244 * Arguments:
245 * mask the signal mask to be restored
247 * Returns:
248 * mask the previous blocked signal mask
251 void
252 enable_timer(int mask)
255 * Set the signal mask
257 sigsetmask(mask);
259 return;