Codename Pineapple

Home page | Mailing list | Docs

Last updated: Sat Feb 3 05:01:49 2007

Asterisk developer's documentation :: Codename Pineapple


sched.h File Reference


Detailed Description

Scheduler Routines (derived from cheops).

Definition in file sched.h.

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define ast_sched_add_object(obj, con, when, callback)   ast_sched_add((con),(when),(callback), ASTOBJ_REF((obj)))
 Convenience macro for objects and reference (add).
#define AST_SCHED_CB(a)   ((ast_sched_cb)(a))
#define ast_sched_del_object(obj, destructor, con, id)
 Convenience macro for objects and reference (del).
#define SCHED_MAX_CACHE   128
 Max num of schedule structs.

Typedefs

typedef int(* ast_sched_cb )(void *data)
 callback for a cheops scheduler A cheops scheduler callback takes a pointer with callback data and

Functions

int ast_sched_add (struct sched_context *con, int when, ast_sched_cb callback, 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, void *data, int variable)
 Schedule callback(data) to happen when ms into the future.
int ast_sched_del (struct sched_context *con, int id)
 Deletes a scheduled event Remove this event from being run. A procedure should not remove its own event, but return 0 instead.
void ast_sched_dump (const struct sched_context *con)
 Dumps the scheduler contents Debugging: Dump the contents of the scheduler to stderr.
int ast_sched_runq (struct sched_context *con)
 Runs the queue.
int ast_sched_wait (struct sched_context *con)
 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.
long ast_sched_when (struct sched_context *con, int id)
 Returns the number of seconds before an event takes place.
sched_contextsched_context_create (void)
 New schedule context.
void sched_context_destroy (struct sched_context *c)
 destroys a schedule context Destroys (free's) the given sched_context structure


Define Documentation

#define ast_sched_add_object obj,
con,
when,
callback   )     ast_sched_add((con),(when),(callback), ASTOBJ_REF((obj)))
 

Convenience macro for objects and reference (add).

Definition at line 134 of file sched.h.

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

Definition at line 59 of file sched.h.

#define ast_sched_del_object obj,
destructor,
con,
id   ) 
 

Convenience macro for objects and reference (del).

Definition at line 140 of file sched.h.

#define SCHED_MAX_CACHE   128
 

Max num of schedule structs.

Note:
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 36 of file sched.h.

Referenced by sched_release().


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:
returns a 0 if it should not be run again, or non-zero if it should be rescheduled to run again

Definition at line 58 of file sched.h.


Function Documentation

int ast_sched_add struct sched_context con,
int  when,
ast_sched_cb  callback,
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 248 of file sched.c.

References ast_sched_add_variable().

Referenced by __attempt_transmit(), __iax2_poke_noanswer(), __oh323_update_info(), __send_lagrq(), __send_ping(), ast_readaudio_callback(), ast_readvideo_callback(), ast_rtp_raw_write(), ast_rtp_read(), auth_fail(), do_register(), do_reload(), dundi_discover(), dundi_query(), dundi_send(), handle_command_response(), handle_response_peerpoke(), iax2_ack_registry(), iax2_call(), iax2_do_register(), iax2_dprequest(), iax2_poke_peer(), iax2_provision(), make_trunk(), mgcp_postrequest(), network_thread(), parse_register_contact(), populate_addr(), qualify_peer(), receive_digit(), reg_source_db(), sip_call(), sip_poke_all_peers(), sip_poke_noanswer(), sip_poke_peer(), sip_scheddestroy(), sip_send_all_registers(), socket_process(), submit_scheduled_batch(), submit_unscheduled_batch(), transmit_register(), update_jbsched(), and update_registry().

00249 {
00250    return ast_sched_add_variable(con, when, callback, data, 0);
00251 }

int ast_sched_add_variable struct sched_context con,
int  when,
ast_sched_cb  callback,
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 215 of file sched.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_dump(), DEBUG, sched_context::eventcnt, sched_context::lock, LOG_DEBUG, LOG_NOTICE, option_debug, sched_alloc(), sched_release(), sched_settime(), and schedule().

Referenced by __sip_reliable_xmit(), _misdn_tasks_add_variable(), ast_sched_add(), dnsmgr_start_refresh(), and do_reload().

00216 {
00217    struct sched *tmp;
00218    int res = -1;
00219    DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
00220    if (!when) {
00221       ast_log(LOG_NOTICE, "Scheduled event in 0 ms?\n");
00222       return -1;
00223    }
00224    ast_mutex_lock(&con->lock);
00225    if ((tmp = sched_alloc(con))) {
00226       tmp->id = con->eventcnt++;
00227       tmp->callback = callback;
00228       tmp->data = data;
00229       tmp->resched = when;
00230       tmp->variable = variable;
00231       tmp->when = ast_tv(0, 0);
00232       if (sched_settime(&tmp->when, when)) {
00233          sched_release(con, tmp);
00234       } else {
00235          schedule(con, tmp);
00236          res = tmp->id;
00237       }
00238    }
00239 #ifdef DUMP_SCHEDULER
00240    /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
00241    if (option_debug)
00242       ast_sched_dump(con);
00243 #endif
00244    ast_mutex_unlock(&con->lock);
00245    return res;
00246 }

int ast_sched_del struct sched_context con,
int  id
 

Deletes a scheduled event Remove this event from being run. A procedure should not remove its own event, but return 0 instead.

Parameters:
con scheduling context to delete item from
id ID of the scheduled item to delete
Returns:
Returns 0 on success, -1 on failure

Definition at line 259 of file sched.c.

References AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_dump(), CRASH, DEBUG, sched_context::lock, LOG_DEBUG, LOG_NOTICE, option_debug, s, sched_release(), and sched_context::schedcnt.

Referenced by __oh323_destroy(), __oh323_update_info(), __sip_ack(), __sip_destroy(), __sip_semi_ack(), ack_trans(), ast_closestream(), ast_rtcp_write_rr(), ast_rtcp_write_sr(), ast_rtp_destroy(), ast_rtp_stop(), auth_fail(), build_gateway(), build_peer(), delete_users(), destroy_packet(), destroy_packets(), destroy_peer(), dnsmgr_start_refresh(), do_reload(), handle_command_response(), handle_response_invite(), handle_response_peerpoke(), handle_response_register(), iax2_ack_registry(), iax2_destroy_helper(), iax2_do_register(), iax2_dprequest(), iax2_frame_free(), iax2_poke_peer(), iax2_provision(), make_trunk(), mgcpsock_read(), misdn_tasks_remove(), parse_register_contact(), qualify_peer(), receive_digit(), reg_source_db(), schedule_delivery(), set_device_host(), sip_cancel_destroy(), sip_destroy_device(), sip_destroy_peer(), sip_poke_all_peers(), sip_poke_peer(), sip_registry_destroy(), sip_scheddestroy(), sip_send_all_registers(), socket_process(), submit_unscheduled_batch(), transmit_register(), update_jbsched(), and update_registry().

00260 {
00261    struct sched *s;
00262 
00263    DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
00264    
00265    ast_mutex_lock(&con->lock);
00266    AST_LIST_TRAVERSE_SAFE_BEGIN(&con->schedq, s, list) {
00267       if (s->id == id) {
00268          AST_LIST_REMOVE_CURRENT(&con->schedq, list);
00269          con->schedcnt--;
00270          sched_release(con, s);
00271          break;
00272       }
00273    }
00274    AST_LIST_TRAVERSE_SAFE_END
00275 
00276 #ifdef DUMP_SCHEDULER
00277    /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
00278    if (option_debug)
00279       ast_sched_dump(con);
00280 #endif
00281    ast_mutex_unlock(&con->lock);
00282 
00283    if (!s) {
00284       ast_log(LOG_NOTICE, "Attempted to delete nonexistent schedule entry %d!\n", id);
00285 #ifdef DO_CRASH
00286       CRASH;
00287 #endif
00288       return -1;
00289    }
00290    
00291    return 0;
00292 }

void ast_sched_dump const struct sched_context con  ) 
 

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

Parameters:
con Context to dump

Definition at line 295 of file sched.c.

References AST_LIST_TRAVERSE, ast_log(), ast_tvsub(), sched_context::eventcnt, LOG_DEBUG, option_debug, and sched_context::schedcnt.

Referenced by ast_sched_add_variable(), and ast_sched_del().

00296 {
00297    struct sched *q;
00298    struct timeval tv = ast_tvnow();
00299 #ifdef SCHED_MAX_CACHE
00300    if (option_debug)
00301       ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n", con->schedcnt, con->eventcnt - 1, con->schedccnt);
00302 #else
00303    if (option_debug)
00304       ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total)\n", con->schedcnt, con->eventcnt - 1);
00305 #endif
00306 
00307    if (option_debug) {
00308    ast_log(LOG_DEBUG, "=============================================================\n");
00309    ast_log(LOG_DEBUG, "|ID    Callback          Data              Time  (sec:ms)   |\n");
00310    ast_log(LOG_DEBUG, "+-----+-----------------+-----------------+-----------------+\n");
00311       AST_LIST_TRAVERSE(&con->schedq, q, list) {
00312          struct timeval delta = ast_tvsub(q->when, tv);
00313 
00314          ast_log(LOG_DEBUG, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", 
00315             q->id,
00316             q->callback,
00317             q->data,
00318             delta.tv_sec,
00319             (long int)delta.tv_usec);
00320       }
00321       ast_log(LOG_DEBUG, "=============================================================\n");
00322    }
00323 }

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.
con context to act upon
Returns:
Returns the number of events processed.

Definition at line 328 of file sched.c.

References AST_LIST_EMPTY, AST_LIST_FIRST, AST_LIST_REMOVE_HEAD, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_tvadd(), DEBUG, sched_context::lock, LOG_DEBUG, sched_release(), sched_settime(), sched_context::schedcnt, and schedule().

Referenced by do_cdr(), do_monitor(), do_refresh(), misdn_tasks_thread_func(), network_thread(), reload_config(), sched_thread(), and waitstream_core().

00329 {
00330    struct sched *current;
00331    struct timeval tv;
00332    int numevents;
00333    int res;
00334 
00335    DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
00336       
00337    ast_mutex_lock(&con->lock);
00338 
00339    for (numevents = 0; !AST_LIST_EMPTY(&con->schedq); numevents++) {
00340       /* schedule all events which are going to expire within 1ms.
00341        * We only care about millisecond accuracy anyway, so this will
00342        * help us get more than one event at one time if they are very
00343        * close together.
00344        */
00345       tv = ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
00346       if (ast_tvcmp(AST_LIST_FIRST(&con->schedq)->when, tv) != -1)
00347          break;
00348       
00349       current = AST_LIST_REMOVE_HEAD(&con->schedq, list);
00350       con->schedcnt--;
00351 
00352       /*
00353        * At this point, the schedule queue is still intact.  We
00354        * have removed the first event and the rest is still there,
00355        * so it's permissible for the callback to add new events, but
00356        * trying to delete itself won't work because it isn't in
00357        * the schedule queue.  If that's what it wants to do, it 
00358        * should return 0.
00359        */
00360          
00361       ast_mutex_unlock(&con->lock);
00362       res = current->callback(current->data);
00363       ast_mutex_lock(&con->lock);
00364          
00365       if (res) {
00366          /*
00367           * If they return non-zero, we should schedule them to be
00368           * run again.
00369           */
00370          if (sched_settime(&current->when, current->variable? res : current->resched)) {
00371             sched_release(con, current);
00372          } else
00373             schedule(con, current);
00374       } else {
00375          /* No longer needed, so release it */
00376          sched_release(con, current);
00377       }
00378    }
00379 
00380    ast_mutex_unlock(&con->lock);
00381    
00382    return numevents;
00383 }

int ast_sched_wait struct sched_context con  ) 
 

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.

Parameters:
con context to act upon
Returns:
Returns "-1" if there is nothing there are no scheduled events (and thus the poll should not timeout)

Definition at line 148 of file sched.c.

References AST_LIST_EMPTY, AST_LIST_FIRST, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), DEBUG, sched_context::lock, and LOG_DEBUG.

Referenced by do_cdr(), do_monitor(), do_refresh(), misdn_tasks_thread_func(), network_thread(), sched_thread(), and waitstream_core().

00149 {
00150    int ms;
00151 
00152    DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
00153 
00154    ast_mutex_lock(&con->lock);
00155    if (AST_LIST_EMPTY(&con->schedq)) {
00156       ms = -1;
00157    } else {
00158       ms = ast_tvdiff_ms(AST_LIST_FIRST(&con->schedq)->when, ast_tvnow());
00159       if (ms < 0)
00160          ms = 0;
00161    }
00162    ast_mutex_unlock(&con->lock);
00163 
00164    return ms;
00165 }

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

References AST_LIST_TRAVERSE, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), DEBUG, sched_context::lock, LOG_DEBUG, and s.

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

00386 {
00387    struct sched *s;
00388    long secs = -1;
00389    DEBUG(ast_log(LOG_DEBUG, "ast_sched_when()\n"));
00390 
00391    ast_mutex_lock(&con->lock);
00392    AST_LIST_TRAVERSE(&con->schedq, s, list) {
00393       if (s->id == id)
00394          break;
00395    }
00396    if (s) {
00397       struct timeval now = ast_tvnow();
00398       secs = s->when.tv_sec - now.tv_sec;
00399    }
00400    ast_mutex_unlock(&con->lock);
00401    
00402    return secs;
00403 }

struct sched_context* sched_context_create void   ) 
 

New schedule context.

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

Definition at line 75 of file sched.c.

References ast_calloc, and ast_mutex_init().

Referenced by ast_cdr_engine_init(), ast_channel_alloc(), dnsmgr_init(), load_module(), and misdn_tasks_init().

00076 {
00077    struct sched_context *tmp;
00078 
00079    if (!(tmp = ast_calloc(1, sizeof(*tmp))))
00080       return NULL;
00081 
00082    ast_mutex_init(&tmp->lock);
00083    tmp->eventcnt = 1;
00084    
00085    return tmp;
00086 }

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

References AST_LIST_REMOVE_HEAD, ast_mutex_destroy(), ast_mutex_lock(), ast_mutex_unlock(), free, sched_context::lock, and s.

Referenced by __unload_module(), ast_channel_free(), ast_hangup(), load_module(), misdn_tasks_destroy(), and unload_module().

00089 {
00090    struct sched *s;
00091 
00092    ast_mutex_lock(&con->lock);
00093 
00094 #ifdef SCHED_MAX_CACHE
00095    /* Eliminate the cache */
00096    while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
00097       free(s);
00098 #endif
00099 
00100    /* And the queue */
00101    while ((s = AST_LIST_REMOVE_HEAD(&con->schedq, list)))
00102       free(s);
00103    
00104    /* And the context */
00105    ast_mutex_unlock(&con->lock);
00106    ast_mutex_destroy(&con->lock);
00107    free(con);
00108 }


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