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>
28 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
30 #ifdef DEBUG_SCHEDULER
31 #define DEBUG(a) do { \
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"
54 AST_LIST_ENTRY(sched
) list
;
55 int id
; /*!< ID number of event */
56 struct timeval when
; /*!< Absolute time event should take place */
57 int resched
; /*!< When to reschedule */
58 int variable
; /*!< Use return value from callback to reschedule */
59 const void *data
; /*!< Data */
60 ast_sched_cb callback
; /*!< Callback */
63 struct sched_context
{
65 unsigned int eventcnt
; /*!< Number of events processed */
66 unsigned int schedcnt
; /*!< Number of outstanding schedule events */
67 AST_LIST_HEAD_NOLOCK(, sched
) schedq
; /*!< Schedule entry and main queue */
69 #ifdef SCHED_MAX_CACHE
70 AST_LIST_HEAD_NOLOCK(, sched
) schedc
; /*!< Cache of unused schedule structures and how many */
71 unsigned int schedccnt
;
75 struct sched_context
*sched_context_create(void)
77 struct sched_context
*tmp
;
79 if (!(tmp
= ast_calloc(1, sizeof(*tmp
))))
82 ast_mutex_init(&tmp
->lock
);
88 void sched_context_destroy(struct sched_context
*con
)
92 ast_mutex_lock(&con
->lock
);
94 #ifdef SCHED_MAX_CACHE
95 /* Eliminate the cache */
96 while ((s
= AST_LIST_REMOVE_HEAD(&con
->schedc
, list
)))
101 while ((s
= AST_LIST_REMOVE_HEAD(&con
->schedq
, list
)))
104 /* And the context */
105 ast_mutex_unlock(&con
->lock
);
106 ast_mutex_destroy(&con
->lock
);
110 static struct sched
*sched_alloc(struct sched_context
*con
)
115 * We keep a small cache of schedule entries
116 * to minimize the number of necessary malloc()'s
118 #ifdef SCHED_MAX_CACHE
119 if ((tmp
= AST_LIST_REMOVE_HEAD(&con
->schedc
, list
)))
123 tmp
= ast_calloc(1, sizeof(*tmp
));
128 static void sched_release(struct sched_context
*con
, struct sched
*tmp
)
131 * Add to the cache, or just free() if we
132 * already have too many cache entries
135 #ifdef SCHED_MAX_CACHE
136 if (con
->schedccnt
< SCHED_MAX_CACHE
) {
137 AST_LIST_INSERT_HEAD(&con
->schedc
, tmp
, list
);
145 * Return the number of milliseconds
146 * until the next scheduled event
148 int ast_sched_wait(struct sched_context
*con
)
152 DEBUG(ast_log(LOG_DEBUG
, "ast_sched_wait()\n"));
154 ast_mutex_lock(&con
->lock
);
155 if (AST_LIST_EMPTY(&con
->schedq
)) {
158 ms
= ast_tvdiff_ms(AST_LIST_FIRST(&con
->schedq
)->when
, ast_tvnow());
162 ast_mutex_unlock(&con
->lock
);
169 * Take a sched structure and put it in the
170 * queue, such that the soonest event is
173 static void schedule(struct sched_context
*con
, struct sched
*s
)
176 struct sched
*cur
= NULL
;
178 AST_LIST_TRAVERSE_SAFE_BEGIN(&con
->schedq
, cur
, list
) {
179 if (ast_tvcmp(s
->when
, cur
->when
) == -1) {
180 AST_LIST_INSERT_BEFORE_CURRENT(&con
->schedq
, s
, list
);
184 AST_LIST_TRAVERSE_SAFE_END
186 AST_LIST_INSERT_TAIL(&con
->schedq
, s
, list
);
192 * given the last event *tv and the offset in milliseconds 'when',
193 * computes the next value,
195 static int sched_settime(struct timeval
*tv
, int when
)
197 struct timeval now
= ast_tvnow();
199 /*ast_log(LOG_DEBUG, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
200 if (ast_tvzero(*tv
)) /* not supplied, default to now */
202 *tv
= ast_tvadd(*tv
, ast_samp2tv(when
, 1000));
203 if (ast_tvcmp(*tv
, now
) < 0) {
204 ast_log(LOG_DEBUG
, "Request to schedule in the past?!?!\n");
212 * Schedule callback(data) to happen when ms into the future
214 int ast_sched_add_variable(struct sched_context
*con
, int when
, ast_sched_cb callback
, const void *data
, int variable
)
218 DEBUG(ast_log(LOG_DEBUG
, "ast_sched_add()\n"));
220 ast_log(LOG_NOTICE
, "Scheduled event in 0 ms?\n");
223 ast_mutex_lock(&con
->lock
);
224 if ((tmp
= sched_alloc(con
))) {
225 tmp
->id
= con
->eventcnt
++;
226 tmp
->callback
= callback
;
229 tmp
->variable
= variable
;
230 tmp
->when
= ast_tv(0, 0);
231 if (sched_settime(&tmp
->when
, when
)) {
232 sched_release(con
, tmp
);
238 #ifdef DUMP_SCHEDULER
239 /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
243 ast_mutex_unlock(&con
->lock
);
247 int ast_sched_add(struct sched_context
*con
, int when
, ast_sched_cb callback
, const void *data
)
249 return ast_sched_add_variable(con
, when
, callback
, data
, 0);
253 * Delete the schedule entry with number
254 * "id". It's nearly impossible that there
255 * would be two or more in the list with that
258 int ast_sched_del(struct sched_context
*con
, int id
)
262 DEBUG(ast_log(LOG_DEBUG
, "ast_sched_del()\n"));
264 ast_mutex_lock(&con
->lock
);
265 AST_LIST_TRAVERSE_SAFE_BEGIN(&con
->schedq
, s
, list
) {
267 AST_LIST_REMOVE_CURRENT(&con
->schedq
, list
);
269 sched_release(con
, s
);
273 AST_LIST_TRAVERSE_SAFE_END
275 #ifdef DUMP_SCHEDULER
276 /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
280 ast_mutex_unlock(&con
->lock
);
284 ast_log(LOG_DEBUG
, "Attempted to delete nonexistent schedule entry %d!\n", id
);
294 /*! \brief Dump the contents of the scheduler to LOG_DEBUG */
295 void ast_sched_dump(const struct sched_context
*con
)
298 struct timeval tv
= ast_tvnow();
299 #ifdef SCHED_MAX_CACHE
300 ast_log(LOG_DEBUG
, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n", con
->schedcnt
, con
->eventcnt
- 1, con
->schedccnt
);
302 ast_log(LOG_DEBUG
, "Asterisk Schedule Dump (%d in Q, %d Total)\n", con
->schedcnt
, con
->eventcnt
- 1);
305 ast_log(LOG_DEBUG
, "=============================================================\n");
306 ast_log(LOG_DEBUG
, "|ID Callback Data Time (sec:ms) |\n");
307 ast_log(LOG_DEBUG
, "+-----+-----------------+-----------------+-----------------+\n");
308 AST_LIST_TRAVERSE(&con
->schedq
, q
, list
) {
309 struct timeval delta
= ast_tvsub(q
->when
, tv
);
311 ast_log(LOG_DEBUG
, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n",
316 (long int)delta
.tv_usec
);
318 ast_log(LOG_DEBUG
, "=============================================================\n");
323 * Launch all events which need to be run at this time.
325 int ast_sched_runq(struct sched_context
*con
)
327 struct sched
*current
;
332 DEBUG(ast_log(LOG_DEBUG
, "ast_sched_runq()\n"));
334 ast_mutex_lock(&con
->lock
);
336 for (numevents
= 0; !AST_LIST_EMPTY(&con
->schedq
); numevents
++) {
337 /* schedule all events which are going to expire within 1ms.
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 tv
= ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
343 if (ast_tvcmp(AST_LIST_FIRST(&con
->schedq
)->when
, tv
) != -1)
346 current
= AST_LIST_REMOVE_HEAD(&con
->schedq
, list
);
350 * At this point, the schedule queue is still intact. We
351 * have removed the first event and the rest is still there,
352 * so it's permissible for the callback to add new events, but
353 * trying to delete itself won't work because it isn't in
354 * the schedule queue. If that's what it wants to do, it
358 ast_mutex_unlock(&con
->lock
);
359 res
= current
->callback(current
->data
);
360 ast_mutex_lock(&con
->lock
);
364 * If they return non-zero, we should schedule them to be
367 if (sched_settime(¤t
->when
, current
->variable
? res
: current
->resched
)) {
368 sched_release(con
, current
);
370 schedule(con
, current
);
372 /* No longer needed, so release it */
373 sched_release(con
, current
);
377 ast_mutex_unlock(&con
->lock
);
382 long ast_sched_when(struct sched_context
*con
,int id
)
386 DEBUG(ast_log(LOG_DEBUG
, "ast_sched_when()\n"));
388 ast_mutex_lock(&con
->lock
);
389 AST_LIST_TRAVERSE(&con
->schedq
, s
, list
) {
394 struct timeval now
= ast_tvnow();
395 secs
= s
->when
.tv_sec
- now
.tv_sec
;
397 ast_mutex_unlock(&con
->lock
);