Fri Feb 10 06:37:27 2012

Asterisk developer's documentation


sched.h File Reference

Scheduler Routines (derived from cheops). More...

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

Go to the source code of this file.

Data Structures

struct  ast_cb_names

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(sched, id)
 Remove a scheduler entry.
#define ast_sched_del_object(obj, destructor, con, id)
 Convenience macro for objects and reference (del).
#define AST_SCHED_DEL_SPINLOCK(sched, id, lock)
 schedule task to get deleted releasing the lock between attempts
#define AST_SCHED_DEL_UNREF(sched, id, refcall)
 schedule task to get deleted and call unref function
#define AST_SCHED_REPLACE(id, sched, when, callback, data)   AST_SCHED_REPLACE_VARIABLE(id, sched, when, callback, data, 0)
#define AST_SCHED_REPLACE_UNREF(id, sched, when, callback, data, unrefcall, addfailcall, refcall)   AST_SCHED_REPLACE_VARIABLE_UNREF(id, sched, when, callback, data, 0, unrefcall, addfailcall, refcall)
#define AST_SCHED_REPLACE_VARIABLE(id, sched, when, callback, data, variable)
#define AST_SCHED_REPLACE_VARIABLE_UNREF(id, sched, when, callback, data, variable, unrefcall, addfailcall, refcall)

Typedefs

typedef int(* ast_sched_cb )(const void *data)
 scheduler callback

Functions

int ast_sched_add (struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
 Adds a scheduled event.
int ast_sched_add_variable (struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
 Adds a scheduled event with rescheduling support.
struct ast_sched_contextast_sched_context_create (void)
 Create a scheduler context.
void ast_sched_context_destroy (struct ast_sched_context *c)
 destroys a schedule context
int ast_sched_del (struct ast_sched_context *con, int id) attribute_warn_unused_result
 Deletes a scheduled event.
void ast_sched_dump (struct ast_sched_context *con)
 Dumps the scheduler contents.
const void * ast_sched_find_data (struct ast_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 ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
 replace a scheduler entry
int ast_sched_replace_variable (int old_id, struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
 replace a scheduler entry
void ast_sched_report (struct ast_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 ast_sched_context *con)
 Runs the queue.
int ast_sched_start_thread (struct ast_sched_context *con)
 Start a thread for processing scheduler entries.
int ast_sched_wait (struct ast_sched_context *con) attribute_warn_unused_result
 Determines number of seconds until the next outstanding event to take place.
long ast_sched_when (struct ast_sched_context *con, int id)
 Returns the number of seconds before an event takes place.


Detailed Description

Scheduler Routines (derived from cheops).

Definition in file sched.h.


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 319 of file sched.h.

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

Definition at line 158 of file sched.h.

#define AST_SCHED_DEL ( sched,
id   ) 

Remove a scheduler entry.

This is a loop construct to ensure that the scheduled task get deleted. The idea is that if we loop attempting to remove the scheduled task, then whatever callback had been running will complete and reinsert the task into the scheduler.

Since macro expansion essentially works like pass-by-name parameter passing, this macro will still work correctly even if the id of the task to delete changes. This holds as long as the name of the id which could change is passed to the macro and not a copy of the value of the id.

Definition at line 46 of file sched.h.

Referenced by __oh323_destroy(), __oh323_update_info(), __sip_reliable_xmit(), __sip_semi_ack(), ack_trans(), ast_rtp_destroy(), ast_rtp_stop(), ast_sched_replace(), ast_sched_replace_variable(), build_gateway(), build_peer(), delete_users(), destroy_packet(), destroy_packets(), destroy_peer(), destroy_trans(), dialog_unlink_all(), dnsmgr_start_refresh(), do_reload(), esc_entry_destructor(), filestream_close(), handle_command_response(), handle_request_cancel(), handle_sip_publish_remove(), iax2_destroy_helper(), iax2_frame_free(), iax2_poke_peer(), make_trunk(), mgcpsock_read(), misdn_tasks_remove(), qualify_peer(), realtime_peer(), receive_digit(), reg_source_db(), schedule_delivery(), sip_cc_agent_stop_offer_timer(), sip_cc_monitor_cancel_available_timer(), sip_registry_destroy(), sip_send_all_mwi_subscriptions(), sip_subscribe_mwi_destroy(), socket_process(), submit_unscheduled_batch(), unlink_peer(), and update_registry().

#define ast_sched_del_object ( obj,
destructor,
con,
id   ) 

Convenience macro for objects and reference (del).

Definition at line 325 of file sched.h.

#define AST_SCHED_DEL_SPINLOCK ( sched,
id,
lock   ) 

schedule task to get deleted releasing the lock between attempts

Since:
1.6.1

Definition at line 81 of file sched.h.

Referenced by iax2_destroy_helper().

#define AST_SCHED_DEL_UNREF ( sched,
id,
refcall   ) 

#define AST_SCHED_REPLACE ( id,
sched,
when,
callback,
data   )     AST_SCHED_REPLACE_VARIABLE(id, sched, when, callback, data, 0)

Definition at line 108 of file sched.h.

Referenced by schedule_calendar_event().

#define AST_SCHED_REPLACE_UNREF ( id,
sched,
when,
callback,
data,
unrefcall,
addfailcall,
refcall   )     AST_SCHED_REPLACE_VARIABLE_UNREF(id, sched, when, callback, data, 0, unrefcall, addfailcall, refcall)

#define AST_SCHED_REPLACE_VARIABLE ( id,
sched,
when,
callback,
data,
variable   ) 

Definition at line 97 of file sched.h.

Referenced by __sip_reliable_xmit().

#define AST_SCHED_REPLACE_VARIABLE_UNREF ( id,
sched,
when,
callback,
data,
variable,
unrefcall,
addfailcall,
refcall   ) 

Note:
Not currently used in the source?
Since:
1.6.1

Definition at line 115 of file sched.h.


Typedef Documentation

typedef int(* ast_sched_cb)(const void *data)

scheduler callback

A scheduler callback takes a pointer with callback data and

Return values:
0 if the callback should not be rescheduled
non-zero if the callback should be scheduled agai

Definition at line 157 of file sched.h.


Function Documentation

int ast_sched_add ( struct ast_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 413 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(), build_peer(), cc_generic_agent_start_offer_timer(), cc_generic_monitor_request_cc(), create_esc_entry(), do_register(), do_reload(), dundi_discover(), dundi_query(), dundi_send(), handle_command_response(), handle_request_invite(), handle_response_invite(), handle_response_subscribe(), iax2_hangup(), iax2_key_rotate(), iax2_sched_add(), mgcp_postrequest(), network_change_event_cb(), parse_register_contact(), populate_addr(), precache_trans(), qualify_peer(), receive_digit(), rtp_red_init(), sip_cc_agent_start_offer_timer(), sip_cc_monitor_request_cc(), sip_scheddestroy(), sip_send_all_mwi_subscriptions(), skinny_sched_add(), start_session_timer(), submit_scheduled_batch(), submit_unscheduled_batch(), transmit_register(), and update_provisional_keepalive().

00414 {
00415    return ast_sched_add_variable(con, when, callback, data, 0);
00416 }

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

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

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

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

00371 {
00372    struct sched *tmp;
00373    int res = -1;
00374 
00375    DEBUG(ast_debug(1, "ast_sched_add()\n"));
00376 
00377    ast_mutex_lock(&con->lock);
00378    if ((tmp = sched_alloc(con))) {
00379       tmp->id = con->eventcnt++;
00380       tmp->callback = callback;
00381       tmp->data = data;
00382       tmp->resched = when;
00383       tmp->variable = variable;
00384       tmp->when = ast_tv(0, 0);
00385       if (sched_settime(&tmp->when, when)) {
00386          sched_release(con, tmp);
00387       } else {
00388          schedule(con, tmp);
00389          res = tmp->id;
00390       }
00391    }
00392 #ifdef DUMP_SCHEDULER
00393    /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
00394    if (option_debug)
00395       ast_sched_dump(con);
00396 #endif
00397    if (con->sched_thread) {
00398       ast_cond_signal(&con->sched_thread->cond);
00399    }
00400    ast_mutex_unlock(&con->lock);
00401 
00402    return res;
00403 }

struct ast_sched_context* ast_sched_context_create ( void   )  [read]

Create a scheduler context.

Returns:
Returns a malloc'd sched_context structure, NULL on failure

Definition at line 206 of file sched.c.

References sched::__heap_index, ast_calloc, ast_hashtab_create(), ast_hashtab_newsize_java(), ast_hashtab_resize_java(), ast_heap_create(), ast_mutex_init, ast_sched_context_destroy(), ast_sched_context::eventcnt, ast_sched_context::lock, sched_cmp(), sched_hash(), ast_sched_context::sched_heap, sched_time_cmp(), and ast_sched_context::schedq_ht.

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

00207 {
00208    struct ast_sched_context *tmp;
00209 
00210    if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
00211       return NULL;
00212    }
00213 
00214    ast_mutex_init(&tmp->lock);
00215    tmp->eventcnt = 1;
00216 
00217    tmp->schedq_ht = ast_hashtab_create(23, sched_cmp, ast_hashtab_resize_java, ast_hashtab_newsize_java, sched_hash, 1);
00218 
00219    if (!(tmp->sched_heap = ast_heap_create(8, sched_time_cmp,
00220          offsetof(struct sched, __heap_index)))) {
00221       ast_sched_context_destroy(tmp);
00222       return NULL;
00223    }
00224 
00225    return tmp;
00226 }

void ast_sched_context_destroy ( struct ast_sched_context c  ) 

destroys a schedule context

Parameters:
c Context to free

Definition at line 228 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::list, ast_sched_context::lock, ast_sched_context::sched_heap, ast_sched_context::sched_thread, sched_thread_destroy(), ast_sched_context::schedc, and ast_sched_context::schedq_ht.

Referenced by __unload_module(), ast_channel_destructor(), ast_hangup(), ast_sched_context_create(), load_module(), misdn_tasks_destroy(), stun_start_monitor(), stun_stop_monitor(), and unload_module().

00229 {
00230    struct sched *s;
00231 
00232    sched_thread_destroy(con);
00233    con->sched_thread = NULL;
00234 
00235    ast_mutex_lock(&con->lock);
00236 
00237 #ifdef SCHED_MAX_CACHE
00238    while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list))) {
00239       ast_free(s);
00240    }
00241 #endif
00242 
00243    if (con->sched_heap) {
00244       while ((s = ast_heap_pop(con->sched_heap))) {
00245          ast_free(s);
00246       }
00247       ast_heap_destroy(con->sched_heap);
00248       con->sched_heap = NULL;
00249    }
00250 
00251    ast_hashtab_destroy(con->schedq_ht, NULL);
00252    con->schedq_ht = NULL;
00253 
00254    ast_mutex_unlock(&con->lock);
00255    ast_mutex_destroy(&con->lock);
00256 
00257    ast_free(con);
00258 }

int ast_sched_del ( struct ast_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. 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).

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

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

Referenced by __sip_ack(), ast_rtp_prop_set(), ast_rtp_stop(), cc_generic_agent_stop_offer_timer(), cc_generic_monitor_cancel_available_timer(), destroy_event(), and skinny_sched_del().

00439 {
00440    struct sched *s, tmp = {
00441       .id = id,
00442    };
00443    int *last_id = ast_threadstorage_get(&last_del_id, sizeof(int *));
00444 
00445    DEBUG(ast_debug(1, "ast_sched_del(%d)\n", id));
00446 
00447    if (id < 0) {
00448       return 0;
00449    }
00450 
00451    ast_mutex_lock(&con->lock);
00452    s = ast_hashtab_lookup(con->schedq_ht, &tmp);
00453    if (s) {
00454       if (!ast_heap_remove(con->sched_heap, s)) {
00455          ast_log(LOG_WARNING,"sched entry %d not in the sched heap?\n", s->id);
00456       }
00457 
00458       if (!ast_hashtab_remove_this_object(con->schedq_ht, s)) {
00459          ast_log(LOG_WARNING,"Found sched entry %d, then couldn't remove it?\n", s->id);
00460       }
00461 
00462       con->schedcnt--;
00463 
00464       sched_release(con, s);
00465    }
00466 
00467 #ifdef DUMP_SCHEDULER
00468    /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
00469    if (option_debug)
00470       ast_sched_dump(con);
00471 #endif
00472    if (con->sched_thread) {
00473       ast_cond_signal(&con->sched_thread->cond);
00474    }
00475    ast_mutex_unlock(&con->lock);
00476 
00477    if (!s && *last_id != id) {
00478       ast_debug(1, "Attempted to delete nonexistent schedule entry %d!\n", id);
00479 #ifndef AST_DEVMODE
00480       ast_assert(s != NULL);
00481 #else
00482       {
00483       char buf[100];
00484       snprintf(buf, sizeof(buf), "s != NULL, id=%d", id);
00485       _ast_assert(0, buf, file, line, function);
00486       }
00487 #endif
00488       *last_id = id;
00489       return -1;
00490    } else if (!s) {
00491       return -1;
00492    }
00493 
00494    return 0;
00495 }

void ast_sched_dump ( struct ast_sched_context con  ) 

Dumps the scheduler contents.

Debugging: Dump the contents of the scheduler to stderr

Parameters:
con Context to dump

Definition at line 535 of file sched.c.

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

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

00536 {
00537    struct sched *q;
00538    struct timeval when = ast_tvnow();
00539    int x;
00540    size_t heap_size;
00541 #ifdef SCHED_MAX_CACHE
00542    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);
00543 #else
00544    ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total, %d high-water)\n", con->schedcnt, con->eventcnt - 1, con->highwater);
00545 #endif
00546 
00547    ast_debug(1, "=============================================================\n");
00548    ast_debug(1, "|ID    Callback          Data              Time  (sec:ms)   |\n");
00549    ast_debug(1, "+-----+-----------------+-----------------+-----------------+\n");
00550    ast_mutex_lock(&con->lock);
00551    heap_size = ast_heap_size(con->sched_heap);
00552    for (x = 1; x <= heap_size; x++) {
00553       struct timeval delta;
00554       q = ast_heap_peek(con->sched_heap, x);
00555       delta = ast_tvsub(q->when, when);
00556       ast_debug(1, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", 
00557          q->id,
00558          q->callback,
00559          q->data,
00560          (long)delta.tv_sec,
00561          (long int)delta.tv_usec);
00562    }
00563    ast_mutex_unlock(&con->lock);
00564    ast_debug(1, "=============================================================\n");
00565 }

const void* ast_sched_find_data ( struct ast_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 418 of file sched.c.

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

00419 {
00420    struct sched tmp,*res;
00421    tmp.id = id;
00422    res = ast_hashtab_lookup(con->schedq_ht, &tmp);
00423    if (res)
00424       return res->data;
00425    return NULL;
00426 }

int ast_sched_replace ( int  old_id,
struct ast_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 405 of file sched.c.

References ast_sched_add(), and AST_SCHED_DEL.

Referenced by iax2_sched_replace().

00406 {
00407    if (old_id > -1) {
00408       AST_SCHED_DEL(con, old_id);
00409    }
00410    return ast_sched_add(con, when, callback, data);
00411 }

int ast_sched_replace_variable ( int  old_id,
struct ast_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 358 of file sched.c.

References ast_sched_add_variable(), and AST_SCHED_DEL.

00359 {
00360    /* 0 means the schedule item is new; do not delete */
00361    if (old_id > 0) {
00362       AST_SCHED_DEL(con, old_id);
00363    }
00364    return ast_sched_add_variable(con, when, callback, data, variable);
00365 }

void ast_sched_report ( struct ast_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 497 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, ast_sched_context::highwater, ast_cb_names::list, ast_sched_context::lock, ast_cb_names::numassocs, ast_sched_context::sched_heap, and ast_sched_context::schedcnt.

Referenced by sip_show_sched().

00498 {
00499    int i, x;
00500    struct sched *cur;
00501    int countlist[cbnames->numassocs + 1];
00502    size_t heap_size;
00503    
00504    memset(countlist, 0, sizeof(countlist));
00505    ast_str_set(buf, 0, " Highwater = %d\n schedcnt = %d\n", con->highwater, con->schedcnt);
00506 
00507    ast_mutex_lock(&con->lock);
00508 
00509    heap_size = ast_heap_size(con->sched_heap);
00510    for (x = 1; x <= heap_size; x++) {
00511       cur = ast_heap_peek(con->sched_heap, x);
00512       /* match the callback to the cblist */
00513       for (i = 0; i < cbnames->numassocs; i++) {
00514          if (cur->callback == cbnames->cblist[i]) {
00515             break;
00516          }
00517       }
00518       if (i < cbnames->numassocs) {
00519          countlist[i]++;
00520       } else {
00521          countlist[cbnames->numassocs]++;
00522       }
00523    }
00524 
00525    ast_mutex_unlock(&con->lock);
00526 
00527    for (i = 0; i < cbnames->numassocs; i++) {
00528       ast_str_append(buf, 0, "    %s : %d\n", cbnames->list[i], countlist[i]);
00529    }
00530 
00531    ast_str_append(buf, 0, "   <unknown> : %d\n", countlist[cbnames->numassocs]);
00532 }

int ast_sched_runq ( struct ast_sched_context con  ) 

Runs the queue.

Run the queue, executing all callbacks which need to be performed at this time.

Parameters:
con Scheduling context to run
con context to act upon
Returns:
Returns the number of events processed.

Definition at line 570 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, ast_sched_context::lock, LOG_ERROR, sched::resched, ast_sched_context::sched_heap, sched_release(), sched_settime(), ast_sched_context::schedcnt, ast_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().

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

int ast_sched_start_thread ( struct ast_sched_context con  ) 

Start a thread for processing scheduler entries.

Parameters:
con the scheduler context this thread will manage
Return values:
0 success
non-zero failure

Definition at line 159 of file sched.c.

References ast_calloc, ast_cond_init, ast_log(), ast_pthread_create_background, AST_PTHREADT_NULL, sched_thread::cond, LOG_ERROR, sched_run(), ast_sched_context::sched_thread, sched_thread_destroy(), and sched_thread::thread.

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

00160 {
00161    struct sched_thread *st;
00162 
00163    if (con->sched_thread) {
00164       ast_log(LOG_ERROR, "Thread already started on this scheduler context\n");
00165       return -1;
00166    }
00167 
00168    if (!(st = ast_calloc(1, sizeof(*st)))) {
00169       return -1;
00170    }
00171 
00172    ast_cond_init(&st->cond, NULL);
00173 
00174    st->thread = AST_PTHREADT_NULL;
00175 
00176    con->sched_thread = st;
00177 
00178    if (ast_pthread_create_background(&st->thread, NULL, sched_run, con)) {
00179       ast_log(LOG_ERROR, "Failed to create scheduler thread\n");
00180       sched_thread_destroy(con);
00181       return -1;
00182    }
00183 
00184    return 0;
00185 }

int ast_sched_wait ( struct ast_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 298 of file sched.c.

References ast_debug, ast_heap_peek(), ast_mutex_lock, ast_mutex_unlock, ast_tvdiff_ms(), ast_tvnow(), DEBUG, ast_sched_context::lock, ast_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().

00299 {
00300    int ms;
00301    struct sched *s;
00302 
00303    DEBUG(ast_debug(1, "ast_sched_wait()\n"));
00304 
00305    ast_mutex_lock(&con->lock);
00306    if ((s = ast_heap_peek(con->sched_heap, 1))) {
00307       ms = ast_tvdiff_ms(s->when, ast_tvnow());
00308       if (ms < 0) {
00309          ms = 0;
00310       }
00311    } else {
00312       ms = -1;
00313    }
00314    ast_mutex_unlock(&con->lock);
00315 
00316    return ms;
00317 }

long ast_sched_when ( struct ast_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 634 of file sched.c.

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

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

00635 {
00636    struct sched *s, tmp;
00637    long secs = -1;
00638    DEBUG(ast_debug(1, "ast_sched_when()\n"));
00639 
00640    ast_mutex_lock(&con->lock);
00641 
00642    /* these next 2 lines replace a lookup loop */
00643    tmp.id = id;
00644    s = ast_hashtab_lookup(con->schedq_ht, &tmp);
00645 
00646    if (s) {
00647       struct timeval now = ast_tvnow();
00648       secs = s->when.tv_sec - now.tv_sec;
00649    }
00650    ast_mutex_unlock(&con->lock);
00651 
00652    return secs;
00653 }


Generated on Fri Feb 10 06:37:27 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6