Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

sched.h File Reference

Go to the source code of this file.

Defines

#define SCHED_MAX_CACHE   128
 Max num of schedule structs.

#define AST_SCHED_CB(a)   ((ast_sched_cb)(a))

Typedefs

typedef int(* ast_sched_cb )(void *data)
 callback for a cheops scheduler


Functions

sched_contextsched_context_create (void)
 New schedule context.

void sched_context_destroy (struct sched_context *c)
 destroys a schedule context

int ast_sched_add (struct sched_context *con, int when, ast_sched_cb callback, void *data)
 Adds a scheduled event.

int ast_sched_del (struct sched_context *con, int id)
 Deletes a scheduled event.

int ast_sched_wait (struct sched_context *con)
 Determines number of seconds until the next outstanding event to take place.

int ast_sched_runq (struct sched_context *con)
 Runs the queue.

void ast_sched_dump (struct sched_context *con)
 Dumps the scheduler contents.


Define Documentation

#define AST_SCHED_CB  )     ((ast_sched_cb)(a))
 

Definition at line 54 of file sched.h.

#define SCHED_MAX_CACHE   128
 

Max num of schedule structs.

The max number of schedule structs to keep around for use. Undefine to disable schedule structure caching. (Only disable this on very low memory machines)

Definition at line 28 of file sched.h.


Typedef Documentation

typedef int(* ast_sched_cb)(void *data)
 

callback for a cheops scheduler

A cheops scheduler callback takes a pointer with callback data and returns a 0 if it should not be run again, or non-zero if it should be rescheduled to run again

Definition at line 53 of file sched.h.

Referenced by ast_sched_add().


Function Documentation

int ast_sched_add struct sched_context con,
int  when,
ast_sched_cb  callback,
void *  data
 

Adds a scheduled event.

Parameters:
con Schduler context to add
when how many milliseconds to wait for event to occur
callback function to call when the amount of time expires
data data to pass to the callback Schedule an event to take place at some point in the future. callback will be called with data as the argument, when milliseconds into the future (approximately) Returns 0 on success, -1 on failure

Definition at line 229 of file sched.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_sched_cb, DEBUG, sched_context::eventcnt, sched_context::lock, LOG_DEBUG, and LOG_NOTICE.

00230 {
00231    /*
00232     * Schedule callback(data) to happen when ms into the future
00233     */
00234    struct sched *tmp;
00235    int res = -1;
00236    DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
00237    if (!when) {
00238       ast_log(LOG_NOTICE, "Scheduled event in 0 ms?\n");
00239       return -1;
00240    }
00241    ast_mutex_lock(&con->lock);
00242    if ((tmp = sched_alloc(con))) {
00243       tmp->id = con->eventcnt++;
00244       tmp->callback = callback;
00245       tmp->data = data;
00246       tmp->resched = when;
00247       tmp->when.tv_sec = 0;
00248       tmp->when.tv_usec = 0;
00249       if (sched_settime(&tmp->when, when)) {
00250          sched_release(con, tmp);
00251       } else {
00252          schedule(con, tmp);
00253          res = tmp->id;
00254       }
00255    }
00256    ast_mutex_unlock(&con->lock);
00257    return res;
00258 }

int ast_sched_del struct sched_context con,
int  id
 

Deletes a scheduled event.

Parameters:
con scheduling context to delete item from
id ID of the scheduled item to delete Remove this event from being run. A procedure should not remove its own event, but return 0 instead. Returns 0 on success, -1 on failure

Definition at line 260 of file sched.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, CRASH, DEBUG, sched_context::lock, LOG_DEBUG, LOG_NOTICE, sched::next, s, sched_context::schedcnt, and sched_context::schedq.

Referenced by ast_closestream().

00261 {
00262    /*
00263     * Delete the schedule entry with number
00264     * "id".  It's nearly impossible that there
00265     * would be two or more in the list with that
00266     * id.
00267     */
00268    struct sched *last=NULL, *s;
00269    DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
00270    ast_mutex_lock(&con->lock);
00271    s = con->schedq;
00272    while(s) {
00273       if (s->id == id) {
00274          if (last)
00275             last->next = s->next;
00276          else
00277             con->schedq = s->next;
00278          con->schedcnt--;
00279          sched_release(con, s);
00280          break;
00281       }
00282       last = s;
00283       s = s->next;
00284    }
00285    ast_mutex_unlock(&con->lock);
00286    if (!s) {
00287       ast_log(LOG_NOTICE, "Attempted to delete non-existant schedule entry %d!\n", id);
00288 #ifdef DO_CRASH
00289       CRASH;
00290 #endif
00291       return -1;
00292    } else
00293       return 0;
00294 }

void ast_sched_dump struct sched_context con  ) 
 

Dumps the scheduler contents.

Parameters:
con Context to dump Debugging: Dump the contents of the scheduler to stderr

Definition at line 296 of file sched.c.

References ast_log(), sched_context::eventcnt, LOG_DEBUG, s, and sched_context::schedq.

00297 {
00298    /*
00299     * Dump the contents of the scheduler to
00300     * stderr
00301     */
00302    struct sched *q;
00303    struct timeval tv;
00304    time_t s, ms;
00305    gettimeofday(&tv, NULL);
00306 #ifdef SCHED_MAX_CACHE
00307    ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n", 
00308                      con-> schedcnt, con->eventcnt - 1, con->schedccnt);
00309 #else
00310    ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total)\n",
00311                      con-> schedcnt, con->eventcnt - 1);
00312 #endif
00313 
00314    ast_log(LOG_DEBUG, "=================================================\n");
00315    ast_log(LOG_DEBUG, "|ID    Callback    Data        Time  (sec:ms)   |\n");
00316    ast_log(LOG_DEBUG, "+-----+-----------+-----------+-----------------+\n");
00317    q = con->schedq;
00318    while(q) {
00319       s =  q->when.tv_sec - tv.tv_sec;
00320       ms = q->when.tv_usec - tv.tv_usec;
00321       if (ms < 0) {
00322          ms += 1000000;
00323          s--;
00324       }
00325       ast_log(LOG_DEBUG, "|%.4d | %p | %p | %.6ld : %.6ld |\n", 
00326             q->id,
00327             q->callback,
00328             q->data,
00329             (long)s,
00330             (long)ms);
00331       q=q->next;
00332    }
00333    ast_log(LOG_DEBUG, "=================================================\n");
00334    
00335 }

int ast_sched_runq struct sched_context con  ) 
 

Runs the queue.

Parameters:
con Scheduling context to run Run the queue, executing all callbacks which need to be performed at this time. Returns the number of events processed.

Definition at line 337 of file sched.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, DEBUG, sched_context::lock, LOG_DEBUG, LOG_NOTICE, sched::next, sched_context::schedcnt, sched_context::schedq, SOONER, and sched::when.

Referenced by ast_waitstream(), ast_waitstream_fr(), and ast_waitstream_full().

00338 {
00339    /*
00340     * Launch all events which need to be run at this time.
00341     */
00342    struct sched *current;
00343    struct timeval tv;
00344    int x=0;
00345    int res;
00346    DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
00347       
00348    ast_mutex_lock(&con->lock);
00349    for(;;) {
00350       if (!con->schedq)
00351          break;
00352       if (gettimeofday(&tv, NULL)) {
00353          /* This should never happen */
00354          ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
00355          break;
00356       }
00357       /* We only care about millisecond accuracy anyway, so this will
00358          help us get more than one event at one time if they are very
00359          close together. */
00360       tv.tv_usec += 1000;
00361       if (SOONER(con->schedq->when, tv)) {
00362          current = con->schedq;
00363          con->schedq = con->schedq->next;
00364          con->schedcnt--;
00365 
00366          /*
00367           * At this point, the schedule queue is still intact.  We
00368           * have removed the first event and the rest is still there,
00369           * so it's permissible for the callback to add new events, but
00370           * trying to delete itself won't work because it isn't in
00371           * the schedule queue.  If that's what it wants to do, it 
00372           * should return 0.
00373           */
00374          
00375          ast_mutex_unlock(&con->lock);
00376          res = current->callback(current->data);
00377          ast_mutex_lock(&con->lock);
00378          
00379          if (res) {
00380             /*
00381              * If they return non-zero, we should schedule them to be
00382              * run again.
00383              */
00384             if (sched_settime(&current->when, current->resched)) {
00385                sched_release(con, current);
00386             } else
00387                schedule(con, current);
00388          } else {
00389             /* No longer needed, so release it */
00390             sched_release(con, current);
00391          }
00392          x++;
00393       } else
00394          break;
00395    }
00396    ast_mutex_unlock(&con->lock);
00397    return x;
00398 }

int ast_sched_wait struct sched_context con  ) 
 

Determines number of seconds until the next outstanding event to take place.

Parameters:
con context to act upon Determine the number of seconds until the next outstanding event should take place, and return the number of milliseconds until it needs to be run. This value is perfect for passing to the poll call. Returns "-1" if there is nothing there are no scheduled events (and thus the poll should not timeout)

Definition at line 138 of file sched.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, DEBUG, sched_context::lock, LOG_DEBUG, sched_context::schedq, and sched::when.

Referenced by ast_waitstream(), ast_waitstream_fr(), and ast_waitstream_full().

00139 {
00140    /*
00141     * Return the number of milliseconds 
00142     * until the next scheduled event
00143     */
00144    struct timeval tv;
00145    int ms;
00146    DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
00147    ast_mutex_lock(&con->lock);
00148    if (!con->schedq) {
00149       ms = -1;
00150    } else if (gettimeofday(&tv, NULL) < 0) {
00151       /* This should never happen */
00152       ms = 0;
00153    } else {
00154       ms = (con->schedq->when.tv_sec - tv.tv_sec) * 1000;
00155       ms += (con->schedq->when.tv_usec - tv.tv_usec) / 1000;
00156       if (ms < 0)
00157          ms = 0;
00158    }
00159    ast_mutex_unlock(&con->lock);
00160    return ms;
00161    
00162 }

struct sched_context* sched_context_create void   ) 
 

New schedule context.

Definition at line 61 of file sched.c.

References ast_mutex_init, and malloc.

Referenced by ast_channel_alloc().

00062 {
00063    struct sched_context *tmp;
00064    tmp = malloc(sizeof(struct sched_context));
00065    if (tmp) {
00066       ast_mutex_init(&tmp->lock);
00067       tmp->eventcnt = 1;
00068       tmp->schedcnt = 0;
00069       tmp->schedq = NULL;
00070 #ifdef SCHED_MAX_CACHE
00071       tmp->schedc = NULL;
00072       tmp->schedccnt = 0;
00073 #endif
00074    }
00075    return tmp;
00076 }

void sched_context_destroy struct sched_context c  ) 
 

destroys a schedule context

Parameters:
c Context to free Destroys (free's) the given sched_context structure Returns 0 on success, -1 on failure

Definition at line 78 of file sched.c.

References ast_mutex_lock, ast_mutex_unlock, free, sched_context::lock, s, and sched_context::schedq.

Referenced by ast_hangup().

00079 {
00080    struct sched *s, *sl;
00081    ast_mutex_lock(&con->lock);
00082 #ifdef SCHED_MAX_CACHE
00083    /* Eliminate the cache */
00084    s = con->schedc;
00085    while(s) {
00086       sl = s;
00087       s = s->next;
00088       free(sl);
00089    }
00090 #endif
00091    /* And the queue */
00092    s = con->schedq;
00093    while(s) {
00094       sl = s;
00095       s = s->next;
00096       free(sl);
00097    }
00098    /* And the context */
00099    ast_mutex_unlock(&con->lock);
00100    free(con);
00101 }


Generated on Sun Apr 18 23:34:18 2004 for Asterisk by doxygen 1.3.6-20040222