Fri Nov 12 12:09:30 2010

Asterisk developer's documentation


sched.c File Reference

Scheduler Routines (from cheops-NG). More...

#include "asterisk.h"
#include <sys/time.h>
#include "asterisk/sched.h"
#include "asterisk/channel.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
#include "asterisk/linkedlists.h"
#include "asterisk/dlinkedlists.h"
#include "asterisk/hashtab.h"
#include "asterisk/heap.h"
Include dependency graph for sched.c:

Go to the source code of this file.

Data Structures

struct  ast_sched_thread
struct  sched
struct  sched_context

Defines

#define DEBUG(a)

Functions

int ast_sched_add (struct sched_context *con, int when, ast_sched_cb callback, const void *data)
 Adds a scheduled event 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) If callback returns 0, no further events will be re-scheduled.
int ast_sched_add_variable (struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable)
 Schedule callback(data) to happen when ms into the future.
int ast_sched_del (struct sched_context *con, int id)
 Delete the schedule entry with number "id". It's nearly impossible that there would be two or more in the list with that id.
void ast_sched_dump (struct sched_context *con)
 Dump the contents of the scheduler to LOG_DEBUG.
const void * ast_sched_find_data (struct sched_context *con, int id)
 Find a sched structure and return the data field associated with it.
int ast_sched_replace (int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data)
 replace a scheduler entry
int ast_sched_replace_variable (int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable)
 replace a scheduler entry
void ast_sched_report (struct sched_context *con, struct ast_str **buf, struct ast_cb_names *cbnames)
 Show statics on what it is in the schedule queue.
int ast_sched_runq (struct sched_context *con)
 Launch all events which need to be run at this time.
int ast_sched_thread_add (struct ast_sched_thread *st, int when, ast_sched_cb cb, const void *data)
 Add a scheduler entry.
int ast_sched_thread_add_variable (struct ast_sched_thread *st, int when, ast_sched_cb cb, const void *data, int variable)
 Add a variable reschedule time scheduler entry.
struct ast_sched_threadast_sched_thread_create (void)
 Create a scheduler with a dedicated thread.
struct ast_sched_threadast_sched_thread_destroy (struct ast_sched_thread *st)
 Destroy a scheduler and its thread.
struct sched_contextast_sched_thread_get_context (struct ast_sched_thread *st)
 Get the scheduler context for a given ast_sched_thread.
void ast_sched_thread_poke (struct ast_sched_thread *st)
 Force re-processing of the scheduler context.
int ast_sched_wait (struct sched_context *con)
 Return the number of milliseconds until the next scheduled event.
long ast_sched_when (struct sched_context *con, int id)
 Returns the number of seconds before an event takes place.
static struct schedsched_alloc (struct sched_context *con)
static int sched_cmp (const void *a, const void *b)
struct sched_contextsched_context_create (void)
 New schedule context.
void sched_context_destroy (struct sched_context *con)
 destroys a schedule context Destroys (free's) the given sched_context structure
static unsigned int sched_hash (const void *obj)
static void sched_release (struct sched_context *con, struct sched *tmp)
static void * sched_run (void *data)
static int sched_settime (struct timeval *t, int when)
 given the last event *tv and the offset in milliseconds 'when', computes the next value,
static int sched_time_cmp (void *a, void *b)
static void schedule (struct sched_context *con, struct sched *s)
 Take a sched structure and put it in the queue, such that the soonest event is first in the list.

Detailed Description

Scheduler Routines (from cheops-NG).

Author:
Mark Spencer <markster@digium.com>

Definition in file sched.c.


Define Documentation

#define DEBUG (  ) 

Definition at line 36 of file sched.c.


Function Documentation

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

Adds a scheduled event 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) If callback returns 0, no further events will be re-scheduled.

Parameters:
con Scheduler 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
Returns:
Returns a schedule item ID on success, -1 on failure

Definition at line 439 of file sched.c.

References ast_sched_add_variable().

Referenced by __oh323_update_info(), ast_readaudio_callback(), ast_readvideo_callback(), ast_rtp_raw_write(), ast_rtp_read(), ast_sched_replace(), ast_sched_thread_add(), build_peer(), do_register(), do_reload(), dundi_discover(), dundi_query(), dundi_send(), handle_command_response(), handle_request_invite(), handle_response_invite(), handle_response_subscribe(), mgcp_postrequest(), parse_register_contact(), populate_addr(), precache_trans(), qualify_peer(), queue_request(), receive_digit(), rtp_red_init(), sip_scheddestroy(), sip_send_all_mwi_subscriptions(), start_session_timer(), submit_scheduled_batch(), submit_unscheduled_batch(), transmit_register(), and update_provisional_keepalive().

00440 {
00441    return ast_sched_add_variable(con, when, callback, data, 0);
00442 }

int ast_sched_add_variable ( struct sched_context con,
int  when,
ast_sched_cb  callback,
const void *  data,
int  variable 
)

Schedule callback(data) to happen when ms into the future.

Adds a scheduled event with rescheduling support

Parameters:
con Scheduler 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
variable If true, the result value of callback function will be used for rescheduling 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) If callback returns 0, no further events will be re-scheduled
Returns:
Returns a schedule item ID on success, -1 on failure

Definition at line 399 of file sched.c.

References ast_debug, ast_mutex_lock(), ast_mutex_unlock(), ast_sched_dump(), ast_tv(), sched::callback, sched::data, DEBUG, sched_context::eventcnt, sched::id, sched_context::lock, option_debug, sched::resched, sched_alloc(), sched_release(), sched_settime(), schedule(), sched::variable, and sched::when.

Referenced by _misdn_tasks_add_variable(), ast_sched_add(), ast_sched_replace_variable(), ast_sched_thread_add_variable(), dnsmgr_start_refresh(), and do_reload().

00400 {
00401    struct sched *tmp;
00402    int res = -1;
00403 
00404    DEBUG(ast_debug(1, "ast_sched_add()\n"));
00405 
00406    ast_mutex_lock(&con->lock);
00407    if ((tmp = sched_alloc(con))) {
00408       tmp->id = con->eventcnt++;
00409       tmp->callback = callback;
00410       tmp->data = data;
00411       tmp->resched = when;
00412       tmp->variable = variable;
00413       tmp->when = ast_tv(0, 0);
00414       if (sched_settime(&tmp->when, when)) {
00415          sched_release(con, tmp);
00416       } else {
00417          schedule(con, tmp);
00418          res = tmp->id;
00419       }
00420    }
00421 #ifdef DUMP_SCHEDULER
00422    /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
00423    if (option_debug)
00424       ast_sched_dump(con);
00425 #endif
00426    ast_mutex_unlock(&con->lock);
00427 
00428    return res;
00429 }

int ast_sched_del ( struct sched_context con,
int  id 
)

Delete the schedule entry with number "id". It's nearly impossible that there would be two or more in the list with that id.

Deletes a scheduled event Remove this event from being run. A procedure should not remove its own event, but return 0 instead. In most cases, you should not call this routine directly, but use the AST_SCHED_DEL() macro instead (especially if you don't intend to do something different when it returns failure).

Definition at line 461 of file sched.c.

References ast_assert, ast_debug, ast_hashtab_lookup(), ast_hashtab_remove_this_object(), ast_heap_remove(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_dump(), DEBUG, sched::id, sched_context::lock, LOG_WARNING, option_debug, s, sched_context::sched_heap, sched_release(), sched_context::schedcnt, and sched_context::schedq_ht.

Referenced by __sip_ack(), and sip_cancel_destroy().

00465 {
00466    struct sched *s, tmp = {
00467       .id = id,
00468    };
00469 
00470    DEBUG(ast_debug(1, "ast_sched_del(%d)\n", id));
00471    
00472    ast_mutex_lock(&con->lock);
00473    s = ast_hashtab_lookup(con->schedq_ht, &tmp);
00474    if (s) {
00475       if (!ast_heap_remove(con->sched_heap, s)) {
00476          ast_log(LOG_WARNING,"sched entry %d not in the sched heap?\n", s->id);
00477       }
00478 
00479       if (!ast_hashtab_remove_this_object(con->schedq_ht, s)) {
00480          ast_log(LOG_WARNING,"Found sched entry %d, then couldn't remove it?\n", s->id);
00481       }
00482 
00483       con->schedcnt--;
00484 
00485       sched_release(con, s);
00486    }
00487    
00488 #ifdef DUMP_SCHEDULER
00489    /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
00490    if (option_debug)
00491       ast_sched_dump(con);
00492 #endif
00493    ast_mutex_unlock(&con->lock);
00494 
00495    if (!s) {
00496       ast_debug(1, "Attempted to delete nonexistent schedule entry %d!\n", id);
00497 #ifndef AST_DEVMODE
00498       ast_assert(s != NULL);
00499 #else
00500       _ast_assert(0, "s != NULL", file, line, function);
00501 #endif
00502       return -1;
00503    }
00504    
00505    return 0;
00506 }

void ast_sched_dump ( struct sched_context con  ) 

Dump the contents of the scheduler to LOG_DEBUG.

Dumps the scheduler contents Debugging: Dump the contents of the scheduler to stderr.

Definition at line 546 of file sched.c.

References ast_debug, ast_heap_peek(), ast_heap_size(), ast_mutex_lock(), ast_mutex_unlock(), ast_tvnow(), ast_tvsub(), sched_context::eventcnt, sched_context::highwater, sched_context::lock, sched_context::sched_heap, sched_context::schedccnt, and sched_context::schedcnt.

Referenced by ast_sched_add_variable(), ast_sched_del(), sip_do_reload(), and unload_module().

00547 {
00548    struct sched *q;
00549    struct timeval when = ast_tvnow();
00550    int x;
00551    size_t heap_size;
00552 #ifdef SCHED_MAX_CACHE
00553    ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache, %d high-water)\n", con->schedcnt, con->eventcnt - 1, con->schedccnt, con->highwater);
00554 #else
00555    ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total, %d high-water)\n", con->schedcnt, con->eventcnt - 1, con->highwater);
00556 #endif
00557 
00558    ast_debug(1, "=============================================================\n");
00559    ast_debug(1, "|ID    Callback          Data              Time  (sec:ms)   |\n");
00560    ast_debug(1, "+-----+-----------------+-----------------+-----------------+\n");
00561    ast_mutex_lock(&con->lock);
00562    heap_size = ast_heap_size(con->sched_heap);
00563    for (x = 1; x <= heap_size; x++) {
00564       struct timeval delta;
00565       q = ast_heap_peek(con->sched_heap, x);
00566       delta = ast_tvsub(q->when, when);
00567       ast_debug(1, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", 
00568          q->id,
00569          q->callback,
00570          q->data,
00571          (long)delta.tv_sec,
00572          (long int)delta.tv_usec);
00573    }
00574    ast_mutex_unlock(&con->lock);
00575    ast_debug(1, "=============================================================\n");
00576 }

const void* ast_sched_find_data ( struct sched_context con,
int  id 
)

Find a sched structure and return the data field associated with it.

Parameters:
con scheduling context in which to search fro the matching id
id ID of the scheduled item to find
Returns:
the data field from the matching sched struct if found; else return NULL if not found.
Since:
1.6.1

Definition at line 444 of file sched.c.

References ast_hashtab_lookup(), sched::data, sched::id, and sched_context::schedq_ht.

00445 {
00446    struct sched tmp,*res;
00447    tmp.id = id;
00448    res = ast_hashtab_lookup(con->schedq_ht, &tmp);
00449    if (res)
00450       return res->data;
00451    return NULL;
00452 }

int ast_sched_replace ( int  old_id,
struct sched_context con,
int  when,
ast_sched_cb  callback,
const void *  data 
)

replace a scheduler entry

Deprecated:
You should use the AST_SCHED_REPLACE() macro instead.

This deletes the scheduler entry for old_id if it exists, and then calls ast_sched_add to create a new entry. A negative old_id will be ignored.

Return values:
-1 failure
otherwise,returns scheduled item ID

Definition at line 431 of file sched.c.

References ast_sched_add(), and AST_SCHED_DEL.

00432 {
00433    if (old_id > -1) {
00434       AST_SCHED_DEL(con, old_id);
00435    }
00436    return ast_sched_add(con, when, callback, data);
00437 }

int ast_sched_replace_variable ( int  old_id,
struct sched_context con,
int  when,
ast_sched_cb  callback,
const void *  data,
int  variable 
)

replace a scheduler entry

Deprecated:
You should use the AST_SCHED_REPLACE_VARIABLE() macro instead.

This deletes the scheduler entry for old_id if it exists, and then calls ast_sched_add to create a new entry. A negative old_id will be ignored.

Return values:
-1 failure
otherwise,returns scheduled item ID

Definition at line 387 of file sched.c.

References ast_sched_add_variable(), and AST_SCHED_DEL.

00388 {
00389    /* 0 means the schedule item is new; do not delete */
00390    if (old_id > 0) {
00391       AST_SCHED_DEL(con, old_id);
00392    }
00393    return ast_sched_add_variable(con, when, callback, data, variable);
00394 }

void ast_sched_report ( struct sched_context con,
struct ast_str **  buf,
struct ast_cb_names cbnames 
)

Show statics on what it is in the schedule queue.

Parameters:
con Schedule context to check
buf dynamic string to store report
cbnames to check against
Since:
1.6.1

Definition at line 508 of file sched.c.

References ast_heap_peek(), ast_heap_size(), ast_mutex_lock(), ast_mutex_unlock(), ast_str_append(), ast_str_set(), sched::callback, ast_cb_names::cblist, sched_context::highwater, ast_cb_names::list, sched_context::lock, ast_cb_names::numassocs, sched_context::sched_heap, and sched_context::schedcnt.

Referenced by sip_show_sched().

00509 {
00510    int i, x;
00511    struct sched *cur;
00512    int countlist[cbnames->numassocs + 1];
00513    size_t heap_size;
00514    
00515    memset(countlist, 0, sizeof(countlist));
00516    ast_str_set(buf, 0, " Highwater = %d\n schedcnt = %d\n", con->highwater, con->schedcnt);
00517 
00518    ast_mutex_lock(&con->lock);
00519 
00520    heap_size = ast_heap_size(con->sched_heap);
00521    for (x = 1; x <= heap_size; x++) {
00522       cur = ast_heap_peek(con->sched_heap, x);
00523       /* match the callback to the cblist */
00524       for (i = 0; i < cbnames->numassocs; i++) {
00525          if (cur->callback == cbnames->cblist[i]) {
00526             break;
00527          }
00528       }
00529       if (i < cbnames->numassocs) {
00530          countlist[i]++;
00531       } else {
00532          countlist[cbnames->numassocs]++;
00533       }
00534    }
00535 
00536    ast_mutex_unlock(&con->lock);
00537 
00538    for (i = 0; i < cbnames->numassocs; i++) {
00539       ast_str_append(buf, 0, "    %s : %d\n", cbnames->list[i], countlist[i]);
00540    }
00541 
00542    ast_str_append(buf, 0, "   <unknown> : %d\n", countlist[cbnames->numassocs]);
00543 }

int ast_sched_runq ( struct sched_context con  ) 

Launch all events which need to be run at this time.

Runs the queue.

Definition at line 581 of file sched.c.

References ast_debug, ast_hashtab_remove_this_object(), ast_heap_peek(), ast_heap_pop(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_tv(), ast_tvadd(), ast_tvcmp(), ast_tvnow(), sched::callback, sched::data, DEBUG, sched::id, sched_context::lock, LOG_ERROR, sched::resched, sched_context::sched_heap, sched_release(), sched_settime(), sched_context::schedcnt, sched_context::schedq_ht, schedule(), sched::variable, and sched::when.

Referenced by background_detect_exec(), dial_exec_full(), do_cdr(), do_monitor(), do_refresh(), handle_speechrecognize(), misdn_tasks_thread_func(), network_thread(), reload_config(), sched_run(), speech_background(), wait_for_winner(), and waitstream_core().

00582 {
00583    struct sched *current;
00584    struct timeval when;
00585    int numevents;
00586    int res;
00587 
00588    DEBUG(ast_debug(1, "ast_sched_runq()\n"));
00589       
00590    ast_mutex_lock(&con->lock);
00591 
00592    for (numevents = 0; (current = ast_heap_peek(con->sched_heap, 1)); numevents++) {
00593       /* schedule all events which are going to expire within 1ms.
00594        * We only care about millisecond accuracy anyway, so this will
00595        * help us get more than one event at one time if they are very
00596        * close together.
00597        */
00598       when = ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
00599       if (ast_tvcmp(current->when, when) != -1) {
00600          break;
00601       }
00602       
00603       current = ast_heap_pop(con->sched_heap);
00604 
00605       if (!ast_hashtab_remove_this_object(con->schedq_ht, current)) {
00606          ast_log(LOG_ERROR,"Sched entry %d was in the schedq list but not in the hashtab???\n", current->id);
00607       }
00608 
00609       con->schedcnt--;
00610 
00611       /*
00612        * At this point, the schedule queue is still intact.  We
00613        * have removed the first event and the rest is still there,
00614        * so it's permissible for the callback to add new events, but
00615        * trying to delete itself won't work because it isn't in
00616        * the schedule queue.  If that's what it wants to do, it 
00617        * should return 0.
00618        */
00619          
00620       ast_mutex_unlock(&con->lock);
00621       res = current->callback(current->data);
00622       ast_mutex_lock(&con->lock);
00623          
00624       if (res) {
00625          /*
00626           * If they return non-zero, we should schedule them to be
00627           * run again.
00628           */
00629          if (sched_settime(&current->when, current->variable? res : current->resched)) {
00630             sched_release(con, current);
00631          } else {
00632             schedule(con, current);
00633          }
00634       } else {
00635          /* No longer needed, so release it */
00636          sched_release(con, current);
00637       }
00638    }
00639 
00640    ast_mutex_unlock(&con->lock);
00641    
00642    return numevents;
00643 }

int ast_sched_thread_add ( struct ast_sched_thread st,
int  when,
ast_sched_cb  cb,
const void *  data 
)

Add a scheduler entry.

Parameters:
st the handle to the scheduler and thread
when the number of ms in the future to run the task. A value <= 0 is treated as "run now".
cb the function to call when the scheduled time arrives
data the parameter to pass to the scheduler callback
Return values:
-1 Failure
>=0 Sched ID of added task

Definition at line 203 of file sched.c.

References ast_cond_signal(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_add(), ast_sched_thread::cond, ast_sched_thread::context, and ast_sched_thread::lock.

Referenced by iax2_hangup(), iax2_key_rotate(), iax2_sched_add(), and iax2_sched_replace().

00205 {
00206    int res;
00207 
00208    ast_mutex_lock(&st->lock);
00209    res = ast_sched_add(st->context, when, cb, data);
00210    if (res != -1) {
00211       ast_cond_signal(&st->cond);
00212    }
00213    ast_mutex_unlock(&st->lock);
00214 
00215    return res;
00216 }

int ast_sched_thread_add_variable ( struct ast_sched_thread st,
int  when,
ast_sched_cb  cb,
const void *  data,
int  variable 
)

Add a variable reschedule time scheduler entry.

Parameters:
st the handle to the scheduler and thread
when the number of ms in the future to run the task. A value <= 0 is treated as "run now".
cb the function to call when the scheduled time arrives
data the parameter to pass to the scheduler callback
variable If this value is non-zero, then the scheduler will use the return value of the scheduler as the amount of time in the future to run the task again. Normally, a return value of 0 means do not re-schedule, and non-zero means re-schedule using the time provided when the scheduler entry was first created.
Return values:
-1 Failure
>=0 Sched ID of added task

Definition at line 188 of file sched.c.

References ast_cond_signal(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_add_variable(), ast_sched_thread::cond, ast_sched_thread::context, and ast_sched_thread::lock.

00190 {
00191    int res;
00192 
00193    ast_mutex_lock(&st->lock);
00194    res = ast_sched_add_variable(st->context, when, cb, data, variable);
00195    if (res != -1) {
00196       ast_cond_signal(&st->cond);
00197    }
00198    ast_mutex_unlock(&st->lock);
00199 
00200    return res;
00201 }

struct ast_sched_thread* ast_sched_thread_create ( void   )  [read]

Create a scheduler with a dedicated thread.

This function should be used to allocate a scheduler context and a dedicated thread for processing scheduler entries. The thread is started immediately.

Return values:
NULL error
non-NULL a handle to the scheduler and its dedicated thread.

Definition at line 160 of file sched.c.

References ast_calloc, ast_cond_init(), ast_log(), ast_mutex_init(), ast_pthread_create_background, AST_PTHREADT_NULL, ast_sched_thread_destroy(), ast_sched_thread::cond, ast_sched_thread::context, ast_sched_thread::lock, LOG_ERROR, sched_context_create(), sched_run(), and ast_sched_thread::thread.

Referenced by load_module().

00161 {
00162    struct ast_sched_thread *st;
00163 
00164    if (!(st = ast_calloc(1, sizeof(*st)))) {
00165       return NULL;
00166    }
00167 
00168    ast_mutex_init(&st->lock);
00169    ast_cond_init(&st->cond, NULL);
00170 
00171    st->thread = AST_PTHREADT_NULL;
00172 
00173    if (!(st->context = sched_context_create())) {
00174       ast_log(LOG_ERROR, "Failed to create scheduler\n");
00175       ast_sched_thread_destroy(st);
00176       return NULL;
00177    }
00178    
00179    if (ast_pthread_create_background(&st->thread, NULL, sched_run, st)) {
00180       ast_log(LOG_ERROR, "Failed to create scheduler thread\n");
00181       ast_sched_thread_destroy(st);
00182       return NULL;
00183    }
00184 
00185    return st;
00186 }

struct ast_sched_thread* ast_sched_thread_destroy ( struct ast_sched_thread st  )  [read]

Destroy a scheduler and its thread.

This function is used to destroy a scheduler context and the dedicated thread that was created for handling scheduler entries. Any entries in the scheduler that have not yet been processed will be thrown away. Once this function is called, the handle must not be used again.

Parameters:
st the handle to the scheduler and thread
Returns:
NULL for convenience

Definition at line 136 of file sched.c.

References ast_cond_destroy(), ast_cond_signal(), ast_free, ast_mutex_destroy(), ast_mutex_lock(), ast_mutex_unlock(), AST_PTHREADT_NULL, ast_sched_thread::cond, ast_sched_thread::context, ast_sched_thread::lock, sched_context_destroy(), ast_sched_thread::stop, and ast_sched_thread::thread.

Referenced by __unload_module(), ast_sched_thread_create(), and load_module().

00137 {
00138    if (st->thread != AST_PTHREADT_NULL) {
00139       ast_mutex_lock(&st->lock);
00140       st->stop = 1;
00141       ast_cond_signal(&st->cond);
00142       ast_mutex_unlock(&st->lock);
00143       pthread_join(st->thread, NULL);
00144       st->thread = AST_PTHREADT_NULL;
00145    }
00146 
00147    ast_mutex_destroy(&st->lock);
00148    ast_cond_destroy(&st->cond);
00149 
00150    if (st->context) {
00151       sched_context_destroy(st->context);
00152       st->context = NULL;
00153    }
00154 
00155    ast_free(st);
00156 
00157    return NULL;
00158 }

struct sched_context* ast_sched_thread_get_context ( struct ast_sched_thread st  )  [read]

Get the scheduler context for a given ast_sched_thread.

This function should be used only when direct access to the scheduler context is required. Its use is discouraged unless necessary. The cases where this is currently required is when you want to take advantage of one of the AST_SCHED macros.

Parameters:
st the handle to the scheduler and thread
Returns:
the sched_context associated with an ast_sched_thread

Definition at line 131 of file sched.c.

References ast_sched_thread::context.

Referenced by iax2_destroy_helper().

00132 {
00133    return st->context;
00134 }

void ast_sched_thread_poke ( struct ast_sched_thread st  ) 

Force re-processing of the scheduler context.

Parameters:
st the handle to the scheduler and thread
Returns:
nothing

Definition at line 124 of file sched.c.

References ast_cond_signal(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_thread::cond, and ast_sched_thread::lock.

Referenced by iax2_transmit().

00125 {
00126    ast_mutex_lock(&st->lock);
00127    ast_cond_signal(&st->cond);
00128    ast_mutex_unlock(&st->lock);
00129 }

int ast_sched_wait ( struct sched_context con  ) 

Return the number of milliseconds until the next scheduled event.

Determines number of seconds until the next outstanding event to take place 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.

Definition at line 327 of file sched.c.

References ast_debug, ast_heap_peek(), ast_mutex_lock(), ast_mutex_unlock(), ast_tvdiff_ms(), ast_tvnow(), DEBUG, sched_context::lock, s, sched_context::sched_heap, and sched::when.

Referenced by background_detect_exec(), dial_exec_full(), do_cdr(), do_monitor(), do_refresh(), handle_speechrecognize(), misdn_tasks_thread_func(), network_thread(), sched_run(), speech_background(), wait_for_winner(), and waitstream_core().

00328 {
00329    int ms;
00330    struct sched *s;
00331 
00332    DEBUG(ast_debug(1, "ast_sched_wait()\n"));
00333 
00334    ast_mutex_lock(&con->lock);
00335    if ((s = ast_heap_peek(con->sched_heap, 1))) {
00336       ms = ast_tvdiff_ms(s->when, ast_tvnow());
00337       if (ms < 0) {
00338          ms = 0;
00339       }
00340    } else {
00341       ms = -1;
00342    }
00343    ast_mutex_unlock(&con->lock);
00344 
00345    return ms;
00346 }

long ast_sched_when ( struct sched_context con,
int  id 
)

Returns the number of seconds before an event takes place.

Parameters:
con Context to use
id Id to dump

Definition at line 645 of file sched.c.

References ast_debug, ast_hashtab_lookup(), ast_mutex_lock(), ast_mutex_unlock(), ast_tvnow(), DEBUG, sched::id, sched_context::lock, s, sched_context::schedq_ht, and sched::when.

Referenced by _sip_show_peer(), handle_cli_status(), and parse_register_contact().

00646 {
00647    struct sched *s, tmp;
00648    long secs = -1;
00649    DEBUG(ast_debug(1, "ast_sched_when()\n"));
00650 
00651    ast_mutex_lock(&con->lock);
00652    
00653    /* these next 2 lines replace a lookup loop */
00654    tmp.id = id;
00655    s = ast_hashtab_lookup(con->schedq_ht, &tmp);
00656    
00657    if (s) {
00658       struct timeval now = ast_tvnow();
00659       secs = s->when.tv_sec - now.tv_sec;
00660    }
00661    ast_mutex_unlock(&con->lock);
00662    
00663    return secs;
00664 }

static struct sched* sched_alloc ( struct sched_context con  )  [static, read]

Definition at line 289 of file sched.c.

References ast_calloc, AST_LIST_REMOVE_HEAD, sched_context::schedc, and sched_context::schedccnt.

Referenced by ast_sched_add_variable().

00290 {
00291    struct sched *tmp;
00292 
00293    /*
00294     * We keep a small cache of schedule entries
00295     * to minimize the number of necessary malloc()'s
00296     */
00297 #ifdef SCHED_MAX_CACHE
00298    if ((tmp = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
00299       con->schedccnt--;
00300    else
00301 #endif
00302       tmp = ast_calloc(1, sizeof(*tmp));
00303 
00304    return tmp;
00305 }

static int sched_cmp ( const void *  a,
const void *  b 
) [static]

Definition at line 220 of file sched.c.

References sched::id.

Referenced by sched_context_create().

00221 {
00222    const struct sched *as = a;
00223    const struct sched *bs = b;
00224    return as->id != bs->id; /* return 0 on a match like strcmp would */
00225 }

struct sched_context* sched_context_create ( void   )  [read]

New schedule context.

Note:
Create a scheduling context
Returns:
Returns a malloc'd sched_context structure, NULL on failure

Definition at line 239 of file sched.c.

References ast_calloc, ast_hashtab_create(), ast_hashtab_newsize_java(), ast_hashtab_resize_java(), ast_heap_create(), ast_mutex_init(), sched_context::eventcnt, sched_context::lock, sched_cmp(), sched_context_destroy(), sched_hash(), sched_context::sched_heap, sched_time_cmp(), and sched_context::schedq_ht.

Referenced by __ast_channel_alloc_ap(), ast_cdr_engine_init(), ast_sched_thread_create(), dnsmgr_init(), load_module(), and misdn_tasks_init().

00240 {
00241    struct sched_context *tmp;
00242 
00243    if (!(tmp = ast_calloc(1, sizeof(*tmp))))
00244       return NULL;
00245 
00246    ast_mutex_init(&tmp->lock);
00247    tmp->eventcnt = 1;
00248 
00249    tmp->schedq_ht = ast_hashtab_create(23, sched_cmp, ast_hashtab_resize_java, ast_hashtab_newsize_java, sched_hash, 1);
00250 
00251    if (!(tmp->sched_heap = ast_heap_create(8, sched_time_cmp,
00252          offsetof(struct sched, __heap_index)))) {
00253       sched_context_destroy(tmp);
00254       return NULL;
00255    }
00256 
00257    return tmp;
00258 }

void sched_context_destroy ( struct sched_context c  ) 

destroys a schedule context Destroys (free's) the given sched_context structure

Parameters:
c Context to free
Returns:
Returns 0 on success, -1 on failure

Definition at line 260 of file sched.c.

References ast_free, ast_hashtab_destroy(), ast_heap_destroy(), ast_heap_pop(), AST_LIST_REMOVE_HEAD, ast_mutex_destroy(), ast_mutex_lock(), ast_mutex_unlock(), sched_context::lock, s, sched_context::sched_heap, sched_context::schedc, and sched_context::schedq_ht.

Referenced by __ast_channel_alloc_ap(), ast_channel_free(), ast_hangup(), ast_sched_thread_destroy(), load_module(), misdn_tasks_destroy(), sched_context_create(), and unload_module().

00261 {
00262    struct sched *s;
00263 
00264    ast_mutex_lock(&con->lock);
00265 
00266 #ifdef SCHED_MAX_CACHE
00267    /* Eliminate the cache */
00268    while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
00269       ast_free(s);
00270 #endif
00271 
00272    if (con->sched_heap) {
00273       while ((s = ast_heap_pop(con->sched_heap))) {
00274          ast_free(s);
00275       }
00276       ast_heap_destroy(con->sched_heap);
00277       con->sched_heap = NULL;
00278    }
00279 
00280    ast_hashtab_destroy(con->schedq_ht, NULL);
00281    con->schedq_ht = NULL;
00282    
00283    /* And the context */
00284    ast_mutex_unlock(&con->lock);
00285    ast_mutex_destroy(&con->lock);
00286    ast_free(con);
00287 }

static unsigned int sched_hash ( const void *  obj  )  [static]

Definition at line 227 of file sched.c.

References sched::id, and s.

Referenced by sched_context_create().

00228 {
00229    const struct sched *s = obj;
00230    unsigned int h = s->id;
00231    return h;
00232 }

static void sched_release ( struct sched_context con,
struct sched tmp 
) [static]

Definition at line 307 of file sched.c.

References ast_free, AST_LIST_INSERT_HEAD, SCHED_MAX_CACHE, sched_context::schedc, and sched_context::schedccnt.

Referenced by ast_sched_add_variable(), ast_sched_del(), and ast_sched_runq().

00308 {
00309    /*
00310     * Add to the cache, or just free() if we
00311     * already have too many cache entries
00312     */
00313 
00314 #ifdef SCHED_MAX_CACHE   
00315    if (con->schedccnt < SCHED_MAX_CACHE) {
00316       AST_LIST_INSERT_HEAD(&con->schedc, tmp, list);
00317       con->schedccnt++;
00318    } else
00319 #endif
00320       ast_free(tmp);
00321 }

static void* sched_run ( void *  data  )  [static]

Definition at line 83 of file sched.c.

References ast_cond_timedwait(), ast_cond_wait(), ast_mutex_lock(), ast_mutex_unlock(), ast_samp2tv(), ast_sched_runq(), ast_sched_wait(), ast_tvadd(), ast_tvnow(), ast_sched_thread::cond, ast_sched_thread::context, ast_sched_thread::lock, and ast_sched_thread::stop.

Referenced by ast_sched_thread_create().

00084 {
00085    struct ast_sched_thread *st = data;
00086 
00087    while (!st->stop) {
00088       int ms;
00089       struct timespec ts = {
00090          .tv_sec = 0,   
00091       };
00092 
00093       ast_mutex_lock(&st->lock);
00094 
00095       if (st->stop) {
00096          ast_mutex_unlock(&st->lock);
00097          return NULL;
00098       }
00099 
00100       ms = ast_sched_wait(st->context);
00101 
00102       if (ms == -1) {
00103          ast_cond_wait(&st->cond, &st->lock);
00104       } else { 
00105          struct timeval tv;
00106          tv = ast_tvadd(ast_tvnow(), ast_samp2tv(ms, 1000));
00107          ts.tv_sec = tv.tv_sec;
00108          ts.tv_nsec = tv.tv_usec * 1000;
00109          ast_cond_timedwait(&st->cond, &st->lock, &ts);
00110       }
00111 
00112       ast_mutex_unlock(&st->lock);
00113 
00114       if (st->stop) {
00115          return NULL;
00116       }
00117 
00118       ast_sched_runq(st->context);
00119    }
00120 
00121    return NULL;
00122 }

static int sched_settime ( struct timeval *  t,
int  when 
) [static]

given the last event *tv and the offset in milliseconds 'when', computes the next value,

Definition at line 373 of file sched.c.

References ast_samp2tv(), ast_tvadd(), ast_tvcmp(), ast_tvnow(), and ast_tvzero().

Referenced by ast_sched_add_variable(), and ast_sched_runq().

00374 {
00375    struct timeval now = ast_tvnow();
00376 
00377    /*ast_debug(1, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
00378    if (ast_tvzero(*t))  /* not supplied, default to now */
00379       *t = now;
00380    *t = ast_tvadd(*t, ast_samp2tv(when, 1000));
00381    if (ast_tvcmp(*t, now) < 0) {
00382       *t = now;
00383    }
00384    return 0;
00385 }

static int sched_time_cmp ( void *  a,
void *  b 
) [static]

Definition at line 234 of file sched.c.

References ast_tvcmp().

Referenced by sched_context_create().

00235 {
00236    return ast_tvcmp(((struct sched *) b)->when, ((struct sched *) a)->when);
00237 }

static void schedule ( struct sched_context con,
struct sched s 
) [static]

Take a sched structure and put it in the queue, such that the soonest event is first in the list.

Definition at line 354 of file sched.c.

References ast_hashtab_insert_safe(), ast_heap_push(), ast_log(), sched_context::highwater, sched::id, LOG_WARNING, sched_context::sched_heap, sched_context::schedcnt, and sched_context::schedq_ht.

Referenced by ast_sched_add_variable(), and ast_sched_runq().

00355 {
00356    ast_heap_push(con->sched_heap, s);
00357 
00358    if (!ast_hashtab_insert_safe(con->schedq_ht, s)) {
00359       ast_log(LOG_WARNING,"Schedule Queue entry %d is already in table!\n", s->id);
00360    }
00361 
00362    con->schedcnt++;
00363 
00364    if (con->schedcnt > con->highwater) {
00365       con->highwater = con->schedcnt;
00366    }
00367 }


Generated by  doxygen 1.6.2