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)
26 #include <asterisk/sched.h>
27 #include <asterisk/logger.h>
28 #include <asterisk/channel.h>
29 #include <asterisk/lock.h>
31 /* Determine if a is sooner than b */
32 #define SOONER(a,b) (((b).tv_sec > (a).tv_sec) || \
33 (((b).tv_sec == (a).tv_sec) && ((b).tv_usec > (a).tv_usec)))
36 struct sched *next; /* Next event in the list */
37 int id; /* ID number of event */
38 struct timeval when; /* Absolute time event should take place */
39 int resched; /* When to reschedule */
40 void *data; /* Data */
41 ast_sched_cb callback; /* Callback */
44 struct sched_context {
46 /* Number of events processed */
49 /* Number of outstanding schedule events */
52 /* Schedule entry and main queue */
55 #ifdef SCHED_MAX_CACHE
56 /* Cache of unused schedule structures and how many */
62 struct sched_context *sched_context_create(void)
64 struct sched_context *tmp;
65 tmp = malloc(sizeof(struct sched_context));
67 memset(tmp, 0, sizeof(struct sched_context));
68 ast_mutex_init(&tmp->lock);
72 #ifdef SCHED_MAX_CACHE
80 void sched_context_destroy(struct sched_context *con)
83 ast_mutex_lock(&con->lock);
84 #ifdef SCHED_MAX_CACHE
85 /* Eliminate the cache */
100 /* And the context */
101 ast_mutex_unlock(&con->lock);
102 ast_mutex_destroy(&con->lock);
106 static struct sched *sched_alloc(struct sched_context *con)
109 * We keep a small cache of schedule entries
110 * to minimize the number of necessary malloc()'s
113 #ifdef SCHED_MAX_CACHE
116 con->schedc = con->schedc->next;
120 tmp = malloc(sizeof(struct sched));
124 static void sched_release(struct sched_context *con, struct sched *tmp)
127 * Add to the cache, or just free() if we
128 * already have too many cache entries
131 #ifdef SCHED_MAX_CACHE
132 if (con->schedccnt < SCHED_MAX_CACHE) {
133 tmp->next = con->schedc;
141 int ast_sched_wait(struct sched_context *con)
144 * Return the number of milliseconds
145 * until the next scheduled event
149 DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
150 ast_mutex_lock(&con->lock);
153 } else if (gettimeofday(&tv, NULL) < 0) {
154 /* This should never happen */
157 ms = (con->schedq->when.tv_sec - tv.tv_sec) * 1000;
158 ms += (con->schedq->when.tv_usec - tv.tv_usec) / 1000;
162 ast_mutex_unlock(&con->lock);
168 static void schedule(struct sched_context *con, struct sched *s)
171 * Take a sched structure and put it in the
172 * queue, such that the soonest event is
176 struct sched *last=NULL;
177 struct sched *current=con->schedq;
179 if (SOONER(s->when, current->when))
182 current = current->next;
184 /* Insert this event into the schedule */
193 static inline int sched_settime(struct timeval *tv, int when)
195 struct timeval tv_tmp;
196 long error_sec, error_usec;
198 if (gettimeofday(&tv_tmp, NULL) < 0) {
199 /* This shouldn't ever happen, but let's be sure */
200 ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
203 /*ast_log(LOG_DEBUG, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
204 if (((unsigned long)(tv->tv_sec) > 0)||((unsigned long)(tv->tv_usec) > 0)) {
205 if ((unsigned long)(tv_tmp.tv_usec) < (unsigned long)(tv->tv_usec)) {
206 tv_tmp.tv_usec += 1000000;
209 error_sec = (unsigned long)(tv_tmp.tv_sec) - (unsigned long)(tv->tv_sec);
210 error_usec = (unsigned long)(tv_tmp.tv_usec) - (unsigned long)(tv->tv_usec);
212 /*ast_log(LOG_DEBUG, "Initializing error\n");*/
216 /*ast_log(LOG_DEBUG, "ERROR -> %lu,%lu\n", error_sec, error_usec);*/
217 if (error_sec * 1000 + error_usec / 1000 < when) {
218 tv->tv_sec = tv_tmp.tv_sec + (when/1000 - error_sec);
219 tv->tv_usec = tv_tmp.tv_usec + ((when % 1000) * 1000 - error_usec);
221 ast_log(LOG_DEBUG, "Request to schedule in the past?!?!\n");
222 tv->tv_sec = tv_tmp.tv_sec;
223 tv->tv_usec = tv_tmp.tv_usec;
225 if (tv->tv_usec > 1000000) {
227 tv->tv_usec-= 1000000;
232 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, void *data)
235 * Schedule callback(data) to happen when ms into the future
239 DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
241 ast_log(LOG_NOTICE, "Scheduled event in 0 ms?\n");
244 ast_mutex_lock(&con->lock);
245 if ((tmp = sched_alloc(con))) {
246 tmp->id = con->eventcnt++;
247 tmp->callback = callback;
250 tmp->when.tv_sec = 0;
251 tmp->when.tv_usec = 0;
252 if (sched_settime(&tmp->when, when)) {
253 sched_release(con, tmp);
259 ast_mutex_unlock(&con->lock);
263 int ast_sched_del(struct sched_context *con, int id)
266 * Delete the schedule entry with number
267 * "id". It's nearly impossible that there
268 * would be two or more in the list with that
271 struct sched *last=NULL, *s;
272 DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
273 ast_mutex_lock(&con->lock);
278 last->next = s->next;
280 con->schedq = s->next;
282 sched_release(con, s);
288 ast_mutex_unlock(&con->lock);
290 ast_log(LOG_NOTICE, "Attempted to delete non-existant schedule entry %d!\n", id);
299 void ast_sched_dump(struct sched_context *con)
302 * Dump the contents of the scheduler to
308 gettimeofday(&tv, NULL);
309 #ifdef SCHED_MAX_CACHE
310 ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n",
311 con-> schedcnt, con->eventcnt - 1, con->schedccnt);
313 ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total)\n",
314 con-> schedcnt, con->eventcnt - 1);
317 ast_log(LOG_DEBUG, "=================================================\n");
318 ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n");
319 ast_log(LOG_DEBUG, "+-----+-----------+-----------+-----------------+\n");
322 s = q->when.tv_sec - tv.tv_sec;
323 ms = q->when.tv_usec - tv.tv_usec;
328 ast_log(LOG_DEBUG, "|%.4d | %p | %p | %.6ld : %.6ld |\n",
336 ast_log(LOG_DEBUG, "=================================================\n");
340 int ast_sched_runq(struct sched_context *con)
343 * Launch all events which need to be run at this time.
345 struct sched *current;
349 DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
351 ast_mutex_lock(&con->lock);
355 if (gettimeofday(&tv, NULL)) {
356 /* This should never happen */
357 ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
360 /* We only care about millisecond accuracy anyway, so this will
361 help us get more than one event at one time if they are very
364 if (SOONER(con->schedq->when, tv)) {
365 current = con->schedq;
366 con->schedq = con->schedq->next;
370 * At this point, the schedule queue is still intact. We
371 * have removed the first event and the rest is still there,
372 * so it's permissible for the callback to add new events, but
373 * trying to delete itself won't work because it isn't in
374 * the schedule queue. If that's what it wants to do, it
378 ast_mutex_unlock(&con->lock);
379 res = current->callback(current->data);
380 ast_mutex_lock(&con->lock);
384 * If they return non-zero, we should schedule them to be
387 if (sched_settime(¤t->when, current->resched)) {
388 sched_release(con, current);
390 schedule(con, current);
392 /* No longer needed, so release it */
393 sched_release(con, current);
399 ast_mutex_unlock(&con->lock);