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"
Go to the source code of this file.
Data Structures | |
struct | ast_sched_thread |
struct | sched |
struct | sched_context |
Macros | |
#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. More... | |
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. More... | |
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. More... | |
void | ast_sched_dump (struct sched_context *con) |
Dump the contents of the scheduler to LOG_DEBUG. More... | |
const void * | ast_sched_find_data (struct sched_context *con, int id) |
Find a sched structure and return the data field associated with it. More... | |
int | ast_sched_replace (int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data) |
replace a scheduler entry More... | |
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 More... | |
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. More... | |
int | ast_sched_runq (struct sched_context *con) |
Launch all events which need to be run at this time. More... | |
int | ast_sched_thread_add (struct ast_sched_thread *st, int when, ast_sched_cb cb, const void *data) |
Add a scheduler entry. More... | |
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. More... | |
struct ast_sched_thread * | ast_sched_thread_create (void) |
Create a scheduler with a dedicated thread. More... | |
struct ast_sched_thread * | ast_sched_thread_destroy (struct ast_sched_thread *st) |
Destroy a scheduler and its thread. More... | |
struct sched_context * | ast_sched_thread_get_context (struct ast_sched_thread *st) |
Get the scheduler context for a given ast_sched_thread. More... | |
void | ast_sched_thread_poke (struct ast_sched_thread *st) |
Force re-processing of the scheduler context. More... | |
int | ast_sched_wait (struct sched_context *con) |
Return the number of milliseconds until the next scheduled event. More... | |
long | ast_sched_when (struct sched_context *con, int id) |
Returns the number of seconds before an event takes place. More... | |
static struct sched * | sched_alloc (struct sched_context *con) |
static int | sched_cmp (const void *a, const void *b) |
struct sched_context * | sched_context_create (void) |
New schedule context. More... | |
void | sched_context_destroy (struct sched_context *con) |
destroys a schedule context Destroys (free's) the given sched_context structure More... | |
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, More... | |
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. More... | |
Variables | |
static struct ast_threadstorage | last_del_id = { .once = PTHREAD_ONCE_INIT , .key_init = __init_last_del_id , .custom_init = NULL , } |
Scheduler Routines (from cheops-NG)
Definition in file sched.c.
#define DEBUG | ( | a | ) |
Definition at line 40 of file sched.c.
Referenced by ast_sched_add_variable(), ast_sched_del(), ast_sched_runq(), ast_sched_wait(), and ast_sched_when().
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.
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 |
Definition at line 446 of file sched.c.
References ast_sched_add_variable().
Referenced by __oh323_update_info(), ast_readaudio_callback(), ast_readvideo_callback(), ast_rtp_raw_write(), ast_rtp_read(), ast_sched_replace(), ast_sched_thread_add(), build_peer(), 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_hangup(), sip_scheddestroy(), sip_send_all_mwi_subscriptions(), start_session_timer(), submit_scheduled_batch(), submit_unscheduled_batch(), transmit_register(), and update_provisional_keepalive().
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
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 |
Definition at line 406 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().
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 468 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, sched_context::lock, LOG_WARNING, option_debug, sched_context::sched_heap, sched_release(), sched_context::schedcnt, and sched_context::schedq_ht.
Referenced by __sip_ack(), ast_rtp_prop_set(), ast_rtp_stop(), and destroy_event().
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 565 of file sched.c.
References ast_debug, ast_heap_peek(), ast_heap_size(), ast_mutex_lock, ast_mutex_unlock, ast_tvnow(), ast_tvsub(), sched_context::eventcnt, sched_context::highwater, sched_context::lock, sched_context::sched_heap, sched_context::schedccnt, and sched_context::schedcnt.
Referenced by ast_sched_add_variable(), ast_sched_del(), handle_dump_sched(), sip_do_reload(), and unload_module().
const void* ast_sched_find_data | ( | struct sched_context * | con, |
int | id | ||
) |
Find a sched structure and return the data field associated with it.
con | scheduling context in which to search fro the matching id |
id | ID of the scheduled item to find |
Definition at line 451 of file sched.c.
References ast_hashtab_lookup(), sched::data, sched::id, and sched_context::schedq_ht.
int ast_sched_replace | ( | int | old_id, |
struct sched_context * | con, | ||
int | when, | ||
ast_sched_cb | callback, | ||
const void * | data | ||
) |
replace a scheduler entry
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.
-1 | failure |
otherwise,returns | scheduled item ID |
Definition at line 438 of file sched.c.
References ast_sched_add(), and AST_SCHED_DEL.
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
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.
-1 | failure |
otherwise,returns | scheduled item ID |
Definition at line 394 of file sched.c.
References ast_sched_add_variable(), and AST_SCHED_DEL.
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.
con | Schedule context to check |
buf | dynamic string to store report |
cbnames | to check against |
Definition at line 527 of file sched.c.
References ast_heap_peek(), ast_heap_size(), ast_mutex_lock, ast_mutex_unlock, ast_str_append(), ast_str_set(), sched::callback, ast_cb_names::cblist, sched_context::highwater, ast_cb_names::list, sched_context::lock, ast_cb_names::numassocs, sched_context::sched_heap, and sched_context::schedcnt.
Referenced by sip_show_sched().
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 600 of file sched.c.
References ast_debug, ast_hashtab_remove_this_object(), ast_heap_peek(), ast_heap_pop(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_tv(), ast_tvadd(), ast_tvcmp(), ast_tvnow(), sched::callback, sched::data, DEBUG, sched::id, sched_context::lock, LOG_ERROR, sched::resched, sched_context::sched_heap, sched_release(), sched_settime(), sched_context::schedcnt, sched_context::schedq_ht, schedule(), sched::variable, and sched::when.
Referenced by background_detect_exec(), dial_exec_full(), do_cdr(), do_monitor(), do_refresh(), handle_speechrecognize(), misdn_tasks_thread_func(), network_thread(), reload_config(), sched_run(), speech_background(), wait_for_winner(), and waitstream_core().
int ast_sched_thread_add | ( | struct ast_sched_thread * | st, |
int | when, | ||
ast_sched_cb | cb, | ||
const void * | data | ||
) |
Add a scheduler entry.
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 |
-1 | Failure |
>=0 | Sched ID of added task |
Definition at line 210 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().
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.
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. |
-1 | Failure |
>=0 | Sched ID of added task |
Definition at line 195 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().
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.
NULL | error |
non-NULL | a handle to the scheduler and its dedicated thread. |
Definition at line 167 of file sched.c.
References ast_calloc, ast_cond_init, ast_log(), ast_mutex_init, ast_pthread_create_background, AST_PTHREADT_NULL, ast_sched_thread_destroy(), ast_sched_thread::cond, ast_sched_thread::context, ast_sched_thread::lock, LOG_ERROR, sched_context_create(), sched_run(), and ast_sched_thread::thread.
Referenced by ast_cc_init(), load_module(), and stun_start_monitor().
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.
st | the handle to the scheduler and thread |
Definition at line 143 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(), cc_shutdown(), load_module(), stun_start_monitor(), and stun_stop_monitor().
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.
st | the handle to the scheduler and thread |
Definition at line 138 of file sched.c.
References ast_sched_thread::context.
Referenced by iax2_destroy_helper().
void ast_sched_thread_poke | ( | struct ast_sched_thread * | st | ) |
Force re-processing of the scheduler context.
st | the handle to the scheduler and thread |
Definition at line 131 of file sched.c.
References ast_cond_signal, ast_mutex_lock, ast_mutex_unlock, ast_sched_thread::cond, and ast_sched_thread::lock.
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 334 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().
long ast_sched_when | ( | struct sched_context * | con, |
int | id | ||
) |
Returns the number of seconds before an event takes place.
con | Context to use |
id | Id to dump |
Definition at line 664 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().
|
static |
Definition at line 296 of file sched.c.
References ast_calloc, AST_LIST_REMOVE_HEAD, sched_context::schedc, and sched_context::schedccnt.
Referenced by ast_sched_add_variable().
|
static |
Definition at line 227 of file sched.c.
References sched::id.
Referenced by sched_context_create().
struct sched_context* sched_context_create | ( | void | ) |
New schedule context.
Definition at line 246 of file sched.c.
References ast_calloc, ast_hashtab_create(), ast_hashtab_newsize_java(), ast_hashtab_resize_java(), ast_heap_create(), ast_mutex_init, sched_context::eventcnt, sched_context::lock, sched_cmp(), sched_context_destroy(), sched_hash(), sched_context::sched_heap, sched_time_cmp(), and sched_context::schedq_ht.
Referenced by __ast_channel_alloc_ap(), ast_cdr_engine_init(), ast_sched_thread_create(), dnsmgr_init(), load_module(), and misdn_tasks_init().
void sched_context_destroy | ( | struct sched_context * | c | ) |
destroys a schedule context Destroys (free's) the given sched_context structure
c | Context to free |
Definition at line 267 of file sched.c.
References ast_free, ast_hashtab_destroy(), ast_heap_destroy(), ast_heap_pop(), AST_LIST_REMOVE_HEAD, ast_mutex_destroy, ast_mutex_lock, ast_mutex_unlock, sched_context::lock, sched_context::sched_heap, sched_context::schedc, and sched_context::schedq_ht.
Referenced by ast_channel_destructor(), ast_hangup(), ast_sched_thread_destroy(), cdr_engine_shutdown(), dnsmgr_shutdown(), load_module(), misdn_tasks_destroy(), sched_context_create(), and unload_module().
|
static |
Definition at line 234 of file sched.c.
References sched::id.
Referenced by sched_context_create().
|
static |
Definition at line 314 of file sched.c.
References ast_free, AST_LIST_INSERT_HEAD, SCHED_MAX_CACHE, sched_context::schedc, and sched_context::schedccnt.
Referenced by ast_sched_add_variable(), ast_sched_del(), and ast_sched_runq().
|
static |
Definition at line 90 of file sched.c.
References ast_cond_timedwait, ast_cond_wait, ast_mutex_lock, ast_mutex_unlock, ast_samp2tv(), ast_sched_runq(), ast_sched_wait(), ast_tvadd(), ast_tvnow(), ast_sched_thread::cond, ast_sched_thread::context, sched::data, ast_sched_thread::lock, and ast_sched_thread::stop.
Referenced by ast_sched_thread_create().
|
static |
given the last event *tv and the offset in milliseconds 'when', computes the next value,
Definition at line 380 of file sched.c.
References ast_samp2tv(), ast_tvadd(), ast_tvcmp(), ast_tvnow(), and ast_tvzero().
Referenced by ast_sched_add_variable(), and ast_sched_runq().
|
static |
Definition at line 241 of file sched.c.
References ast_tvcmp().
Referenced by sched_context_create().
|
static |
Take a sched structure and put it in the queue, such that the soonest event is first in the list.
Definition at line 361 of file sched.c.
References ast_hashtab_insert_safe(), ast_heap_push(), ast_log(), sched_context::highwater, sched::id, LOG_WARNING, sched_context::sched_heap, sched_context::schedcnt, and sched_context::schedq_ht.
Referenced by ast_sched_add_variable(), and ast_sched_runq().
|
static |
Definition at line 55 of file sched.c.
Referenced by ast_sched_del().