Version 0.1.6 from FTP
[asterisk/asterisk.git] / sched.c
1 /*
2  * Asterisk
3  * 
4  * Mark Spencer <markster@marko.net>
5  *
6  * Copyright(C) 1999, Adtran, Inc.
7  * 
8  * Distributed under the terms of the GNU General Public License (GPL) Version 2
9  *
10  * Scheduler Routines (form cheops-NG)
11  *
12  */
13
14 #ifdef DEBUG_SCHEDULER
15 #define DEBUG(a) DEBUG_M(a)
16 #else
17 #define DEBUG(a) 
18 #endif
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/time.h>
23 #include <unistd.h>
24 #include <asterisk/sched.h>
25 #include <asterisk/logger.h>
26
27 /* Determine if a is sooner than b */
28 #define SOONER(a,b) (((b).tv_sec > (a).tv_sec) || \
29                                          (((b).tv_sec == (a).tv_sec) && ((b).tv_usec > (a).tv_usec)))
30
31 struct sched {
32         struct sched *next;                             /* Next event in the list */
33         int id;                                                 /* ID number of event */
34         struct timeval when;                    /* Absolute time event should take place */
35         int resched;                                    /* When to reschedule */
36         void *data;                                     /* Data */
37         ast_sched_cb callback;          /* Callback */
38 };
39
40 struct sched_context {
41         /* Number of events processed */
42         int eventcnt;
43
44         /* Number of outstanding schedule events */
45         int schedcnt;
46
47         /* Schedule entry and main queue */
48         struct sched *schedq;
49
50 #ifdef SCHED_MAX_CACHE
51         /* Cache of unused schedule structures and how many */
52         struct sched *schedc;
53         int schedccnt;
54 #endif
55 };
56
57 struct sched_context *sched_context_create(void)
58 {
59         struct sched_context *tmp;
60         tmp = malloc(sizeof(struct sched_context));
61         if (tmp) {
62                 tmp->eventcnt = 1;
63                 tmp->schedcnt = 0;
64                 tmp->schedq = NULL;
65                 tmp->schedc = NULL;
66                 tmp->schedccnt = 0;
67         }
68         return tmp;
69 }
70
71 void sched_context_destroy(struct sched_context *con)
72 {
73         struct sched *s, *sl;
74         /* Eliminate the cache */
75         s = con->schedc;
76         while(s) {
77                 sl = s;
78                 s = s->next;
79                 free(sl);
80         }
81         /* And the queue */
82         s = con->schedq;
83         while(s) {
84                 sl = s;
85                 s = s->next;
86                 free(sl);
87         }
88         /* And the context */
89         free(con);
90 }
91
92 static struct sched *sched_alloc(struct sched_context *con)
93 {
94         /*
95          * We keep a small cache of schedule entries
96          * to minimize the number of necessary malloc()'s
97          */
98         struct sched *tmp;
99 #ifdef SCHED_MAX_CACHE
100         if (con->schedc) {
101                 tmp = con->schedc;
102                 con->schedc = con->schedc->next;
103                 con->schedccnt--;
104         } else
105 #endif
106                 tmp = malloc(sizeof(struct sched));
107         return tmp;
108 }
109
110 static void sched_release(struct sched_context *con, struct sched *tmp)
111 {
112         /*
113          * Add to the cache, or just free() if we
114          * already have too many cache entries
115          */
116
117 #ifdef SCHED_MAX_CACHE   
118         if (con->schedccnt < SCHED_MAX_CACHE) {
119                 tmp->next = con->schedc;
120                 con->schedc = tmp;
121                 con->schedccnt++;
122         } else
123 #endif
124                 free(tmp);
125 }
126
127 int ast_sched_wait(struct sched_context *con)
128 {
129         /*
130          * Return the number of milliseconds 
131          * until the next scheduled event
132          */
133         struct timeval tv;
134         int ms;
135         DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
136         if (!con->schedq)
137                 return -1;
138         if (gettimeofday(&tv, NULL) < 0) {
139                 /* This should never happen */
140                 return 0;
141         };
142         ms = (con->schedq->when.tv_sec - tv.tv_sec) * 1000;
143         ms += (con->schedq->when.tv_usec - tv.tv_usec) / 1000;
144         if (ms < 0)
145                 ms = 0;
146         return ms;
147         
148 }
149
150
151 static void schedule(struct sched_context *con, struct sched *s)
152 {
153         /*
154          * Take a sched structure and put it in the
155          * queue, such that the soonest event is
156          * first in the list. 
157          */
158          
159         struct sched *last=NULL;
160         struct sched *current=con->schedq;
161         while(current) {
162                 if (SOONER(s->when, current->when))
163                         break;
164                 last = current;
165                 current = current->next;
166         }
167         /* Insert this event into the schedule */
168         s->next = current;
169         if (last) 
170                 last->next = s;
171         else
172                 con->schedq = s;
173         con->schedcnt++;
174 }
175
176 static inline int sched_settime(struct timeval *tv, int when)
177 {
178         if (gettimeofday(tv, NULL) < 0) {
179                         /* This shouldn't ever happen, but let's be sure */
180                         ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
181                         return -1;
182         }
183         tv->tv_sec += when/1000;
184         tv->tv_usec += (when % 1000) * 1000;
185         if (tv->tv_usec > 1000000) {
186                 tv->tv_sec++;
187                 tv->tv_usec-= 1000000;
188         }
189         return 0;
190 }
191
192 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, void *data)
193 {
194         /*
195          * Schedule callback(data) to happen when ms into the future
196          */
197         struct sched *tmp;
198         DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
199         if (!when) {
200                 ast_log(LOG_NOTICE, "Scheduled event in 0 ms?");
201                 return -1;
202         }
203         if ((tmp = sched_alloc(con))) {
204                 tmp->id = con->eventcnt++;
205                 tmp->callback = callback;
206                 tmp->data = data;
207                 tmp->resched = when;
208                 if (sched_settime(&tmp->when, when)) {
209                         sched_release(con, tmp);
210                         return -1;
211                 } else
212                         schedule(con, tmp);
213         } else 
214                 return -1;
215         return tmp->id;
216 }
217
218 int ast_sched_del(struct sched_context *con, int id)
219 {
220         /*
221          * Delete the schedule entry with number
222          * "id".  It's nearly impossible that there
223          * would be two or more in the list with that
224          * id.
225          */
226         struct sched *last=NULL, *s;
227         DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
228         s = con->schedq;
229         while(s) {
230                 if (s->id == id) {
231                         if (last)
232                                 last->next = s->next;
233                         else
234                                 con->schedq = s->next;
235                         con->schedcnt--;
236                         return 0;
237                 }
238                 last = s;
239                 s = s->next;
240         }
241         ast_log(LOG_NOTICE, "Attempted to delete non-existant schedule entry %d!\n", id);
242 #ifdef FORCE_CRASH
243         crash();
244 #endif
245         return -1;
246 }
247
248 void ast_sched_dump(struct sched_context *con)
249 {
250         /*
251          * Dump the contents of the scheduler to
252          * stderr
253          */
254         struct sched *q;
255         struct timeval tv;
256         time_t s, ms;
257         gettimeofday(&tv, NULL);
258         ast_log(LOG_DEBUG, "Cheops Schedule Dump (%d in Q, %d Total, %d Cache)\n", 
259                                                          con-> schedcnt, con->eventcnt - 1, con->schedccnt);
260         ast_log(LOG_DEBUG, "=================================================\n");
261         ast_log(LOG_DEBUG, "|ID    Callback    Data        Time  (sec:ms)   |\n");
262         ast_log(LOG_DEBUG, "+-----+-----------+-----------+-----------------+\n");
263         q = con->schedq;
264         while(q) {
265                 s =  q->when.tv_sec - tv.tv_sec;
266                 ms = q->when.tv_usec - tv.tv_usec;
267                 if (ms < 0) {
268                         ms += 1000000;
269                         s--;
270                 }
271                 ast_log(LOG_DEBUG, "|%.4d | %p | %p | %.6ld : %.6ld |\n", 
272                                 q->id,
273                                 q->callback,
274                                 q->data,
275                                 s,
276                                 ms);
277                 q=q->next;
278         }
279         ast_log(LOG_DEBUG, "=================================================\n");
280         
281 }
282
283 int ast_sched_runq(struct sched_context *con)
284 {
285         /*
286          * Launch all events which need to be run at this time.
287          */
288         struct sched *current;
289         struct timeval tv;
290         int x=0;
291         DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
292                 
293         for(;;) {
294                 if (!con->schedq)
295                         break;
296                 if (gettimeofday(&tv, NULL)) {
297                         /* This should never happen */
298                         ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
299                         return 0;
300                 }
301                 /* We only care about millisecond accuracy anyway, so this will
302                    help us get more than one event at one time if they are very
303                    close together. */
304                 tv.tv_usec += 1000;
305                 if (SOONER(con->schedq->when, tv)) {
306                         current = con->schedq;
307                         con->schedq = con->schedq->next;
308                         con->schedcnt--;
309
310                         /*
311                          * At this point, the schedule queue is still intact.  We
312                          * have removed the first event and the rest is still there,
313                          * so it's permissible for the callback to add new events, but
314                          * trying to delete itself won't work because it isn't in
315                          * the schedule queue.  If that's what it wants to do, it 
316                          * should return 0.
317                          */
318                         if (current->callback(current->data)) {
319                                 /*
320                                  * If they return non-zero, we should schedule them to be
321                                  * run again.
322                                  */
323                                 if (sched_settime(&current->when, current->resched)) {
324                                         sched_release(con, current);
325                                 } else
326                                         schedule(con, current);
327                         } else {
328                                 /* No longer needed, so release it */
329                                 sched_release(con, current);
330                         }
331                         x++;
332                 } else
333                         break;
334         }
335         return x;
336 }