Asterisk - The Open Source Telephony Project GIT-master-f36a736
Data Structures | Macros | Typedefs | Functions
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
 

Macros

#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) More...
 
#define AST_SCHED_CB(a)   ((ast_sched_cb)(a))
 
#define AST_SCHED_DEL(sched, id)
 Remove a scheduler entry. More...
 
#define AST_SCHED_DEL_ACCESSOR(sched, obj, getter, setter)
 
#define ast_sched_del_object(obj, destructor, con, id)
 Convenience macro for objects and reference (del) More...
 
#define AST_SCHED_DEL_SPINLOCK(sched, id, lock)
 schedule task to get deleted releasing the lock between attempts More...
 
#define AST_SCHED_DEL_UNREF(sched, id, refcall)
 schedule task to get deleted and call unref function More...
 
#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 More...
 

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. More...
 
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. 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_contextast_sched_context_create (void)
 Create a scheduler context. More...
 
void ast_sched_context_destroy (struct ast_sched_context *c)
 destroys a schedule context More...
 
int ast_sched_del (struct ast_sched_context *con, int id) attribute_warn_unused_result
 Deletes a scheduled event. More...
 
int ast_sched_del_nonrunning (struct ast_sched_context *con, int id) attribute_warn_unused_result
 Deletes a scheduled event with care against the event running. More...
 
void ast_sched_dump (struct ast_sched_context *con)
 Dumps the scheduler contents. 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) attribute_warn_unused_result
 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) attribute_warn_unused_result
 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)
 Runs the queue. 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) attribute_warn_unused_result
 Determines number of seconds until the next outstanding event to take place. More...
 
long ast_sched_when (struct ast_sched_context *con, int id)
 Returns the number of seconds before an event takes place. More...
 

Detailed Description

Scheduler Routines (derived from cheops)

Definition in file sched.h.

Macro Definition Documentation

◆ ast_sched_add_object

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

◆ AST_SCHED_CB

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

Definition at line 179 of file sched.h.

◆ AST_SCHED_DEL

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

◆ AST_SCHED_DEL_ACCESSOR

#define AST_SCHED_DEL_ACCESSOR (   sched,
  obj,
  getter,
  setter 
)

Definition at line 59 of file sched.h.

◆ ast_sched_del_object

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

Convenience macro for objects and reference (del)

Definition at line 376 of file sched.h.

◆ AST_SCHED_DEL_SPINLOCK

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

◆ AST_SCHED_DEL_UNREF

#define AST_SCHED_DEL_UNREF (   sched,
  id,
  refcall 
)

schedule task to get deleted and call unref function

Only calls the unref function if the task is actually deleted by ast_sched_del_nonrunning. If a failure occurs or the task is currently running and not rescheduled then refcall is not invoked.

See also
AST_SCHED_DEL
Since
1.6.1

Definition at line 82 of file sched.h.

◆ AST_SCHED_REPLACE

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

Definition at line 127 of file sched.h.

◆ AST_SCHED_REPLACE_UNREF

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

◆ AST_SCHED_REPLACE_VARIABLE

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

Definition at line 116 of file sched.h.

◆ AST_SCHED_REPLACE_VARIABLE_UNREF

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

Typedef Documentation

◆ ast_sched_cb

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

scheduler callback

A scheduler callback takes a pointer with callback data and

Return values
0if the callback should not be rescheduled
non-zeroif the callback should be scheduled again

Definition at line 178 of file sched.h.

Function Documentation

◆ ast_sched_add()

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
conScheduler context to add
whenhow many milliseconds to wait for event to occur
callbackfunction to call when the amount of time expires
datadata to pass to the callback
Return values
-1on failure
Returns
schedule item ID

Definition at line 567 of file sched.c.

568{
569 return ast_sched_add_variable(con, when, callback, data, 0);
570}
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.
Definition: sched.c:526

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

◆ ast_sched_add_variable()

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
conScheduler context to add
whenhow many milliseconds to wait for event to occur
callbackfunction to call when the amount of time expires
datadata to pass to the callback
variableIf 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

Return values
-1on failure
Returns
scheduled item ID

Adds a scheduled event with rescheduling support.

Definition at line 526 of file sched.c.

527{
528 struct sched *tmp;
529 int res = -1;
530
531 DEBUG(ast_debug(1, "ast_sched_add()\n"));
532
533 ast_mutex_lock(&con->lock);
534 if ((tmp = sched_alloc(con))) {
535 con->eventcnt++;
536 tmp->callback = callback;
537 tmp->data = data;
538 tmp->resched = when;
539 tmp->variable = variable;
540 tmp->when = ast_tv(0, 0);
541 tmp->deleted = 0;
542
543 sched_settime(&tmp->when, when);
544 schedule(con, tmp);
545 res = tmp->sched_id->id;
546 }
547#ifdef DUMP_SCHEDULER
548 /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
549 ast_sched_dump(con);
550#endif
551 if (con->sched_thread) {
553 }
554 ast_mutex_unlock(&con->lock);
555
556 return res;
557}
static int tmp()
Definition: bt_open.c:389
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_mutex_unlock(a)
Definition: lock.h:190
#define ast_mutex_lock(a)
Definition: lock.h:189
#define ast_cond_signal(cond)
Definition: lock.h:203
static struct sched * sched_alloc(struct ast_sched_context *con)
Definition: sched.c:380
static void sched_settime(struct timeval *t, int when)
given the last event *tv and the offset in milliseconds 'when', computes the next value,
Definition: sched.c:490
void ast_sched_dump(struct ast_sched_context *con)
Dump the contents of the scheduler to LOG_DEBUG.
Definition: sched.c:743
#define DEBUG(a)
Definition: sched.c:36
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.
Definition: sched.c:460
unsigned int eventcnt
Definition: sched.c:113
struct sched_thread * sched_thread
Definition: sched.c:118
ast_mutex_t lock
Definition: sched.c:112
ast_cond_t cond
Definition: sched.c:107
Definition: sched.c:76
const void * data
Definition: sched.c:91
struct timeval when
Definition: sched.c:80
int variable
Definition: sched.c:90
ast_sched_cb callback
Definition: sched.c:92
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:235

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

◆ ast_sched_clean_by_callback()

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.

Parameters
conScheduler Context
matchCallback to match
cleanup_cbCallback to run
Note
The return of cleanup_cb is ignored. No events are rescheduled.

Definition at line 409 of file sched.c.

410{
411 int i = 1;
412 struct sched *current;
413
414 ast_mutex_lock(&con->lock);
415 while ((current = ast_heap_peek(con->sched_heap, i))) {
416 if (current->callback != match) {
417 i++;
418 continue;
419 }
420
422
423 cleanup_cb(current->data);
425 }
426 ast_mutex_unlock(&con->lock);
427}
static int match(struct ast_sockaddr *addr, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
Definition: chan_iax2.c:2362
void * ast_heap_remove(struct ast_heap *h, void *elm)
Remove a specific element from a heap.
Definition: heap.c:251
void * ast_heap_peek(struct ast_heap *h, unsigned int index)
Peek at an element on a heap.
Definition: heap.c:267
size_t current
Definition: main/cli.c:113
static int cleanup_cb(void *obj, void *arg, int flags)
Definition: res_stasis.c:309
static void sched_release(struct ast_sched_context *con, struct sched *tmp)
Definition: sched.c:360
struct ast_heap * sched_heap
Definition: sched.c:117

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

◆ ast_sched_context_create()

struct ast_sched_context * ast_sched_context_create ( void  )

Create a scheduler context.

Return values
NULLon failure
Returns
a malloc'd sched_context structure

Definition at line 238 of file sched.c.

239{
240 struct ast_sched_context *tmp;
241
242 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
243 return NULL;
244 }
245
246 ast_mutex_init(&tmp->lock);
247 tmp->eventcnt = 1;
248
249 AST_LIST_HEAD_INIT_NOLOCK(&tmp->id_queue);
250
251 if (!(tmp->sched_heap = ast_heap_create(8, sched_time_cmp,
252 offsetof(struct sched, __heap_index)))) {
254 return NULL;
255 }
256
257 return tmp;
258}
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_heap_create(init_height, cmp_fn, index_offset)
Create a max heap.
Definition: heap.h:100
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:681
#define ast_mutex_init(pmutex)
Definition: lock.h:186
#define NULL
Definition: resample.c:96
static int sched_time_cmp(void *va, void *vb)
Definition: sched.c:225
void ast_sched_context_destroy(struct ast_sched_context *con)
destroys a schedule context
Definition: sched.c:271

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

◆ ast_sched_context_destroy()

void ast_sched_context_destroy ( struct ast_sched_context c)

destroys a schedule context

Parameters
cContext to free

Definition at line 271 of file sched.c.

272{
273 struct sched *s;
274 struct sched_id *sid;
275
277 con->sched_thread = NULL;
278
279 ast_mutex_lock(&con->lock);
280
281#ifdef SCHED_MAX_CACHE
282 while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list))) {
283 sched_free(s);
284 }
285#endif
286
287 if (con->sched_heap) {
288 while ((s = ast_heap_pop(con->sched_heap))) {
289 sched_free(s);
290 }
291 ast_heap_destroy(con->sched_heap);
292 con->sched_heap = NULL;
293 }
294
295 while ((sid = AST_LIST_REMOVE_HEAD(&con->id_queue, list))) {
296 ast_free(sid);
297 }
298
299 ast_mutex_unlock(&con->lock);
300 ast_mutex_destroy(&con->lock);
301
302 ast_free(con);
303}
#define ast_free(a)
Definition: astmm.h:180
struct ast_heap * ast_heap_destroy(struct ast_heap *h)
Destroy a max heap.
Definition: heap.c:146
void * ast_heap_pop(struct ast_heap *h)
Pop the max element off of the heap.
Definition: heap.c:262
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
#define ast_mutex_destroy(a)
Definition: lock.h:188
static void sched_free(struct sched *task)
Definition: sched.c:260
static void sched_thread_destroy(struct ast_sched_context *con)
Definition: sched.c:175
Scheduler ID holder.
Definition: sched.c:70
struct sched_id::@385 list

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

◆ ast_sched_del()

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
conscheduling context to delete item from
idID of the scheduled item to delete
Return values
-1on failure
0on success
Deprecated:
in favor of ast_sched_del_nonrunning which checks if the event is running and rescheduled

Deletes a scheduled event.

Definition at line 614 of file sched.c.

615{
616 return ast_sched_del_nonrunning(con, id) ? -1 : 0;
617}
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,...
Definition: sched.c:627

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

◆ ast_sched_del_nonrunning()

int ast_sched_del_nonrunning ( struct ast_sched_context con,
int  id 
)

Deletes a scheduled event with care against the event running.

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
conscheduling context to delete item from
idID of the scheduled item to delete
Return values
-1on failure
-2event was running but was deleted because it was not rescheduled
0on success

Deletes a scheduled event with care against the event running.

Definition at line 627 of file sched.c.

628{
629 struct sched *s = NULL;
630 int *last_id = ast_threadstorage_get(&last_del_id, sizeof(int));
631 int res = 0;
632
633 DEBUG(ast_debug(1, "ast_sched_del(%d)\n", id));
634
635 if (id < 0) {
636 return 0;
637 }
638
639 ast_mutex_lock(&con->lock);
640
641 s = sched_find(con, id);
642 if (s) {
643 if (!ast_heap_remove(con->sched_heap, s)) {
644 ast_log(LOG_WARNING,"sched entry %d not in the sched heap?\n", s->sched_id->id);
645 }
646 sched_release(con, s);
647 } else if (con->currently_executing && (id == con->currently_executing->sched_id->id)) {
648 if (con->executing_thread_id == pthread_self()) {
649 /* The scheduled callback is trying to delete itself.
650 * Not good as that is a deadlock. */
652 "BUG! Trying to delete sched %d from within the callback %p. "
653 "Ignoring so we don't deadlock\n",
654 id, con->currently_executing->callback);
656 /* We'll return -1 below because s is NULL.
657 * The caller will rightly assume that the unscheduling failed. */
658 } else {
659 s = con->currently_executing;
660 s->deleted = 1;
661 /* Wait for executing task to complete so that the caller of
662 * ast_sched_del() does not free memory out from under the task. */
663 while (con->currently_executing && (id == con->currently_executing->sched_id->id)) {
664 ast_cond_wait(&s->cond, &con->lock);
665 }
666 /* This is not rescheduled so the caller of ast_sched_del_nonrunning needs to know
667 * that it was still deleted
668 */
669 if (!s->rescheduled) {
670 res = -2;
671 }
672 /* ast_sched_runq knows we are waiting on this item and is passing responsibility for
673 * its destruction to us
674 */
675 sched_release(con, s);
676 s = NULL;
677 }
678 }
679
680#ifdef DUMP_SCHEDULER
681 /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
682 ast_sched_dump(con);
683#endif
684 if (con->sched_thread) {
686 }
687 ast_mutex_unlock(&con->lock);
688
689 if(res == -2){
690 return res;
691 }
692 else if (!s && *last_id != id) {
693 ast_debug(1, "Attempted to delete nonexistent schedule entry %d!\n", id);
694 /* Removing nonexistent schedule entry shouldn't trigger assert (it was enabled in DEV_MODE);
695 * because in many places entries is deleted without having valid id. */
696 *last_id = id;
697 return -1;
698 } else if (!s) {
699 return -1;
700 }
701
702 return res;
703}
enum queue_result id
Definition: app_queue.c:1667
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
#define LOG_WARNING
void ast_log_backtrace(void)
Log a backtrace of the current thread's execution stack to the Asterisk log.
Definition: logger.c:2510
#define ast_cond_wait(cond, mutex)
Definition: lock.h:205
static struct ast_threadstorage last_del_id
Definition: sched.c:58
static struct sched * sched_find(struct ast_sched_context *con, int id)
Definition: sched.c:572
pthread_t executing_thread_id
Definition: sched.c:122
struct sched * currently_executing
Definition: sched.c:120
int id
Definition: sched.c:72
ast_cond_t cond
Definition: sched.c:98
unsigned int rescheduled
Definition: sched.c:102
struct sched_id * sched_id
Definition: sched.c:79
unsigned int deleted
Definition: sched.c:100
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.

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

◆ ast_sched_dump()

void ast_sched_dump ( struct ast_sched_context con)

Dumps the scheduler contents.

Debugging: Dump the contents of the scheduler to stderr

Parameters
conContext to dump

Dumps the scheduler contents.

Definition at line 743 of file sched.c.

744{
745 struct sched *q;
746 struct timeval when;
747 int x;
748 size_t heap_size;
749
750 if (!DEBUG_ATLEAST(1)) {
751 return;
752 }
753
754 when = ast_tvnow();
755#ifdef SCHED_MAX_CACHE
756 ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%zu in Q, %u Total, %u Cache, %u high-water)\n",
757 ast_heap_size(con->sched_heap), con->eventcnt - 1, con->schedccnt, con->highwater);
758#else
759 ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%zu in Q, %u Total, %u high-water)\n",
760 ast_heap_size(con->sched_heap), con->eventcnt - 1, con->highwater);
761#endif
762
763 ast_log(LOG_DEBUG, "=============================================================\n");
764 ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n");
765 ast_log(LOG_DEBUG, "+-----+-----------------+-----------------+-----------------+\n");
766 ast_mutex_lock(&con->lock);
767 heap_size = ast_heap_size(con->sched_heap);
768 for (x = 1; x <= heap_size; x++) {
769 struct timeval delta;
770 q = ast_heap_peek(con->sched_heap, x);
771 delta = ast_tvsub(q->when, when);
772 ast_log(LOG_DEBUG, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n",
773 q->sched_id->id,
774 q->callback,
775 q->data,
776 (long)delta.tv_sec,
777 (long int)delta.tv_usec);
778 }
779 ast_mutex_unlock(&con->lock);
780 ast_log(LOG_DEBUG, "=============================================================\n");
781}
size_t ast_heap_size(struct ast_heap *h)
Get the current size of a heap.
Definition: heap.c:276
#define DEBUG_ATLEAST(level)
#define LOG_DEBUG
unsigned int schedccnt
Definition: sched.c:126
unsigned int highwater
Definition: sched.c:114
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: extconf.c:2297
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159

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

◆ ast_sched_find_data()

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
conscheduling context in which to search fro the matching id
idID of the scheduled item to find
Return values
NULLif not found
Returns
the data field from the matching sched struct if found.
Since
1.6.1

Definition at line 589 of file sched.c.

590{
591 struct sched *s;
592 const void *data = NULL;
593
594 ast_mutex_lock(&con->lock);
595
596 s = sched_find(con, id);
597 if (s) {
598 data = s->data;
599 }
600
601 ast_mutex_unlock(&con->lock);
602
603 return data;
604}

References ast_mutex_lock, ast_mutex_unlock, sched::data, ast_sched_context::lock, NULL, and sched_find().

◆ ast_sched_replace()

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
-1on failure
Returns
scheduled item ID

Definition at line 559 of file sched.c.

560{
561 if (old_id > -1) {
562 AST_SCHED_DEL(con, old_id);
563 }
564 return ast_sched_add(con, when, callback, data);
565}
int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data)
Adds a scheduled event.
Definition: sched.c:567
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
Definition: sched.h:46

References ast_sched_add(), AST_SCHED_DEL, sched::callback, sched::data, and sched::when.

Referenced by iax2_sched_replace().

◆ ast_sched_replace_variable()

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
-1on failure
Returns
scheduled item ID

Definition at line 514 of file sched.c.

515{
516 /* 0 means the schedule item is new; do not delete */
517 if (old_id > 0) {
518 AST_SCHED_DEL(con, old_id);
519 }
520 return ast_sched_add_variable(con, when, callback, data, variable);
521}

References ast_sched_add_variable(), and AST_SCHED_DEL.

◆ ast_sched_report()

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
conSchedule context to check
bufdynamic string to store report
cbnamesto check against
Since
1.6.1

Definition at line 705 of file sched.c.

706{
707 int i, x;
708 struct sched *cur;
709 int countlist[cbnames->numassocs + 1];
710 size_t heap_size;
711
712 memset(countlist, 0, sizeof(countlist));
713 ast_str_set(buf, 0, " Highwater = %u\n schedcnt = %zu\n", con->highwater, ast_heap_size(con->sched_heap));
714
715 ast_mutex_lock(&con->lock);
716
717 heap_size = ast_heap_size(con->sched_heap);
718 for (x = 1; x <= heap_size; x++) {
719 cur = ast_heap_peek(con->sched_heap, x);
720 /* match the callback to the cblist */
721 for (i = 0; i < cbnames->numassocs; i++) {
722 if (cur->callback == cbnames->cblist[i]) {
723 break;
724 }
725 }
726 if (i < cbnames->numassocs) {
727 countlist[i]++;
728 } else {
729 countlist[cbnames->numassocs]++;
730 }
731 }
732
733 ast_mutex_unlock(&con->lock);
734
735 for (i = 0; i < cbnames->numassocs; i++) {
736 ast_str_append(buf, 0, " %s : %d\n", cbnames->list[i], countlist[i]);
737 }
738
739 ast_str_append(buf, 0, " <unknown> : %d\n", countlist[cbnames->numassocs]);
740}
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
char * list[10]
Definition: sched.h:194
int numassocs
Definition: sched.h:193
ast_sched_cb cblist[10]
Definition: sched.h:195

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.

◆ ast_sched_runq()

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
conScheduling context to run
Returns
the number of events processed.

Runs the queue.

Definition at line 786 of file sched.c.

787{
788 struct sched *current;
789 struct timeval when;
790 int numevents;
791 int res;
792
793 DEBUG(ast_debug(1, "ast_sched_runq()\n"));
794
795 ast_mutex_lock(&con->lock);
796
797 when = ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
798 for (numevents = 0; (current = ast_heap_peek(con->sched_heap, 1)); numevents++) {
799 /* schedule all events which are going to expire within 1ms.
800 * We only care about millisecond accuracy anyway, so this will
801 * help us get more than one event at one time if they are very
802 * close together.
803 */
804 if (ast_tvcmp(current->when, when) != -1) {
805 break;
806 }
807
809
810 /*
811 * At this point, the schedule queue is still intact. We
812 * have removed the first event and the rest is still there,
813 * so it's permissible for the callback to add new events, but
814 * trying to delete itself won't work because it isn't in
815 * the schedule queue. If that's what it wants to do, it
816 * should return 0.
817 */
818
820 con->executing_thread_id = pthread_self();
821 ast_mutex_unlock(&con->lock);
822 res = current->callback(current->data);
823 ast_mutex_lock(&con->lock);
825 ast_cond_signal(&current->cond);
826
827 if (current->deleted) {
828 /*
829 * Another thread is waiting on this scheduled item. That thread
830 * will be responsible for it's destruction
831 */
832 current->rescheduled = res ? 1 : 0;
833 } else if (res) {
834 /*
835 * If they return non-zero, we should schedule them to be
836 * run again.
837 */
838 sched_settime(&current->when, current->variable ? res : current->resched);
839 schedule(con, current);
840 } else {
841 /* No longer needed, so release it */
843 }
844 }
845
846 ast_mutex_unlock(&con->lock);
847
848 return numevents;
849}
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compress two struct timeval instances returning -1, 0, 1 if the first arg is smaller,...
Definition: time.h:137
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2282

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

◆ ast_sched_start_thread()

int ast_sched_start_thread ( struct ast_sched_context con)

Start a thread for processing scheduler entries.

Parameters
conthe scheduler context this thread will manage
Return values
0success
non-zerofailure

Definition at line 197 of file sched.c.

198{
199 struct sched_thread *st;
200
201 if (con->sched_thread) {
202 ast_log(LOG_ERROR, "Thread already started on this scheduler context\n");
203 return -1;
204 }
205
206 if (!(st = ast_calloc(1, sizeof(*st)))) {
207 return -1;
208 }
209
210 ast_cond_init(&st->cond, NULL);
211
213
214 con->sched_thread = st;
215
217 ast_log(LOG_ERROR, "Failed to create scheduler thread\n");
219 return -1;
220 }
221
222 return 0;
223}
#define AST_PTHREADT_NULL
Definition: lock.h:66
#define ast_cond_init(cond, attr)
Definition: lock.h:201
static void * sched_run(void *data)
Definition: sched.c:134
pthread_t thread
Definition: sched.c:106
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:592

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

◆ ast_sched_wait()

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
concontext to act upon
Return values
-1if there is nothing there are no scheduled events (and thus the poll should not timeout)

Determines number of seconds until the next outstanding event to take place.

Definition at line 433 of file sched.c.

434{
435 int ms;
436 struct sched *s;
437
438 DEBUG(ast_debug(1, "ast_sched_wait()\n"));
439
440 ast_mutex_lock(&con->lock);
441 if ((s = ast_heap_peek(con->sched_heap, 1))) {
442 ms = ast_tvdiff_ms(s->when, ast_tvnow());
443 if (ms < 0) {
444 ms = 0;
445 }
446 } else {
447 ms = -1;
448 }
449 ast_mutex_unlock(&con->lock);
450
451 return ms;
452}
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107

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

◆ ast_sched_when()

long ast_sched_when ( struct ast_sched_context con,
int  id 
)

Returns the number of seconds before an event takes place.

Parameters
conContext to use
idId to dump

Definition at line 851 of file sched.c.

852{
853 struct sched *s;
854 long secs = -1;
855 DEBUG(ast_debug(1, "ast_sched_when()\n"));
856
857 ast_mutex_lock(&con->lock);
858
859 s = sched_find(con, id);
860 if (s) {
861 struct timeval now = ast_tvnow();
862 secs = s->when.tv_sec - now.tv_sec;
863 }
864
865 ast_mutex_unlock(&con->lock);
866
867 return secs;
868}

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