Skip to content
Snippets Groups Projects
sched.c 9.97 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    /*
    
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 1999 - 2005, Digium, Inc.
     *
    
    Russell Bryant's avatar
    Russell Bryant committed
     * Mark Spencer <markster@digium.com>
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * 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.
     */
    
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * \brief Scheduler Routines (from cheops-NG)
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * \author Mark Spencer <markster@digium.com>
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    #ifdef DEBUG_SCHEDULER
    
    #define DEBUG(a) do { \
    	if (option_debug) \
    		DEBUG_M(a) \
    	} while (0)
    
    Mark Spencer's avatar
    Mark Spencer committed
    #else
    #define DEBUG(a) 
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    #include <unistd.h>
    
    #include "asterisk/sched.h"
    #include "asterisk/logger.h"
    #include "asterisk/channel.h"
    #include "asterisk/lock.h"
    
    #include "asterisk/linkedlists.h"
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    struct sched {
    
    	AST_LIST_ENTRY(sched) list;
    	int id;                       /*!< ID number of event */
    	struct timeval when;          /*!< Absolute time event should take place */
    	int resched;                  /*!< When to reschedule */
    	int variable;                 /*!< Use return value from callback to reschedule */
    	void *data;                   /*!< Data */
    	ast_sched_cb callback;        /*!< Callback */
    
    Mark Spencer's avatar
    Mark Spencer committed
    };
    
    struct sched_context {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_t lock;
    
    	unsigned int eventcnt;                  /*!< Number of events processed */
    	unsigned int schedcnt;                  /*!< Number of outstanding schedule events */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	AST_LIST_HEAD_NOLOCK(, sched) schedq;   /*!< Schedule entry and main queue */
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    #ifdef SCHED_MAX_CACHE
    
    	AST_LIST_HEAD_NOLOCK(, sched) schedc;   /*!< Cache of unused schedule structures and how many */
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    };
    
    struct sched_context *sched_context_create(void)
    {
    	struct sched_context *tmp;
    
    
    	if (!(tmp = ast_calloc(1, sizeof(*tmp))))
    		return NULL;
    
    	ast_mutex_init(&tmp->lock);
    	tmp->eventcnt = 1;
    	
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return tmp;
    }
    
    void sched_context_destroy(struct sched_context *con)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_lock(&con->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #ifdef SCHED_MAX_CACHE
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Eliminate the cache */
    
    	while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* And the queue */
    
    	while ((s = AST_LIST_REMOVE_HEAD(&con->schedq, list)))
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* And the context */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_unlock(&con->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    }
    
    static struct sched *sched_alloc(struct sched_context *con)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/*
    	 * We keep a small cache of schedule entries
    	 * to minimize the number of necessary malloc()'s
    	 */
    #ifdef SCHED_MAX_CACHE
    
    	if ((tmp = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
    
    Mark Spencer's avatar
    Mark Spencer committed
    		con->schedccnt--;
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    Mark Spencer's avatar
    Mark Spencer committed
    	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) {
    
    		AST_LIST_INSERT_HEAD(&con->schedc, tmp, list);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		con->schedccnt++;
    	} else
    #endif
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief
     * Return the number of milliseconds 
     * until the next scheduled event
     */
    
    Mark Spencer's avatar
    Mark Spencer committed
    int ast_sched_wait(struct sched_context *con)
    {
    	int ms;
    
    	DEBUG(ast_debug(1, "ast_sched_wait()\n"));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_lock(&con->lock);
    
    	if (AST_LIST_EMPTY(&con->schedq)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ms = -1;
    	} else {
    
    		ms = ast_tvdiff_ms(AST_LIST_FIRST(&con->schedq)->when, ast_tvnow());
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (ms < 0)
    			ms = 0;
    	}
    	ast_mutex_unlock(&con->lock);
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief
     * Take a sched structure and put it in the
     * queue, such that the soonest event is
     * first in the list. 
     */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static void schedule(struct sched_context *con, struct sched *s)
    {
    	 
    
    	struct sched *cur = NULL;
    	
    	AST_LIST_TRAVERSE_SAFE_BEGIN(&con->schedq, cur, list) {
    
    		if (ast_tvcmp(s->when, cur->when) == -1) {
    
    			AST_LIST_INSERT_BEFORE_CURRENT(&con->schedq, s, list);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	AST_LIST_TRAVERSE_SAFE_END
    	if (!cur)
    		AST_LIST_INSERT_TAIL(&con->schedq, s, list);
    	
    
    Mark Spencer's avatar
    Mark Spencer committed
    	con->schedcnt++;
    }
    
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief
    
     * given the last event *tv and the offset in milliseconds 'when',
     * computes the next value,
     */
    static int sched_settime(struct timeval *tv, int when)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	/*ast_debug(1, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
    
    	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) {
    
    		ast_debug(1, "Request to schedule in the past?!?!\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    	return 0;
    }
    
    
    int ast_sched_replace_variable(int old_id, struct sched_context *con, int when, ast_sched_cb callback, void *data, int variable)
    {
    	if (old_id > -1)
    		ast_sched_del(con, old_id);
    	return ast_sched_add_variable(con, when, callback, data, variable);
    }
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief
     * Schedule callback(data) to happen when ms into the future
     */
    
    int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, void *data, int variable)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	struct sched *tmp;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int res = -1;
    
    	DEBUG(ast_debug(1, "ast_sched_add()\n"));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (!when) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_NOTICE, "Scheduled event in 0 ms?\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return -1;
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_lock(&con->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if ((tmp = sched_alloc(con))) {
    		tmp->id = con->eventcnt++;
    		tmp->callback = callback;
    		tmp->data = data;
    		tmp->resched = when;
    
    		tmp->variable = variable;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (sched_settime(&tmp->when, when)) {
    			sched_release(con, tmp);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		} else {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			schedule(con, tmp);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			res = tmp->id;
    		}
    	}
    
    #ifdef DUMP_SCHEDULER
    	/* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_unlock(&con->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return res;
    
    int ast_sched_replace(int old_id, struct sched_context *con, int when, ast_sched_cb callback, void *data)
    {
    	if (old_id > -1)
    		ast_sched_del(con, old_id);
    	return ast_sched_add(con, when, callback, data);
    }
    
    
    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);
    }
    
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief
     * Delete the schedule entry with number
     * "id".  It's nearly impossible that there
     * would be two or more in the list with that
     * id.
     */
    
    Mark Spencer's avatar
    Mark Spencer committed
    int ast_sched_del(struct sched_context *con, int id)
    {
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	struct sched *s;
    
    	DEBUG(ast_debug(1, "ast_sched_del()\n"));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_lock(&con->lock);
    
    	AST_LIST_TRAVERSE_SAFE_BEGIN(&con->schedq, s, list) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (s->id == id) {
    
    			AST_LIST_REMOVE_CURRENT(&con->schedq, list);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			con->schedcnt--;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			sched_release(con, s);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			break;
    
    #ifdef DUMP_SCHEDULER
    	/* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_unlock(&con->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (!s) {
    
    		ast_debug(1, "Attempted to delete nonexistent schedule entry %d!\n", id);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #ifdef DO_CRASH
    
    Mark Spencer's avatar
    Mark Spencer committed
    		CRASH;
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return -1;
    
    /*! \brief Dump the contents of the scheduler to LOG_DEBUG */
    
    void ast_sched_dump(const struct sched_context *con)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	struct sched *q;
    
    Mark Spencer's avatar
    Mark Spencer committed
    #ifdef SCHED_MAX_CACHE
    
    	ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n", con->schedcnt, con->eventcnt - 1, con->schedccnt);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #else
    
    	ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total)\n", con->schedcnt, con->eventcnt - 1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    
    	ast_debug(1, "=============================================================\n");
    	ast_debug(1, "|ID    Callback          Data              Time  (sec:ms)   |\n");
    	ast_debug(1, "+-----+-----------------+-----------------+-----------------+\n");
    	AST_LIST_TRAVERSE(&con->schedq, q, list) {
    		struct timeval delta = ast_tvsub(q->when, tv);
    
    		ast_debug(1, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", 
    			q->id,
    			q->callback,
    			q->data,
    			delta.tv_sec,
    			(long int)delta.tv_usec);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	ast_debug(1, "=============================================================\n");
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief
     * Launch all events which need to be run at this time.
     */
    
    Mark Spencer's avatar
    Mark Spencer committed
    int ast_sched_runq(struct sched_context *con)
    {
    	struct sched *current;
    	struct timeval tv;
    
    	DEBUG(ast_debug(1, "ast_sched_runq()\n"));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_lock(&con->lock);
    
    
    	for (numevents = 0; !AST_LIST_EMPTY(&con->schedq); numevents++) {
    
    		/* 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));
    
    		if (ast_tvcmp(AST_LIST_FIRST(&con->schedq)->when, tv) != -1)
    
    			break;
    		
    		current = AST_LIST_REMOVE_HEAD(&con->schedq, list);
    		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.
    		 */
    
    		ast_mutex_unlock(&con->lock);
    		res = current->callback(current->data);
    		ast_mutex_lock(&con->lock);
    
    		if (res) {
    		 	/*
    			 * If they return non-zero, we should schedule them to be
    			 * run again.
    			 */
    			if (sched_settime(&current->when, current->variable? res : current->resched)) {
    				sched_release(con, current);
    			} else
    				schedule(con, current);
    		} else {
    			/* No longer needed, so release it */
    		 	sched_release(con, current);
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_mutex_unlock(&con->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    }
    
    
    long ast_sched_when(struct sched_context *con,int id)
    {
    	struct sched *s;
    
    	DEBUG(ast_debug(1, "ast_sched_when()\n"));
    
    
    	ast_mutex_lock(&con->lock);
    
    	AST_LIST_TRAVERSE(&con->schedq, s, list) {
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    		if (s->id == id)
    			break;
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    		secs = s->when.tv_sec - now.tv_sec;
    
    	}
    	ast_mutex_unlock(&con->lock);
    
    	return secs;
    }