Wed Jan 8 2020 09:50:20

Asterisk developer's documentation


sched.c File Reference

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/linkedlists.h"
#include "asterisk/dlinkedlists.h"
#include "asterisk/hashtab.h"
#include "asterisk/heap.h"
#include "asterisk/threadstorage.h"

Go to the source code of this file.

Data Structures

struct  ast_sched_thread
 
struct  sched
 
struct  sched_context
 

Macros

#define DEBUG(a)
 

Functions

static void __init_last_del_id (void)
 
int ast_sched_add (struct 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. More...
 
int ast_sched_add_variable (struct 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...
 
int ast_sched_del (struct 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. More...
 
void ast_sched_dump (struct sched_context *con)
 Dump the contents of the scheduler to LOG_DEBUG. More...
 
const void * ast_sched_find_data (struct 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 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 sched_context *con, int when, ast_sched_cb callback, const void *data, int variable)
 replace a scheduler entry More...
 
void ast_sched_report (struct 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 sched_context *con)
 Launch all events which need to be run at this time. More...
 
int ast_sched_thread_add (struct ast_sched_thread *st, int when, ast_sched_cb cb, const void *data)
 Add a scheduler entry. More...
 
int ast_sched_thread_add_variable (struct ast_sched_thread *st, int when, ast_sched_cb cb, const void *data, int variable)
 Add a variable reschedule time scheduler entry. More...
 
struct ast_sched_threadast_sched_thread_create (void)
 Create a scheduler with a dedicated thread. More...
 
struct ast_sched_threadast_sched_thread_destroy (struct ast_sched_thread *st)
 Destroy a scheduler and its thread. More...
 
struct sched_contextast_sched_thread_get_context (struct ast_sched_thread *st)
 Get the scheduler context for a given ast_sched_thread. More...
 
void ast_sched_thread_poke (struct ast_sched_thread *st)
 Force re-processing of the scheduler context. More...
 
int ast_sched_wait (struct sched_context *con)
 Return the number of milliseconds until the next scheduled event. More...
 
long ast_sched_when (struct sched_context *con, int id)
 Returns the number of seconds before an event takes place. More...
 
static struct schedsched_alloc (struct sched_context *con)
 
static int sched_cmp (const void *a, const void *b)
 
struct sched_contextsched_context_create (void)
 New schedule context. More...
 
void sched_context_destroy (struct sched_context *con)
 destroys a schedule context Destroys (free's) the given sched_context structure More...
 
static unsigned int sched_hash (const void *obj)
 
static void sched_release (struct sched_context *con, struct sched *tmp)
 
static void * sched_run (void *data)
 
static int sched_settime (struct timeval *t, int when)
 given the last event *tv and the offset in milliseconds 'when', computes the next value, More...
 
static int sched_time_cmp (void *a, void *b)
 
static void schedule (struct 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...
 

Variables

static struct ast_threadstorage last_del_id = { .once = PTHREAD_ONCE_INIT , .key_init = __init_last_del_id , .custom_init = NULL , }
 

Detailed Description

Scheduler Routines (from cheops-NG)

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file sched.c.

Macro Definition Documentation

#define DEBUG (   a)

Function Documentation

static void __init_last_del_id ( void  )
static

Definition at line 55 of file sched.c.

57 {
int ast_sched_add ( struct 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
Returns
Returns a schedule item ID on success, -1 on failure

Definition at line 446 of file sched.c.

References ast_sched_add_variable().

Referenced by __oh323_update_info(), ast_readaudio_callback(), ast_readvideo_callback(), ast_rtp_raw_write(), ast_rtp_read(), ast_sched_replace(), ast_sched_thread_add(), build_peer(), create_esc_entry(), do_register(), do_reload(), dundi_discover(), dundi_query(), dundi_send(), handle_command_response(), handle_request_invite(), handle_response_invite(), handle_response_subscribe(), mgcp_postrequest(), network_change_event_cb(), parse_register_contact(), populate_addr(), precache_trans(), qualify_peer(), receive_digit(), rtp_red_init(), sip_cc_agent_start_offer_timer(), sip_cc_monitor_request_cc(), sip_hangup(), sip_scheddestroy(), sip_send_all_mwi_subscriptions(), start_session_timer(), submit_scheduled_batch(), submit_unscheduled_batch(), transmit_register(), and update_provisional_keepalive().

447 {
448  return ast_sched_add_variable(con, when, callback, data, 0);
449 }
int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
Schedule callback(data) to happen when ms into the future.
Definition: sched.c:406
int ast_sched_add_variable ( struct 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

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
Returns
Returns a schedule item ID on success, -1 on failure

Definition at line 406 of file sched.c.

References ast_debug, ast_mutex_lock, ast_mutex_unlock, ast_sched_dump(), ast_tv(), sched::callback, sched::data, DEBUG, sched_context::eventcnt, sched::id, sched_context::lock, option_debug, sched::resched, sched_alloc(), sched_release(), sched_settime(), schedule(), sched::variable, and sched::when.

Referenced by _misdn_tasks_add_variable(), ast_sched_add(), ast_sched_replace_variable(), ast_sched_thread_add_variable(), dnsmgr_start_refresh(), and do_reload().

407 {
408  struct sched *tmp;
409  int res = -1;
410 
411  DEBUG(ast_debug(1, "ast_sched_add()\n"));
412 
413  ast_mutex_lock(&con->lock);
414  if ((tmp = sched_alloc(con))) {
415  tmp->id = con->eventcnt++;
416  tmp->callback = callback;
417  tmp->data = data;
418  tmp->resched = when;
419  tmp->variable = variable;
420  tmp->when = ast_tv(0, 0);
421  if (sched_settime(&tmp->when, when)) {
422  sched_release(con, tmp);
423  } else {
424  schedule(con, tmp);
425  res = tmp->id;
426  }
427  }
428 #ifdef DUMP_SCHEDULER
429  /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
430  if (option_debug)
431  ast_sched_dump(con);
432 #endif
433  ast_mutex_unlock(&con->lock);
434 
435  return res;
436 }
static void sched_release(struct sched_context *con, struct sched *tmp)
Definition: sched.c:314
int option_debug
Definition: asterisk.c:182
static struct sched * sched_alloc(struct sched_context *con)
Definition: sched.c:296
void ast_sched_dump(struct sched_context *con)
Dumps the scheduler contents Debugging: Dump the contents of the scheduler to stderr.
Definition: sched.c:565
Definition: sched.c:57
#define ast_mutex_lock(a)
Definition: lock.h:155
int resched
Definition: sched.c:61
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
struct timeval when
Definition: sched.c:60
ast_mutex_t lock
Definition: sched.c:69
int id
Definition: sched.c:59
static void schedule(struct 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:361
const void * data
Definition: sched.c:63
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:179
#define DEBUG(a)
Definition: sched.c:40
int variable
Definition: sched.c:62
static int sched_settime(struct timeval *t, int when)
given the last event *tv and the offset in milliseconds &#39;when&#39;, computes the next value...
Definition: sched.c:380
ast_sched_cb callback
Definition: sched.c:64
unsigned int eventcnt
Definition: sched.c:70
#define ast_mutex_unlock(a)
Definition: lock.h:156
int ast_sched_del ( struct 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.

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

Definition at line 468 of file sched.c.

References ast_assert, ast_debug, ast_hashtab_lookup(), ast_hashtab_remove_this_object(), ast_heap_remove(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_sched_dump(), ast_threadstorage_get(), DEBUG, sched::id, last_del_id, sched_context::lock, LOG_WARNING, option_debug, sched_context::sched_heap, sched_release(), sched_context::schedcnt, and sched_context::schedq_ht.

Referenced by __sip_ack(), ast_rtp_prop_set(), ast_rtp_stop(), and destroy_event().

472 {
473  struct sched *s, tmp = {
474  .id = id,
475  };
476  int *last_id = ast_threadstorage_get(&last_del_id, sizeof(int));
477 
478  DEBUG(ast_debug(1, "ast_sched_del(%d)\n", id));
479 
480  if (id < 0) {
481  return 0;
482  }
483 
484  ast_mutex_lock(&con->lock);
485  s = ast_hashtab_lookup(con->schedq_ht, &tmp);
486  if (s) {
487  if (!ast_heap_remove(con->sched_heap, s)) {
488  ast_log(LOG_WARNING,"sched entry %d not in the sched heap?\n", s->id);
489  }
490 
492  ast_log(LOG_WARNING,"Found sched entry %d, then couldn't remove it?\n", s->id);
493  }
494 
495  con->schedcnt--;
496 
497  sched_release(con, s);
498  }
499 
500 #ifdef DUMP_SCHEDULER
501  /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
502  if (option_debug)
503  ast_sched_dump(con);
504 #endif
505  ast_mutex_unlock(&con->lock);
506 
507  if (!s && *last_id != id) {
508  ast_debug(1, "Attempted to delete nonexistent schedule entry %d!\n", id);
509 #ifndef AST_DEVMODE
510  ast_assert(s != NULL);
511 #else
512  {
513  char buf[100];
514  snprintf(buf, sizeof(buf), "s != NULL, id=%d", id);
515  _ast_assert(0, buf, file, line, function);
516  }
517 #endif
518  *last_id = id;
519  return -1;
520  } else if (!s) {
521  return -1;
522  }
523 
524  return 0;
525 }
static struct ast_threadstorage last_del_id
Definition: sched.c:55
void * ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj)
Lookup this object in the hash table.
Definition: hashtab.c:534
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
static void sched_release(struct sched_context *con, struct sched *tmp)
Definition: sched.c:314
struct ast_hashtab * schedq_ht
Definition: sched.c:73
int option_debug
Definition: asterisk.c:182
void ast_sched_dump(struct sched_context *con)
Dumps the scheduler contents Debugging: Dump the contents of the scheduler to stderr.
Definition: sched.c:565
#define LOG_WARNING
Definition: logger.h:144
Definition: sched.c:57
#define ast_assert(a)
Definition: utils.h:738
#define ast_mutex_lock(a)
Definition: lock.h:155
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
struct ast_heap * sched_heap
Definition: sched.c:74
ast_mutex_t lock
Definition: sched.c:69
int id
Definition: sched.c:59
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
void * ast_heap_remove(struct ast_heap *h, void *elm)
Remove a specific element from a heap.
Definition: heap.c:284
#define DEBUG(a)
Definition: sched.c:40
enum queue_result id
Definition: app_queue.c:1090
unsigned int schedcnt
Definition: sched.c:71
#define ast_mutex_unlock(a)
Definition: lock.h:156
void * ast_hashtab_remove_this_object(struct ast_hashtab *tab, void *obj)
Hash the object and then compare ptrs in bucket list instead of calling the compare routine...
Definition: hashtab.c:859
void ast_sched_dump ( struct sched_context con)

Dump the contents of the scheduler to LOG_DEBUG.

Dumps the scheduler contents Debugging: Dump the contents of the scheduler to stderr.

Definition at line 565 of file sched.c.

References ast_debug, ast_heap_peek(), ast_heap_size(), ast_mutex_lock, ast_mutex_unlock, ast_tvnow(), ast_tvsub(), sched_context::eventcnt, sched_context::highwater, sched_context::lock, sched_context::sched_heap, sched_context::schedccnt, and sched_context::schedcnt.

Referenced by ast_sched_add_variable(), ast_sched_del(), handle_dump_sched(), sip_do_reload(), and unload_module().

566 {
567  struct sched *q;
568  struct timeval when = ast_tvnow();
569  int x;
570  size_t heap_size;
571 #ifdef SCHED_MAX_CACHE
572  ast_debug(1, "Asterisk Schedule Dump (%u in Q, %u Total, %u Cache, %u high-water)\n", con->schedcnt, con->eventcnt - 1, con->schedccnt, con->highwater);
573 #else
574  ast_debug(1, "Asterisk Schedule Dump (%u in Q, %u Total, %u high-water)\n", con->schedcnt, con->eventcnt - 1, con->highwater);
575 #endif
576 
577  ast_debug(1, "=============================================================\n");
578  ast_debug(1, "|ID Callback Data Time (sec:ms) |\n");
579  ast_debug(1, "+-----+-----------------+-----------------+-----------------+\n");
580  ast_mutex_lock(&con->lock);
581  heap_size = ast_heap_size(con->sched_heap);
582  for (x = 1; x <= heap_size; x++) {
583  struct timeval delta;
584  q = ast_heap_peek(con->sched_heap, x);
585  delta = ast_tvsub(q->when, when);
586  ast_debug(1, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n",
587  q->id,
588  q->callback,
589  q->data,
590  (long)delta.tv_sec,
591  (long int)delta.tv_usec);
592  }
593  ast_mutex_unlock(&con->lock);
594  ast_debug(1, "=============================================================\n");
595 }
Definition: sched.c:57
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
#define ast_mutex_lock(a)
Definition: lock.h:155
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
struct ast_heap * sched_heap
Definition: sched.c:74
unsigned int schedccnt
Definition: sched.c:78
ast_mutex_t lock
Definition: sched.c:69
unsigned int highwater
Definition: sched.c:72
size_t ast_heap_size(struct ast_heap *h)
Get the current size of a heap.
Definition: heap.c:309
void * ast_heap_peek(struct ast_heap *h, unsigned int index)
Peek at an element on a heap.
Definition: heap.c:300
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: utils.c:1601
unsigned int eventcnt
Definition: sched.c:70
unsigned int schedcnt
Definition: sched.c:71
#define ast_mutex_unlock(a)
Definition: lock.h:156
const void* ast_sched_find_data ( struct 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
Returns
the data field from the matching sched struct if found; else return NULL if not found.
Since
1.6.1

Definition at line 451 of file sched.c.

References ast_hashtab_lookup(), sched::data, sched::id, and sched_context::schedq_ht.

452 {
453  struct sched tmp,*res;
454  tmp.id = id;
455  res = ast_hashtab_lookup(con->schedq_ht, &tmp);
456  if (res)
457  return res->data;
458  return NULL;
459 }
void * ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj)
Lookup this object in the hash table.
Definition: hashtab.c:534
struct ast_hashtab * schedq_ht
Definition: sched.c:73
Definition: sched.c:57
int id
Definition: sched.c:59
const void * data
Definition: sched.c:63
enum queue_result id
Definition: app_queue.c:1090
int ast_sched_replace ( int  old_id,
struct 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
-1failure
otherwise,returnsscheduled item ID

Definition at line 438 of file sched.c.

References ast_sched_add(), and AST_SCHED_DEL.

439 {
440  if (old_id > -1) {
441  AST_SCHED_DEL(con, old_id);
442  }
443  return ast_sched_add(con, when, callback, data);
444 }
int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
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.
Definition: sched.c:446
#define AST_SCHED_DEL(sched, id)
a loop construct to ensure that the scheduled task get deleted. The idea is that if we loop attemptin...
Definition: sched.h:51
struct timeval when
Definition: sched.c:60
const void * data
Definition: sched.c:63
ast_sched_cb callback
Definition: sched.c:64
int ast_sched_replace_variable ( int  old_id,
struct 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
-1failure
otherwise,returnsscheduled item ID

Definition at line 394 of file sched.c.

References ast_sched_add_variable(), and AST_SCHED_DEL.

395 {
396  /* 0 means the schedule item is new; do not delete */
397  if (old_id > 0) {
398  AST_SCHED_DEL(con, old_id);
399  }
401 }
int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
Schedule callback(data) to happen when ms into the future.
Definition: sched.c:406
#define AST_SCHED_DEL(sched, id)
a loop construct to ensure that the scheduled task get deleted. The idea is that if we loop attemptin...
Definition: sched.h:51
struct timeval when
Definition: sched.c:60
const void * data
Definition: sched.c:63
int variable
Definition: sched.c:62
ast_sched_cb callback
Definition: sched.c:64
void ast_sched_report ( struct 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 527 of file sched.c.

References ast_heap_peek(), ast_heap_size(), ast_mutex_lock, ast_mutex_unlock, ast_str_append(), ast_str_set(), sched::callback, ast_cb_names::cblist, sched_context::highwater, ast_cb_names::list, sched_context::lock, ast_cb_names::numassocs, sched_context::sched_heap, and sched_context::schedcnt.

Referenced by sip_show_sched().

528 {
529  int i, x;
530  struct sched *cur;
531  int countlist[cbnames->numassocs + 1];
532  size_t heap_size;
533 
534  memset(countlist, 0, sizeof(countlist));
535  ast_str_set(buf, 0, " Highwater = %u\n schedcnt = %u\n", con->highwater, con->schedcnt);
536 
537  ast_mutex_lock(&con->lock);
538 
539  heap_size = ast_heap_size(con->sched_heap);
540  for (x = 1; x <= heap_size; x++) {
541  cur = ast_heap_peek(con->sched_heap, x);
542  /* match the callback to the cblist */
543  for (i = 0; i < cbnames->numassocs; i++) {
544  if (cur->callback == cbnames->cblist[i]) {
545  break;
546  }
547  }
548  if (i < cbnames->numassocs) {
549  countlist[i]++;
550  } else {
551  countlist[cbnames->numassocs]++;
552  }
553  }
554 
555  ast_mutex_unlock(&con->lock);
556 
557  for (i = 0; i < cbnames->numassocs; i++) {
558  ast_str_append(buf, 0, " %s : %d\n", cbnames->list[i], countlist[i]);
559  }
560 
561  ast_str_append(buf, 0, " <unknown> : %d\n", countlist[cbnames->numassocs]);
562 }
char * list[10]
Definition: sched.h:165
ast_sched_cb cblist[10]
Definition: sched.h:166
Definition: sched.c:57
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:900
#define ast_mutex_lock(a)
Definition: lock.h:155
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:874
int numassocs
Definition: sched.h:164
struct ast_heap * sched_heap
Definition: sched.c:74
ast_mutex_t lock
Definition: sched.c:69
unsigned int highwater
Definition: sched.c:72
size_t ast_heap_size(struct ast_heap *h)
Get the current size of a heap.
Definition: heap.c:309
void * ast_heap_peek(struct ast_heap *h, unsigned int index)
Peek at an element on a heap.
Definition: heap.c:300
ast_sched_cb callback
Definition: sched.c:64
unsigned int schedcnt
Definition: sched.c:71
#define ast_mutex_unlock(a)
Definition: lock.h:156
int ast_sched_runq ( struct sched_context con)

Launch all events which need to be run at this time.

Runs the queue.

Definition at line 600 of file sched.c.

References ast_debug, ast_hashtab_remove_this_object(), ast_heap_peek(), ast_heap_pop(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_tv(), ast_tvadd(), ast_tvcmp(), ast_tvnow(), sched::callback, sched::data, DEBUG, sched::id, sched_context::lock, LOG_ERROR, sched::resched, sched_context::sched_heap, sched_release(), sched_settime(), sched_context::schedcnt, sched_context::schedq_ht, schedule(), sched::variable, and sched::when.

Referenced by background_detect_exec(), dial_exec_full(), do_cdr(), do_monitor(), do_refresh(), handle_speechrecognize(), misdn_tasks_thread_func(), network_thread(), reload_config(), sched_run(), speech_background(), wait_for_winner(), and waitstream_core().

601 {
602  struct sched *current;
603  struct timeval when;
604  int numevents;
605  int res;
606 
607  DEBUG(ast_debug(1, "ast_sched_runq()\n"));
608 
609  ast_mutex_lock(&con->lock);
610 
611  when = ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
612  for (numevents = 0; (current = ast_heap_peek(con->sched_heap, 1)); numevents++) {
613  /* schedule all events which are going to expire within 1ms.
614  * We only care about millisecond accuracy anyway, so this will
615  * help us get more than one event at one time if they are very
616  * close together.
617  */
618  if (ast_tvcmp(current->when, when) != -1) {
619  break;
620  }
621 
622  current = ast_heap_pop(con->sched_heap);
623 
624  if (!ast_hashtab_remove_this_object(con->schedq_ht, current)) {
625  ast_log(LOG_ERROR,"Sched entry %d was in the schedq list but not in the hashtab???\n", current->id);
626  }
627 
628  con->schedcnt--;
629 
630  /*
631  * At this point, the schedule queue is still intact. We
632  * have removed the first event and the rest is still there,
633  * so it's permissible for the callback to add new events, but
634  * trying to delete itself won't work because it isn't in
635  * the schedule queue. If that's what it wants to do, it
636  * should return 0.
637  */
638 
639  ast_mutex_unlock(&con->lock);
640  res = current->callback(current->data);
641  ast_mutex_lock(&con->lock);
642 
643  if (res) {
644  /*
645  * If they return non-zero, we should schedule them to be
646  * run again.
647  */
648  if (sched_settime(&current->when, current->variable? res : current->resched)) {
649  sched_release(con, current);
650  } else {
651  schedule(con, current);
652  }
653  } else {
654  /* No longer needed, so release it */
655  sched_release(con, current);
656  }
657  }
658 
659  ast_mutex_unlock(&con->lock);
660 
661  return numevents;
662 }
static void sched_release(struct sched_context *con, struct sched *tmp)
Definition: sched.c:314
struct ast_hashtab * schedq_ht
Definition: sched.c:73
Definition: sched.c:57
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
#define ast_mutex_lock(a)
Definition: lock.h:155
void * ast_heap_pop(struct ast_heap *h)
Pop the max element off of the heap.
Definition: heap.c:295
int resched
Definition: sched.c:61
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
struct ast_heap * sched_heap
Definition: sched.c:74
struct timeval when
Definition: sched.c:60
ast_mutex_t lock
Definition: sched.c:69
#define LOG_ERROR
Definition: logger.h:155
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compres two struct timeval instances returning -1, 0, 1 if the first arg is smaller, equal or greater to the second.
Definition: time.h:120
int id
Definition: sched.c:59
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: utils.c:1587
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static void schedule(struct 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:361
const void * data
Definition: sched.c:63
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:179
void * ast_heap_peek(struct ast_heap *h, unsigned int index)
Peek at an element on a heap.
Definition: heap.c:300
#define DEBUG(a)
Definition: sched.c:40
int variable
Definition: sched.c:62
static int sched_settime(struct timeval *t, int when)
given the last event *tv and the offset in milliseconds &#39;when&#39;, computes the next value...
Definition: sched.c:380
ast_sched_cb callback
Definition: sched.c:64
unsigned int schedcnt
Definition: sched.c:71
#define ast_mutex_unlock(a)
Definition: lock.h:156
void * ast_hashtab_remove_this_object(struct ast_hashtab *tab, void *obj)
Hash the object and then compare ptrs in bucket list instead of calling the compare routine...
Definition: hashtab.c:859
int ast_sched_thread_add ( struct ast_sched_thread st,
int  when,
ast_sched_cb  cb,
const void *  data 
)

Add a scheduler entry.

Parameters
stthe handle to the scheduler and thread
whenthe number of ms in the future to run the task. A value <= 0 is treated as "run now".
cbthe function to call when the scheduled time arrives
datathe parameter to pass to the scheduler callback
Return values
-1Failure
>=0Sched ID of added task

Definition at line 210 of file sched.c.

References ast_cond_signal, ast_mutex_lock, ast_mutex_unlock, ast_sched_add(), ast_sched_thread::cond, ast_sched_thread::context, and ast_sched_thread::lock.

Referenced by cc_generic_agent_start_offer_timer(), cc_generic_monitor_request_cc(), iax2_hangup(), iax2_key_rotate(), iax2_sched_add(), and iax2_sched_replace().

212 {
213  int res;
214 
215  ast_mutex_lock(&st->lock);
216  res = ast_sched_add(st->context, when, cb, data);
217  if (res != -1) {
218  ast_cond_signal(&st->cond);
219  }
220  ast_mutex_unlock(&st->lock);
221 
222  return res;
223 }
ast_mutex_t lock
Definition: sched.c:84
int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
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.
Definition: sched.c:446
ast_cond_t cond
Definition: sched.c:85
#define ast_mutex_lock(a)
Definition: lock.h:155
#define ast_cond_signal(cond)
Definition: lock.h:169
struct sched_context * context
Definition: sched.c:86
#define ast_mutex_unlock(a)
Definition: lock.h:156
int ast_sched_thread_add_variable ( struct ast_sched_thread st,
int  when,
ast_sched_cb  cb,
const void *  data,
int  variable 
)

Add a variable reschedule time scheduler entry.

Parameters
stthe handle to the scheduler and thread
whenthe number of ms in the future to run the task. A value <= 0 is treated as "run now".
cbthe function to call when the scheduled time arrives
datathe parameter to pass to the scheduler callback
variableIf this value is non-zero, then the scheduler will use the return value of the scheduler as the amount of time in the future to run the task again. Normally, a return value of 0 means do not re-schedule, and non-zero means re-schedule using the time provided when the scheduler entry was first created.
Return values
-1Failure
>=0Sched ID of added task

Definition at line 195 of file sched.c.

References ast_cond_signal, ast_mutex_lock, ast_mutex_unlock, ast_sched_add_variable(), ast_sched_thread::cond, ast_sched_thread::context, and ast_sched_thread::lock.

Referenced by stun_start_monitor().

197 {
198  int res;
199 
200  ast_mutex_lock(&st->lock);
201  res = ast_sched_add_variable(st->context, when, cb, data, variable);
202  if (res != -1) {
203  ast_cond_signal(&st->cond);
204  }
205  ast_mutex_unlock(&st->lock);
206 
207  return res;
208 }
ast_mutex_t lock
Definition: sched.c:84
int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
Schedule callback(data) to happen when ms into the future.
Definition: sched.c:406
ast_cond_t cond
Definition: sched.c:85
#define ast_mutex_lock(a)
Definition: lock.h:155
#define ast_cond_signal(cond)
Definition: lock.h:169
struct sched_context * context
Definition: sched.c:86
#define ast_mutex_unlock(a)
Definition: lock.h:156
struct ast_sched_thread* ast_sched_thread_create ( void  )

Create a scheduler with a dedicated thread.

This function should be used to allocate a scheduler context and a dedicated thread for processing scheduler entries. The thread is started immediately.

Return values
NULLerror
non-NULLa handle to the scheduler and its dedicated thread.

Definition at line 167 of file sched.c.

References ast_calloc, ast_cond_init, ast_log(), ast_mutex_init, ast_pthread_create_background, AST_PTHREADT_NULL, ast_sched_thread_destroy(), ast_sched_thread::cond, ast_sched_thread::context, ast_sched_thread::lock, LOG_ERROR, sched_context_create(), sched_run(), and ast_sched_thread::thread.

Referenced by ast_cc_init(), load_module(), and stun_start_monitor().

168 {
169  struct ast_sched_thread *st;
170 
171  if (!(st = ast_calloc(1, sizeof(*st)))) {
172  return NULL;
173  }
174 
175  ast_mutex_init(&st->lock);
176  ast_cond_init(&st->cond, NULL);
177 
179 
180  if (!(st->context = sched_context_create())) {
181  ast_log(LOG_ERROR, "Failed to create scheduler\n");
183  return NULL;
184  }
185 
186  if (ast_pthread_create_background(&st->thread, NULL, sched_run, st)) {
187  ast_log(LOG_ERROR, "Failed to create scheduler thread\n");
189  return NULL;
190  }
191 
192  return st;
193 }
ast_mutex_t lock
Definition: sched.c:84
struct ast_sched_thread * ast_sched_thread_destroy(struct ast_sched_thread *st)
Destroy a scheduler and its thread.
Definition: sched.c:143
static void * sched_run(void *data)
Definition: sched.c:90
#define ast_cond_init(cond, attr)
Definition: lock.h:167
ast_cond_t cond
Definition: sched.c:85
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:426
#define AST_PTHREADT_NULL
Definition: lock.h:65
struct sched_context * context
Definition: sched.c:86
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
pthread_t thread
Definition: sched.c:83
struct sched_context * sched_context_create(void)
New schedule context.
Definition: sched.c:246
#define ast_calloc(a, b)
Definition: astmm.h:82
#define ast_mutex_init(pmutex)
Definition: lock.h:152
struct ast_sched_thread* ast_sched_thread_destroy ( struct ast_sched_thread st)

Destroy a scheduler and its thread.

This function is used to destroy a scheduler context and the dedicated thread that was created for handling scheduler entries. Any entries in the scheduler that have not yet been processed will be thrown away. Once this function is called, the handle must not be used again.

Parameters
stthe handle to the scheduler and thread
Returns
NULL for convenience

Definition at line 143 of file sched.c.

References ast_cond_destroy, ast_cond_signal, ast_free, ast_mutex_destroy, ast_mutex_lock, ast_mutex_unlock, AST_PTHREADT_NULL, ast_sched_thread::cond, ast_sched_thread::context, ast_sched_thread::lock, sched_context_destroy(), ast_sched_thread::stop, and ast_sched_thread::thread.

Referenced by __unload_module(), ast_sched_thread_create(), cc_shutdown(), load_module(), stun_start_monitor(), and stun_stop_monitor().

144 {
145  if (st->thread != AST_PTHREADT_NULL) {
146  ast_mutex_lock(&st->lock);
147  st->stop = 1;
148  ast_cond_signal(&st->cond);
149  ast_mutex_unlock(&st->lock);
150  pthread_join(st->thread, NULL);
152  }
153 
154  ast_mutex_destroy(&st->lock);
155  ast_cond_destroy(&st->cond);
156 
157  if (st->context) {
159  st->context = NULL;
160  }
161 
162  ast_free(st);
163 
164  return NULL;
165 }
ast_mutex_t lock
Definition: sched.c:84
ast_cond_t cond
Definition: sched.c:85
#define ast_mutex_lock(a)
Definition: lock.h:155
#define ast_cond_signal(cond)
Definition: lock.h:169
#define AST_PTHREADT_NULL
Definition: lock.h:65
struct sched_context * context
Definition: sched.c:86
void sched_context_destroy(struct sched_context *c)
destroys a schedule context Destroys (free&#39;s) the given sched_context structure
Definition: sched.c:267
#define ast_cond_destroy(cond)
Definition: lock.h:168
unsigned int stop
Definition: sched.c:87
pthread_t thread
Definition: sched.c:83
#define ast_free(a)
Definition: astmm.h:97
#define ast_mutex_destroy(a)
Definition: lock.h:154
#define ast_mutex_unlock(a)
Definition: lock.h:156
struct sched_context* ast_sched_thread_get_context ( struct ast_sched_thread st)

Get the scheduler context for a given ast_sched_thread.

This function should be used only when direct access to the scheduler context is required. Its use is discouraged unless necessary. The cases where this is currently required is when you want to take advantage of one of the AST_SCHED macros.

Parameters
stthe handle to the scheduler and thread
Returns
the sched_context associated with an ast_sched_thread

Definition at line 138 of file sched.c.

References ast_sched_thread::context.

Referenced by iax2_destroy_helper().

139 {
140  return st->context;
141 }
struct sched_context * context
Definition: sched.c:86
void ast_sched_thread_poke ( struct ast_sched_thread st)

Force re-processing of the scheduler context.

Parameters
stthe handle to the scheduler and thread
Returns
nothing

Definition at line 131 of file sched.c.

References ast_cond_signal, ast_mutex_lock, ast_mutex_unlock, ast_sched_thread::cond, and ast_sched_thread::lock.

132 {
133  ast_mutex_lock(&st->lock);
134  ast_cond_signal(&st->cond);
135  ast_mutex_unlock(&st->lock);
136 }
ast_mutex_t lock
Definition: sched.c:84
ast_cond_t cond
Definition: sched.c:85
#define ast_mutex_lock(a)
Definition: lock.h:155
#define ast_cond_signal(cond)
Definition: lock.h:169
#define ast_mutex_unlock(a)
Definition: lock.h:156
int ast_sched_wait ( struct 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 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.

Definition at line 334 of file sched.c.

References ast_debug, ast_heap_peek(), ast_mutex_lock, ast_mutex_unlock, ast_tvdiff_ms(), ast_tvnow(), DEBUG, sched_context::lock, sched_context::sched_heap, and sched::when.

Referenced by background_detect_exec(), dial_exec_full(), do_cdr(), do_monitor(), do_refresh(), handle_speechrecognize(), misdn_tasks_thread_func(), network_thread(), sched_run(), speech_background(), wait_for_winner(), and waitstream_core().

335 {
336  int ms;
337  struct sched *s;
338 
339  DEBUG(ast_debug(1, "ast_sched_wait()\n"));
340 
341  ast_mutex_lock(&con->lock);
342  if ((s = ast_heap_peek(con->sched_heap, 1))) {
343  ms = ast_tvdiff_ms(s->when, ast_tvnow());
344  if (ms < 0) {
345  ms = 0;
346  }
347  } else {
348  ms = -1;
349  }
350  ast_mutex_unlock(&con->lock);
351 
352  return ms;
353 }
Definition: sched.c:57
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
#define ast_mutex_lock(a)
Definition: lock.h:155
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:90
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
struct ast_heap * sched_heap
Definition: sched.c:74
struct timeval when
Definition: sched.c:60
ast_mutex_t lock
Definition: sched.c:69
void * ast_heap_peek(struct ast_heap *h, unsigned int index)
Peek at an element on a heap.
Definition: heap.c:300
#define DEBUG(a)
Definition: sched.c:40
#define ast_mutex_unlock(a)
Definition: lock.h:156
long ast_sched_when ( struct 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 664 of file sched.c.

References ast_debug, ast_hashtab_lookup(), ast_mutex_lock, ast_mutex_unlock, ast_tvnow(), DEBUG, sched::id, sched_context::lock, sched_context::schedq_ht, and sched::when.

Referenced by _sip_show_peer(), handle_cli_status(), and parse_register_contact().

665 {
666  struct sched *s, tmp;
667  long secs = -1;
668  DEBUG(ast_debug(1, "ast_sched_when()\n"));
669 
670  ast_mutex_lock(&con->lock);
671 
672  /* these next 2 lines replace a lookup loop */
673  tmp.id = id;
674  s = ast_hashtab_lookup(con->schedq_ht, &tmp);
675 
676  if (s) {
677  struct timeval now = ast_tvnow();
678  secs = s->when.tv_sec - now.tv_sec;
679  }
680  ast_mutex_unlock(&con->lock);
681 
682  return secs;
683 }
void * ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj)
Lookup this object in the hash table.
Definition: hashtab.c:534
struct ast_hashtab * schedq_ht
Definition: sched.c:73
Definition: sched.c:57
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
#define ast_mutex_lock(a)
Definition: lock.h:155
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
struct timeval when
Definition: sched.c:60
ast_mutex_t lock
Definition: sched.c:69
int id
Definition: sched.c:59
#define DEBUG(a)
Definition: sched.c:40
enum queue_result id
Definition: app_queue.c:1090
#define ast_mutex_unlock(a)
Definition: lock.h:156
static struct sched* sched_alloc ( struct sched_context con)
static

Definition at line 296 of file sched.c.

References ast_calloc, AST_LIST_REMOVE_HEAD, sched_context::schedc, and sched_context::schedccnt.

Referenced by ast_sched_add_variable().

297 {
298  struct sched *tmp;
299 
300  /*
301  * We keep a small cache of schedule entries
302  * to minimize the number of necessary malloc()'s
303  */
304 #ifdef SCHED_MAX_CACHE
305  if ((tmp = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
306  con->schedccnt--;
307  else
308 #endif
309  tmp = ast_calloc(1, sizeof(*tmp));
310 
311  return tmp;
312 }
struct sched_context::@297 schedc
Definition: sched.c:57
unsigned int schedccnt
Definition: sched.c:78
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:818
struct sched::@296 list
#define ast_calloc(a, b)
Definition: astmm.h:82
static int sched_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 227 of file sched.c.

References sched::id.

Referenced by sched_context_create().

228 {
229  const struct sched *as = a;
230  const struct sched *bs = b;
231  return as->id != bs->id; /* return 0 on a match like strcmp would */
232 }
Definition: sched.c:57
int id
Definition: sched.c:59
struct sched_context* sched_context_create ( void  )

New schedule context.

Note
Create a scheduling context
Returns
Returns a malloc'd sched_context structure, NULL on failure

Definition at line 246 of file sched.c.

References ast_calloc, ast_hashtab_create(), ast_hashtab_newsize_java(), ast_hashtab_resize_java(), ast_heap_create(), ast_mutex_init, sched_context::eventcnt, sched_context::lock, sched_cmp(), sched_context_destroy(), sched_hash(), sched_context::sched_heap, sched_time_cmp(), and sched_context::schedq_ht.

Referenced by __ast_channel_alloc_ap(), ast_cdr_engine_init(), ast_sched_thread_create(), dnsmgr_init(), load_module(), and misdn_tasks_init().

247 {
248  struct sched_context *tmp;
249 
250  if (!(tmp = ast_calloc(1, sizeof(*tmp))))
251  return NULL;
252 
253  ast_mutex_init(&tmp->lock);
254  tmp->eventcnt = 1;
255 
257 
258  if (!(tmp->sched_heap = ast_heap_create(8, sched_time_cmp,
259  offsetof(struct sched, __heap_index)))) {
261  return NULL;
262  }
263 
264  return tmp;
265 }
static unsigned int sched_hash(const void *obj)
Definition: sched.c:234
int ast_hashtab_newsize_java(struct ast_hashtab *tab)
Create a prime number roughly 2x the current table size.
Definition: hashtab.c:131
struct ast_hashtab * schedq_ht
Definition: sched.c:73
Definition: sched.c:57
int ast_hashtab_resize_java(struct ast_hashtab *tab)
Determines if a table resize should occur using the Java algorithm (if the table load factor is 75% o...
Definition: hashtab.c:88
struct ast_heap * sched_heap
Definition: sched.c:74
static int sched_cmp(const void *a, const void *b)
Definition: sched.c:227
struct ast_hashtab * ast_hashtab_create(int initial_buckets, int(*compare)(const void *a, const void *b), int(*resize)(struct ast_hashtab *), int(*newsize)(struct ast_hashtab *tab), unsigned int(*hash)(const void *obj), int do_locking)
Create the hashtable list.
Definition: hashtab.c:226
ast_mutex_t lock
Definition: sched.c:69
void sched_context_destroy(struct sched_context *c)
destroys a schedule context Destroys (free&#39;s) the given sched_context structure
Definition: sched.c:267
static int sched_time_cmp(void *a, void *b)
Definition: sched.c:241
#define ast_calloc(a, b)
Definition: astmm.h:82
struct ast_heap * ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_fn, ssize_t index_offset)
Create a max heap.
Definition: heap.c:118
#define ast_mutex_init(pmutex)
Definition: lock.h:152
unsigned int eventcnt
Definition: sched.c:70
void sched_context_destroy ( struct sched_context c)

destroys a schedule context Destroys (free's) the given sched_context structure

Parameters
cContext to free
Returns
Returns 0 on success, -1 on failure

Definition at line 267 of file sched.c.

References ast_free, ast_hashtab_destroy(), ast_heap_destroy(), ast_heap_pop(), AST_LIST_REMOVE_HEAD, ast_mutex_destroy, ast_mutex_lock, ast_mutex_unlock, sched_context::lock, sched_context::sched_heap, sched_context::schedc, and sched_context::schedq_ht.

Referenced by ast_channel_destructor(), ast_hangup(), ast_sched_thread_destroy(), cdr_engine_shutdown(), dnsmgr_shutdown(), load_module(), misdn_tasks_destroy(), sched_context_create(), and unload_module().

268 {
269  struct sched *s;
270 
271  ast_mutex_lock(&con->lock);
272 
273 #ifdef SCHED_MAX_CACHE
274  /* Eliminate the cache */
275  while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
276  ast_free(s);
277 #endif
278 
279  if (con->sched_heap) {
280  while ((s = ast_heap_pop(con->sched_heap))) {
281  ast_free(s);
282  }
284  con->sched_heap = NULL;
285  }
286 
287  ast_hashtab_destroy(con->schedq_ht, NULL);
288  con->schedq_ht = NULL;
289 
290  /* And the context */
291  ast_mutex_unlock(&con->lock);
292  ast_mutex_destroy(&con->lock);
293  ast_free(con);
294 }
struct ast_hashtab * schedq_ht
Definition: sched.c:73
struct sched_context::@297 schedc
Definition: sched.c:57
struct ast_heap * ast_heap_destroy(struct ast_heap *h)
Destroy a max heap.
Definition: heap.c:163
#define ast_mutex_lock(a)
Definition: lock.h:155
void * ast_heap_pop(struct ast_heap *h)
Pop the max element off of the heap.
Definition: heap.c:295
struct ast_heap * sched_heap
Definition: sched.c:74
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:818
ast_mutex_t lock
Definition: sched.c:69
struct sched::@296 list
#define ast_free(a)
Definition: astmm.h:97
#define ast_mutex_destroy(a)
Definition: lock.h:154
#define ast_mutex_unlock(a)
Definition: lock.h:156
void ast_hashtab_destroy(struct ast_hashtab *tab, void(*objdestroyfunc)(void *obj))
This func will free the hash table and all its memory.
Definition: hashtab.c:388
static unsigned int sched_hash ( const void *  obj)
static

Definition at line 234 of file sched.c.

References sched::id.

Referenced by sched_context_create().

235 {
236  const struct sched *s = obj;
237  unsigned int h = s->id;
238  return h;
239 }
Definition: sched.c:57
int id
Definition: sched.c:59
static void sched_release ( struct sched_context con,
struct sched tmp 
)
static

Definition at line 314 of file sched.c.

References ast_free, AST_LIST_INSERT_HEAD, SCHED_MAX_CACHE, sched_context::schedc, and sched_context::schedccnt.

Referenced by ast_sched_add_variable(), ast_sched_del(), and ast_sched_runq().

315 {
316  /*
317  * Add to the cache, or just free() if we
318  * already have too many cache entries
319  */
320 
321 #ifdef SCHED_MAX_CACHE
322  if (con->schedccnt < SCHED_MAX_CACHE) {
323  AST_LIST_INSERT_HEAD(&con->schedc, tmp, list);
324  con->schedccnt++;
325  } else
326 #endif
327  ast_free(tmp);
328 }
struct sched_context::@297 schedc
#define SCHED_MAX_CACHE
Max num of schedule structs.
Definition: sched.h:37
unsigned int schedccnt
Definition: sched.c:78
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:696
struct sched::@296 list
#define ast_free(a)
Definition: astmm.h:97
static void* sched_run ( void *  data)
static

Definition at line 90 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(), ast_sched_thread::cond, ast_sched_thread::context, sched::data, ast_sched_thread::lock, and ast_sched_thread::stop.

Referenced by ast_sched_thread_create().

91 {
92  struct ast_sched_thread *st = data;
93 
94  while (!st->stop) {
95  int ms;
96  struct timespec ts = {
97  .tv_sec = 0,
98  };
99 
100  ast_mutex_lock(&st->lock);
101 
102  if (st->stop) {
103  ast_mutex_unlock(&st->lock);
104  return NULL;
105  }
106 
107  ms = ast_sched_wait(st->context);
108 
109  if (ms == -1) {
110  ast_cond_wait(&st->cond, &st->lock);
111  } else {
112  struct timeval tv;
113  tv = ast_tvadd(ast_tvnow(), ast_samp2tv(ms, 1000));
114  ts.tv_sec = tv.tv_sec;
115  ts.tv_nsec = tv.tv_usec * 1000;
116  ast_cond_timedwait(&st->cond, &st->lock, &ts);
117  }
118 
119  ast_mutex_unlock(&st->lock);
120 
121  if (st->stop) {
122  return NULL;
123  }
124 
125  ast_sched_runq(st->context);
126  }
127 
128  return NULL;
129 }
ast_mutex_t lock
Definition: sched.c:84
#define ast_cond_wait(cond, mutex)
Definition: lock.h:171
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
ast_cond_t cond
Definition: sched.c:85
#define ast_mutex_lock(a)
Definition: lock.h:155
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 ...
Definition: time.h:191
struct sched_context * context
Definition: sched.c:86
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: utils.c:1587
unsigned int stop
Definition: sched.c:87
int ast_sched_runq(struct sched_context *con)
Runs the queue.
Definition: sched.c:600
int ast_sched_wait(struct sched_context *con) attribute_warn_unused_result
Determines number of seconds until the next outstanding event to take place Determine the number of s...
Definition: sched.c:334
struct timeval tv
#define ast_cond_timedwait(cond, mutex, time)
Definition: lock.h:172
#define ast_mutex_unlock(a)
Definition: lock.h:156
static int sched_settime ( struct timeval *  t,
int  when 
)
static

given the last event *tv and the offset in milliseconds 'when', computes the next value,

Definition at line 380 of file sched.c.

References ast_samp2tv(), ast_tvadd(), ast_tvcmp(), ast_tvnow(), and ast_tvzero().

Referenced by ast_sched_add_variable(), and ast_sched_runq().

381 {
382  struct timeval now = ast_tvnow();
383 
384  /*ast_debug(1, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
385  if (ast_tvzero(*t)) /* not supplied, default to now */
386  *t = now;
387  *t = ast_tvadd(*t, ast_samp2tv(when, 1000));
388  if (ast_tvcmp(*t, now) < 0) {
389  *t = now;
390  }
391  return 0;
392 }
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:100
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
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 ...
Definition: time.h:191
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compres two struct timeval instances returning -1, 0, 1 if the first arg is smaller, equal or greater to the second.
Definition: time.h:120
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: utils.c:1587
static int sched_time_cmp ( void *  a,
void *  b 
)
static

Definition at line 241 of file sched.c.

References ast_tvcmp().

Referenced by sched_context_create().

242 {
243  return ast_tvcmp(((struct sched *) b)->when, ((struct sched *) a)->when);
244 }
Definition: sched.c:57
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compres two struct timeval instances returning -1, 0, 1 if the first arg is smaller, equal or greater to the second.
Definition: time.h:120
static void schedule ( struct sched_context con,
struct sched s 
)
static

Take a sched structure and put it in the queue, such that the soonest event is first in the list.

Definition at line 361 of file sched.c.

References ast_hashtab_insert_safe(), ast_heap_push(), ast_log(), sched_context::highwater, sched::id, LOG_WARNING, sched_context::sched_heap, sched_context::schedcnt, and sched_context::schedq_ht.

Referenced by ast_sched_add_variable(), and ast_sched_runq().

362 {
363  ast_heap_push(con->sched_heap, s);
364 
365  if (!ast_hashtab_insert_safe(con->schedq_ht, s)) {
366  ast_log(LOG_WARNING,"Schedule Queue entry %d is already in table!\n", s->id);
367  }
368 
369  con->schedcnt++;
370 
371  if (con->schedcnt > con->highwater) {
372  con->highwater = con->schedcnt;
373  }
374 }
struct ast_hashtab * schedq_ht
Definition: sched.c:73
#define LOG_WARNING
Definition: logger.h:144
int ast_heap_push(struct ast_heap *h, void *elm)
Push an element on to a heap.
Definition: heap.c:250
struct ast_heap * sched_heap
Definition: sched.c:74
int id
Definition: sched.c:59
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
unsigned int highwater
Definition: sched.c:72
int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj)
Check and insert new object only if it is not there.
Definition: hashtab.c:503
unsigned int schedcnt
Definition: sched.c:71

Variable Documentation

struct ast_threadstorage last_del_id = { .once = PTHREAD_ONCE_INIT , .key_init = __init_last_del_id , .custom_init = NULL , }
static

Definition at line 55 of file sched.c.

Referenced by ast_sched_del().