]> git.ipfire.org Git - thirdparty/squid.git/blob - src/event.cc
LINT
[thirdparty/squid.git] / src / event.cc
1
2 /*
3 * $Id: event.cc,v 1.9 1997/11/05 05:29:23 wessels Exp $
4 *
5 * DEBUG: section 41 Event Processing
6 * AUTHOR: Henrik Nordstrom
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * --------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by
14 * the National Science Foundation.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 */
31
32 #include "squid.h"
33
34 /* The list of event processes */
35 struct ev_entry {
36 EVH *func;
37 void *arg;
38 const char *name;
39 time_t when;
40 struct ev_entry *next;
41 };
42
43 static struct ev_entry *tasks = NULL;
44
45 void
46 eventAdd(const char *name, EVH * func, void *arg, time_t when)
47 {
48 struct ev_entry *event = xcalloc(1, sizeof(struct ev_entry));
49 struct ev_entry **E;
50 event->func = func;
51 event->arg = arg;
52 event->name = name;
53 event->when = squid_curtime + when;
54 debug(41, 7) ("eventAdd: Adding '%s', in %d seconds\n", name, when);
55 /* Insert after the last event with the same or earlier time */
56 for (E = &tasks; *E; E = &(*E)->next) {
57 if ((*E)->when > event->when)
58 break;
59 }
60 event->next = *E;
61 *E = event;
62 }
63
64 void
65 eventDelete(EVH * func, void *arg)
66 {
67 struct ev_entry **E;
68 struct ev_entry *event;
69 for (E = &tasks; (event = *E) != NULL; E = &(*E)->next) {
70 if (event->func != func)
71 continue;
72 if (event->arg != arg)
73 continue;
74 *E = event->next;
75 xfree(event);
76 return;
77 }
78 debug_trap("eventDelete: event not found");
79 }
80
81 void
82 eventRun(void)
83 {
84 struct ev_entry *event = NULL;
85 EVH *func;
86 void *arg;
87 if ((event = tasks) == NULL)
88 return;
89 if (event->when > squid_curtime)
90 return;
91 debug(41, 7) ("eventRun: Running '%s'\n", event->name);
92 func = event->func;
93 arg = event->arg;
94 event->func = NULL;
95 event->arg = NULL;
96 tasks = event->next;
97 safe_free(event);
98 func(arg);
99 }
100
101 time_t
102 eventNextTime(void)
103 {
104 if (!tasks)
105 return (time_t) 10;
106 return tasks->when - squid_curtime;
107 }