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 220 of file sched.c.

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

00221 {
00222    /*
00223     * Schedule callback(data) to happen when ms into the future
00224     */
00225    struct sched *tmp;
00226    DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
00227    if (!when) {
00228       ast_log(LOG_NOTICE, "Scheduled event in 0 ms?");
00229       return -1;
00230    }
00231    if ((tmp = sched_alloc(con))) {
00232       tmp->id = con->eventcnt++;
00233       tmp->callback = callback;
00234       tmp->data = data;
00235       tmp->resched = when;
00236       tmp->when.tv_sec = 0;
00237       tmp->when.tv_usec = 0;
00238       if (sched_settime(&tmp->when, when)) {
00239          sched_release(con, tmp);
00240          return -1;
00241       } else
00242          schedule(con, tmp);
00243    } else 
00244       return -1;
00245    return tmp->id;
00246 }

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 248 of file sched.c.

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

Referenced by ast_closestream().

00249 {
00250    /*
00251     * Delete the schedule entry with number
00252     * "id".  It's nearly impossible that there
00253     * would be two or more in the list with that
00254     * id.
00255     */
00256    struct sched *last=NULL, *s;
00257    DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
00258    s = con->schedq;
00259    while(s) {
00260       if (s->id == id) {
00261          if (last)
00262             last->next = s->next;
00263          else
00264             con->schedq = s->next;
00265          con->schedcnt--;
00266          sched_release(con, s);
00267          return 0;
00268       }
00269       last = s;
00270       s = s->next;
00271    }
00272    ast_log(LOG_NOTICE, "Attempted to delete non-existant schedule entry %d!\n", id);
00273 #ifdef DO_CRASH
00274    CRASH;
00275 #endif
00276    return -1;
00277 }

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 279 of file sched.c.

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

00280 {
00281    /*
00282     * Dump the contents of the scheduler to
00283     * stderr
00284     */
00285    struct sched *q;
00286    struct timeval tv;
00287    time_t s, ms;
00288    gettimeofday(&tv, NULL);
00289 #ifdef SCHED_MAX_CACHE
00290    ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n", 
00291                      con-> schedcnt, con->eventcnt - 1, con->schedccnt);
00292 #else
00293    ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total)\n",
00294                      con-> schedcnt, con->eventcnt - 1);
00295 #endif
00296 
00297    ast_log(LOG_DEBUG, "=================================================\n");
00298    ast_log(LOG_DEBUG, "|ID    Callback    Data        Time  (sec:ms)   |\n");
00299    ast_log(LOG_DEBUG, "+-----+-----------+-----------+-----------------+\n");
00300    q = con->schedq;
00301    while(q) {
00302       s =  q->when.tv_sec - tv.tv_sec;
00303       ms = q->when.tv_usec - tv.tv_usec;
00304       if (ms < 0) {
00305          ms += 1000000;
00306          s--;
00307       }
00308       ast_log(LOG_DEBUG, "|%.4d | %p | %p | %.6ld : %.6ld |\n", 
00309             q->id,
00310             q->callback,
00311             q->data,
00312             (long)s,
00313             (long)ms);
00314       q=q->next;
00315    }
00316    ast_log(LOG_DEBUG, "=================================================\n");
00317    
00318 }

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 320 of file sched.c.

References ast_log(), DEBUG, 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().

00321 {
00322    /*
00323     * Launch all events which need to be run at this time.
00324     */
00325    struct sched *current;
00326    struct timeval tv;
00327    int x=0;
00328    DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
00329       
00330    for(;;) {
00331       if (!con->schedq)
00332          break;
00333       if (gettimeofday(&tv, NULL)) {
00334          /* This should never happen */
00335          ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
00336          return 0;
00337       }
00338       /* We only care about millisecond accuracy anyway, so this will
00339          help us get more than one event at one time if they are very
00340          close together. */
00341       tv.tv_usec += 1000;
00342       if (SOONER(con->schedq->when, tv)) {
00343          current = con->schedq;
00344          con->schedq = con->schedq->next;
00345          con->schedcnt--;
00346 
00347          /*
00348           * At this point, the schedule queue is still intact.  We
00349           * have removed the first event and the rest is still there,
00350           * so it's permissible for the callback to add new events, but
00351           * trying to delete itself won't work because it isn't in
00352           * the schedule queue.  If that's what it wants to do, it 
00353           * should return 0.
00354           */
00355          if (current->callback(current->data)) {
00356             /*
00357              * If they return non-zero, we should schedule them to be
00358              * run again.
00359              */
00360             if (sched_settime(&current->when, current->resched)) {
00361                sched_release(con, current);
00362             } else
00363                schedule(con, current);
00364          } else {
00365             /* No longer needed, so release it */
00366             sched_release(con, current);
00367          }
00368          x++;
00369       } else
00370          break;
00371    }
00372    return x;
00373 }

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 132 of file sched.c.

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

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

00133 {
00134    /*
00135     * Return the number of milliseconds 
00136     * until the next scheduled event
00137     */
00138    struct timeval tv;
00139    int ms;
00140    DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
00141    if (!con->schedq)
00142       return -1;
00143    if (gettimeofday(&tv, NULL) < 0) {
00144       /* This should never happen */
00145       return 0;
00146    };
00147    ms = (con->schedq->when.tv_sec - tv.tv_sec) * 1000;
00148    ms += (con->schedq->when.tv_usec - tv.tv_usec) / 1000;
00149    if (ms < 0)
00150       ms = 0;
00151    return ms;
00152    
00153 }

struct sched_context* sched_context_create void   ) 
 

New schedule context.

Definition at line 58 of file sched.c.

References malloc.

Referenced by ast_channel_alloc().

00059 {
00060    struct sched_context *tmp;
00061    tmp = malloc(sizeof(struct sched_context));
00062    if (tmp) {
00063       tmp->eventcnt = 1;
00064       tmp->schedcnt = 0;
00065       tmp->schedq = NULL;
00066 #ifdef SCHED_MAX_CACHE
00067       tmp->schedc = NULL;
00068       tmp->schedccnt = 0;
00069 #endif
00070    }
00071    return tmp;
00072 }

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 74 of file sched.c.

References free, sched::next, and sched_context::schedq.

Referenced by ast_hangup().

00075 {
00076    struct sched *s, *sl;
00077 #ifdef SCHED_MAX_CACHE
00078    /* Eliminate the cache */
00079    s = con->schedc;
00080    while(s) {
00081       sl = s;
00082       s = s->next;
00083       free(sl);
00084    }
00085 #endif
00086    /* And the queue */
00087    s = con->schedq;
00088    while(s) {
00089       sl = s;
00090       s = s->next;
00091       free(sl);
00092    }
00093    /* And the context */
00094    free(con);
00095 }


Generated on Fri Oct 31 07:05:18 2003 for Asterisk by doxygen 1.3.4