Sat Mar 10 01:55:45 2012

Asterisk developer's documentation


sched.h File Reference

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

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)
 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.
#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)
#define ast_sched_thread_del(st, id)
 Delete a scheduler entry.
#define SCHED_MAX_CACHE   128
 Max num of schedule structs.

Typedefs

typedef int(*) ast_sched_cb (const 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, const void *data) attribute_warn_unused_result
 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) attribute_warn_unused_result
 Schedule callback(data) to happen when ms into the future.
int ast_sched_del (struct sched_context *con, int id) attribute_warn_unused_result
 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).
void ast_sched_dump (struct sched_context *con)
 Dumps the scheduler contents Debugging: Dump the contents of the scheduler to stderr.
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) attribute_warn_unused_result
 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) attribute_warn_unused_result
 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)
 Runs the queue.
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.
ast_sched_threadast_sched_thread_create (void)
 Create a scheduler with a dedicated thread.
ast_sched_threadast_sched_thread_destroy (struct ast_sched_thread *st)
 Destroy a scheduler and its thread.
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) attribute_warn_unused_result
 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


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

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

Definition at line 161 of file sched.h.

#define AST_SCHED_DEL ( sched,
id   ) 

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 51 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_peer(), destroy_packet(), destroy_packets(), destroy_peer(), dialog_unlink_all(), dnsmgr_start_refresh(), do_reload(), esc_entry_destructor(), filestream_close(), handle_command_response(), handle_request_cancel(), handle_sip_publish_remove(), mgcpsock_read(), misdn_tasks_remove(), qualify_peer(), receive_digit(), sip_cc_agent_stop_offer_timer(), sip_cc_monitor_cancel_available_timer(), sip_registry_destroy(), sip_send_all_mwi_subscriptions(), sip_subscribe_mwi_destroy(), and submit_unscheduled_batch().

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

Convenience macro for objects and reference (del).

Definition at line 301 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 86 of file sched.h.

Referenced by iax2_destroy_helper().

#define AST_SCHED_DEL_UNREF ( sched,
id,
refcall   ) 

schedule task to get deleted and call unref function

See also:
AST_SCHED_DEL
Since:
1.6.1

Definition at line 69 of file sched.h.

Referenced by build_peer(), cleanup_all_regs(), dialog_unlink_all(), handle_request_invite(), handle_response_invite(), handle_response_register(), interpret_t38_parameters(), parse_register_contact(), peer_sched_cleanup(), restart_session_timer(), send_response(), sip_cancel_destroy(), sip_hangup(), sip_poke_peer(), sip_unregister(), start_session_timer(), stop_session_timer(), and update_provisional_keepalive().

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

Definition at line 113 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)

Definition at line 137 of file sched.h.

Referenced by handle_response_peerpoke(), handle_response_register(), handle_sip_publish_modify(), handle_sip_publish_refresh(), reg_source_db(), sip_call(), sip_poke_all_peers(), sip_poke_noanswer(), sip_poke_peer(), sip_send_all_registers(), and transmit_register().

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

Definition at line 102 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 120 of file sched.h.

#define ast_sched_thread_del ( st,
id   ) 

Value:

({ \
   struct sched_context *__tmp_context = ast_sched_thread_get_context(st); \
   AST_SCHED_DEL(__tmp_context, id); \
})
Delete a scheduler entry.

This uses the AST_SCHED_DEL macro internally.

Parameters:
st the handle to the scheduler and thread
id scheduler entry id to delete
Return values:
0 success
non-zero failure

Definition at line 403 of file sched.h.

Referenced by build_peer(), cc_generic_agent_stop_offer_timer(), cc_generic_monitor_cancel_available_timer(), delete_users(), iax2_destroy_helper(), iax2_frame_free(), iax2_poke_peer(), iax2_sched_replace(), make_trunk(), reg_source_db(), schedule_delivery(), socket_process(), unlink_peer(), and update_registry().

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

Referenced by sched_release().


Typedef Documentation

typedef int(*) ast_sched_cb(const 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 160 of file sched.h.


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 442 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(), 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(), 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(), start_session_timer(), submit_scheduled_batch(), submit_unscheduled_batch(), transmit_register(), and update_provisional_keepalive().

00443 {
00444    return ast_sched_add_variable(con, when, callback, data, 0);
00445 }

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 402 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().

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

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. 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 464 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(), ast_threadstorage_get(), DEBUG, sched::id, last_del_id, LOG_WARNING, option_debug, and sched_release().

Referenced by __sip_ack(), ast_rtp_prop_set(), ast_rtp_stop(), and destroy_event().

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

void ast_sched_dump ( 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 561 of file sched.c.

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

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

00562 {
00563    struct sched *q;
00564    struct timeval when = ast_tvnow();
00565    int x;
00566    size_t heap_size;
00567 #ifdef SCHED_MAX_CACHE
00568    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);
00569 #else
00570    ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total, %d high-water)\n", con->schedcnt, con->eventcnt - 1, con->highwater);
00571 #endif
00572 
00573    ast_debug(1, "=============================================================\n");
00574    ast_debug(1, "|ID    Callback          Data              Time  (sec:ms)   |\n");
00575    ast_debug(1, "+-----+-----------------+-----------------+-----------------+\n");
00576    ast_mutex_lock(&con->lock);
00577    heap_size = ast_heap_size(con->sched_heap);
00578    for (x = 1; x <= heap_size; x++) {
00579       struct timeval delta;
00580       q = ast_heap_peek(con->sched_heap, x);
00581       delta = ast_tvsub(q->when, when);
00582       ast_debug(1, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", 
00583          q->id,
00584          q->callback,
00585          q->data,
00586          (long)delta.tv_sec,
00587          (long int)delta.tv_usec);
00588    }
00589    ast_mutex_unlock(&con->lock);
00590    ast_debug(1, "=============================================================\n");
00591 }

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

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

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

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

References ast_sched_add(), and AST_SCHED_DEL.

00435 {
00436    if (old_id > -1) {
00437       AST_SCHED_DEL(con, old_id);
00438    }
00439    return ast_sched_add(con, when, callback, data);
00440 }

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

References ast_sched_add_variable(), and AST_SCHED_DEL.

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

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

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

Referenced by sip_show_sched().

00524 {
00525    int i, x;
00526    struct sched *cur;
00527    int countlist[cbnames->numassocs + 1];
00528    size_t heap_size;
00529    
00530    memset(countlist, 0, sizeof(countlist));
00531    ast_str_set(buf, 0, " Highwater = %d\n schedcnt = %d\n", con->highwater, con->schedcnt);
00532 
00533    ast_mutex_lock(&con->lock);
00534 
00535    heap_size = ast_heap_size(con->sched_heap);
00536    for (x = 1; x <= heap_size; x++) {
00537       cur = ast_heap_peek(con->sched_heap, x);
00538       /* match the callback to the cblist */
00539       for (i = 0; i < cbnames->numassocs; i++) {
00540          if (cur->callback == cbnames->cblist[i]) {
00541             break;
00542          }
00543       }
00544       if (i < cbnames->numassocs) {
00545          countlist[i]++;
00546       } else {
00547          countlist[cbnames->numassocs]++;
00548       }
00549    }
00550 
00551    ast_mutex_unlock(&con->lock);
00552 
00553    for (i = 0; i < cbnames->numassocs; i++) {
00554       ast_str_append(buf, 0, "    %s : %d\n", cbnames->list[i], countlist[i]);
00555    }
00556 
00557    ast_str_append(buf, 0, "   <unknown> : %d\n", countlist[cbnames->numassocs]);
00558 }

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 596 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(), sched_run(), speech_background(), wait_for_winner(), and waitstream_core().

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

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 206 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 cc_generic_agent_start_offer_timer(), cc_generic_monitor_request_cc(), iax2_hangup(), iax2_key_rotate(), iax2_sched_add(), and iax2_sched_replace().

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

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 191 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.

Referenced by stun_start_monitor().

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

struct ast_sched_thread* ast_sched_thread_create ( void   ) 

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 163 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(), LOG_ERROR, sched_context_create(), and sched_run().

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

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

struct ast_sched_thread* ast_sched_thread_destroy ( struct ast_sched_thread st  ) 

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 139 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(), load_module(), stun_start_monitor(), and stun_stop_monitor().

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

struct sched_context* ast_sched_thread_get_context ( struct ast_sched_thread st  ) 

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

References ast_sched_thread::context.

Referenced by iax2_destroy_helper().

00135 {
00136    return st->context;
00137 }

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

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

00128 {
00129    ast_mutex_lock(&st->lock);
00130    ast_cond_signal(&st->cond);
00131    ast_mutex_unlock(&st->lock);
00132 }

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 330 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, 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().

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

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

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

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

00661 {
00662    struct sched *s, tmp;
00663    long secs = -1;
00664    DEBUG(ast_debug(1, "ast_sched_when()\n"));
00665 
00666    ast_mutex_lock(&con->lock);
00667    
00668    /* these next 2 lines replace a lookup loop */
00669    tmp.id = id;
00670    s = ast_hashtab_lookup(con->schedq_ht, &tmp);
00671    
00672    if (s) {
00673       struct timeval now = ast_tvnow();
00674       secs = s->when.tv_sec - now.tv_sec;
00675    }
00676    ast_mutex_unlock(&con->lock);
00677    
00678    return secs;
00679 }

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 242 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_cmp(), sched_context_destroy(), sched_hash(), and sched_time_cmp().

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

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

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 263 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, sched_context::lock, sched_context::sched_heap, sched_context::schedc, and sched_context::schedq_ht.

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

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


Generated on Sat Mar 10 01:55:45 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7