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