2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
8 * This software is distributed under the terms
9 * of the GPL, which you should have received
10 * along with this source.
12 * Scheduler code for time based functionality
19 #include "scheduler.h"
21 struct schedule_entry
*events
;
23 void init_scheduler (void)
28 struct timeval
*process_schedule (struct timeval
*ptv
)
30 /* Check queue for events which should be
31 executed right now. Execute them, then
32 see how long we should set the next timer
34 struct schedule_entry
*p
= events
;
39 gettimeofday (&now
, NULL
);
41 if (TVLESSEQ (p
->tv
, now
))
43 events
= events
->next
;
44 /* This needs to be executed, as it has expired.
45 It is expected that p->func will free p->data
53 /* When we get here, either there are no more events
54 in the queue, or the remaining events need to happen
55 in the future, so we should schedule another alarm */
58 then
.tv_sec
= events
->tv
.tv_sec
- now
.tv_sec
;
59 then
.tv_usec
= events
->tv
.tv_usec
- now
.tv_usec
;
63 then
.tv_usec
+= 1000000;
65 if ((then
.tv_sec
<= 0) && (then
.tv_usec
<= 0))
67 l2tp_log (LOG_WARNING
, "%s: Whoa... Scheduling for <=0 time???\n",
81 struct schedule_entry
*schedule (struct timeval tv
, void (*func
) (void *),
84 /* Schedule func to be run at relative time tv with data
85 as arguments. If it has already expired, run it
86 immediately. The queue should be in order of
88 struct schedule_entry
*p
= events
, *q
= NULL
;
91 gettimeofday (&tv
, NULL
);
92 tv
.tv_sec
+= diff
.tv_sec
;
93 tv
.tv_usec
+= diff
.tv_usec
;
94 if (tv
.tv_usec
> 1000000)
97 tv
.tv_usec
-= 1000000;
101 if (TVLESS (tv
, p
->tv
))
109 (struct schedule_entry
*) malloc (sizeof (struct schedule_entry
));
114 q
= (struct schedule_entry
*) malloc (sizeof (struct schedule_entry
));
125 inline struct schedule_entry
*aschedule (struct timeval tv
,
126 void (*func
) (void *), void *data
)
128 /* Schedule func to be run at absolute time tv in the future with data
131 gettimeofday (&now
, NULL
);
132 tv
.tv_usec
-= now
.tv_usec
;
135 tv
.tv_usec
+= 1000000;
138 tv
.tv_sec
-= now
.tv_sec
;
139 return schedule (tv
, func
, data
);
142 void deschedule (struct schedule_entry
*s
)
144 struct schedule_entry
*p
= events
, *q
= NULL
;
157 events
= events
->next
;