4 * Mark Spencer <markster@marko.net>
6 * Copyright(C) 1999, Adtran, Inc.
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>
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)))
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 */
40 struct sched_context {
41 /* Number of events processed */
44 /* Number of outstanding schedule events */
47 /* Schedule entry and main queue */
50 #ifdef SCHED_MAX_CACHE
51 /* Cache of unused schedule structures and how many */
57 struct sched_context *sched_context_create(void)
59 struct sched_context *tmp;
60 tmp = malloc(sizeof(struct sched_context));
71 void sched_context_destroy(struct sched_context *con)
74 /* Eliminate the cache */
92 static struct sched *sched_alloc(struct sched_context *con)
95 * We keep a small cache of schedule entries
96 * to minimize the number of necessary malloc()'s
99 #ifdef SCHED_MAX_CACHE
102 con->schedc = con->schedc->next;
106 tmp = malloc(sizeof(struct sched));
110 static void sched_release(struct sched_context *con, struct sched *tmp)
113 * Add to the cache, or just free() if we
114 * already have too many cache entries
117 #ifdef SCHED_MAX_CACHE
118 if (con->schedccnt < SCHED_MAX_CACHE) {
119 tmp->next = con->schedc;
127 int ast_sched_wait(struct sched_context *con)
130 * Return the number of milliseconds
131 * until the next scheduled event
135 DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
138 if (gettimeofday(&tv, NULL) < 0) {
139 /* This should never happen */
142 ms = (con->schedq->when.tv_sec - tv.tv_sec) * 1000;
143 ms += (con->schedq->when.tv_usec - tv.tv_usec) / 1000;
151 static void schedule(struct sched_context *con, struct sched *s)
154 * Take a sched structure and put it in the
155 * queue, such that the soonest event is
159 struct sched *last=NULL;
160 struct sched *current=con->schedq;
162 if (SOONER(s->when, current->when))
165 current = current->next;
167 /* Insert this event into the schedule */
176 static inline int sched_settime(struct timeval *tv, int when)
178 if (gettimeofday(tv, NULL) < 0) {
179 /* This shouldn't ever happen, but let's be sure */
180 ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
183 tv->tv_sec += when/1000;
184 tv->tv_usec += (when % 1000) * 1000;
185 if (tv->tv_usec > 1000000) {
187 tv->tv_usec-= 1000000;
192 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, void *data)
195 * Schedule callback(data) to happen when ms into the future
198 DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
200 ast_log(LOG_NOTICE, "Scheduled event in 0 ms?");
203 if ((tmp = sched_alloc(con))) {
204 tmp->id = con->eventcnt++;
205 tmp->callback = callback;
208 if (sched_settime(&tmp->when, when)) {
209 sched_release(con, tmp);
218 int ast_sched_del(struct sched_context *con, int id)
221 * Delete the schedule entry with number
222 * "id". It's nearly impossible that there
223 * would be two or more in the list with that
226 struct sched *last=NULL, *s;
227 DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
232 last->next = s->next;
234 con->schedq = s->next;
241 ast_log(LOG_NOTICE, "Attempted to delete non-existant schedule entry %d!\n", id);
248 void ast_sched_dump(struct sched_context *con)
251 * Dump the contents of the scheduler to
257 gettimeofday(&tv, NULL);
258 ast_log(LOG_DEBUG, "Cheops Schedule Dump (%d in Q, %d Total, %d Cache)\n",
259 con-> schedcnt, con->eventcnt - 1, con->schedccnt);
260 ast_log(LOG_DEBUG, "=================================================\n");
261 ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n");
262 ast_log(LOG_DEBUG, "+-----+-----------+-----------+-----------------+\n");
265 s = q->when.tv_sec - tv.tv_sec;
266 ms = q->when.tv_usec - tv.tv_usec;
271 ast_log(LOG_DEBUG, "|%.4d | %p | %p | %.6ld : %.6ld |\n",
279 ast_log(LOG_DEBUG, "=================================================\n");
283 int ast_sched_runq(struct sched_context *con)
286 * Launch all events which need to be run at this time.
288 struct sched *current;
291 DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
296 if (gettimeofday(&tv, NULL)) {
297 /* This should never happen */
298 ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
301 /* We only care about millisecond accuracy anyway, so this will
302 help us get more than one event at one time if they are very
305 if (SOONER(con->schedq->when, tv)) {
306 current = con->schedq;
307 con->schedq = con->schedq->next;
311 * At this point, the schedule queue is still intact. We
312 * have removed the first event and the rest is still there,
313 * so it's permissible for the callback to add new events, but
314 * trying to delete itself won't work because it isn't in
315 * the schedule queue. If that's what it wants to do, it
318 if (current->callback(current->data)) {
320 * If they return non-zero, we should schedule them to be
323 if (sched_settime(¤t->when, current->resched)) {
324 sched_release(con, current);
326 schedule(con, current);
328 /* No longer needed, so release it */
329 sched_release(con, current);