Wed Jan 8 2020 09:49:51

Asterisk developer's documentation


sched.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief Scheduler Routines (from cheops-NG)
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413586 $")
33 
34 #ifdef DEBUG_SCHEDULER
35 #define DEBUG(a) do { \
36  if (option_debug) \
37  DEBUG_M(a) \
38  } while (0)
39 #else
40 #define DEBUG(a)
41 #endif
42 
43 #include <sys/time.h>
44 
45 #include "asterisk/sched.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/lock.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/linkedlists.h"
50 #include "asterisk/dlinkedlists.h"
51 #include "asterisk/hashtab.h"
52 #include "asterisk/heap.h"
53 #include "asterisk/threadstorage.h"
54 
56 
57 struct sched {
59  int id; /*!< ID number of event */
60  struct timeval when; /*!< Absolute time event should take place */
61  int resched; /*!< When to reschedule */
62  int variable; /*!< Use return value from callback to reschedule */
63  const void *data; /*!< Data */
64  ast_sched_cb callback; /*!< Callback */
65  ssize_t __heap_index;
66 };
67 
68 struct sched_context {
70  unsigned int eventcnt; /*!< Number of events processed */
71  unsigned int schedcnt; /*!< Number of outstanding schedule events */
72  unsigned int highwater; /*!< highest count so far */
73  struct ast_hashtab *schedq_ht; /*!< hash table for fast searching */
75 
76 #ifdef SCHED_MAX_CACHE
77  AST_LIST_HEAD_NOLOCK(, sched) schedc; /*!< Cache of unused schedule structures and how many */
78  unsigned int schedccnt;
79 #endif
80 };
81 
83  pthread_t thread;
86  struct sched_context *context;
87  unsigned int stop:1;
88 };
89 
90 static void *sched_run(void *data)
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 }
130 
132 {
133  ast_mutex_lock(&st->lock);
134  ast_cond_signal(&st->cond);
135  ast_mutex_unlock(&st->lock);
136 }
137 
138 struct sched_context *ast_sched_thread_get_context(struct ast_sched_thread *st)
139 {
140  return st->context;
141 }
142 
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 }
166 
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 }
194 
196  const void *data, int variable)
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 }
209 
211  const void *data)
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 }
224 
225 /* hash routines for sched */
226 
227 static int sched_cmp(const void *a, const void *b)
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 }
233 
234 static unsigned int sched_hash(const void *obj)
235 {
236  const struct sched *s = obj;
237  unsigned int h = s->id;
238  return h;
239 }
240 
241 static int sched_time_cmp(void *a, void *b)
242 {
243  return ast_tvcmp(((struct sched *) b)->when, ((struct sched *) a)->when);
244 }
245 
246 struct sched_context *sched_context_create(void)
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 }
266 
267 void sched_context_destroy(struct sched_context *con)
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 }
295 
296 static struct sched *sched_alloc(struct sched_context *con)
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 }
313 
314 static void sched_release(struct sched_context *con, struct sched *tmp)
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 }
329 
330 /*! \brief
331  * Return the number of milliseconds
332  * until the next scheduled event
333  */
334 int ast_sched_wait(struct sched_context *con)
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 }
354 
355 
356 /*! \brief
357  * Take a sched structure and put it in the
358  * queue, such that the soonest event is
359  * first in the list.
360  */
361 static void schedule(struct sched_context *con, struct sched *s)
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 }
375 
376 /*! \brief
377  * given the last event *tv and the offset in milliseconds 'when',
378  * computes the next value,
379  */
380 static int sched_settime(struct timeval *t, int when)
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 }
393 
394 int ast_sched_replace_variable(int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable)
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  }
400  return ast_sched_add_variable(con, when, callback, data, variable);
401 }
402 
403 /*! \brief
404  * Schedule callback(data) to happen when ms into the future
405  */
406 int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable)
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 }
437 
438 int ast_sched_replace(int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data)
439 {
440  if (old_id > -1) {
441  AST_SCHED_DEL(con, old_id);
442  }
443  return ast_sched_add(con, when, callback, data);
444 }
445 
446 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, const void *data)
447 {
448  return ast_sched_add_variable(con, when, callback, data, 0);
449 }
450 
451 const void *ast_sched_find_data(struct sched_context *con, int id)
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 }
460 
461 /*! \brief
462  * Delete the schedule entry with number
463  * "id". It's nearly impossible that there
464  * would be two or more in the list with that
465  * id.
466  */
467 #ifndef AST_DEVMODE
468 int ast_sched_del(struct sched_context *con, int id)
469 #else
470 int _ast_sched_del(struct sched_context *con, int id, const char *file, int line, const char *function)
471 #endif
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 }
526 
527 void ast_sched_report(struct sched_context *con, struct ast_str **buf, struct ast_cb_names *cbnames)
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 }
563 
564 /*! \brief Dump the contents of the scheduler to LOG_DEBUG */
565 void ast_sched_dump(struct sched_context *con)
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 }
596 
597 /*! \brief
598  * Launch all events which need to be run at this time.
599  */
600 int ast_sched_runq(struct sched_context *con)
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 }
663 
664 long ast_sched_when(struct sched_context *con,int id)
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 }
char * list[10]
Definition: sched.h:165
static unsigned int sched_hash(const void *obj)
Definition: sched.c:234
static struct ast_threadstorage last_del_id
Definition: sched.c:55
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:81
ast_mutex_t lock
Definition: sched.c:84
int ast_sched_del(struct sched_context *con, int id) attribute_warn_unused_result
Deletes a scheduled event Remove this event from being run. A procedure should not remove its own eve...
Definition: sched.c:468
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
void * ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj)
Lookup this object in the hash table.
Definition: hashtab.c:534
struct ast_sched_thread * ast_sched_thread_destroy(struct ast_sched_thread *st)
Destroy a scheduler and its thread.
Definition: sched.c:143
int ast_hashtab_newsize_java(struct ast_hashtab *tab)
Create a prime number roughly 2x the current table size.
Definition: hashtab.c:131
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
struct ast_sched_thread * ast_sched_thread_create(void)
Create a scheduler with a dedicated thread.
Definition: sched.c:167
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.
Definition: sched.c:195
static void sched_release(struct sched_context *con, struct sched *tmp)
Definition: sched.c:314
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
struct ast_hashtab * schedq_ht
Definition: sched.c:73
int option_debug
Definition: asterisk.c:182
ast_sched_cb cblist[10]
Definition: sched.h:166
static struct sched * sched_alloc(struct sched_context *con)
Definition: sched.c:296
static void * sched_run(void *data)
Definition: sched.c:90
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
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
struct sched_context::@297 schedc
Generic (perhaps overly so) hashtable implementation Hash Table support in Asterisk.
int ast_heap_push(struct ast_heap *h, void *elm)
Push an element on to a heap.
Definition: heap.c:250
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:100
Definition: heap.c:38
Definition: sched.c:57
ssize_t __heap_index
Definition: sched.c:65
struct ast_heap * ast_heap_destroy(struct ast_heap *h)
Destroy a max heap.
Definition: heap.c:163
unsigned int stop
Definition: app_meetme.c:969
#define ast_cond_wait(cond, mutex)
Definition: lock.h:171
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_cond_init(cond, attr)
Definition: lock.h:167
#define SCHED_MAX_CACHE
Max num of schedule structs.
Definition: sched.h:37
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_assert(a)
Definition: utils.h:738
#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
Definitions to aid in the use of thread local storage.
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_cond_signal(cond)
Definition: lock.h:169
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
Utility functions.
pthread_cond_t ast_cond_t
Definition: lock.h:144
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:426
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
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
Max Heap data structure.
A set of macros to manage doubly-linked lists.
General Asterisk PBX channel definitions.
#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 ast_heap * sched_heap
Definition: sched.c:74
static int sched_cmp(const void *a, const void *b)
Definition: sched.c:227
unsigned int schedccnt
Definition: sched.c:78
#define AST_PTHREADT_NULL
Definition: lock.h:65
Scheduler Routines (derived from cheops)
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
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.
Definition: sched.c:527
int ast_sched_replace(int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
replace a scheduler entry
Definition: sched.c:438
A set of macros to manage forward-linked lists.
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:818
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
struct sched_context * context
Definition: sched.c:86
const void * ast_sched_find_data(struct sched_context *con, int id)
Find a sched structure and return the data field associated with it.
Definition: sched.c:451
struct timeval when
Definition: sched.c:60
ast_mutex_t lock
Definition: sched.c:69
#define AST_LIST_HEAD_NOLOCK(name, type)
Defines a structure to be used to hold a list of specified type (with no lock).
Definition: linkedlists.h:224
#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
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
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
long ast_sched_when(struct sched_context *con, int id)
Returns the number of seconds before an event takes place.
Definition: sched.c:664
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
#define ast_cond_destroy(cond)
Definition: lock.h:168
unsigned int stop
Definition: sched.c:87
unsigned int highwater
Definition: sched.c:72
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
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:409
pthread_t thread
Definition: sched.c:83
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:696
int ast_sched_replace_variable(int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
replace a scheduler entry
Definition: sched.c:394
struct sched_context * ast_sched_thread_get_context(struct ast_sched_thread *st)
Get the scheduler context for a given ast_sched_thread.
Definition: sched.c:138
struct sched::@296 list
#define ast_free(a)
Definition: astmm.h:97
void ast_sched_thread_poke(struct ast_sched_thread *st)
Force re-processing of the scheduler context.
Definition: sched.c:131
const void * data
Definition: sched.c:63
static int sched_time_cmp(void *a, void *b)
Definition: sched.c:241
int ast_sched_runq(struct sched_context *con)
Runs the queue.
Definition: sched.c:600
void * ast_heap_remove(struct ast_heap *h, void *elm)
Remove a specific element from a heap.
Definition: heap.c:284
struct sched_context * sched_context_create(void)
New schedule context.
Definition: sched.c:246
#define ast_calloc(a, b)
Definition: astmm.h:82
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
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:179
size_t ast_heap_size(struct ast_heap *h)
Get the current size of a heap.
Definition: heap.c:309
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
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
void * ast_heap_peek(struct ast_heap *h, unsigned int index)
Peek at an element on a heap.
Definition: heap.c:300
int(* ast_sched_cb)(const void *data)
callback for a cheops scheduler A cheops scheduler callback takes a pointer with callback data and ...
Definition: sched.h:160
#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
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: utils.c:1601
int ast_sched_thread_add(struct ast_sched_thread *st, int when, ast_sched_cb cb, const void *data)
Add a scheduler entry.
Definition: sched.c:210
#define ast_mutex_init(pmutex)
Definition: lock.h:152
#define ast_mutex_destroy(a)
Definition: lock.h:154
ast_sched_cb callback
Definition: sched.c:64
#define ast_cond_timedwait(cond, mutex, time)
Definition: lock.h:172
unsigned int eventcnt
Definition: sched.c:70
Structure for mutex and tracking information.
Definition: lock.h:121
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
unsigned int schedcnt
Definition: sched.c:71
#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
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