1999-10-13 04:15:49 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 - 2005, Digium, Inc.
|
|
|
|
*
|
2005-04-15 17:34:14 +00:00
|
|
|
* Mark Spencer <markster@digium.com>
|
1999-10-13 04:15:49 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1999-10-13 04:15:49 +00:00
|
|
|
*
|
2005-04-15 17:34:14 +00:00
|
|
|
* Scheduler Routines (from cheops-NG)
|
1999-10-13 04:15:49 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef DEBUG_SCHEDULER
|
|
|
|
#define DEBUG(a) DEBUG_M(a)
|
|
|
|
#else
|
|
|
|
#define DEBUG(a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
2004-06-22 17:42:14 +00:00
|
|
|
#include <string.h>
|
2003-11-21 18:38:42 +00:00
|
|
|
|
2005-06-06 20:27:51 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
2005-06-06 22:12:19 +00:00
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
2005-06-06 20:27:51 +00:00
|
|
|
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/sched.h"
|
|
|
|
#include "asterisk/logger.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/lock.h"
|
2005-07-15 23:00:47 +00:00
|
|
|
#include "asterisk/utils.h"
|
1999-10-13 04:15:49 +00:00
|
|
|
|
|
|
|
/* Determine if a is sooner than b */
|
|
|
|
#define SOONER(a,b) (((b).tv_sec > (a).tv_sec) || \
|
|
|
|
(((b).tv_sec == (a).tv_sec) && ((b).tv_usec > (a).tv_usec)))
|
|
|
|
|
|
|
|
struct sched {
|
2005-04-15 17:34:14 +00:00
|
|
|
struct sched *next; /* Next event in the list */
|
|
|
|
int id; /* ID number of event */
|
|
|
|
struct timeval when; /* Absolute time event should take place */
|
|
|
|
int resched; /* When to reschedule */
|
2005-08-22 22:55:06 +00:00
|
|
|
int variable; /* Use return value from callback to reschedule */
|
2005-04-15 17:34:14 +00:00
|
|
|
void *data; /* Data */
|
1999-10-13 04:15:49 +00:00
|
|
|
ast_sched_cb callback; /* Callback */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sched_context {
|
2003-11-21 18:38:42 +00:00
|
|
|
ast_mutex_t lock;
|
1999-10-13 04:15:49 +00:00
|
|
|
/* Number of events processed */
|
|
|
|
int eventcnt;
|
|
|
|
|
|
|
|
/* Number of outstanding schedule events */
|
|
|
|
int schedcnt;
|
|
|
|
|
|
|
|
/* Schedule entry and main queue */
|
|
|
|
struct sched *schedq;
|
|
|
|
|
|
|
|
#ifdef SCHED_MAX_CACHE
|
|
|
|
/* Cache of unused schedule structures and how many */
|
|
|
|
struct sched *schedc;
|
|
|
|
int schedccnt;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sched_context *sched_context_create(void)
|
|
|
|
{
|
|
|
|
struct sched_context *tmp;
|
|
|
|
tmp = malloc(sizeof(struct sched_context));
|
|
|
|
if (tmp) {
|
2004-06-22 17:42:14 +00:00
|
|
|
memset(tmp, 0, sizeof(struct sched_context));
|
2003-11-21 18:38:42 +00:00
|
|
|
ast_mutex_init(&tmp->lock);
|
1999-10-13 04:15:49 +00:00
|
|
|
tmp->eventcnt = 1;
|
|
|
|
tmp->schedcnt = 0;
|
|
|
|
tmp->schedq = NULL;
|
2001-04-23 16:50:12 +00:00
|
|
|
#ifdef SCHED_MAX_CACHE
|
1999-10-13 04:15:49 +00:00
|
|
|
tmp->schedc = NULL;
|
|
|
|
tmp->schedccnt = 0;
|
2001-04-23 16:50:12 +00:00
|
|
|
#endif
|
1999-10-13 04:15:49 +00:00
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sched_context_destroy(struct sched_context *con)
|
|
|
|
{
|
|
|
|
struct sched *s, *sl;
|
2003-11-21 18:38:42 +00:00
|
|
|
ast_mutex_lock(&con->lock);
|
2001-04-23 16:50:12 +00:00
|
|
|
#ifdef SCHED_MAX_CACHE
|
1999-10-13 04:15:49 +00:00
|
|
|
/* Eliminate the cache */
|
|
|
|
s = con->schedc;
|
|
|
|
while(s) {
|
|
|
|
sl = s;
|
|
|
|
s = s->next;
|
|
|
|
free(sl);
|
|
|
|
}
|
2001-04-23 16:50:12 +00:00
|
|
|
#endif
|
1999-10-13 04:15:49 +00:00
|
|
|
/* And the queue */
|
|
|
|
s = con->schedq;
|
|
|
|
while(s) {
|
|
|
|
sl = s;
|
|
|
|
s = s->next;
|
|
|
|
free(sl);
|
|
|
|
}
|
|
|
|
/* And the context */
|
2003-11-21 18:38:42 +00:00
|
|
|
ast_mutex_unlock(&con->lock);
|
2004-06-22 17:42:14 +00:00
|
|
|
ast_mutex_destroy(&con->lock);
|
1999-10-13 04:15:49 +00:00
|
|
|
free(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct sched *sched_alloc(struct sched_context *con)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We keep a small cache of schedule entries
|
|
|
|
* to minimize the number of necessary malloc()'s
|
|
|
|
*/
|
|
|
|
struct sched *tmp;
|
|
|
|
#ifdef SCHED_MAX_CACHE
|
|
|
|
if (con->schedc) {
|
|
|
|
tmp = con->schedc;
|
|
|
|
con->schedc = con->schedc->next;
|
|
|
|
con->schedccnt--;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
tmp = malloc(sizeof(struct sched));
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sched_release(struct sched_context *con, struct sched *tmp)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Add to the cache, or just free() if we
|
|
|
|
* already have too many cache entries
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef SCHED_MAX_CACHE
|
|
|
|
if (con->schedccnt < SCHED_MAX_CACHE) {
|
|
|
|
tmp->next = con->schedc;
|
|
|
|
con->schedc = tmp;
|
|
|
|
con->schedccnt++;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sched_wait(struct sched_context *con)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Return the number of milliseconds
|
|
|
|
* until the next scheduled event
|
|
|
|
*/
|
|
|
|
int ms;
|
|
|
|
DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
|
2003-11-21 18:38:42 +00:00
|
|
|
ast_mutex_lock(&con->lock);
|
|
|
|
if (!con->schedq) {
|
|
|
|
ms = -1;
|
|
|
|
} else {
|
2005-07-15 23:00:47 +00:00
|
|
|
ms = ast_tvdiff_ms(con->schedq->when, ast_tvnow());
|
2003-11-21 18:38:42 +00:00
|
|
|
if (ms < 0)
|
|
|
|
ms = 0;
|
|
|
|
}
|
|
|
|
ast_mutex_unlock(&con->lock);
|
1999-10-13 04:15:49 +00:00
|
|
|
return ms;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void schedule(struct sched_context *con, struct sched *s)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Take a sched structure and put it in the
|
|
|
|
* queue, such that the soonest event is
|
|
|
|
* first in the list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct sched *last=NULL;
|
|
|
|
struct sched *current=con->schedq;
|
|
|
|
while(current) {
|
|
|
|
if (SOONER(s->when, current->when))
|
|
|
|
break;
|
|
|
|
last = current;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
/* Insert this event into the schedule */
|
|
|
|
s->next = current;
|
|
|
|
if (last)
|
|
|
|
last->next = s;
|
|
|
|
else
|
|
|
|
con->schedq = s;
|
|
|
|
con->schedcnt++;
|
|
|
|
}
|
|
|
|
|
2005-07-15 23:00:47 +00:00
|
|
|
/*
|
|
|
|
* given the last event *tv and the offset in milliseconds 'when',
|
|
|
|
* computes the next value,
|
|
|
|
*/
|
|
|
|
static int sched_settime(struct timeval *tv, int when)
|
1999-10-13 04:15:49 +00:00
|
|
|
{
|
2005-07-15 23:00:47 +00:00
|
|
|
struct timeval now = ast_tvnow();
|
2003-07-27 03:53:19 +00:00
|
|
|
|
|
|
|
/*ast_log(LOG_DEBUG, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
|
2005-07-15 23:00:47 +00:00
|
|
|
if (ast_tvzero(*tv)) /* not supplied, default to now */
|
|
|
|
*tv = now;
|
|
|
|
*tv = ast_tvadd(*tv, ast_samp2tv(when, 1000));
|
|
|
|
if (ast_tvcmp(*tv, now) < 0) {
|
2004-07-20 13:43:33 +00:00
|
|
|
ast_log(LOG_DEBUG, "Request to schedule in the past?!?!\n");
|
2005-07-15 23:00:47 +00:00
|
|
|
*tv = now;
|
1999-10-13 04:15:49 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-22 22:55:06 +00:00
|
|
|
|
|
|
|
int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, void *data, int variable)
|
1999-10-13 04:15:49 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Schedule callback(data) to happen when ms into the future
|
|
|
|
*/
|
|
|
|
struct sched *tmp;
|
2003-11-21 18:38:42 +00:00
|
|
|
int res = -1;
|
1999-10-13 04:15:49 +00:00
|
|
|
DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
|
|
|
|
if (!when) {
|
2003-11-12 08:57:23 +00:00
|
|
|
ast_log(LOG_NOTICE, "Scheduled event in 0 ms?\n");
|
1999-10-13 04:15:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-11-21 18:38:42 +00:00
|
|
|
ast_mutex_lock(&con->lock);
|
1999-10-13 04:15:49 +00:00
|
|
|
if ((tmp = sched_alloc(con))) {
|
|
|
|
tmp->id = con->eventcnt++;
|
|
|
|
tmp->callback = callback;
|
|
|
|
tmp->data = data;
|
|
|
|
tmp->resched = when;
|
2005-08-22 22:55:06 +00:00
|
|
|
tmp->variable = variable;
|
2005-07-15 23:00:47 +00:00
|
|
|
tmp->when = ast_tv(0, 0);
|
1999-10-13 04:15:49 +00:00
|
|
|
if (sched_settime(&tmp->when, when)) {
|
|
|
|
sched_release(con, tmp);
|
2003-11-21 18:38:42 +00:00
|
|
|
} else {
|
1999-10-13 04:15:49 +00:00
|
|
|
schedule(con, tmp);
|
2003-11-21 18:38:42 +00:00
|
|
|
res = tmp->id;
|
|
|
|
}
|
|
|
|
}
|
2005-07-15 22:21:31 +00:00
|
|
|
#ifdef DUMP_SCHEDULER
|
|
|
|
/* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
|
|
|
|
ast_sched_dump(con);
|
|
|
|
#endif
|
2003-11-21 20:41:43 +00:00
|
|
|
ast_mutex_unlock(&con->lock);
|
2003-11-21 18:38:42 +00:00
|
|
|
return res;
|
1999-10-13 04:15:49 +00:00
|
|
|
}
|
|
|
|
|
2005-08-22 22:55:06 +00:00
|
|
|
int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, void *data)
|
|
|
|
{
|
|
|
|
return ast_sched_add_variable(con, when, callback, data, 0);
|
|
|
|
}
|
|
|
|
|
1999-10-13 04:15:49 +00:00
|
|
|
int ast_sched_del(struct sched_context *con, int id)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Delete the schedule entry with number
|
|
|
|
* "id". It's nearly impossible that there
|
|
|
|
* would be two or more in the list with that
|
|
|
|
* id.
|
|
|
|
*/
|
|
|
|
struct sched *last=NULL, *s;
|
|
|
|
DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
|
2003-11-21 18:38:42 +00:00
|
|
|
ast_mutex_lock(&con->lock);
|
1999-10-13 04:15:49 +00:00
|
|
|
s = con->schedq;
|
|
|
|
while(s) {
|
|
|
|
if (s->id == id) {
|
|
|
|
if (last)
|
|
|
|
last->next = s->next;
|
|
|
|
else
|
|
|
|
con->schedq = s->next;
|
|
|
|
con->schedcnt--;
|
2002-09-05 21:32:54 +00:00
|
|
|
sched_release(con, s);
|
2003-11-21 18:38:42 +00:00
|
|
|
break;
|
1999-10-13 04:15:49 +00:00
|
|
|
}
|
|
|
|
last = s;
|
|
|
|
s = s->next;
|
|
|
|
}
|
2005-07-15 22:21:31 +00:00
|
|
|
#ifdef DUMP_SCHEDULER
|
|
|
|
/* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
|
|
|
|
ast_sched_dump(con);
|
|
|
|
#endif
|
2003-11-21 18:38:42 +00:00
|
|
|
ast_mutex_unlock(&con->lock);
|
|
|
|
if (!s) {
|
2005-04-13 23:33:47 +00:00
|
|
|
ast_log(LOG_NOTICE, "Attempted to delete nonexistent schedule entry %d!\n", id);
|
2001-06-26 19:17:27 +00:00
|
|
|
#ifdef DO_CRASH
|
2003-11-21 18:38:42 +00:00
|
|
|
CRASH;
|
1999-10-13 04:15:49 +00:00
|
|
|
#endif
|
2003-11-21 18:38:42 +00:00
|
|
|
return -1;
|
|
|
|
} else
|
|
|
|
return 0;
|
1999-10-13 04:15:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-15 22:21:31 +00:00
|
|
|
void ast_sched_dump(const struct sched_context *con)
|
1999-10-13 04:15:49 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Dump the contents of the scheduler to
|
|
|
|
* stderr
|
|
|
|
*/
|
|
|
|
struct sched *q;
|
2005-07-15 23:00:47 +00:00
|
|
|
struct timeval tv = ast_tvnow();
|
2001-04-23 16:50:12 +00:00
|
|
|
#ifdef SCHED_MAX_CACHE
|
2005-07-15 22:21:31 +00:00
|
|
|
ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n", con->schedcnt, con->eventcnt - 1, con->schedccnt);
|
2001-04-23 16:50:12 +00:00
|
|
|
#else
|
2005-07-15 22:21:31 +00:00
|
|
|
ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total)\n", con->schedcnt, con->eventcnt - 1);
|
2001-04-23 16:50:12 +00:00
|
|
|
#endif
|
|
|
|
|
2005-07-15 22:21:31 +00:00
|
|
|
ast_log(LOG_DEBUG, "=============================================================\n");
|
|
|
|
ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n");
|
|
|
|
ast_log(LOG_DEBUG, "+-----+-----------------+-----------------+-----------------+\n");
|
2005-07-15 23:00:47 +00:00
|
|
|
for (q = con->schedq; q; q = q->next) {
|
|
|
|
struct timeval delta = ast_tvsub(q->when, tv);
|
|
|
|
|
2005-07-15 22:21:31 +00:00
|
|
|
ast_log(LOG_DEBUG, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n",
|
2005-07-15 23:00:47 +00:00
|
|
|
q->id,
|
|
|
|
q->callback,
|
|
|
|
q->data,
|
|
|
|
delta.tv_sec,
|
2005-09-05 17:46:09 +00:00
|
|
|
(long int)delta.tv_usec);
|
1999-10-13 04:15:49 +00:00
|
|
|
}
|
2005-07-15 22:21:31 +00:00
|
|
|
ast_log(LOG_DEBUG, "=============================================================\n");
|
1999-10-13 04:15:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sched_runq(struct sched_context *con)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Launch all events which need to be run at this time.
|
|
|
|
*/
|
|
|
|
struct sched *current;
|
|
|
|
struct timeval tv;
|
|
|
|
int x=0;
|
2003-11-21 22:05:08 +00:00
|
|
|
int res;
|
1999-10-13 04:15:49 +00:00
|
|
|
DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
|
|
|
|
|
2003-11-21 18:38:42 +00:00
|
|
|
ast_mutex_lock(&con->lock);
|
1999-10-13 04:15:49 +00:00
|
|
|
for(;;) {
|
|
|
|
if (!con->schedq)
|
|
|
|
break;
|
2005-07-15 23:00:47 +00:00
|
|
|
|
|
|
|
/* schedule all events which are going to expire within 1ms.
|
|
|
|
* We only care about millisecond accuracy anyway, so this will
|
|
|
|
* help us get more than one event at one time if they are very
|
|
|
|
* close together.
|
|
|
|
*/
|
|
|
|
tv = ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
|
1999-10-13 04:15:49 +00:00
|
|
|
if (SOONER(con->schedq->when, tv)) {
|
|
|
|
current = con->schedq;
|
|
|
|
con->schedq = con->schedq->next;
|
|
|
|
con->schedcnt--;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, the schedule queue is still intact. We
|
|
|
|
* have removed the first event and the rest is still there,
|
|
|
|
* so it's permissible for the callback to add new events, but
|
|
|
|
* trying to delete itself won't work because it isn't in
|
|
|
|
* the schedule queue. If that's what it wants to do, it
|
|
|
|
* should return 0.
|
|
|
|
*/
|
2003-11-21 22:05:08 +00:00
|
|
|
|
|
|
|
ast_mutex_unlock(&con->lock);
|
|
|
|
res = current->callback(current->data);
|
|
|
|
ast_mutex_lock(&con->lock);
|
|
|
|
|
|
|
|
if (res) {
|
1999-10-13 04:15:49 +00:00
|
|
|
/*
|
|
|
|
* If they return non-zero, we should schedule them to be
|
|
|
|
* run again.
|
|
|
|
*/
|
2005-08-22 22:55:06 +00:00
|
|
|
if (sched_settime(¤t->when, current->variable? res : current->resched)) {
|
1999-10-13 04:15:49 +00:00
|
|
|
sched_release(con, current);
|
|
|
|
} else
|
|
|
|
schedule(con, current);
|
|
|
|
} else {
|
|
|
|
/* No longer needed, so release it */
|
|
|
|
sched_release(con, current);
|
|
|
|
}
|
|
|
|
x++;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2003-11-21 18:38:42 +00:00
|
|
|
ast_mutex_unlock(&con->lock);
|
1999-10-13 04:15:49 +00:00
|
|
|
return x;
|
|
|
|
}
|
2005-04-13 18:46:35 +00:00
|
|
|
|
|
|
|
long ast_sched_when(struct sched_context *con,int id)
|
|
|
|
{
|
|
|
|
struct sched *s;
|
|
|
|
long secs;
|
|
|
|
DEBUG(ast_log(LOG_DEBUG, "ast_sched_when()\n"));
|
|
|
|
|
|
|
|
ast_mutex_lock(&con->lock);
|
|
|
|
s=con->schedq;
|
|
|
|
while (s!=NULL) {
|
|
|
|
if (s->id==id) break;
|
|
|
|
s=s->next;
|
|
|
|
}
|
|
|
|
secs=-1;
|
|
|
|
if (s!=NULL) {
|
2005-07-15 23:00:47 +00:00
|
|
|
struct timeval now = ast_tvnow();
|
|
|
|
secs=s->when.tv_sec-now.tv_sec;
|
2005-04-13 18:46:35 +00:00
|
|
|
}
|
|
|
|
ast_mutex_unlock(&con->lock);
|
|
|
|
return secs;
|
|
|
|
}
|