2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Scheduler Routines (from cheops-NG)
23 * \author Mark Spencer <markster@digium.com>
26 #ifdef DEBUG_SCHEDULER
27 #define DEBUG(a) do { \
43 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
45 #include "asterisk/sched.h"
46 #include "asterisk/logger.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/lock.h"
49 #include "asterisk/utils.h"
50 #include "asterisk/linkedlists.h"
51 #include "asterisk/options.h"
53 /* Determine if a is sooner than b */
54 #define SOONER(a,b) (((b).tv_sec > (a).tv_sec) || \
55 (((b).tv_sec == (a).tv_sec) && ((b).tv_usec > (a).tv_usec)))
58 AST_LIST_ENTRY(sched
) list
;
59 int id
; /*!< ID number of event */
60 struct timeval when
; /*!< Absolute time event should take place */
61 int resched
; /*!< When to reschedule */
62 int variable
; /*!< Use return value from callback to reschedule */
63 void *data
; /*!< Data */
64 ast_sched_cb callback
; /*!< Callback */
67 struct sched_context
{
69 int eventcnt
; /*!< Number of events processed */
70 int schedcnt
; /*!< Number of outstanding schedule events */
71 AST_LIST_HEAD_NOLOCK(, sched
) schedq
; /*!< Schedule entry and main queue */
73 #ifdef SCHED_MAX_CACHE
74 AST_LIST_HEAD_NOLOCK(, sched
) schedc
; /*!< Cache of unused schedule structures and how many */
79 struct sched_context
*sched_context_create(void)
81 struct sched_context
*tmp
;
83 if (!(tmp
= ast_calloc(1, sizeof(*tmp
))))
86 ast_mutex_init(&tmp
->lock
);
92 void sched_context_destroy(struct sched_context
*con
)
96 ast_mutex_lock(&con
->lock
);
98 #ifdef SCHED_MAX_CACHE
99 /* Eliminate the cache */
100 while ((s
= AST_LIST_REMOVE_HEAD(&con
->schedc
, list
)))
105 while ((s
= AST_LIST_REMOVE_HEAD(&con
->schedq
, list
)))
108 /* And the context */
109 ast_mutex_unlock(&con
->lock
);
110 ast_mutex_destroy(&con
->lock
);
114 static struct sched
*sched_alloc(struct sched_context
*con
)
119 * We keep a small cache of schedule entries
120 * to minimize the number of necessary malloc()'s
122 #ifdef SCHED_MAX_CACHE
123 if ((tmp
= AST_LIST_REMOVE_HEAD(&con
->schedc
, list
)))
127 tmp
= ast_calloc(1, sizeof(*tmp
));
132 static void sched_release(struct sched_context
*con
, struct sched
*tmp
)
135 * Add to the cache, or just free() if we
136 * already have too many cache entries
139 #ifdef SCHED_MAX_CACHE
140 if (con
->schedccnt
< SCHED_MAX_CACHE
) {
141 AST_LIST_INSERT_HEAD(&con
->schedc
, tmp
, list
);
149 * Return the number of milliseconds
150 * until the next scheduled event
152 int ast_sched_wait(struct sched_context
*con
)
156 DEBUG(ast_log(LOG_DEBUG
, "ast_sched_wait()\n"));
158 ast_mutex_lock(&con
->lock
);
159 if (AST_LIST_EMPTY(&con
->schedq
)) {
162 ms
= ast_tvdiff_ms(AST_LIST_FIRST(&con
->schedq
)->when
, ast_tvnow());
166 ast_mutex_unlock(&con
->lock
);
173 * Take a sched structure and put it in the
174 * queue, such that the soonest event is
177 static void schedule(struct sched_context
*con
, struct sched
*s
)
180 struct sched
*cur
= NULL
;
182 AST_LIST_TRAVERSE_SAFE_BEGIN(&con
->schedq
, cur
, list
) {
183 if (SOONER(s
->when
, cur
->when
)) {
184 AST_LIST_INSERT_BEFORE_CURRENT(&con
->schedq
, s
, list
);
188 AST_LIST_TRAVERSE_SAFE_END
190 AST_LIST_INSERT_TAIL(&con
->schedq
, s
, list
);
196 * given the last event *tv and the offset in milliseconds 'when',
197 * computes the next value,
199 static int sched_settime(struct timeval
*tv
, int when
)
201 struct timeval now
= ast_tvnow();
203 /*ast_log(LOG_DEBUG, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
204 if (ast_tvzero(*tv
)) /* not supplied, default to now */
206 *tv
= ast_tvadd(*tv
, ast_samp2tv(when
, 1000));
207 if (ast_tvcmp(*tv
, now
) < 0) {
208 ast_log(LOG_DEBUG
, "Request to schedule in the past?!?!\n");
216 * Schedule callback(data) to happen when ms into the future
218 int ast_sched_add_variable(struct sched_context
*con
, int when
, ast_sched_cb callback
, void *data
, int variable
)
222 DEBUG(ast_log(LOG_DEBUG
, "ast_sched_add()\n"));
224 ast_log(LOG_NOTICE
, "Scheduled event in 0 ms?\n");
227 ast_mutex_lock(&con
->lock
);
228 if ((tmp
= sched_alloc(con
))) {
229 tmp
->id
= con
->eventcnt
++;
230 tmp
->callback
= callback
;
233 tmp
->variable
= variable
;
234 tmp
->when
= ast_tv(0, 0);
235 if (sched_settime(&tmp
->when
, when
)) {
236 sched_release(con
, tmp
);
242 #ifdef DUMP_SCHEDULER
243 /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
247 ast_mutex_unlock(&con
->lock
);
251 int ast_sched_add(struct sched_context
*con
, int when
, ast_sched_cb callback
, void *data
)
253 return ast_sched_add_variable(con
, when
, callback
, data
, 0);
257 * Delete the schedule entry with number
258 * "id". It's nearly impossible that there
259 * would be two or more in the list with that
262 int ast_sched_del(struct sched_context
*con
, int id
)
266 DEBUG(ast_log(LOG_DEBUG
, "ast_sched_del()\n"));
268 ast_mutex_lock(&con
->lock
);
269 AST_LIST_TRAVERSE_SAFE_BEGIN(&con
->schedq
, s
, list
) {
271 AST_LIST_REMOVE_CURRENT(&con
->schedq
, list
);
273 sched_release(con
, s
);
277 AST_LIST_TRAVERSE_SAFE_END
279 #ifdef DUMP_SCHEDULER
280 /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
284 ast_mutex_unlock(&con
->lock
);
287 ast_log(LOG_NOTICE
, "Attempted to delete nonexistent schedule entry %d!\n", id
);
297 /*! \brief Dump the contents of the scheduler to LOG_DEBUG */
298 void ast_sched_dump(const struct sched_context
*con
)
301 struct timeval tv
= ast_tvnow();
302 #ifdef SCHED_MAX_CACHE
303 ast_log(LOG_DEBUG
, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n", con
->schedcnt
, con
->eventcnt
- 1, con
->schedccnt
);
305 ast_log(LOG_DEBUG
, "Asterisk Schedule Dump (%d in Q, %d Total)\n", con
->schedcnt
, con
->eventcnt
- 1);
308 ast_log(LOG_DEBUG
, "=============================================================\n");
309 ast_log(LOG_DEBUG
, "|ID Callback Data Time (sec:ms) |\n");
310 ast_log(LOG_DEBUG
, "+-----+-----------------+-----------------+-----------------+\n");
311 AST_LIST_TRAVERSE(&con
->schedq
, q
, list
) {
312 struct timeval delta
= ast_tvsub(q
->when
, tv
);
314 ast_log(LOG_DEBUG
, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n",
319 (long int)delta
.tv_usec
);
321 ast_log(LOG_DEBUG
, "=============================================================\n");
326 * Launch all events which need to be run at this time.
328 int ast_sched_runq(struct sched_context
*con
)
330 struct sched
*current
;
334 DEBUG(ast_log(LOG_DEBUG
, "ast_sched_runq()\n"));
336 ast_mutex_lock(&con
->lock
);
338 if (AST_LIST_EMPTY(&con
->schedq
))
341 /* schedule all events which are going to expire within 1ms.
342 * We only care about millisecond accuracy anyway, so this will
343 * help us get more than one event at one time if they are very
346 tv
= ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
347 if (SOONER(AST_LIST_FIRST(&con
->schedq
)->when
, tv
)) {
348 current
= AST_LIST_REMOVE_HEAD(&con
->schedq
, list
);
352 * At this point, the schedule queue is still intact. We
353 * have removed the first event and the rest is still there,
354 * so it's permissible for the callback to add new events, but
355 * trying to delete itself won't work because it isn't in
356 * the schedule queue. If that's what it wants to do, it
360 ast_mutex_unlock(&con
->lock
);
361 res
= current
->callback(current
->data
);
362 ast_mutex_lock(&con
->lock
);
366 * If they return non-zero, we should schedule them to be
369 if (sched_settime(¤t
->when
, current
->variable
? res
: current
->resched
)) {
370 sched_release(con
, current
);
372 schedule(con
, current
);
374 /* No longer needed, so release it */
375 sched_release(con
, current
);
381 ast_mutex_unlock(&con
->lock
);
385 long ast_sched_when(struct sched_context
*con
,int id
)
389 DEBUG(ast_log(LOG_DEBUG
, "ast_sched_when()\n"));
391 ast_mutex_lock(&con
->lock
);
392 AST_LIST_TRAVERSE(&con
->schedq
, s
, list
) {
398 struct timeval now
= ast_tvnow();
399 secs
= s
->when
.tv_sec
- now
.tv_sec
;
401 ast_mutex_unlock(&con
->lock
);