35 #define TASK_BUCKETS 53 86 if (!schtd->interval) {
96 schtd->is_running = 1;
100 res = schtd->task(schtd->task_data);
103 schtd->is_running = 0;
118 schtd->interval = res;
122 delay = schtd->interval;
128 schtd->next_periodic =
ast_tvadd(schtd->next_periodic,
136 if (schtd->current_scheduler_id < 0) {
139 ast_log(
LOG_ERROR,
"Sched %p: Failed to reschedule task %s\n", schtd, schtd->name);
146 ast_log(
LOG_DEBUG,
"Sched %p: Rescheduled %s for %d ms\n", schtd, schtd->name,
174 ao2_t_ref(schtd, +1,
"Give ref to run_task()");
185 ao2_t_ref(schtd, -1,
"Failed so release run_task() ref");
239 int *
interval,
int *time_left,
struct timeval *next_start)
243 memcpy(queued, &schtd->
when_queued,
sizeof(
struct timeval));
246 memcpy(last_start, &schtd->
last_start,
sizeof(
struct timeval));
249 memcpy(last_end, &schtd->
last_end,
sizeof(
struct timeval));
256 if (time_left || next_start) {
258 struct timeval since_when;
274 delay = delay < 0 ? 0 : delay;
283 memcpy(next_start, &next,
sizeof(
struct timeval));
307 int *
interval,
int *time_left,
struct timeval *next_start)
435 schtd->
task = sip_task;
440 strcpy(schtd->
name, name);
445 sprintf(schtd->
name,
"task_%08x", task_id);
488 #define TIME_FORMAT "%a %T" 506 e->
command =
"pjsip show scheduled_tasks";
507 e->
usage =
"Usage: pjsip show scheduled_tasks [ like <pattern> ]\n" 508 " Show scheduled pjsip tasks\n";
520 if (strcasecmp(a->
argv[3],
"like")) {
523 regrc = regcomp(®ex, a->
argv[4], REG_EXTENDED | REG_ICASE | REG_NOSUB);
526 regerror(regrc, ®ex, err, 256);
527 ast_cli(a->
fd,
"PJSIP Scheduled Tasks: Error: %s\n", err);
535 ast_sip_sched_task_sort_fn,
NULL);
537 ast_cli(a->
fd,
"PJSIP Scheduled Tasks: Unable to allocate temporary container\n");
542 ast_cli(a->
fd,
"PJSIP Scheduled Tasks: Unable to sort temporary container\n");
550 ast_cli(a->
fd,
"PJSIP Scheduled Tasks:\n\n");
553 "<Task Name....................................> <Interval> <Times Run> <State>" 554 " <Queued....> <Last Start> <Next Start.....secs>\n" 555 "==============================================================================" 556 "===================================================\n");
565 if (using_regex && regexec(®ex, schtd->
name, 0,
NULL, 0) == REG_NOMATCH) {
571 if (next_run_sec < 0) {
587 sprintf(times_run,
"%d", schtd->
run_count);
589 ast_cli(a->
fd,
"%-46.46s %9d %9s %-5s %-12s %-12s %-12s %8d\n",
617 if (!scheduler_context) {
630 ast_sip_sched_task_sort_fn, ast_sip_sched_task_cmp_fn);
646 if (scheduler_context) {
661 scheduler_context =
NULL;
#define ao2_t_ref(o, delta, tag)
Reference/unreference an object and return the old refcount.
int ast_sched_start_thread(struct ast_sched_context *con)
Start a thread for processing scheduler entries.
#define AST_CLI_DEFINE(fn, txt,...)
ast_sip_scheduler_task_flags
Task flags for the res_pjsip scheduler.
Asterisk main include file. File version handling, generic pbx functions.
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
The arg parameter is a search key, but is not an object.
AO2_STRING_FIELD_HASH_FN(ast_sip_sched_task, name)
struct timeval last_start
int ast_sip_sched_task_cancel(struct ast_sip_sched_task *schtd)
Cancels the next invocation of a task.
descriptor for a cli entry.
static struct ast_sched_context * scheduler_context
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
struct timeval when_queued
static char * cli_show_tasks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
#define ast_cli_register_multiple(e, len)
Register multiple commands.
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
int ast_sip_destroy_scheduler(void)
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
void ast_cli(int fd, const char *fmt,...)
int ast_sip_sched_task_get_times_by_name(const char *name, struct timeval *queued, struct timeval *last_start, struct timeval *last_end)
Gets the last start and end times of the task by name.
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
int ast_sip_sched_is_task_running(struct ast_sip_sched_task *schtd)
Checks if the task is currently running.
#define ast_strlen_zero(foo)
struct ast_sip_sched_task * ast_sip_schedule_task(struct ast_taskprocessor *serializer, int interval, ast_sip_task sip_task, const char *name, void *task_data, enum ast_sip_scheduler_task_flags flags)
Schedule a task to run in the res_pjsip thread pool.
static int push_to_serializer(const void *data)
int ast_sip_sched_task_get_next_run_by_name(const char *name)
Gets the number of milliseconds until the next invocation.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
struct timeval next_periodic
#define ao2_ref(o, delta)
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
int ast_sip_sched_task_get_times(struct ast_sip_sched_task *schtd, struct timeval *queued, struct timeval *last_start, struct timeval *last_end)
Gets the last start and end times of the task.
int ast_sip_sched_task_get_times_by_name2(const char *name, struct timeval *queued, struct timeval *last_start, struct timeval *last_end, int *interval, int *time_left, struct timeval *next_start)
Gets the queued, last start, last_end, time left, interval, next run by task name.
struct ast_sched_context * ast_sched_context_create(void)
Create a scheduler context.
static struct ast_cli_entry cli_commands[]
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
int ast_sip_sched_is_task_running_by_name(const char *name)
Checks if the task is currently running.
#define ao2_unlink(container, obj)
int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags)
Copy all object references in the src container into the dest container.
int ast_sip_push_task(struct ast_taskprocessor *serializer, int(*sip_task)(void *), void *task_data)
Pushes a task to SIP servants.
int ast_sip_sched_task_get_times2(struct ast_sip_sched_task *schtd, struct timeval *queued, struct timeval *last_start, struct timeval *last_end, int *interval, int *time_left, struct timeval *next_start)
Gets the queued, last start, last_end, time left, interval, next run.
enum ast_sip_scheduler_task_flags flags
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
userdata associated with baseline taskprocessor test
#define ao2_iterator_next(iter)
#define ao2_alloc(data_size, destructor_fn)
int ast_sip_initialize_scheduler(void)
Initialize scheduler.
The next invocation of the task is at last finish + interval.
int ast_sip_sched_task_get_next_run(struct ast_sip_sched_task *schtd)
Gets the number of milliseconds until the next invocation.
static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
int ast_sched_del(struct ast_sched_context *con, int id) attribute_warn_unused_result
Deletes a scheduled event.
static int run_task(void *data)
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
#define ao2_find(container, arg, flags)
An API for managing task processing threads that can be shared across modules.
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.
The scheduled task's events are tracked in the debug log.
A ast_taskprocessor structure is a singleton by name.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
static struct ao2_container * tasks
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
void * ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
Unreference the specified taskprocessor and its reference count will decrement.
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
struct ast_taskprocessor * serializer
#define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn)
Reject objects with duplicate keys in container.
static void schtd_dtor(void *data)
int ast_sip_sched_task_get_name(struct ast_sip_sched_task *schtd, char *name, size_t maxlen)
Gets the task name.
AO2_STRING_FIELD_CMP_FN(ast_sip_sched_task, name)
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
void ast_sched_context_destroy(struct ast_sched_context *c)
destroys a schedule context
int(* ast_sip_task)(void *user_data)
int ast_sip_sched_task_cancel_by_name(const char *name)
Cancels the next invocation of a task by name.
AO2_STRING_FIELD_SORT_FN(ast_sip_sched_task, name)
#define ao2_link(container, obj)