sched.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, 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. /*! \file
  19. *
  20. * \brief Scheduler Routines (from cheops-NG)
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #ifdef DEBUG_SCHEDULER
  27. #define DEBUG(a) do { \
  28. if (option_debug) \
  29. DEBUG_M(a) \
  30. } while (0)
  31. #else
  32. #define DEBUG(a)
  33. #endif
  34. #include <sys/time.h>
  35. #include "asterisk/sched.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/lock.h"
  38. #include "asterisk/utils.h"
  39. #include "asterisk/linkedlists.h"
  40. struct sched {
  41. AST_LIST_ENTRY(sched) list;
  42. int id; /*!< ID number of event */
  43. struct timeval when; /*!< Absolute time event should take place */
  44. int resched; /*!< When to reschedule */
  45. int variable; /*!< Use return value from callback to reschedule */
  46. const void *data; /*!< Data */
  47. ast_sched_cb callback; /*!< Callback */
  48. };
  49. struct sched_context {
  50. ast_mutex_t lock;
  51. unsigned int eventcnt; /*!< Number of events processed */
  52. unsigned int schedcnt; /*!< Number of outstanding schedule events */
  53. AST_LIST_HEAD_NOLOCK(, sched) schedq; /*!< Schedule entry and main queue */
  54. #ifdef SCHED_MAX_CACHE
  55. AST_LIST_HEAD_NOLOCK(, sched) schedc; /*!< Cache of unused schedule structures and how many */
  56. unsigned int schedccnt;
  57. #endif
  58. };
  59. struct sched_context *sched_context_create(void)
  60. {
  61. struct sched_context *tmp;
  62. if (!(tmp = ast_calloc(1, sizeof(*tmp))))
  63. return NULL;
  64. ast_mutex_init(&tmp->lock);
  65. tmp->eventcnt = 1;
  66. return tmp;
  67. }
  68. void sched_context_destroy(struct sched_context *con)
  69. {
  70. struct sched *s;
  71. ast_mutex_lock(&con->lock);
  72. #ifdef SCHED_MAX_CACHE
  73. /* Eliminate the cache */
  74. while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
  75. ast_free(s);
  76. #endif
  77. /* And the queue */
  78. while ((s = AST_LIST_REMOVE_HEAD(&con->schedq, list)))
  79. ast_free(s);
  80. /* And the context */
  81. ast_mutex_unlock(&con->lock);
  82. ast_mutex_destroy(&con->lock);
  83. ast_free(con);
  84. }
  85. static struct sched *sched_alloc(struct sched_context *con)
  86. {
  87. struct sched *tmp;
  88. /*
  89. * We keep a small cache of schedule entries
  90. * to minimize the number of necessary malloc()'s
  91. */
  92. #ifdef SCHED_MAX_CACHE
  93. if ((tmp = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
  94. con->schedccnt--;
  95. else
  96. #endif
  97. tmp = ast_calloc(1, sizeof(*tmp));
  98. return tmp;
  99. }
  100. static void sched_release(struct sched_context *con, struct sched *tmp)
  101. {
  102. /*
  103. * Add to the cache, or just free() if we
  104. * already have too many cache entries
  105. */
  106. #ifdef SCHED_MAX_CACHE
  107. if (con->schedccnt < SCHED_MAX_CACHE) {
  108. AST_LIST_INSERT_HEAD(&con->schedc, tmp, list);
  109. con->schedccnt++;
  110. } else
  111. #endif
  112. ast_free(tmp);
  113. }
  114. /*! \brief
  115. * Return the number of milliseconds
  116. * until the next scheduled event
  117. */
  118. int ast_sched_wait(struct sched_context *con)
  119. {
  120. int ms;
  121. DEBUG(ast_debug(1, "ast_sched_wait()\n"));
  122. ast_mutex_lock(&con->lock);
  123. if (AST_LIST_EMPTY(&con->schedq)) {
  124. ms = -1;
  125. } else {
  126. ms = ast_tvdiff_ms(AST_LIST_FIRST(&con->schedq)->when, ast_tvnow());
  127. if (ms < 0)
  128. ms = 0;
  129. }
  130. ast_mutex_unlock(&con->lock);
  131. return ms;
  132. }
  133. /*! \brief
  134. * Take a sched structure and put it in the
  135. * queue, such that the soonest event is
  136. * first in the list.
  137. */
  138. static void schedule(struct sched_context *con, struct sched *s)
  139. {
  140. struct sched *cur = NULL;
  141. AST_LIST_TRAVERSE_SAFE_BEGIN(&con->schedq, cur, list) {
  142. if (ast_tvcmp(s->when, cur->when) == -1) {
  143. AST_LIST_INSERT_BEFORE_CURRENT(s, list);
  144. break;
  145. }
  146. }
  147. AST_LIST_TRAVERSE_SAFE_END;
  148. if (!cur)
  149. AST_LIST_INSERT_TAIL(&con->schedq, s, list);
  150. con->schedcnt++;
  151. }
  152. /*! \brief
  153. * given the last event *tv and the offset in milliseconds 'when',
  154. * computes the next value,
  155. */
  156. static int sched_settime(struct timeval *tv, int when)
  157. {
  158. struct timeval now = ast_tvnow();
  159. /*ast_debug(1, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
  160. if (ast_tvzero(*tv)) /* not supplied, default to now */
  161. *tv = now;
  162. *tv = ast_tvadd(*tv, ast_samp2tv(when, 1000));
  163. if (ast_tvcmp(*tv, now) < 0) {
  164. *tv = now;
  165. }
  166. return 0;
  167. }
  168. int ast_sched_replace_variable(int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable)
  169. {
  170. /* 0 means the schedule item is new; do not delete */
  171. if (old_id > 0)
  172. ast_sched_del(con, old_id);
  173. return ast_sched_add_variable(con, when, callback, data, variable);
  174. }
  175. /*! \brief
  176. * Schedule callback(data) to happen when ms into the future
  177. */
  178. int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable)
  179. {
  180. struct sched *tmp;
  181. int res = -1;
  182. DEBUG(ast_debug(1, "ast_sched_add()\n"));
  183. ast_mutex_lock(&con->lock);
  184. if ((tmp = sched_alloc(con))) {
  185. tmp->id = con->eventcnt++;
  186. tmp->callback = callback;
  187. tmp->data = data;
  188. tmp->resched = when;
  189. tmp->variable = variable;
  190. tmp->when = ast_tv(0, 0);
  191. if (sched_settime(&tmp->when, when)) {
  192. sched_release(con, tmp);
  193. } else {
  194. schedule(con, tmp);
  195. res = tmp->id;
  196. }
  197. }
  198. #ifdef DUMP_SCHEDULER
  199. /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
  200. if (option_debug)
  201. ast_sched_dump(con);
  202. #endif
  203. ast_mutex_unlock(&con->lock);
  204. return res;
  205. }
  206. int ast_sched_replace(int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data)
  207. {
  208. if (old_id > -1)
  209. ast_sched_del(con, old_id);
  210. return ast_sched_add(con, when, callback, data);
  211. }
  212. int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, const void *data)
  213. {
  214. return ast_sched_add_variable(con, when, callback, data, 0);
  215. }
  216. /*! \brief
  217. * Delete the schedule entry with number
  218. * "id". It's nearly impossible that there
  219. * would be two or more in the list with that
  220. * id.
  221. */
  222. #ifndef AST_DEVMODE
  223. int ast_sched_del(struct sched_context *con, int id)
  224. #else
  225. int _ast_sched_del(struct sched_context *con, int id, const char *file, int line, const char *function)
  226. #endif
  227. {
  228. struct sched *s;
  229. DEBUG(ast_debug(1, "ast_sched_del()\n"));
  230. ast_mutex_lock(&con->lock);
  231. AST_LIST_TRAVERSE_SAFE_BEGIN(&con->schedq, s, list) {
  232. if (s->id == id) {
  233. AST_LIST_REMOVE_CURRENT(list);
  234. con->schedcnt--;
  235. sched_release(con, s);
  236. break;
  237. }
  238. }
  239. AST_LIST_TRAVERSE_SAFE_END;
  240. #ifdef DUMP_SCHEDULER
  241. /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
  242. if (option_debug)
  243. ast_sched_dump(con);
  244. #endif
  245. ast_mutex_unlock(&con->lock);
  246. if (!s) {
  247. ast_debug(1, "Attempted to delete nonexistent schedule entry %d!\n", id);
  248. #ifndef AST_DEVMODE
  249. ast_assert(s != NULL);
  250. #else
  251. _ast_assert(0, "s != NULL", file, line, function);
  252. #endif
  253. return -1;
  254. }
  255. return 0;
  256. }
  257. /*! \brief Dump the contents of the scheduler to LOG_DEBUG */
  258. void ast_sched_dump(const struct sched_context *con)
  259. {
  260. struct sched *q;
  261. struct timeval tv = ast_tvnow();
  262. #ifdef SCHED_MAX_CACHE
  263. ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n", con->schedcnt, con->eventcnt - 1, con->schedccnt);
  264. #else
  265. ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total)\n", con->schedcnt, con->eventcnt - 1);
  266. #endif
  267. ast_debug(1, "=============================================================\n");
  268. ast_debug(1, "|ID Callback Data Time (sec:ms) |\n");
  269. ast_debug(1, "+-----+-----------------+-----------------+-----------------+\n");
  270. AST_LIST_TRAVERSE(&con->schedq, q, list) {
  271. struct timeval delta = ast_tvsub(q->when, tv);
  272. ast_debug(1, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n",
  273. q->id,
  274. q->callback,
  275. q->data,
  276. (long) delta.tv_sec,
  277. (long int)delta.tv_usec);
  278. }
  279. ast_debug(1, "=============================================================\n");
  280. }
  281. /*! \brief
  282. * Launch all events which need to be run at this time.
  283. */
  284. int ast_sched_runq(struct sched_context *con)
  285. {
  286. struct sched *current;
  287. struct timeval tv;
  288. int numevents;
  289. int res;
  290. DEBUG(ast_debug(1, "ast_sched_runq()\n"));
  291. ast_mutex_lock(&con->lock);
  292. for (numevents = 0; !AST_LIST_EMPTY(&con->schedq); numevents++) {
  293. /* schedule all events which are going to expire within 1ms.
  294. * We only care about millisecond accuracy anyway, so this will
  295. * help us get more than one event at one time if they are very
  296. * close together.
  297. */
  298. tv = ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
  299. if (ast_tvcmp(AST_LIST_FIRST(&con->schedq)->when, tv) != -1)
  300. break;
  301. current = AST_LIST_REMOVE_HEAD(&con->schedq, list);
  302. con->schedcnt--;
  303. /*
  304. * At this point, the schedule queue is still intact. We
  305. * have removed the first event and the rest is still there,
  306. * so it's permissible for the callback to add new events, but
  307. * trying to delete itself won't work because it isn't in
  308. * the schedule queue. If that's what it wants to do, it
  309. * should return 0.
  310. */
  311. ast_mutex_unlock(&con->lock);
  312. res = current->callback(current->data);
  313. ast_mutex_lock(&con->lock);
  314. if (res) {
  315. /*
  316. * If they return non-zero, we should schedule them to be
  317. * run again.
  318. */
  319. if (sched_settime(&current->when, current->variable? res : current->resched)) {
  320. sched_release(con, current);
  321. } else
  322. schedule(con, current);
  323. } else {
  324. /* No longer needed, so release it */
  325. sched_release(con, current);
  326. }
  327. }
  328. ast_mutex_unlock(&con->lock);
  329. return numevents;
  330. }
  331. long ast_sched_when(struct sched_context *con,int id)
  332. {
  333. struct sched *s;
  334. long secs = -1;
  335. DEBUG(ast_debug(1, "ast_sched_when()\n"));
  336. ast_mutex_lock(&con->lock);
  337. AST_LIST_TRAVERSE(&con->schedq, s, list) {
  338. if (s->id == id)
  339. break;
  340. }
  341. if (s) {
  342. struct timeval now = ast_tvnow();
  343. secs = s->when.tv_sec - now.tv_sec;
  344. }
  345. ast_mutex_unlock(&con->lock);
  346. return secs;
  347. }