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