Skip to content
Snippets Groups Projects
Commit 55aab4cf authored by Russell Bryant's avatar Russell Bryant
Browse files

convert the list of scheduled items in a scheduler context to use the

list macros.  Also, use ast_calloc instead of malloc in one place


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@24910 65c4cc65-6c06-0410-ace0-fbb531ad65f3
parent 326afa49
No related branches found
No related tags found
No related merge requests found
...@@ -44,29 +44,30 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") ...@@ -44,29 +44,30 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/channel.h" #include "asterisk/channel.h"
#include "asterisk/lock.h" #include "asterisk/lock.h"
#include "asterisk/utils.h" #include "asterisk/utils.h"
#include "asterisk/linkedlists.h"
/* Determine if a is sooner than b */ /* Determine if a is sooner than b */
#define SOONER(a,b) (((b).tv_sec > (a).tv_sec) || \ #define SOONER(a,b) (((b).tv_sec > (a).tv_sec) || \
(((b).tv_sec == (a).tv_sec) && ((b).tv_usec > (a).tv_usec))) (((b).tv_sec == (a).tv_sec) && ((b).tv_usec > (a).tv_usec)))
struct sched { struct sched {
struct sched *next; /*!< Next event in the list */ AST_LIST_ENTRY(sched) list;
int id; /*!< ID number of event */ int id; /*!< ID number of event */
struct timeval when; /*!< Absolute time event should take place */ struct timeval when; /*!< Absolute time event should take place */
int resched; /*!< When to reschedule */ int resched; /*!< When to reschedule */
int variable; /*!< Use return value from callback to reschedule */ int variable; /*!< Use return value from callback to reschedule */
void *data; /*!< Data */ void *data; /*!< Data */
ast_sched_cb callback; /*!< Callback */ ast_sched_cb callback; /*!< Callback */
}; };
struct sched_context { struct sched_context {
ast_mutex_t lock; ast_mutex_t lock;
int eventcnt; /*!< Number of events processed */ int eventcnt; /*!< Number of events processed */
int schedcnt; /*!< Number of outstanding schedule events */ int schedcnt; /*!< Number of outstanding schedule events */
struct sched *schedq; /*!< Schedule entry and main queue */ AST_LIST_HEAD_NOLOCK(, sched) schedq; /*!< Schedule entry and main queue */
#ifdef SCHED_MAX_CACHE #ifdef SCHED_MAX_CACHE
struct sched *schedc; /*!< Cache of unused schedule structures and how many */ AST_LIST_HEAD_NOLOCK(, sched) schedc; /*!< Cache of unused schedule structures and how many */
int schedccnt; int schedccnt;
#endif #endif
}; };
...@@ -86,24 +87,20 @@ struct sched_context *sched_context_create(void) ...@@ -86,24 +87,20 @@ struct sched_context *sched_context_create(void)
void sched_context_destroy(struct sched_context *con) void sched_context_destroy(struct sched_context *con)
{ {
struct sched *s, *sl; struct sched *s;
ast_mutex_lock(&con->lock); ast_mutex_lock(&con->lock);
#ifdef SCHED_MAX_CACHE #ifdef SCHED_MAX_CACHE
/* Eliminate the cache */ /* Eliminate the cache */
s = con->schedc; while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
while(s) { free(s);
sl = s;
s = s->next;
free(sl);
}
#endif #endif
/* And the queue */ /* And the queue */
s = con->schedq; while ((s = AST_LIST_REMOVE_HEAD(&con->schedq, list)))
while(s) { free(s);
sl = s;
s = s->next;
free(sl);
}
/* And the context */ /* And the context */
ast_mutex_unlock(&con->lock); ast_mutex_unlock(&con->lock);
ast_mutex_destroy(&con->lock); ast_mutex_destroy(&con->lock);
...@@ -112,19 +109,19 @@ void sched_context_destroy(struct sched_context *con) ...@@ -112,19 +109,19 @@ void sched_context_destroy(struct sched_context *con)
static struct sched *sched_alloc(struct sched_context *con) static struct sched *sched_alloc(struct sched_context *con)
{ {
struct sched *tmp;
/* /*
* We keep a small cache of schedule entries * We keep a small cache of schedule entries
* to minimize the number of necessary malloc()'s * to minimize the number of necessary malloc()'s
*/ */
struct sched *tmp;
#ifdef SCHED_MAX_CACHE #ifdef SCHED_MAX_CACHE
if (con->schedc) { if ((tmp = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
tmp = con->schedc;
con->schedc = con->schedc->next;
con->schedccnt--; con->schedccnt--;
} else else
#endif #endif
tmp = malloc(sizeof(struct sched)); tmp = ast_calloc(1, sizeof(*tmp));
return tmp; return tmp;
} }
...@@ -137,8 +134,7 @@ static void sched_release(struct sched_context *con, struct sched *tmp) ...@@ -137,8 +134,7 @@ static void sched_release(struct sched_context *con, struct sched *tmp)
#ifdef SCHED_MAX_CACHE #ifdef SCHED_MAX_CACHE
if (con->schedccnt < SCHED_MAX_CACHE) { if (con->schedccnt < SCHED_MAX_CACHE) {
tmp->next = con->schedc; AST_LIST_INSERT_HEAD(&con->schedc, tmp, list);
con->schedc = tmp;
con->schedccnt++; con->schedccnt++;
} else } else
#endif #endif
...@@ -152,18 +148,20 @@ static void sched_release(struct sched_context *con, struct sched *tmp) ...@@ -152,18 +148,20 @@ static void sched_release(struct sched_context *con, struct sched *tmp)
int ast_sched_wait(struct sched_context *con) int ast_sched_wait(struct sched_context *con)
{ {
int ms; int ms;
DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n")); DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
ast_mutex_lock(&con->lock); ast_mutex_lock(&con->lock);
if (!con->schedq) { if (AST_LIST_EMPTY(&con->schedq)) {
ms = -1; ms = -1;
} else { } else {
ms = ast_tvdiff_ms(con->schedq->when, ast_tvnow()); ms = ast_tvdiff_ms(AST_LIST_FIRST(&con->schedq)->when, ast_tvnow());
if (ms < 0) if (ms < 0)
ms = 0; ms = 0;
} }
ast_mutex_unlock(&con->lock); ast_mutex_unlock(&con->lock);
return ms; return ms;
} }
...@@ -175,20 +173,18 @@ int ast_sched_wait(struct sched_context *con) ...@@ -175,20 +173,18 @@ int ast_sched_wait(struct sched_context *con)
static void schedule(struct sched_context *con, struct sched *s) static void schedule(struct sched_context *con, struct sched *s)
{ {
struct sched *last=NULL; struct sched *cur = NULL;
struct sched *current=con->schedq;
while(current) { AST_LIST_TRAVERSE_SAFE_BEGIN(&con->schedq, cur, list) {
if (SOONER(s->when, current->when)) if (SOONER(s->when, cur->when)) {
AST_LIST_INSERT_BEFORE_CURRENT(&con->schedq, s, list);
break; break;
last = current; }
current = current->next;
} }
/* Insert this event into the schedule */ AST_LIST_TRAVERSE_SAFE_END
s->next = current; if (!cur)
if (last) AST_LIST_INSERT_TAIL(&con->schedq, s, list);
last->next = s;
else
con->schedq = s;
con->schedcnt++; con->schedcnt++;
} }
...@@ -231,7 +227,6 @@ int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb cal ...@@ -231,7 +227,6 @@ int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb cal
tmp->data = data; tmp->data = data;
tmp->resched = when; tmp->resched = when;
tmp->variable = variable; tmp->variable = variable;
tmp->when = ast_tv(0, 0);
if (sched_settime(&tmp->when, when)) { if (sched_settime(&tmp->when, when)) {
sched_release(con, tmp); sched_release(con, tmp);
} else { } else {
...@@ -260,44 +255,41 @@ int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, vo ...@@ -260,44 +255,41 @@ int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, vo
*/ */
int ast_sched_del(struct sched_context *con, int id) int ast_sched_del(struct sched_context *con, int id)
{ {
struct sched *last=NULL, *s; struct sched *s;
DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n")); DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
ast_mutex_lock(&con->lock); ast_mutex_lock(&con->lock);
s = con->schedq; AST_LIST_TRAVERSE_SAFE_BEGIN(&con->schedq, s, list) {
while(s) {
if (s->id == id) { if (s->id == id) {
if (last) AST_LIST_REMOVE_CURRENT(&con->schedq, list);
last->next = s->next;
else
con->schedq = s->next;
con->schedcnt--; con->schedcnt--;
sched_release(con, s); sched_release(con, s);
break; break;
} }
last = s;
s = s->next;
} }
AST_LIST_TRAVERSE_SAFE_END
#ifdef DUMP_SCHEDULER #ifdef DUMP_SCHEDULER
/* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */ /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
ast_sched_dump(con); ast_sched_dump(con);
#endif #endif
ast_mutex_unlock(&con->lock); ast_mutex_unlock(&con->lock);
if (!s) { if (!s) {
ast_log(LOG_NOTICE, "Attempted to delete nonexistent schedule entry %d!\n", id); ast_log(LOG_NOTICE, "Attempted to delete nonexistent schedule entry %d!\n", id);
#ifdef DO_CRASH #ifdef DO_CRASH
CRASH; CRASH;
#endif #endif
return -1; return -1;
} else }
return 0;
return 0;
} }
/*! \brief Dump the contents of the scheduler to stderr */
void ast_sched_dump(const struct sched_context *con) void ast_sched_dump(const struct sched_context *con)
{ {
/*
* Dump the contents of the scheduler to
* stderr
*/
struct sched *q; struct sched *q;
struct timeval tv = ast_tvnow(); struct timeval tv = ast_tvnow();
#ifdef SCHED_MAX_CACHE #ifdef SCHED_MAX_CACHE
...@@ -309,7 +301,7 @@ void ast_sched_dump(const struct sched_context *con) ...@@ -309,7 +301,7 @@ void ast_sched_dump(const struct sched_context *con)
ast_log(LOG_DEBUG, "=============================================================\n"); ast_log(LOG_DEBUG, "=============================================================\n");
ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n"); ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n");
ast_log(LOG_DEBUG, "+-----+-----------------+-----------------+-----------------+\n"); ast_log(LOG_DEBUG, "+-----+-----------------+-----------------+-----------------+\n");
for (q = con->schedq; q; q = q->next) { AST_LIST_TRAVERSE(&con->schedq, q, list) {
struct timeval delta = ast_tvsub(q->when, tv); struct timeval delta = ast_tvsub(q->when, tv);
ast_log(LOG_DEBUG, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", ast_log(LOG_DEBUG, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n",
...@@ -336,7 +328,7 @@ int ast_sched_runq(struct sched_context *con) ...@@ -336,7 +328,7 @@ int ast_sched_runq(struct sched_context *con)
ast_mutex_lock(&con->lock); ast_mutex_lock(&con->lock);
for(;;) { for(;;) {
if (!con->schedq) if (AST_LIST_EMPTY(&con->schedq))
break; break;
/* schedule all events which are going to expire within 1ms. /* schedule all events which are going to expire within 1ms.
...@@ -345,9 +337,8 @@ int ast_sched_runq(struct sched_context *con) ...@@ -345,9 +337,8 @@ int ast_sched_runq(struct sched_context *con)
* close together. * close together.
*/ */
tv = ast_tvadd(ast_tvnow(), ast_tv(0, 1000)); tv = ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
if (SOONER(con->schedq->when, tv)) { if (SOONER(AST_LIST_FIRST(&con->schedq)->when, tv)) {
current = con->schedq; current = AST_LIST_REMOVE_HEAD(&con->schedq, list);
con->schedq = con->schedq->next;
con->schedcnt--; con->schedcnt--;
/* /*
...@@ -391,15 +382,16 @@ long ast_sched_when(struct sched_context *con,int id) ...@@ -391,15 +382,16 @@ long ast_sched_when(struct sched_context *con,int id)
DEBUG(ast_log(LOG_DEBUG, "ast_sched_when()\n")); DEBUG(ast_log(LOG_DEBUG, "ast_sched_when()\n"));
ast_mutex_lock(&con->lock); ast_mutex_lock(&con->lock);
for (s = con->schedq; s; s = s->next) { AST_LIST_TRAVERSE(&con->schedq, s, list) {
if (s->id == id) if (s->id == id)
break; break;
} }
secs=-1; secs = -1;
if (s!=NULL) { if (s) {
struct timeval now = ast_tvnow(); struct timeval now = ast_tvnow();
secs = s->when.tv_sec - now.tv_sec; secs = s->when.tv_sec - now.tv_sec;
} }
ast_mutex_unlock(&con->lock); ast_mutex_unlock(&con->lock);
return secs; return secs;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment