File: timer.c

package info (click to toggle)
radvd 1:0.7.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 608 kB
  • ctags: 280
  • sloc: sh: 5,816; ansic: 2,801; yacc: 415; lex: 121; makefile: 46
file content (157 lines) | stat: -rw-r--r-- 3,465 bytes parent folder | download
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
/*
 *   $Id: timer.c,v 1.6 2001/11/14 19:58:11 lutchann Exp $
 *
 *   Authors:
 *    Pedro Roque		<roque@di.fc.ul.pt>
 *    Lars Fenneberg		<lf@elemental.net>
 *
 *   This software is Copyright 1996-2000 by the above mentioned author(s), 
 *   All Rights Reserved.
 *
 *   The license which is distributed with this software in the file COPYRIGHT
 *   applies to this software. If your distribution is missing this file, you
 *   may request it from <lutchann@litech.org>.
 *
 */

#include <config.h>
#include <includes.h>
#include <radvd.h>

static struct timer_lst timers_head = {
	{LONG_MAX, LONG_MAX},
	NULL, NULL,
	&timers_head, &timers_head
};

static void alarm_handler(int sig);

static void
schedule_timer(void)
{
	struct timer_lst *tm = timers_head.next;
	struct timeval tv;

	gettimeofday(&tv, NULL);

	if (tm != &timers_head)
	{
		struct itimerval next;
	       
	        memset(&next, 0, sizeof(next));
	       
	        timersub(&tm->expires, &tv, &next.it_value);

		signal(SIGALRM, alarm_handler);

		if ((next.it_value.tv_sec > 0) || 
				((next.it_value.tv_sec == 0) && (next.it_value.tv_usec > 0)))
		{
			dlog(LOG_DEBUG, 4, "calling alarm: %ld secs, %ld usecs", 
					next.it_value.tv_sec, next.it_value.tv_usec);
			setitimer(ITIMER_REAL, &next,  NULL);
		}
		else
		{
			dlog(LOG_DEBUG, 4, "next timer has already expired, queueing signal");	
			kill(getpid(), SIGALRM);
		}
	}
}

void
set_timer(struct timer_lst *tm, double secs)
{
	struct timeval tv;
	struct timer_lst *lst;
	sigset_t bmask, oldmask;
	struct timeval firein;

	dlog(LOG_DEBUG, 3, "setting timer: %.2f secs", secs);

	firein.tv_sec = (long)secs;
	firein.tv_usec = (long)((secs - (double)firein.tv_sec) * 1000000);

	dlog(LOG_DEBUG, 5, "setting timer: %ld secs %ld usecs", firein.tv_sec, firein.tv_usec);

	gettimeofday(&tv, NULL);
	timeradd(&tv, &firein, &tm->expires);

	sigemptyset(&bmask);
	sigaddset(&bmask, SIGALRM);
	sigprocmask(SIG_BLOCK, &bmask, &oldmask);

	lst = &timers_head;

	do {
		lst = lst->next;
	} while ((tm->expires.tv_sec > lst->expires.tv_sec) ||
		 ((tm->expires.tv_sec == lst->expires.tv_sec) && 
		  (tm->expires.tv_usec > lst->expires.tv_usec)));

	tm->next = lst;
	tm->prev = lst->prev;
	lst->prev = tm;
	tm->prev->next = tm;

	dlog(LOG_DEBUG, 5, "calling schedule_timer from set_timer context");
	schedule_timer();

	sigprocmask(SIG_SETMASK, &oldmask, NULL);
}

void
clear_timer(struct timer_lst *tm)
{
	sigset_t bmask, oldmask;

	sigemptyset(&bmask);
	sigaddset(&bmask, SIGALRM);
	sigprocmask(SIG_BLOCK, &bmask, &oldmask);
	
	tm->prev->next = tm->next;
	tm->next->prev = tm->prev;
	
	tm->prev = tm->next = NULL;
	
	dlog(LOG_DEBUG, 5, "calling schedule_timer from clear_timer context");
	schedule_timer();

	sigprocmask(SIG_SETMASK, &oldmask, NULL);
}

static void
alarm_handler(int sig)
{
	struct timer_lst *tm, *back;
	struct timeval tv;

	gettimeofday(&tv, NULL);
	tm = timers_head.next;

	while ((tm->expires.tv_sec < tv.tv_sec)
			|| ((tm->expires.tv_sec == tv.tv_sec) 
			    && (tm->expires.tv_usec <= tv.tv_usec)))
	{		
		tm->prev->next = tm->next;
		tm->next->prev = tm->prev;

		back = tm;
		tm = tm->next;
		back->prev = back->next = NULL;

		(*back->handler)(back->data);
	}

	dlog(LOG_DEBUG, 5, "calling schedule_timer from alarm_handler context");
	schedule_timer();
}


void
init_timer(struct timer_lst *tm, void (*handler)(void *), void *data)
{
	memset(tm, 0, sizeof(struct timer_lst));
	tm->handler = handler;
	tm->data = data;
}