Codename Pineapple

Home page | Mailing list | Docs

Last updated: Sat Feb 3 05:00:55 2007

Asterisk developer's documentation :: Codename Pineapple


sched.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Scheduler Routines (from cheops-NG)
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 44253 $")
00029 
00030 #ifdef DEBUG_SCHEDULER
00031 #define DEBUG(a) do { \
00032    if (option_debug) \
00033       DEBUG_M(a) \
00034    } while (0)
00035 #else
00036 #define DEBUG(a) 
00037 #endif
00038 
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <sys/time.h>
00042 #include <unistd.h>
00043 #include <string.h>
00044 
00045 #include "asterisk/sched.h"
00046 #include "asterisk/logger.h"
00047 #include "asterisk/channel.h"
00048 #include "asterisk/lock.h"
00049 #include "asterisk/utils.h"
00050 #include "asterisk/linkedlists.h"
00051 #include "asterisk/options.h"
00052 
00053 struct sched {
00054    AST_LIST_ENTRY(sched) list;
00055    int id;                       /*!< ID number of event */
00056    struct timeval when;          /*!< Absolute time event should take place */
00057    int resched;                  /*!< When to reschedule */
00058    int variable;                 /*!< Use return value from callback to reschedule */
00059    void *data;                   /*!< Data */
00060    ast_sched_cb callback;        /*!< Callback */
00061 };
00062 
00063 struct sched_context {
00064    ast_mutex_t lock;
00065    unsigned int eventcnt;                  /*!< Number of events processed */
00066    unsigned int schedcnt;                  /*!< Number of outstanding schedule events */
00067    AST_LIST_HEAD_NOLOCK(, sched) schedq;   /*!< Schedule entry and main queue */
00068 
00069 #ifdef SCHED_MAX_CACHE
00070    AST_LIST_HEAD_NOLOCK(, sched) schedc;   /*!< Cache of unused schedule structures and how many */
00071    unsigned int schedccnt;
00072 #endif
00073 };
00074 
00075 struct sched_context *sched_context_create(void)
00076 {
00077    struct sched_context *tmp;
00078 
00079    if (!(tmp = ast_calloc(1, sizeof(*tmp))))
00080       return NULL;
00081 
00082    ast_mutex_init(&tmp->lock);
00083    tmp->eventcnt = 1;
00084    
00085    return tmp;
00086 }
00087 
00088 void sched_context_destroy(struct sched_context *con)
00089 {
00090    struct sched *s;
00091 
00092    ast_mutex_lock(&con->lock);
00093 
00094 #ifdef SCHED_MAX_CACHE
00095    /* Eliminate the cache */
00096    while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
00097       free(s);
00098 #endif
00099 
00100    /* And the queue */
00101    while ((s = AST_LIST_REMOVE_HEAD(&con->schedq, list)))
00102       free(s);
00103    
00104    /* And the context */
00105    ast_mutex_unlock(&con->lock);
00106    ast_mutex_destroy(&con->lock);
00107    free(con);
00108 }
00109 
00110 static struct sched *sched_alloc(struct sched_context *con)
00111 {
00112    struct sched *tmp;
00113 
00114    /*
00115     * We keep a small cache of schedule entries
00116     * to minimize the number of necessary malloc()'s
00117     */
00118 #ifdef SCHED_MAX_CACHE
00119    if ((tmp = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
00120       con->schedccnt--;
00121    else
00122 #endif
00123       tmp = ast_calloc(1, sizeof(*tmp));
00124 
00125    return tmp;
00126 }
00127 
00128 static void sched_release(struct sched_context *con, struct sched *tmp)
00129 {
00130    /*
00131     * Add to the cache, or just free() if we
00132     * already have too many cache entries
00133     */
00134 
00135 #ifdef SCHED_MAX_CACHE   
00136    if (con->schedccnt < SCHED_MAX_CACHE) {
00137       AST_LIST_INSERT_HEAD(&con->schedc, tmp, list);
00138       con->schedccnt++;
00139    } else
00140 #endif
00141       free(tmp);
00142 }
00143 
00144 /*! \brief
00145  * Return the number of milliseconds 
00146  * until the next scheduled event
00147  */
00148 int ast_sched_wait(struct sched_context *con)
00149 {
00150    int ms;
00151 
00152    DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
00153 
00154    ast_mutex_lock(&con->lock);
00155    if (AST_LIST_EMPTY(&con->schedq)) {
00156       ms = -1;
00157    } else {
00158       ms = ast_tvdiff_ms(AST_LIST_FIRST(&con->schedq)->when, ast_tvnow());
00159       if (ms < 0)
00160          ms = 0;
00161    }
00162    ast_mutex_unlock(&con->lock);
00163 
00164    return ms;
00165 }
00166 
00167 
00168 /*! \brief
00169  * Take a sched structure and put it in the
00170  * queue, such that the soonest event is
00171  * first in the list. 
00172  */
00173 static void schedule(struct sched_context *con, struct sched *s)
00174 {
00175     
00176    struct sched *cur = NULL;
00177    
00178    AST_LIST_TRAVERSE_SAFE_BEGIN(&con->schedq, cur, list) {
00179       if (ast_tvcmp(s->when, cur->when) == -1) {
00180          AST_LIST_INSERT_BEFORE_CURRENT(&con->schedq, s, list);
00181          break;
00182       }
00183    }
00184    AST_LIST_TRAVERSE_SAFE_END
00185    if (!cur)
00186       AST_LIST_INSERT_TAIL(&con->schedq, s, list);
00187    
00188    con->schedcnt++;
00189 }
00190 
00191 /*! \brief
00192  * given the last event *tv and the offset in milliseconds 'when',
00193  * computes the next value,
00194  */
00195 static int sched_settime(struct timeval *tv, int when)
00196 {
00197    struct timeval now = ast_tvnow();
00198 
00199    /*ast_log(LOG_DEBUG, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
00200    if (ast_tvzero(*tv)) /* not supplied, default to now */
00201       *tv = now;
00202    *tv = ast_tvadd(*tv, ast_samp2tv(when, 1000));
00203    if (ast_tvcmp(*tv, now) < 0) {
00204       if (option_debug)
00205          ast_log(LOG_DEBUG, "Request to schedule in the past?!?!\n");
00206       *tv = now;
00207    }
00208    return 0;
00209 }
00210 
00211 
00212 /*! \brief
00213  * Schedule callback(data) to happen when ms into the future
00214  */
00215 int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, void *data, int variable)
00216 {
00217    struct sched *tmp;
00218    int res = -1;
00219    DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
00220    if (!when) {
00221       ast_log(LOG_NOTICE, "Scheduled event in 0 ms?\n");
00222       return -1;
00223    }
00224    ast_mutex_lock(&con->lock);
00225    if ((tmp = sched_alloc(con))) {
00226       tmp->id = con->eventcnt++;
00227       tmp->callback = callback;
00228       tmp->data = data;
00229       tmp->resched = when;
00230       tmp->variable = variable;
00231       tmp->when = ast_tv(0, 0);
00232       if (sched_settime(&tmp->when, when)) {
00233          sched_release(con, tmp);
00234       } else {
00235          schedule(con, tmp);
00236          res = tmp->id;
00237       }
00238    }
00239 #ifdef DUMP_SCHEDULER
00240    /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
00241    if (option_debug)
00242       ast_sched_dump(con);
00243 #endif
00244    ast_mutex_unlock(&con->lock);
00245    return res;
00246 }
00247 
00248 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, void *data)
00249 {
00250    return ast_sched_add_variable(con, when, callback, data, 0);
00251 }
00252 
00253 /*! \brief
00254  * Delete the schedule entry with number
00255  * "id".  It's nearly impossible that there
00256  * would be two or more in the list with that
00257  * id.
00258  */
00259 int ast_sched_del(struct sched_context *con, int id)
00260 {
00261    struct sched *s;
00262 
00263    DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
00264    
00265    ast_mutex_lock(&con->lock);
00266    AST_LIST_TRAVERSE_SAFE_BEGIN(&con->schedq, s, list) {
00267       if (s->id == id) {
00268          AST_LIST_REMOVE_CURRENT(&con->schedq, list);
00269          con->schedcnt--;
00270          sched_release(con, s);
00271          break;
00272       }
00273    }
00274    AST_LIST_TRAVERSE_SAFE_END
00275 
00276 #ifdef DUMP_SCHEDULER
00277    /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
00278    if (option_debug)
00279       ast_sched_dump(con);
00280 #endif
00281    ast_mutex_unlock(&con->lock);
00282 
00283    if (!s) {
00284       ast_log(LOG_NOTICE, "Attempted to delete nonexistent schedule entry %d!\n", id);
00285 #ifdef DO_CRASH
00286       CRASH;
00287 #endif
00288       return -1;
00289    }
00290    
00291    return 0;
00292 }
00293 
00294 /*! \brief Dump the contents of the scheduler to LOG_DEBUG */
00295 void ast_sched_dump(const struct sched_context *con)
00296 {
00297    struct sched *q;
00298    struct timeval tv = ast_tvnow();
00299 #ifdef SCHED_MAX_CACHE
00300    if (option_debug)
00301       ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n", con->schedcnt, con->eventcnt - 1, con->schedccnt);
00302 #else
00303    if (option_debug)
00304       ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total)\n", con->schedcnt, con->eventcnt - 1);
00305 #endif
00306 
00307    if (option_debug) {
00308    ast_log(LOG_DEBUG, "=============================================================\n");
00309    ast_log(LOG_DEBUG, "|ID    Callback          Data              Time  (sec:ms)   |\n");
00310    ast_log(LOG_DEBUG, "+-----+-----------------+-----------------+-----------------+\n");
00311       AST_LIST_TRAVERSE(&con->schedq, q, list) {
00312          struct timeval delta = ast_tvsub(q->when, tv);
00313 
00314          ast_log(LOG_DEBUG, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", 
00315             q->id,
00316             q->callback,
00317             q->data,
00318             delta.tv_sec,
00319             (long int)delta.tv_usec);
00320       }
00321       ast_log(LOG_DEBUG, "=============================================================\n");
00322    }
00323 }
00324 
00325 /*! \brief
00326  * Launch all events which need to be run at this time.
00327  */
00328 int ast_sched_runq(struct sched_context *con)
00329 {
00330    struct sched *current;
00331    struct timeval tv;
00332    int numevents;
00333    int res;
00334 
00335    DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
00336       
00337    ast_mutex_lock(&con->lock);
00338 
00339    for (numevents = 0; !AST_LIST_EMPTY(&con->schedq); numevents++) {
00340       /* schedule all events which are going to expire within 1ms.
00341        * We only care about millisecond accuracy anyway, so this will
00342        * help us get more than one event at one time if they are very
00343        * close together.
00344        */
00345       tv = ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
00346       if (ast_tvcmp(AST_LIST_FIRST(&con->schedq)->when, tv) != -1)
00347          break;
00348       
00349       current = AST_LIST_REMOVE_HEAD(&con->schedq, list);
00350       con->schedcnt--;
00351 
00352       /*
00353        * At this point, the schedule queue is still intact.  We
00354        * have removed the first event and the rest is still there,
00355        * so it's permissible for the callback to add new events, but
00356        * trying to delete itself won't work because it isn't in
00357        * the schedule queue.  If that's what it wants to do, it 
00358        * should return 0.
00359        */
00360          
00361       ast_mutex_unlock(&con->lock);
00362       res = current->callback(current->data);
00363       ast_mutex_lock(&con->lock);
00364          
00365       if (res) {
00366          /*
00367           * If they return non-zero, we should schedule them to be
00368           * run again.
00369           */
00370          if (sched_settime(&current->when, current->variable? res : current->resched)) {
00371             sched_release(con, current);
00372          } else
00373             schedule(con, current);
00374       } else {
00375          /* No longer needed, so release it */
00376          sched_release(con, current);
00377       }
00378    }
00379 
00380    ast_mutex_unlock(&con->lock);
00381    
00382    return numevents;
00383 }
00384 
00385 long ast_sched_when(struct sched_context *con,int id)
00386 {
00387    struct sched *s;
00388    long secs = -1;
00389    DEBUG(ast_log(LOG_DEBUG, "ast_sched_when()\n"));
00390 
00391    ast_mutex_lock(&con->lock);
00392    AST_LIST_TRAVERSE(&con->schedq, s, list) {
00393       if (s->id == id)
00394          break;
00395    }
00396    if (s) {
00397       struct timeval now = ast_tvnow();
00398       secs = s->when.tv_sec - now.tv_sec;
00399    }
00400    ast_mutex_unlock(&con->lock);
00401    
00402    return secs;
00403 }

Asterisk is a trademark for Digium, inc.. | Edvina.net | Asterisk.org | This documentation was generated with Doxygen