FreeSWITCH API Documentation  1.7.0
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_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

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 208 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_core_init(), switch_core_session_sched_heartbeat(), switch_ivr_schedule_broadcast(), switch_ivr_schedule_hangup(), switch_ivr_schedule_transfer(), switch_nat_late_init(), and switch_rtp_init().

211 {
212  switch_scheduler_task_container_t *container, *tp;
213  switch_event_t *event;
215  switch_ssize_t hlen = -1;
216 
217  switch_mutex_lock(globals.task_mutex);
218  switch_zmalloc(container, sizeof(*container));
219  switch_assert(func);
220 
221  if (task_runtime < now) {
222  container->task.repeat = (uint32_t)task_runtime;
223  task_runtime += now;
224  }
225 
226  container->func = func;
227  container->task.created = now;
228  container->task.runtime = task_runtime;
229  container->task.group = strdup(group ? group : "none");
230  container->task.cmd_id = cmd_id;
231  container->task.cmd_arg = cmd_arg;
232  container->flags = flags;
233  container->desc = strdup(desc ? desc : "none");
234  container->task.hash = switch_ci_hashfunc_default(container->task.group, &hlen);
235 
236  for (tp = globals.task_list; tp && tp->next; tp = tp->next);
237 
238  if (tp) {
239  tp->next = container;
240  } else {
241  globals.task_list = container;
242  }
243 
244  for (container->task.task_id = 0; !container->task.task_id; container->task.task_id = ++globals.task_id);
245 
246  switch_mutex_unlock(globals.task_mutex);
247 
248  tp = container;
249  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Added task %u %s (%s) to run at %" SWITCH_INT64_T_FMT "\n",
250  tp->task.task_id, tp->desc, switch_str_nil(tp->task.group), tp->task.runtime);
251 
253  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Task-ID", "%u", tp->task.task_id);
254  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Task-Desc", tp->desc);
257  switch_queue_push(globals.event_queue, event);
258  event = NULL;
259  }
260  return container->task.task_id;
261 }
#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:98
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
int64_t switch_time_t
Definition: switch_apr.h:188
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
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:903
struct switch_scheduler_task_container * next
#define SWITCH_INT64_T_FMT
#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:383
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
switch_status_t switch_queue_push(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1129
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)
static struct @6 globals
switch_scheduler_task_t task
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 293 of file switch_scheduler.c.

References switch_scheduler_task_container::destroyed, globals, switch_scheduler_task::group, switch_scheduler_task::hash, hash, switch_scheduler_task_container::next, SSHF_NO_DEL, SWITCH_CHANNEL_LOG, switch_ci_hashfunc_default(), 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().

294 {
296  uint32_t delcnt = 0;
297  switch_ssize_t hlen = -1;
298  unsigned long hash = 0;
299 
300  if (zstr(group)) {
301  return 0;
302  }
303 
304  hash = switch_ci_hashfunc_default(group, &hlen);
305 
306  switch_mutex_lock(globals.task_mutex);
307  for (tp = globals.task_list; tp; tp = tp->next) {
308  if (tp->destroyed) {
309  continue;
310  }
311  if (hash == tp->task.hash && !strcmp(tp->task.group, group)) {
312  if (switch_test_flag(tp, SSHF_NO_DEL)) {
313  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Attempt made to delete undeletable task #%u (group %s)\n",
314  tp->task.task_id, group);
315  continue;
316  }
317  tp->destroyed++;
318  delcnt++;
319  }
320  }
321  switch_mutex_unlock(globals.task_mutex);
322 
323  return delcnt;
324 }
#define SWITCH_CHANNEL_LOG
unsigned int switch_ci_hashfunc_default(const char *char_key, switch_ssize_t *klen)
Definition: switch_apr.c:98
switch_hash_t * hash
Definition: switch_event.c:74
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
intptr_t switch_ssize_t
struct switch_scheduler_task_container * next
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
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.
static struct @6 globals
switch_scheduler_task_t task
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 263 of file switch_scheduler.c.

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

264 {
266  uint32_t delcnt = 0;
267 
268  switch_mutex_lock(globals.task_mutex);
269  for (tp = globals.task_list; tp; tp = tp->next) {
270  if (tp->task.task_id == task_id) {
271  if (switch_test_flag(tp, SSHF_NO_DEL)) {
272  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Attempt made to delete undeletable task #%u (group %s)\n",
273  tp->task.task_id, tp->task.group);
274  break;
275  }
276 
277  if (tp->running) {
278  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Attempt made to delete running task #%u (group %s)\n",
279  tp->task.task_id, tp->task.group);
280  break;
281  }
282 
283  tp->destroyed++;
284  delcnt++;
285  break;
286  }
287  }
288  switch_mutex_unlock(globals.task_mutex);
289 
290  return delcnt;
291 }
#define SWITCH_CHANNEL_LOG
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
struct switch_scheduler_task_container * next
uint32_t task_id
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
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.
static struct @6 globals
switch_scheduler_task_t task
void switch_scheduler_task_thread_start ( void  )

Start the scheduler system.

Definition at line 328 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().

329 {
330 
331  switch_threadattr_t *thd_attr;
332 
333  switch_core_new_memory_pool(&globals.memory_pool);
334  switch_threadattr_create(&thd_attr, globals.memory_pool);
335  switch_mutex_init(&globals.task_mutex, SWITCH_MUTEX_NESTED, globals.memory_pool);
336  switch_queue_create(&globals.event_queue, 250000, globals.memory_pool);
337 
339 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
#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:270
switch_thread_t * task_thread_p
static void *SWITCH_THREAD_FUNC switch_scheduler_task_thread(switch_thread_t *thread, void *obj)
switch_status_t switch_threadattr_create(switch_threadattr_t **new_attr, switch_memory_pool_t *pool)
Definition: switch_apr.c:642
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:675
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1109
static struct @6 globals
void switch_scheduler_task_thread_stop ( void  )

Stop the scheduler system.

Definition at line 341 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().

342 {
343  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping Task Thread\n");
344  if (globals.task_thread_running == 1) {
345  int sanity = 0;
346  switch_status_t st;
347 
348  globals.task_thread_running = -1;
349 
351 
352  while (globals.task_thread_running) {
353  switch_yield(100000);
354  if (++sanity > 10) {
355  break;
356  }
357  }
358  }
359 
361 
362 }
#define SWITCH_CHANNEL_LOG
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
switch_status_t switch_thread_join(switch_status_t *retval, switch_thread_t *thd)
Definition: switch_apr.c:1255
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
switch_thread_t * task_thread_p
switch_status_t
Common return values.
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.
static struct @6 globals