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