ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/libio/misc/event.c
Revision: 65
Committed: Mon Oct 3 23:33:16 2005 UTC (20 years, 10 months ago) by adx
Content type: text/x-csrc
File size: 6123 byte(s)
Log Message:
- removed external references from libio/misc
- imported s_misc.c to libio, moved CurrentTime there

File Contents

# User Rev Content
1 adx 61 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * event.c: Event functions.
4     *
5     * Copyright (C) 1998-2000 Regents of the University of California
6     * Copyright (C) 2001-2002 Hybrid Development Team
7     *
8     * Code borrowed from the squid web cache by Adrian Chadd.
9     * Original header:
10     *
11     * DEBUG: section 41 Event Processing
12     * AUTHOR: Henrik Nordstrom
13     *
14     * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
15     * ----------------------------------------------------------
16     *
17     * Squid is the result of efforts by numerous individuals from the
18     * Internet community. Development is led by Duane Wessels of the
19     * National Laboratory for Applied Network Research and funded by the
20     * National Science Foundation. Squid is Copyrighted (C) 1998 by
21     * the Regents of the University of California. Please see the
22     * COPYRIGHT file for full details. Squid incorporates software
23     * developed and/or copyrighted by other sources. Please see the
24     * CREDITS file for full details.
25     *
26     * This program is free software; you can redistribute it and/or modify
27     * it under the terms of the GNU General Public License as published by
28     * the Free Software Foundation; either version 2 of the License, or
29     * (at your option) any later version.
30     *
31     * This program is distributed in the hope that it will be useful,
32     * but WITHOUT ANY WARRANTY; without even the implied warranty of
33     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34     * GNU General Public License for more details.
35     *
36     * You should have received a copy of the GNU General Public License
37     * along with this program; if not, write to the Free Software
38     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
39     * USA
40     *
41     * $Id: event.c 33 2005-10-02 20:50:00Z knight $
42     */
43    
44     /*
45     * How it's used:
46     *
47     * Should be pretty self-explanatory. Events are added to the static
48     * array event_table with a frequency time telling eventRun how often
49     * to execute it.
50     */
51    
52     #include "stdinc.h"
53     #include "event.h"
54     #include "memory.h"
55     #include "s_log.h"
56 adx 65 #include "s_misc.h"
57 adx 61
58 adx 65 const char *last_event_ran = NULL;
59     struct ev_entry event_table[MAX_EVENTS];
60    
61 adx 61 static time_t event_time_min = -1;
62     static int eventFind(EVH *func, void *arg);
63    
64     /*
65     * void eventAdd(const char *name, EVH *func, void *arg, time_t when)
66     *
67     * Input: Name of event, function to call, arguments to pass, and frequency
68     * of the event.
69     * Output: None
70     * Side Effects: Adds the event to the event list.
71     */
72     void
73     eventAdd(const char *name, EVH *func, void *arg, time_t when)
74     {
75     int i;
76    
77     /* find first inactive index, or use next index */
78     for (i = 0; i < MAX_EVENTS; i++)
79     {
80     if (event_table[i].active == 0)
81     {
82     event_table[i].func = func;
83     event_table[i].name = name;
84     event_table[i].arg = arg;
85     event_table[i].when = CurrentTime + when;
86     event_table[i].frequency = when;
87     event_table[i].active = 1;
88    
89     if ((event_table[i].when < event_time_min) || (event_time_min == -1))
90     event_time_min = event_table[i].when;
91    
92     return;
93     }
94     }
95     /* XXX if reach here, its an error */
96     ilog(L_ERROR, "Event table is full! (%d)", i);
97     }
98    
99     /*
100     * void eventDelete(EVH *func, void *arg)
101     *
102     * Input: Function handler, argument that was passed.
103     * Output: None
104     * Side Effects: Removes the event from the event list
105     */
106     void
107     eventDelete(EVH *func, void *arg)
108     {
109     int i = eventFind(func, arg);
110    
111     if (i == -1)
112     return;
113    
114     event_table[i].name = NULL;
115     event_table[i].func = NULL;
116     event_table[i].arg = NULL;
117     event_table[i].active = 0;
118     }
119    
120     /*
121     * void eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_isa)
122     *
123     * Input: Name of event, function to call, arguments to pass, and frequency
124     * of the event.
125     * Output: None
126     * Side Effects: Adds the event to the event list within +- 1/3 of the
127     * specified frequency.
128     */
129     void
130     eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_ish)
131     {
132     if (delta_ish >= 3.0)
133     {
134     const time_t two_third = (2 * delta_ish) / 3;
135     delta_ish = two_third + ((rand() % 1000) * two_third) / 1000;
136     /*
137     * XXX I hate the above magic, I don't even know if its right.
138     * Grr. -- adrian
139     */
140     }
141    
142     eventAdd(name, func, arg, delta_ish);
143     }
144    
145     /*
146     * void eventRun(void)
147     *
148     * Input: None
149     * Output: None
150     * Side Effects: Runs pending events in the event list
151     */
152     void
153     eventRun(void)
154     {
155     int i;
156    
157     for (i = 0; i < MAX_EVENTS; i++)
158     {
159     if (event_table[i].active && (event_table[i].when <= CurrentTime))
160     {
161     last_event_ran = event_table[i].name;
162     event_table[i].func(event_table[i].arg);
163     event_table[i].when = CurrentTime + event_table[i].frequency;
164     event_time_min = -1;
165     }
166     }
167     }
168    
169     /*
170     * time_t eventNextTime(void)
171     *
172     * Input: None
173     * Output: Specifies the next time eventRun() should be run
174     * Side Effects: None
175     */
176     time_t
177     eventNextTime(void)
178     {
179     int i;
180    
181     if (event_time_min == -1)
182     {
183     for (i = 0; i < MAX_EVENTS; i++)
184     {
185     if (event_table[i].active && ((event_table[i].when < event_time_min) || (event_time_min == -1)))
186     event_time_min = event_table[i].when;
187     }
188     }
189    
190 adx 65 return event_time_min;
191 adx 61 }
192    
193     /*
194     * void eventInit(void)
195     *
196     * Input: None
197     * Output: None
198     * Side Effects: Initializes the event system.
199     */
200     void
201     eventInit(void)
202     {
203     last_event_ran = NULL;
204     memset(event_table, 0, sizeof(event_table));
205     }
206    
207     /*
208     * int eventFind(EVH *func, void *arg)
209     *
210     * Input: Event function and the argument passed to it
211     * Output: Index to the slow in the event_table
212     * Side Effects: None
213     */
214     static int
215     eventFind(EVH *func, void *arg)
216     {
217     int i;
218    
219     for (i = 0; i < MAX_EVENTS; i++)
220     {
221     if ((event_table[i].func == func) &&
222     (event_table[i].arg == arg) &&
223     event_table[i].active)
224 adx 65 return i;
225 adx 61 }
226    
227 adx 65 return -1;
228 adx 61 }
229    
230     /*
231     * void set_back_events(time_t by)
232     * Input: Time to set back events by.
233     * Output: None.
234     * Side-effects: Sets back all events by "by" seconds.
235     */
236     void
237     set_back_events(time_t by)
238     {
239     int i;
240    
241     for (i = 0; i < MAX_EVENTS; i++)
242     {
243     if (event_table[i].when > by)
244     event_table[i].when -= by;
245     else
246     event_table[i].when = 0;
247     }
248     }

Properties

Name Value
svn:eol-style native
svn:keywords "Id Revision"