Asterisk - The Open Source Telephony Project GIT-master-f36a736
|
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/heap.h"
#include "asterisk/threadstorage.h"
Go to the source code of this file.
Data Structures | |
struct | ast_sched_context |
struct | sched |
struct | sched_id |
Scheduler ID holder. More... | |
struct | sched_thread |
Macros | |
#define | DEBUG(a) |
#define | ID_QUEUE_INCREMENT 16 |
#define | SCHED_MAX_CACHE 128 |
Max num of schedule structs. More... | |
Functions | |
static void | __init_last_del_id (void) |
static int | add_ids (struct ast_sched_context *con) |
Add new scheduler IDs to the queue. More... | |
int | ast_sched_add (struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) |
Adds a scheduled event. More... | |
int | ast_sched_add_variable (struct ast_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... | |
void | ast_sched_clean_by_callback (struct ast_sched_context *con, ast_sched_cb match, ast_sched_cb cleanup_cb) |
Clean all scheduled events with matching callback. More... | |
struct ast_sched_context * | ast_sched_context_create (void) |
Create a scheduler context. More... | |
void | ast_sched_context_destroy (struct ast_sched_context *con) |
destroys a schedule context More... | |
int | ast_sched_del (struct ast_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. Deprecated in favor of ast_sched_del_nonrunning which checks running event status. More... | |
int | ast_sched_del_nonrunning (struct ast_sched_context *con, int id) |
Delete the schedule entry with number "id". If running, wait for the task to complete, check to see if it is rescheduled then schedule the release. It's nearly impossible that there would be two or more in the list with that id. More... | |
void | ast_sched_dump (struct ast_sched_context *con) |
Dump the contents of the scheduler to LOG_DEBUG. More... | |
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. More... | |
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 More... | |
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 More... | |
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. More... | |
int | ast_sched_runq (struct ast_sched_context *con) |
Launch all events which need to be run at this time. More... | |
int | ast_sched_start_thread (struct ast_sched_context *con) |
Start a thread for processing scheduler entries. More... | |
int | ast_sched_wait (struct ast_sched_context *con) |
Return the number of milliseconds until the next scheduled event. More... | |
long | ast_sched_when (struct ast_sched_context *con, int id) |
Returns the number of seconds before an event takes place. More... | |
static struct sched * | sched_alloc (struct ast_sched_context *con) |
static struct sched * | sched_find (struct ast_sched_context *con, int id) |
static void | sched_free (struct sched *task) |
static void | sched_release (struct ast_sched_context *con, struct sched *tmp) |
static void * | sched_run (void *data) |
static void | sched_settime (struct timeval *t, int when) |
given the last event *tv and the offset in milliseconds 'when', computes the next value, More... | |
static void | sched_thread_destroy (struct ast_sched_context *con) |
static int | sched_time_cmp (void *va, void *vb) |
static void | schedule (struct ast_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... | |
static int | set_sched_id (struct ast_sched_context *con, struct sched *new_sched) |
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 SCHED_MAX_CACHE 128 |
|
static |
Add new scheduler IDs to the queue.
The | number of IDs added to the queue |
Definition at line 312 of file sched.c.
References ast_calloc, AST_LIST_INSERT_TAIL, sched_id::id, ast_sched_context::id_queue, ID_QUEUE_INCREMENT, ast_sched_context::id_queue_size, and sched_id::list.
Referenced by set_sched_id().
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
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 |
-1 | on failure |
Definition at line 567 of file sched.c.
References ast_sched_add_variable(), sched::callback, sched::data, and sched::when.
Referenced by ast_readaudio_callback(), ast_readvideo_callback(), ast_rtp_interpret(), ast_sched_replace(), ast_sip_schedule_task(), AST_TEST_DEFINE(), build_peer(), cc_generic_agent_start_offer_timer(), cc_generic_monitor_request_cc(), dns_query_recurring_resolution_callback(), do_register(), dundi_discover(), dundi_query(), dundi_send(), handle_cli_sched_bench(), handle_command_response(), iax2_hangup(), iax2_key_rotate(), iax2_sched_add(), mbl_call(), memory_cache_stale_update_full(), memory_cache_stale_update_object(), populate_addr(), precache_trans(), qualify_peer(), rtp_instance_parse_transport_wide_cc(), rtp_raw_write(), rtp_red_init(), run_task(), schedule_cache_expiration(), schedule_notification(), and transaction_sched_timer().
int ast_sched_add_variable | ( | struct ast_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.
Definition at line 526 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, ast_sched_context::lock, sched_alloc(), sched_settime(), ast_sched_context::sched_thread, schedule(), tmp(), sched::variable, and sched::when.
Referenced by apply_negotiated_sdp_stream(), ast_sched_add(), ast_sched_replace_variable(), dnsmgr_start_refresh(), do_reload(), global_loaded(), monitored_transport_state_callback(), start_batch_mode(), and stun_start_monitor().
void ast_sched_clean_by_callback | ( | struct ast_sched_context * | con, |
ast_sched_cb | match, | ||
ast_sched_cb | cleanup_cb | ||
) |
Clean all scheduled events with matching callback.
con | Scheduler Context |
match | Callback to match |
cleanup_cb | Callback to run |
Definition at line 409 of file sched.c.
References ast_heap_peek(), ast_heap_remove(), ast_mutex_lock, ast_mutex_unlock, cleanup_cb(), current, ast_sched_context::lock, match(), ast_sched_context::sched_heap, and sched_release().
Referenced by __unload_module(), ast_sip_destroy_transport_management(), and global_loaded().
struct ast_sched_context * ast_sched_context_create | ( | void | ) |
Create a scheduler context.
NULL | on failure |
Definition at line 238 of file sched.c.
References ast_calloc, ast_heap_create, AST_LIST_HEAD_INIT_NOLOCK, ast_mutex_init, ast_sched_context_destroy(), NULL, sched_time_cmp(), and tmp().
Referenced by __ast_channel_alloc_ap(), aeap_general_initialize(), ast_sip_initialize_distributor(), ast_sip_initialize_scheduler(), ast_sip_initialize_transport_management(), AST_TEST_DEFINE(), dns_core_init(), handle_cli_sched_bench(), load_module(), mbl_load_device(), and stun_start_monitor().
void ast_sched_context_destroy | ( | struct ast_sched_context * | c | ) |
destroys a schedule context
c | Context to free |
Definition at line 271 of file sched.c.
References ast_free, ast_heap_destroy(), ast_heap_pop(), AST_LIST_REMOVE_HEAD, ast_mutex_destroy, ast_mutex_lock, ast_mutex_unlock, ast_sched_context::id_queue, sched_id::list, ast_sched_context::lock, NULL, sched_free(), ast_sched_context::sched_heap, ast_sched_context::sched_thread, sched_thread_destroy(), and ast_sched_context::schedc.
Referenced by __unload_module(), aeap_general_finalize(), ast_channel_destructor(), ast_hangup(), ast_sched_context_create(), ast_sched_context_destroy_wrapper(), ast_sip_destroy_distributor(), ast_sip_destroy_scheduler(), ast_sip_destroy_transport_management(), ast_sip_initialize_scheduler(), ast_sip_initialize_transport_management(), AST_TEST_DEFINE(), cdr_engine_shutdown(), dns_shutdown(), handle_cli_sched_bench(), load_module(), mbl_load_device(), stun_start_monitor(), stun_stop_monitor(), and unload_module().
int ast_sched_del | ( | struct ast_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. Deprecated in favor of ast_sched_del_nonrunning which checks running event status.
Deletes a scheduled event.
Definition at line 614 of file sched.c.
References ast_sched_del_nonrunning().
Referenced by ast_rtp_prop_set(), ast_rtp_stop(), ast_sip_sched_task_cancel(), AST_TEST_DEFINE(), cc_generic_agent_stop_offer_timer(), cc_generic_monitor_cancel_available_timer(), destroy_event(), and handle_cli_sched_bench().
int ast_sched_del_nonrunning | ( | struct ast_sched_context * | con, |
int | id | ||
) |
Delete the schedule entry with number "id". If running, wait for the task to complete, check to see if it is rescheduled then schedule the release. It's nearly impossible that there would be two or more in the list with that id.
Deletes a scheduled event with care against the event running.
Definition at line 627 of file sched.c.
References ast_cond_signal, ast_cond_wait, ast_debug, ast_heap_remove(), ast_log, ast_log_backtrace(), ast_mutex_lock, ast_mutex_unlock, ast_sched_dump(), ast_threadstorage_get(), sched::callback, sched::cond, sched_thread::cond, ast_sched_context::currently_executing, DEBUG, sched::deleted, ast_sched_context::executing_thread_id, id, sched_id::id, last_del_id, ast_sched_context::lock, LOG_ERROR, LOG_WARNING, NULL, sched::rescheduled, sched_find(), ast_sched_context::sched_heap, sched::sched_id, sched_release(), and ast_sched_context::sched_thread.
Referenced by ast_sched_del().
void ast_sched_dump | ( | struct ast_sched_context * | con | ) |
Dump the contents of the scheduler to LOG_DEBUG.
Dumps the scheduler contents.
Definition at line 743 of file sched.c.
References ast_heap_peek(), ast_heap_size(), ast_log, ast_mutex_lock, ast_mutex_unlock, ast_tvnow(), ast_tvsub(), DEBUG_ATLEAST, ast_sched_context::eventcnt, ast_sched_context::highwater, ast_sched_context::lock, LOG_DEBUG, ast_sched_context::sched_heap, and ast_sched_context::schedccnt.
Referenced by ast_sched_add_variable(), ast_sched_del_nonrunning(), and handle_dump_sched().
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.
con | scheduling context in which to search fro the matching id |
id | ID of the scheduled item to find |
NULL | if not found |
Definition at line 589 of file sched.c.
References ast_mutex_lock, ast_mutex_unlock, sched::data, ast_sched_context::lock, NULL, and sched_find().
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
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 | on failure |
Definition at line 559 of file sched.c.
References ast_sched_add(), AST_SCHED_DEL, sched::callback, sched::data, and sched::when.
Referenced by iax2_sched_replace().
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
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 | on failure |
Definition at line 514 of file sched.c.
References ast_sched_add_variable(), and AST_SCHED_DEL.
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.
con | Schedule context to check |
buf | dynamic string to store report |
cbnames | to check against |
Definition at line 705 of file sched.c.
References ast_heap_peek(), ast_heap_size(), ast_mutex_lock, ast_mutex_unlock, ast_str_append(), ast_str_set(), buf, sched::callback, ast_cb_names::cblist, ast_sched_context::highwater, ast_cb_names::list, ast_sched_context::lock, ast_cb_names::numassocs, and ast_sched_context::sched_heap.
int ast_sched_runq | ( | struct ast_sched_context * | con | ) |
Launch all events which need to be run at this time.
Runs the queue.
Definition at line 786 of file sched.c.
References ast_cond_signal, ast_debug, ast_heap_peek(), ast_heap_pop(), ast_mutex_lock, ast_mutex_unlock, ast_tv(), ast_tvadd(), ast_tvcmp(), ast_tvnow(), current, ast_sched_context::currently_executing, DEBUG, ast_sched_context::executing_thread_id, ast_sched_context::lock, NULL, ast_sched_context::sched_heap, sched_release(), sched_settime(), and schedule().
Referenced by AST_TEST_DEFINE(), background_detect_exec(), dial_exec_full(), do_cdr(), do_monitor(), do_monitor_headset(), do_refresh(), handle_speechrecognize(), network_thread(), sched_run(), speech_background(), wait_for_winner(), and waitstream_core().
int ast_sched_start_thread | ( | struct ast_sched_context * | con | ) |
Start a thread for processing scheduler entries.
con | the scheduler context this thread will manage |
0 | success |
non-zero | failure |
Definition at line 197 of file sched.c.
References ast_calloc, ast_cond_init, ast_log, ast_pthread_create_background, AST_PTHREADT_NULL, sched_thread::cond, LOG_ERROR, NULL, sched_run(), ast_sched_context::sched_thread, sched_thread_destroy(), and sched_thread::thread.
Referenced by aeap_general_initialize(), ast_sip_initialize_distributor(), ast_sip_initialize_scheduler(), ast_sip_initialize_transport_management(), AST_TEST_DEFINE(), dns_core_init(), load_module(), and stun_start_monitor().
int ast_sched_wait | ( | struct ast_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.
Definition at line 433 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 AST_TEST_DEFINE(), background_detect_exec(), dial_exec_full(), do_cdr(), do_monitor(), do_monitor_headset(), do_refresh(), handle_speechrecognize(), network_thread(), sched_run(), speech_background(), wait_for_winner(), and waitstream_core().
long ast_sched_when | ( | struct ast_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 851 of file sched.c.
References ast_debug, ast_mutex_lock, ast_mutex_unlock, ast_tvnow(), DEBUG, ast_sched_context::lock, sched_find(), and sched::when.
Referenced by handle_cli_status().
|
static |
Definition at line 380 of file sched.c.
References ast_calloc, ast_cond_init, AST_LIST_REMOVE_HEAD, sched::list, NULL, sched_release(), ast_sched_context::schedc, ast_sched_context::schedccnt, set_sched_id(), and tmp().
Referenced by ast_sched_add_variable().
|
static |
Definition at line 572 of file sched.c.
References ast_heap_peek(), ast_heap_size(), sched_id::id, NULL, ast_sched_context::sched_heap, and sched::sched_id.
Referenced by ast_sched_del_nonrunning(), ast_sched_find_data(), and ast_sched_when().
|
static |
Definition at line 260 of file sched.c.
References ast_cond_destroy, ast_free, and task().
Referenced by ast_sched_context_destroy(), and sched_release().
|
static |
Definition at line 360 of file sched.c.
References AST_LIST_INSERT_HEAD, AST_LIST_INSERT_TAIL, ast_sched_context::id_queue, sched_id::list, NULL, sched_free(), SCHED_MAX_CACHE, ast_sched_context::schedc, ast_sched_context::schedccnt, and tmp().
Referenced by ast_sched_clean_by_callback(), ast_sched_del_nonrunning(), ast_sched_runq(), and sched_alloc().
|
static |
Definition at line 134 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(), sched_thread::cond, ast_sched_context::lock, NULL, ast_sched_context::sched_thread, and sched_thread::stop.
Referenced by ast_sched_start_thread().
|
static |
given the last event *tv and the offset in milliseconds 'when', computes the next value,
Definition at line 490 of file sched.c.
References ast_assert, ast_log, ast_samp2tv(), ast_tvadd(), ast_tvcmp(), ast_tvnow(), ast_tvzero(), and LOG_WARNING.
Referenced by ast_sched_add_variable(), and ast_sched_runq().
|
static |
Definition at line 175 of file sched.c.
References ast_cond_destroy, ast_cond_signal, ast_free, ast_mutex_lock, ast_mutex_unlock, AST_PTHREADT_NULL, sched_thread::cond, ast_sched_context::lock, NULL, ast_sched_context::sched_thread, sched_thread::stop, and sched_thread::thread.
Referenced by ast_sched_context_destroy(), and ast_sched_start_thread().
|
static |
Definition at line 225 of file sched.c.
References a, ast_tvcmp(), and b.
Referenced by ast_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 460 of file sched.c.
References ast_heap_push, ast_heap_size(), ast_sched_context::highwater, ast_sched_context::sched_heap, sched::tie_breaker, and ast_sched_context::tie_breaker.
Referenced by ast_sched_add_variable(), and ast_sched_runq().
|
static |
Definition at line 350 of file sched.c.
References add_ids(), AST_LIST_EMPTY, AST_LIST_REMOVE_HEAD, ast_sched_context::id_queue, sched_id::list, and sched::sched_id.
Referenced by sched_alloc().
|
static |
Definition at line 58 of file sched.c.
Referenced by ast_sched_del_nonrunning().