Sun Oct 16 2011 08:43:07

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 "asterisk/threadstorage.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

static void __init_last_del_id (void)
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.

Variables

static struct ast_threadstorage last_del_id = { .once = PTHREAD_ONCE_INIT , .key_init = __init_last_del_id , .custom_init = NULL , }

Detailed Description

Scheduler Routines (from cheops-NG)

Author:
Mark Spencer <markster@digium.com>

Definition in file sched.c.


Define Documentation

#define DEBUG (   a)

Function Documentation

static void __init_last_del_id ( void  ) [static]

Definition at line 51 of file sched.c.

{
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:
conScheduler context to add
whenhow many milliseconds to wait for event to occur
callbackfunction to call when the amount of time expires
datadata to pass to the callback
Returns:
Returns a schedule item ID on success, -1 on failure

Definition at line 442 of file sched.c.

References ast_sched_add_variable().

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

{
   return ast_sched_add_variable(con, when, callback, data, 0);
}
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:
conScheduler context to add
whenhow many milliseconds to wait for event to occur
callbackfunction to call when the amount of time expires
datadata to pass to the callback
variableIf 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 402 of file sched.c.

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

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

{
   struct sched *tmp;
   int res = -1;

   DEBUG(ast_debug(1, "ast_sched_add()\n"));

   ast_mutex_lock(&con->lock);
   if ((tmp = sched_alloc(con))) {
      tmp->id = con->eventcnt++;
      tmp->callback = callback;
      tmp->data = data;
      tmp->resched = when;
      tmp->variable = variable;
      tmp->when = ast_tv(0, 0);
      if (sched_settime(&tmp->when, when)) {
         sched_release(con, tmp);
      } else {
         schedule(con, tmp);
         res = tmp->id;
      }
   }
#ifdef DUMP_SCHEDULER
   /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
   if (option_debug)
      ast_sched_dump(con);
#endif
   ast_mutex_unlock(&con->lock);

   return res;
}
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 464 of file sched.c.

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

Referenced by __sip_ack(), and destroy_event().

{
   struct sched *s, tmp = {
      .id = id,
   };
   int *last_id = ast_threadstorage_get(&last_del_id, sizeof(int *));

   DEBUG(ast_debug(1, "ast_sched_del(%d)\n", id));

   if (id < 0) {
      return 0;
   }

   ast_mutex_lock(&con->lock);
   s = ast_hashtab_lookup(con->schedq_ht, &tmp);
   if (s) {
      if (!ast_heap_remove(con->sched_heap, s)) {
         ast_log(LOG_WARNING,"sched entry %d not in the sched heap?\n", s->id);
      }

      if (!ast_hashtab_remove_this_object(con->schedq_ht, s)) {
         ast_log(LOG_WARNING,"Found sched entry %d, then couldn't remove it?\n", s->id);
      }

      con->schedcnt--;

      sched_release(con, s);
   }

#ifdef DUMP_SCHEDULER
   /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
   if (option_debug)
      ast_sched_dump(con);
#endif
   ast_mutex_unlock(&con->lock);

   if (!s && *last_id != id) {
      ast_debug(1, "Attempted to delete nonexistent schedule entry %d!\n", id);
#ifndef AST_DEVMODE
      ast_assert(s != NULL);
#else
      {
      char buf[100];
      snprintf(buf, sizeof(buf), "s != NULL, id=%d", id);
      _ast_assert(0, buf, file, line, function);
      }
#endif
      *last_id = id;
      return -1;
   } else if (!s) {
      return -1;
   }

   return 0;
}
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 561 of file sched.c.

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

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

{
   struct sched *q;
   struct timeval when = ast_tvnow();
   int x;
   size_t heap_size;
#ifdef SCHED_MAX_CACHE
   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);
#else
   ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total, %d high-water)\n", con->schedcnt, con->eventcnt - 1, con->highwater);
#endif

   ast_debug(1, "=============================================================\n");
   ast_debug(1, "|ID    Callback          Data              Time  (sec:ms)   |\n");
   ast_debug(1, "+-----+-----------------+-----------------+-----------------+\n");
   ast_mutex_lock(&con->lock);
   heap_size = ast_heap_size(con->sched_heap);
   for (x = 1; x <= heap_size; x++) {
      struct timeval delta;
      q = ast_heap_peek(con->sched_heap, x);
      delta = ast_tvsub(q->when, when);
      ast_debug(1, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", 
         q->id,
         q->callback,
         q->data,
         (long)delta.tv_sec,
         (long int)delta.tv_usec);
   }
   ast_mutex_unlock(&con->lock);
   ast_debug(1, "=============================================================\n");
}
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:
conscheduling context in which to search fro the matching id
idID 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 447 of file sched.c.

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

{
   struct sched tmp,*res;
   tmp.id = id;
   res = ast_hashtab_lookup(con->schedq_ht, &tmp);
   if (res)
      return res->data;
   return NULL;
}
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:
-1failure
otherwise,returnsscheduled item ID

Definition at line 434 of file sched.c.

References AST_SCHED_DEL, and ast_sched_add().

{
   if (old_id > -1) {
      AST_SCHED_DEL(con, old_id);
   }
   return ast_sched_add(con, when, callback, data);
}
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:
-1failure
otherwise,returnsscheduled item ID

Definition at line 390 of file sched.c.

References AST_SCHED_DEL, and ast_sched_add_variable().

{
   /* 0 means the schedule item is new; do not delete */
   if (old_id > 0) {
      AST_SCHED_DEL(con, old_id);
   }
   return ast_sched_add_variable(con, when, callback, data, variable);
}
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:
conSchedule context to check
bufdynamic string to store report
cbnamesto check against
Since:
1.6.1

Definition at line 523 of file sched.c.

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

Referenced by sip_show_sched().

{
   int i, x;
   struct sched *cur;
   int countlist[cbnames->numassocs + 1];
   size_t heap_size;
   
   memset(countlist, 0, sizeof(countlist));
   ast_str_set(buf, 0, " Highwater = %d\n schedcnt = %d\n", con->highwater, con->schedcnt);

   ast_mutex_lock(&con->lock);

   heap_size = ast_heap_size(con->sched_heap);
   for (x = 1; x <= heap_size; x++) {
      cur = ast_heap_peek(con->sched_heap, x);
      /* match the callback to the cblist */
      for (i = 0; i < cbnames->numassocs; i++) {
         if (cur->callback == cbnames->cblist[i]) {
            break;
         }
      }
      if (i < cbnames->numassocs) {
         countlist[i]++;
      } else {
         countlist[cbnames->numassocs]++;
      }
   }

   ast_mutex_unlock(&con->lock);

   for (i = 0; i < cbnames->numassocs; i++) {
      ast_str_append(buf, 0, "    %s : %d\n", cbnames->list[i], countlist[i]);
   }

   ast_str_append(buf, 0, "   <unknown> : %d\n", countlist[cbnames->numassocs]);
}
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 596 of file sched.c.

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

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

{
   struct sched *current;
   struct timeval when;
   int numevents;
   int res;

   DEBUG(ast_debug(1, "ast_sched_runq()\n"));
      
   ast_mutex_lock(&con->lock);

   when = ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
   for (numevents = 0; (current = ast_heap_peek(con->sched_heap, 1)); 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.
       */
      if (ast_tvcmp(current->when, when) != -1) {
         break;
      }
      
      current = ast_heap_pop(con->sched_heap);

      if (!ast_hashtab_remove_this_object(con->schedq_ht, current)) {
         ast_log(LOG_ERROR,"Sched entry %d was in the schedq list but not in the hashtab???\n", current->id);
      }

      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);
      }
   }

   ast_mutex_unlock(&con->lock);
   
   return numevents;
}
int ast_sched_thread_add ( struct ast_sched_thread st,
int  when,
ast_sched_cb  cb,
const void *  data 
)

Add a scheduler entry.

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

Definition at line 206 of file sched.c.

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

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

{
   int res;

   ast_mutex_lock(&st->lock);
   res = ast_sched_add(st->context, when, cb, data);
   if (res != -1) {
      ast_cond_signal(&st->cond);
   }
   ast_mutex_unlock(&st->lock);

   return res;
}
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:
stthe handle to the scheduler and thread
whenthe number of ms in the future to run the task. A value <= 0 is treated as "run now".
cbthe function to call when the scheduled time arrives
datathe parameter to pass to the scheduler callback
variableIf 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:
-1Failure
>=0Sched ID of added task

Definition at line 191 of file sched.c.

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

Referenced by stun_start_monitor().

{
   int res;

   ast_mutex_lock(&st->lock);
   res = ast_sched_add_variable(st->context, when, cb, data, variable);
   if (res != -1) {
      ast_cond_signal(&st->cond);
   }
   ast_mutex_unlock(&st->lock);

   return res;
}
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:
NULLerror
non-NULLa handle to the scheduler and its dedicated thread.

Definition at line 163 of file sched.c.

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

Referenced by ast_cc_init(), load_module(), and stun_start_monitor().

{
   struct ast_sched_thread *st;

   if (!(st = ast_calloc(1, sizeof(*st)))) {
      return NULL;
   }

   ast_mutex_init(&st->lock);
   ast_cond_init(&st->cond, NULL);

   st->thread = AST_PTHREADT_NULL;

   if (!(st->context = sched_context_create())) {
      ast_log(LOG_ERROR, "Failed to create scheduler\n");
      ast_sched_thread_destroy(st);
      return NULL;
   }
   
   if (ast_pthread_create_background(&st->thread, NULL, sched_run, st)) {
      ast_log(LOG_ERROR, "Failed to create scheduler thread\n");
      ast_sched_thread_destroy(st);
      return NULL;
   }

   return st;
}
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:
stthe handle to the scheduler and thread
Returns:
NULL for convenience

Definition at line 139 of file sched.c.

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

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

{
   if (st->thread != AST_PTHREADT_NULL) {
      ast_mutex_lock(&st->lock);
      st->stop = 1;
      ast_cond_signal(&st->cond);
      ast_mutex_unlock(&st->lock);
      pthread_join(st->thread, NULL);
      st->thread = AST_PTHREADT_NULL;
   }

   ast_mutex_destroy(&st->lock);
   ast_cond_destroy(&st->cond);

   if (st->context) {
      sched_context_destroy(st->context);
      st->context = NULL;
   }

   ast_free(st);

   return NULL;
}
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:
stthe handle to the scheduler and thread
Returns:
the sched_context associated with an ast_sched_thread

Definition at line 134 of file sched.c.

References ast_sched_thread::context.

Referenced by iax2_destroy_helper().

{
   return st->context;
}
void ast_sched_thread_poke ( struct ast_sched_thread st)

Force re-processing of the scheduler context.

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

Definition at line 127 of file sched.c.

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

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

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

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

{
   int ms;
   struct sched *s;

   DEBUG(ast_debug(1, "ast_sched_wait()\n"));

   ast_mutex_lock(&con->lock);
   if ((s = ast_heap_peek(con->sched_heap, 1))) {
      ms = ast_tvdiff_ms(s->when, ast_tvnow());
      if (ms < 0) {
         ms = 0;
      }
   } else {
      ms = -1;
   }
   ast_mutex_unlock(&con->lock);

   return ms;
}
long ast_sched_when ( struct sched_context con,
int  id 
)

Returns the number of seconds before an event takes place.

Parameters:
conContext to use
idId to dump

Definition at line 660 of file sched.c.

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

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

{
   struct sched *s, tmp;
   long secs = -1;
   DEBUG(ast_debug(1, "ast_sched_when()\n"));

   ast_mutex_lock(&con->lock);
   
   /* these next 2 lines replace a lookup loop */
   tmp.id = id;
   s = ast_hashtab_lookup(con->schedq_ht, &tmp);
   
   if (s) {
      struct timeval now = ast_tvnow();
      secs = s->when.tv_sec - now.tv_sec;
   }
   ast_mutex_unlock(&con->lock);
   
   return secs;
}
static struct sched* sched_alloc ( struct sched_context con) [static, read]

Definition at line 292 of file sched.c.

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

Referenced by ast_sched_add_variable().

{
   struct sched *tmp;

   /*
    * 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)))
      con->schedccnt--;
   else
#endif
      tmp = ast_calloc(1, sizeof(*tmp));

   return tmp;
}
static int sched_cmp ( const void *  a,
const void *  b 
) [static]

Definition at line 223 of file sched.c.

References sched::id.

Referenced by sched_context_create().

{
   const struct sched *as = a;
   const struct sched *bs = b;
   return as->id != bs->id; /* return 0 on a match like strcmp would */
}
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 242 of file sched.c.

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

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

{
   struct sched_context *tmp;

   if (!(tmp = ast_calloc(1, sizeof(*tmp))))
      return NULL;

   ast_mutex_init(&tmp->lock);
   tmp->eventcnt = 1;

   tmp->schedq_ht = ast_hashtab_create(23, sched_cmp, ast_hashtab_resize_java, ast_hashtab_newsize_java, sched_hash, 1);

   if (!(tmp->sched_heap = ast_heap_create(8, sched_time_cmp,
         offsetof(struct sched, __heap_index)))) {
      sched_context_destroy(tmp);
      return NULL;
   }

   return tmp;
}
void sched_context_destroy ( struct sched_context c)

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

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

Definition at line 263 of file sched.c.

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

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

{
   struct sched *s;

   ast_mutex_lock(&con->lock);

#ifdef SCHED_MAX_CACHE
   /* Eliminate the cache */
   while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
      ast_free(s);
#endif

   if (con->sched_heap) {
      while ((s = ast_heap_pop(con->sched_heap))) {
         ast_free(s);
      }
      ast_heap_destroy(con->sched_heap);
      con->sched_heap = NULL;
   }

   ast_hashtab_destroy(con->schedq_ht, NULL);
   con->schedq_ht = NULL;
   
   /* And the context */
   ast_mutex_unlock(&con->lock);
   ast_mutex_destroy(&con->lock);
   ast_free(con);
}
static unsigned int sched_hash ( const void *  obj) [static]

Definition at line 230 of file sched.c.

References sched::id.

Referenced by sched_context_create().

{
   const struct sched *s = obj;
   unsigned int h = s->id;
   return h;
}
static void sched_release ( struct sched_context con,
struct sched tmp 
) [static]

Definition at line 310 of file sched.c.

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

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

{
   /*
    * 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);
      con->schedccnt++;
   } else
#endif
      ast_free(tmp);
}
static void* sched_run ( void *  data) [static]

Definition at line 86 of file sched.c.

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

Referenced by ast_sched_thread_create().

{
   struct ast_sched_thread *st = data;

   while (!st->stop) {
      int ms;
      struct timespec ts = {
         .tv_sec = 0,   
      };

      ast_mutex_lock(&st->lock);

      if (st->stop) {
         ast_mutex_unlock(&st->lock);
         return NULL;
      }

      ms = ast_sched_wait(st->context);

      if (ms == -1) {
         ast_cond_wait(&st->cond, &st->lock);
      } else { 
         struct timeval tv;
         tv = ast_tvadd(ast_tvnow(), ast_samp2tv(ms, 1000));
         ts.tv_sec = tv.tv_sec;
         ts.tv_nsec = tv.tv_usec * 1000;
         ast_cond_timedwait(&st->cond, &st->lock, &ts);
      }

      ast_mutex_unlock(&st->lock);

      if (st->stop) {
         return NULL;
      }

      ast_sched_runq(st->context);
   }

   return NULL;
}
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 376 of file sched.c.

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

Referenced by ast_sched_add_variable(), and ast_sched_runq().

{
   struct timeval now = ast_tvnow();

   /*ast_debug(1, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
   if (ast_tvzero(*t))  /* not supplied, default to now */
      *t = now;
   *t = ast_tvadd(*t, ast_samp2tv(when, 1000));
   if (ast_tvcmp(*t, now) < 0) {
      *t = now;
   }
   return 0;
}
static int sched_time_cmp ( void *  a,
void *  b 
) [static]

Definition at line 237 of file sched.c.

References ast_tvcmp().

Referenced by sched_context_create().

{
   return ast_tvcmp(((struct sched *) b)->when, ((struct sched *) a)->when);
}
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 357 of file sched.c.

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

Referenced by ast_sched_add_variable(), and ast_sched_runq().

{
   ast_heap_push(con->sched_heap, s);

   if (!ast_hashtab_insert_safe(con->schedq_ht, s)) {
      ast_log(LOG_WARNING,"Schedule Queue entry %d is already in table!\n", s->id);
   }

   con->schedcnt++;

   if (con->schedcnt > con->highwater) {
      con->highwater = con->schedcnt;
   }
}

Variable Documentation

struct ast_threadstorage last_del_id = { .once = PTHREAD_ONCE_INIT , .key_init = __init_last_del_id , .custom_init = NULL , } [static]

Definition at line 51 of file sched.c.

Referenced by ast_sched_del().