RTS API Documentation  1.10.11
Data Structures | Functions
Scheduler
+ Collaboration diagram for Scheduler:

Data Structures

struct  switch_scheduler_task
 

Functions

uint32_t switch_scheduler_add_task (time_t task_runtime, switch_scheduler_func_t func, const char *desc, const char *group, uint32_t cmd_id, void *cmd_arg, switch_scheduler_flag_t flags)
 Schedule a task in the future. More...
 
uint32_t switch_scheduler_add_task_ex (time_t task_runtime, switch_scheduler_func_t func, const char *desc, const char *group, uint32_t cmd_id, void *cmd_arg, switch_scheduler_flag_t flags, uint32_t *task_id)
 Schedule a task in the future. More...
 
uint32_t switch_scheduler_del_task_id (uint32_t task_id)
 Delete a scheduled task. More...
 
uint32_t switch_scheduler_del_task_group (const char *group)
 Delete a scheduled task based on the group name. More...
 
void switch_scheduler_task_thread_start (void)
 Start the scheduler system. More...
 
void switch_scheduler_task_thread_stop (void)
 Stop the scheduler system. More...
 

Detailed Description

Function Documentation

◆ switch_scheduler_add_task()

uint32_t switch_scheduler_add_task ( time_t  task_runtime,
switch_scheduler_func_t  func,
const char *  desc,
const char *  group,
uint32_t  cmd_id,
void *  cmd_arg,
switch_scheduler_flag_t  flags 
)

Schedule a task in the future.

Parameters
task_runtimethe time in epoch seconds to execute the task.
functhe callback function to execute when the task is executed.
descan arbitrary description of the task.
groupa group id tag to link multiple tasks to a single entity.
cmd_idan arbitrary index number be used in the callback.
cmd_arguser data to be passed to the callback.
flagsflags to alter behaviour
Returns
the id of the task

Definition at line 211 of file switch_scheduler.c.

References switch_scheduler_add_task_ex(), and task_id.

Referenced by switch_core_init(), switch_core_session_sched_heartbeat(), switch_ivr_schedule_broadcast(), switch_ivr_schedule_hangup(), switch_ivr_schedule_transfer(), and switch_nat_late_init().

214 {
215  uint32_t task_id;
216 
217  switch_scheduler_add_task_ex(task_runtime, func, desc, group, cmd_id, cmd_arg, flags, &task_id);
218 
219  return task_id;
220 }
uint32_t switch_scheduler_add_task_ex(time_t task_runtime, switch_scheduler_func_t func, const char *desc, const char *group, uint32_t cmd_id, void *cmd_arg, switch_scheduler_flag_t flags, uint32_t *task_id)
Schedule a task in the future.
uint32_t task_id

◆ switch_scheduler_add_task_ex()

uint32_t switch_scheduler_add_task_ex ( time_t  task_runtime,
switch_scheduler_func_t  func,
const char *  desc,
const char *  group,
uint32_t  cmd_id,
void *  cmd_arg,
switch_scheduler_flag_t  flags,
uint32_t *  task_id 
)

Schedule a task in the future.

Parameters
task_runtimethe time in epoch seconds to execute the task.
functhe callback function to execute when the task is executed.
descan arbitrary description of the task.
groupa group id tag to link multiple tasks to a single entity.
cmd_idan arbitrary index number be used in the callback.
cmd_arguser data to be passed to the callback.
flagsflags to alter behaviour
task_idpointer to put the id of the task to
Returns
the id of the task

Definition at line 222 of file switch_scheduler.c.

References switch_scheduler_task::cmd_arg, switch_scheduler_task::cmd_id, switch_scheduler_task::created, switch_scheduler_task_container::desc, switch_scheduler_task_container::flags, switch_scheduler_task_container::func, globals, switch_scheduler_task::group, switch_scheduler_task::hash, switch_scheduler_task_container::next, switch_scheduler_task::repeat, switch_scheduler_task::runtime, switch_assert, SWITCH_CHANNEL_LOG, switch_ci_hashfunc_default(), switch_epoch_time_now(), switch_event_add_header(), switch_event_add_header_string(), SWITCH_EVENT_ADD_SCHEDULE, switch_event_create, SWITCH_INT64_T_FMT, SWITCH_LOG_DEBUG, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_queue_push(), SWITCH_STACK_BOTTOM, SWITCH_STATUS_SUCCESS, switch_str_nil, switch_zmalloc, switch_scheduler_task_container::task, and switch_scheduler_task::task_id.

Referenced by switch_scheduler_add_task().

225 {
226  uint32_t result;
227  switch_scheduler_task_container_t *container, *tp;
228  switch_event_t *event;
230  switch_ssize_t hlen = -1;
231 
232  switch_mutex_lock(globals.task_mutex);
233  switch_zmalloc(container, sizeof(*container));
234  switch_assert(func);
236 
237  if (task_runtime < now) {
238  container->task.repeat = (uint32_t)task_runtime;
239  task_runtime += now;
240  }
241 
242  container->func = func;
243  container->task.created = now;
244  container->task.runtime = task_runtime;
245  container->task.group = strdup(group ? group : "none");
246  container->task.cmd_id = cmd_id;
247  container->task.cmd_arg = cmd_arg;
248  container->flags = flags;
249  container->desc = strdup(desc ? desc : "none");
250  container->task.hash = switch_ci_hashfunc_default(container->task.group, &hlen);
251 
252  for (tp = globals.task_list; tp && tp->next; tp = tp->next);
253 
254  if (tp) {
255  tp->next = container;
256  } else {
257  globals.task_list = container;
258  }
259 
260  for (container->task.task_id = 0; !container->task.task_id; container->task.task_id = ++globals.task_id);
261 
262  tp = container;
263  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Added task %u %s (%s) to run at %" SWITCH_INT64_T_FMT "\n",
264  tp->task.task_id, tp->desc, switch_str_nil(tp->task.group), tp->task.runtime);
265 
267  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Task-ID", "%u", tp->task.task_id);
268  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Task-Desc", tp->desc);
271  switch_queue_push(globals.event_queue, event);
272  event = NULL;
273  }
274 
275  result = *task_id = container->task.task_id;
276 
277  switch_mutex_unlock(globals.task_mutex);
278 
279  return result;
280 }
#define SWITCH_CHANNEL_LOG
switch_scheduler_func_t func
switch_status_t switch_event_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *fmt,...) PRINTF_FUNCTION(4
Add a header to an event.
Representation of an event.
Definition: switch_event.h:80
unsigned int switch_ci_hashfunc_default(const char *char_key, switch_ssize_t *klen)
Definition: switch_apr.c:121
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
int64_t switch_time_t
Definition: switch_apr.h:188
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
intptr_t switch_ssize_t
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
#define switch_zmalloc(ptr, len)
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:993
struct switch_scheduler_task_container * next
#define switch_event_create(event, id)
Create a new event assuming it will not be custom event and therefore hiding the unused parameters...
Definition: switch_event.h:384
#define SWITCH_INT64_T_FMT
uint32_t task_id
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:322
static struct @8 globals
switch_status_t switch_queue_push(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1253
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
switch_scheduler_task_t task

◆ switch_scheduler_del_task_group()

uint32_t switch_scheduler_del_task_group ( const char *  group)

Delete a scheduled task based on the group name.

Parameters
groupthe group name
Returns
the number of jobs deleted

Definition at line 313 of file switch_scheduler.c.

References switch_scheduler_task_container::destroy_requested, switch_scheduler_task_container::destroyed, globals, switch_scheduler_task::group, switch_scheduler_task::hash, hash, switch_scheduler_task_container::next, switch_scheduler_task_container::running, SSHF_NO_DEL, SWITCH_CHANNEL_LOG, switch_ci_hashfunc_default(), SWITCH_LOG_INFO, switch_log_printf(), SWITCH_LOG_WARNING, switch_mutex_lock(), switch_mutex_unlock(), switch_test_flag, switch_scheduler_task_container::task, switch_scheduler_task::task_id, and zstr.

Referenced by switch_core_session_perform_destroy().

314 {
316  uint32_t delcnt = 0;
317  switch_ssize_t hlen = -1;
318  unsigned long hash = 0;
319 
320  if (zstr(group)) {
321  return 0;
322  }
323 
324  hash = switch_ci_hashfunc_default(group, &hlen);
325 
326  switch_mutex_lock(globals.task_mutex);
327  for (tp = globals.task_list; tp; tp = tp->next) {
328  if (tp->destroyed) {
329  continue;
330  }
331  if (hash == tp->task.hash && !strcmp(tp->task.group, group)) {
332  if (switch_test_flag(tp, SSHF_NO_DEL)) {
333  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Attempt made to delete undeletable task #%u (group %s)\n",
334  tp->task.task_id, group);
335  continue;
336  }
337  if (tp->running) {
338  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Attempt made to delete running task #%u (group %s)\n",
339  tp->task.task_id, tp->task.group);
340  tp->destroy_requested++;
341  } else {
342  tp->destroyed++;
343  }
344  delcnt++;
345  }
346  }
347  switch_mutex_unlock(globals.task_mutex);
348 
349  return delcnt;
350 }
#define SWITCH_CHANNEL_LOG
unsigned int switch_ci_hashfunc_default(const char *char_key, switch_ssize_t *klen)
Definition: switch_apr.c:121
switch_hash_t * hash
Definition: switch_event.c:76
#define zstr(x)
Definition: switch_utils.h:314
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
intptr_t switch_ssize_t
struct switch_scheduler_task_container * next
static struct @8 globals
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:693
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_scheduler_task_t task

◆ switch_scheduler_del_task_id()

uint32_t switch_scheduler_del_task_id ( uint32_t  task_id)

Delete a scheduled task.

Parameters
task_idthe id of the task
Returns
the number of jobs deleted

Definition at line 282 of file switch_scheduler.c.

References switch_scheduler_task_container::destroy_requested, switch_scheduler_task_container::destroyed, globals, switch_scheduler_task::group, switch_scheduler_task_container::next, switch_scheduler_task_container::running, SSHF_NO_DEL, SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, switch_log_printf(), SWITCH_LOG_WARNING, switch_mutex_lock(), switch_mutex_unlock(), switch_test_flag, switch_scheduler_task_container::task, and switch_scheduler_task::task_id.

Referenced by switch_core_session_unsched_heartbeat().

283 {
285  uint32_t delcnt = 0;
286 
287  switch_mutex_lock(globals.task_mutex);
288  for (tp = globals.task_list; tp; tp = tp->next) {
289  if (tp->task.task_id == task_id) {
290  if (switch_test_flag(tp, SSHF_NO_DEL)) {
291  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Attempt made to delete undeletable task #%u (group %s)\n",
292  tp->task.task_id, tp->task.group);
293  break;
294  }
295 
296  if (tp->running) {
297  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Attempt made to delete running task #%u (group %s)\n",
298  tp->task.task_id, tp->task.group);
299  tp->destroy_requested++;
300  } else {
301  tp->destroyed++;
302  }
303 
304  delcnt++;
305  break;
306  }
307  }
308  switch_mutex_unlock(globals.task_mutex);
309 
310  return delcnt;
311 }
#define SWITCH_CHANNEL_LOG
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
struct switch_scheduler_task_container * next
uint32_t task_id
static struct @8 globals
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:693
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_scheduler_task_t task

◆ switch_scheduler_task_thread_start()

void switch_scheduler_task_thread_start ( void  )

Start the scheduler system.

Definition at line 354 of file switch_scheduler.c.

References globals, switch_core_new_memory_pool, switch_mutex_init(), SWITCH_MUTEX_NESTED, switch_queue_create(), switch_scheduler_task_thread(), switch_thread_create(), switch_threadattr_create(), and task_thread_p.

Referenced by switch_core_init().

355 {
356 
357  switch_threadattr_t *thd_attr;
358 
359  switch_core_new_memory_pool(&globals.memory_pool);
360  switch_threadattr_create(&thd_attr, globals.memory_pool);
361  switch_mutex_init(&globals.task_mutex, SWITCH_MUTEX_NESTED, globals.memory_pool);
362  switch_queue_create(&globals.event_queue, 250000, globals.memory_pool);
363 
365 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core&#39;s master pool.
Definition: switch_core.h:633
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:293
switch_thread_t * task_thread_p
static void *SWITCH_THREAD_FUNC switch_scheduler_task_thread(switch_thread_t *thread, void *obj)
static struct @8 globals
switch_status_t switch_threadattr_create(switch_threadattr_t **new_attr, switch_memory_pool_t *pool)
Definition: switch_apr.c:665
switch_status_t switch_thread_create(switch_thread_t **new_thread, switch_threadattr_t *attr, switch_thread_start_t func, void *data, switch_memory_pool_t *cont)
Definition: switch_apr.c:698
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1233

◆ switch_scheduler_task_thread_stop()

void switch_scheduler_task_thread_stop ( void  )

Stop the scheduler system.

Definition at line 367 of file switch_scheduler.c.

References globals, SWITCH_CHANNEL_LOG, switch_core_destroy_memory_pool, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_thread_join(), switch_yield, and task_thread_p.

Referenced by switch_core_destroy().

368 {
369  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping Task Thread\n");
370  if (globals.task_thread_running == 1) {
371  int sanity = 0;
372  switch_status_t st;
373 
374  globals.task_thread_running = -1;
375 
377 
378  while (globals.task_thread_running) {
379  switch_yield(100000);
380  if (++sanity > 10) {
381  break;
382  }
383  }
384  }
385 
387 
388 }
#define SWITCH_CHANNEL_LOG
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:642
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:998
switch_status_t switch_thread_join(switch_status_t *retval, switch_thread_t *thd)
Definition: switch_apr.c:1379
switch_thread_t * task_thread_p
switch_status_t
Common return values.
static struct @8 globals
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.