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>
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));
65 #ifdef SCHED_MAX_CACHE
73 void sched_context_destroy(struct sched_context *con)
76 #ifdef SCHED_MAX_CACHE
77 /* Eliminate the cache */
96 static struct sched *sched_alloc(struct sched_context *con)
99 * We keep a small cache of schedule entries
100 * to minimize the number of necessary malloc()'s
103 #ifdef SCHED_MAX_CACHE
106 con->schedc = con->schedc->next;
110 tmp = malloc(sizeof(struct sched));
114 static void sched_release(struct sched_context *con, struct sched *tmp)
117 * Add to the cache, or just free() if we
118 * already have too many cache entries
121 #ifdef SCHED_MAX_CACHE
122 if (con->schedccnt < SCHED_MAX_CACHE) {
123 tmp->next = con->schedc;
131 int ast_sched_wait(struct sched_context *con)
134 * Return the number of milliseconds
135 * until the next scheduled event
139 DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
142 if (gettimeofday(&tv, NULL) < 0) {
143 /* This should never happen */
146 ms = (con->schedq->when.tv_sec - tv.tv_sec) * 1000;
147 ms += (con->schedq->when.tv_usec - tv.tv_usec) / 1000;
155 static void schedule(struct sched_context *con, struct sched *s)
158 * Take a sched structure and put it in the
159 * queue, such that the soonest event is
163 struct sched *last=NULL;
164 struct sched *current=con->schedq;
166 if (SOONER(s->when, current->when))
169 current = current->next;
171 /* Insert this event into the schedule */
180 static inline int sched_settime(struct timeval *tv, int when)
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");
187 tv->tv_sec += when/1000;
188 tv->tv_usec += (when % 1000) * 1000;
189 if (tv->tv_usec > 1000000) {
191 tv->tv_usec-= 1000000;
196 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, void *data)
199 * Schedule callback(data) to happen when ms into the future
202 DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
204 ast_log(LOG_NOTICE, "Scheduled event in 0 ms?");
207 if ((tmp = sched_alloc(con))) {
208 tmp->id = con->eventcnt++;
209 tmp->callback = callback;
212 if (sched_settime(&tmp->when, when)) {
213 sched_release(con, tmp);
222 int ast_sched_del(struct sched_context *con, int id)
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
230 struct sched *last=NULL, *s;
231 DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
236 last->next = s->next;
238 con->schedq = s->next;
245 ast_log(LOG_NOTICE, "Attempted to delete non-existant schedule entry %d!\n", id);
252 void ast_sched_dump(struct sched_context *con)
255 * Dump the contents of the scheduler to
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);
266 ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total)\n",
267 con-> schedcnt, con->eventcnt - 1);
270 ast_log(LOG_DEBUG, "=================================================\n");
271 ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n");
272 ast_log(LOG_DEBUG, "+-----+-----------+-----------+-----------------+\n");
275 s = q->when.tv_sec - tv.tv_sec;
276 ms = q->when.tv_usec - tv.tv_usec;
281 ast_log(LOG_DEBUG, "|%.4d | %p | %p | %.6ld : %.6ld |\n",
289 ast_log(LOG_DEBUG, "=================================================\n");
293 int ast_sched_runq(struct sched_context *con)
296 * Launch all events which need to be run at this time.
298 struct sched *current;
301 DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
306 if (gettimeofday(&tv, NULL)) {
307 /* This should never happen */
308 ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
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
315 if (SOONER(con->schedq->when, tv)) {
316 current = con->schedq;
317 con->schedq = con->schedq->next;
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
328 if (current->callback(current->data)) {
330 * If they return non-zero, we should schedule them to be
333 if (sched_settime(¤t->when, current->resched)) {
334 sched_release(con, current);
336 schedule(con, current);
338 /* No longer needed, so release it */
339 sched_release(con, current);