4 * Mark Spencer <markster@marko.net>
6 * Copyright(C) Mark Spencer
8 * Distributed under the terms of the GNU General Public License (GPL) Version 2
10 * Scheduler Routines (form cheops-NG)
14 #ifdef DEBUG_SCHEDULER
15 #define DEBUG(a) DEBUG_M(a)
24 #include <asterisk/sched.h>
25 #include <asterisk/logger.h>
26 #include <asterisk/channel.h>
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)))
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 */
41 struct sched_context {
42 /* Number of events processed */
45 /* Number of outstanding schedule events */
48 /* Schedule entry and main queue */
51 #ifdef SCHED_MAX_CACHE
52 /* Cache of unused schedule structures and how many */
58 struct sched_context *sched_context_create(void)
60 struct sched_context *tmp;
61 tmp = malloc(sizeof(struct sched_context));
66 #ifdef SCHED_MAX_CACHE
74 void sched_context_destroy(struct sched_context *con)
77 #ifdef SCHED_MAX_CACHE
78 /* Eliminate the cache */
97 static struct sched *sched_alloc(struct sched_context *con)
100 * We keep a small cache of schedule entries
101 * to minimize the number of necessary malloc()'s
104 #ifdef SCHED_MAX_CACHE
107 con->schedc = con->schedc->next;
111 tmp = malloc(sizeof(struct sched));
115 static void sched_release(struct sched_context *con, struct sched *tmp)
118 * Add to the cache, or just free() if we
119 * already have too many cache entries
122 #ifdef SCHED_MAX_CACHE
123 if (con->schedccnt < SCHED_MAX_CACHE) {
124 tmp->next = con->schedc;
132 int ast_sched_wait(struct sched_context *con)
135 * Return the number of milliseconds
136 * until the next scheduled event
140 DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
143 if (gettimeofday(&tv, NULL) < 0) {
144 /* This should never happen */
147 ms = (con->schedq->when.tv_sec - tv.tv_sec) * 1000;
148 ms += (con->schedq->when.tv_usec - tv.tv_usec) / 1000;
156 static void schedule(struct sched_context *con, struct sched *s)
159 * Take a sched structure and put it in the
160 * queue, such that the soonest event is
164 struct sched *last=NULL;
165 struct sched *current=con->schedq;
167 if (SOONER(s->when, current->when))
170 current = current->next;
172 /* Insert this event into the schedule */
181 static inline int sched_settime(struct timeval *tv, int when)
183 struct timeval tv_tmp;
184 long error_sec, error_usec;
186 if (gettimeofday(&tv_tmp, NULL) < 0) {
187 /* This shouldn't ever happen, but let's be sure */
188 ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
191 /*ast_log(LOG_DEBUG, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
192 if (((unsigned long)(tv->tv_sec) > 0)&&((unsigned long)(tv->tv_usec) > 0)) {
193 if ((unsigned long)(tv_tmp.tv_usec) < (unsigned long)(tv->tv_usec)) {
194 tv_tmp.tv_usec += 1000000;
197 error_sec = (unsigned long)(tv_tmp.tv_sec) - (unsigned long)(tv->tv_sec);
198 error_usec = (unsigned long)(tv_tmp.tv_usec) - (unsigned long)(tv->tv_usec);
200 /*ast_log(LOG_DEBUG, "Initializing error\n");*/
204 /*ast_log(LOG_DEBUG, "ERROR -> %lu,%lu\n", error_sec, error_usec);*/
205 if (error_sec * 1000 + error_usec / 1000 < when) {
206 tv->tv_sec = tv_tmp.tv_sec + (when/1000 - error_sec);
207 tv->tv_usec = tv_tmp.tv_usec + ((when % 1000) * 1000 - error_usec);
209 ast_log(LOG_NOTICE, "Request to schedule in the past?!?!\n");
210 tv->tv_sec = tv_tmp.tv_sec;
211 tv->tv_usec = tv_tmp.tv_usec;
213 if (tv->tv_usec > 1000000) {
215 tv->tv_usec-= 1000000;
220 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, void *data)
223 * Schedule callback(data) to happen when ms into the future
226 DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
228 ast_log(LOG_NOTICE, "Scheduled event in 0 ms?");
231 if ((tmp = sched_alloc(con))) {
232 tmp->id = con->eventcnt++;
233 tmp->callback = callback;
236 tmp->when.tv_sec = 0;
237 tmp->when.tv_usec = 0;
238 if (sched_settime(&tmp->when, when)) {
239 sched_release(con, tmp);
248 int ast_sched_del(struct sched_context *con, int id)
251 * Delete the schedule entry with number
252 * "id". It's nearly impossible that there
253 * would be two or more in the list with that
256 struct sched *last=NULL, *s;
257 DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
262 last->next = s->next;
264 con->schedq = s->next;
266 sched_release(con, s);
272 ast_log(LOG_NOTICE, "Attempted to delete non-existant schedule entry %d!\n", id);
279 void ast_sched_dump(struct sched_context *con)
282 * Dump the contents of the scheduler to
288 gettimeofday(&tv, NULL);
289 #ifdef SCHED_MAX_CACHE
290 ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n",
291 con-> schedcnt, con->eventcnt - 1, con->schedccnt);
293 ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total)\n",
294 con-> schedcnt, con->eventcnt - 1);
297 ast_log(LOG_DEBUG, "=================================================\n");
298 ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n");
299 ast_log(LOG_DEBUG, "+-----+-----------+-----------+-----------------+\n");
302 s = q->when.tv_sec - tv.tv_sec;
303 ms = q->when.tv_usec - tv.tv_usec;
308 ast_log(LOG_DEBUG, "|%.4d | %p | %p | %.6ld : %.6ld |\n",
316 ast_log(LOG_DEBUG, "=================================================\n");
320 int ast_sched_runq(struct sched_context *con)
323 * Launch all events which need to be run at this time.
325 struct sched *current;
328 DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
333 if (gettimeofday(&tv, NULL)) {
334 /* This should never happen */
335 ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
338 /* We only care about millisecond accuracy anyway, so this will
339 help us get more than one event at one time if they are very
342 if (SOONER(con->schedq->when, tv)) {
343 current = con->schedq;
344 con->schedq = con->schedq->next;
348 * At this point, the schedule queue is still intact. We
349 * have removed the first event and the rest is still there,
350 * so it's permissible for the callback to add new events, but
351 * trying to delete itself won't work because it isn't in
352 * the schedule queue. If that's what it wants to do, it
355 if (current->callback(current->data)) {
357 * If they return non-zero, we should schedule them to be
360 if (sched_settime(¤t->when, current->resched)) {
361 sched_release(con, current);
363 schedule(con, current);
365 /* No longer needed, so release it */
366 sched_release(con, current);