res_timing_pthread.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@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. * \author Russell Bryant <russell@digium.com>
  21. *
  22. * \brief pthread timing interface
  23. */
  24. /*** MODULEINFO
  25. <support_level>extended</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
  29. #include <stdbool.h>
  30. #include <math.h>
  31. #include <unistd.h>
  32. #include <fcntl.h>
  33. #include "asterisk/module.h"
  34. #include "asterisk/timing.h"
  35. #include "asterisk/utils.h"
  36. #include "asterisk/astobj2.h"
  37. #include "asterisk/time.h"
  38. #include "asterisk/lock.h"
  39. static void *timing_funcs_handle;
  40. static void *pthread_timer_open(void);
  41. static void pthread_timer_close(void *data);
  42. static int pthread_timer_set_rate(void *data, unsigned int rate);
  43. static int pthread_timer_ack(void *data, unsigned int quantity);
  44. static int pthread_timer_enable_continuous(void *data);
  45. static int pthread_timer_disable_continuous(void *data);
  46. static enum ast_timer_event pthread_timer_get_event(void *data);
  47. static unsigned int pthread_timer_get_max_rate(void *data);
  48. static int pthread_timer_fd(void *data);
  49. static struct ast_timing_interface pthread_timing = {
  50. .name = "pthread",
  51. .priority = 0, /* use this as a last resort */
  52. .timer_open = pthread_timer_open,
  53. .timer_close = pthread_timer_close,
  54. .timer_set_rate = pthread_timer_set_rate,
  55. .timer_ack = pthread_timer_ack,
  56. .timer_enable_continuous = pthread_timer_enable_continuous,
  57. .timer_disable_continuous = pthread_timer_disable_continuous,
  58. .timer_get_event = pthread_timer_get_event,
  59. .timer_get_max_rate = pthread_timer_get_max_rate,
  60. .timer_fd = pthread_timer_fd,
  61. };
  62. /* 1 tick / 10 ms */
  63. #define MAX_RATE 100
  64. static struct ao2_container *pthread_timers;
  65. #define PTHREAD_TIMER_BUCKETS 563
  66. enum {
  67. PIPE_READ = 0,
  68. PIPE_WRITE = 1
  69. };
  70. enum pthread_timer_state {
  71. TIMER_STATE_IDLE,
  72. TIMER_STATE_TICKING,
  73. };
  74. struct pthread_timer {
  75. int pipe[2];
  76. enum pthread_timer_state state;
  77. unsigned int rate;
  78. /*! Interval in ms for current rate */
  79. unsigned int interval;
  80. unsigned int tick_count;
  81. unsigned int pending_ticks;
  82. struct timeval start;
  83. bool continuous:1;
  84. bool pipe_signaled:1;
  85. };
  86. static void pthread_timer_destructor(void *obj);
  87. static void signal_pipe(struct pthread_timer *timer);
  88. static void unsignal_pipe(struct pthread_timer *timer);
  89. static void ack_ticks(struct pthread_timer *timer, unsigned int num);
  90. /*!
  91. * \brief Data for the timing thread
  92. */
  93. static struct {
  94. pthread_t thread;
  95. ast_mutex_t lock;
  96. ast_cond_t cond;
  97. unsigned int stop:1;
  98. } timing_thread;
  99. static void *pthread_timer_open(void)
  100. {
  101. struct pthread_timer *timer;
  102. int i;
  103. if (!(timer = ao2_alloc(sizeof(*timer), pthread_timer_destructor))) {
  104. errno = ENOMEM;
  105. return NULL;
  106. }
  107. timer->pipe[PIPE_READ] = timer->pipe[PIPE_WRITE] = -1;
  108. timer->state = TIMER_STATE_IDLE;
  109. if (pipe(timer->pipe)) {
  110. ao2_ref(timer, -1);
  111. return NULL;
  112. }
  113. for (i = 0; i < ARRAY_LEN(timer->pipe); ++i) {
  114. int flags = fcntl(timer->pipe[i], F_GETFL);
  115. flags |= O_NONBLOCK;
  116. fcntl(timer->pipe[i], F_SETFL, flags);
  117. }
  118. ao2_lock(pthread_timers);
  119. if (!ao2_container_count(pthread_timers)) {
  120. ast_mutex_lock(&timing_thread.lock);
  121. ast_cond_signal(&timing_thread.cond);
  122. ast_mutex_unlock(&timing_thread.lock);
  123. }
  124. ao2_link(pthread_timers, timer);
  125. ao2_unlock(pthread_timers);
  126. return timer;
  127. }
  128. static void pthread_timer_close(void *data)
  129. {
  130. struct pthread_timer *timer = data;
  131. ao2_ref(timer, -1);
  132. }
  133. static int pthread_timer_set_rate(void *data, unsigned int rate)
  134. {
  135. struct pthread_timer *timer = data;
  136. if (rate > MAX_RATE) {
  137. ast_log(LOG_ERROR, "res_timing_pthread only supports timers at a "
  138. "max rate of %d / sec\n", MAX_RATE);
  139. errno = EINVAL;
  140. return -1;
  141. }
  142. ao2_lock(timer);
  143. if ((timer->rate = rate)) {
  144. timer->interval = roundf(1000.0 / ((float) rate));
  145. timer->start = ast_tvnow();
  146. timer->state = TIMER_STATE_TICKING;
  147. } else {
  148. timer->interval = 0;
  149. timer->start = ast_tv(0, 0);
  150. timer->state = TIMER_STATE_IDLE;
  151. }
  152. timer->tick_count = 0;
  153. ao2_unlock(timer);
  154. return 0;
  155. }
  156. static int pthread_timer_ack(void *data, unsigned int quantity)
  157. {
  158. struct pthread_timer *timer = data;
  159. ast_assert(quantity > 0);
  160. ao2_lock(timer);
  161. ack_ticks(timer, quantity);
  162. ao2_unlock(timer);
  163. return 0;
  164. }
  165. static int pthread_timer_enable_continuous(void *data)
  166. {
  167. struct pthread_timer *timer = data;
  168. ao2_lock(timer);
  169. if (!timer->continuous) {
  170. timer->continuous = true;
  171. signal_pipe(timer);
  172. }
  173. ao2_unlock(timer);
  174. return 0;
  175. }
  176. static int pthread_timer_disable_continuous(void *data)
  177. {
  178. struct pthread_timer *timer = data;
  179. ao2_lock(timer);
  180. if (timer->continuous) {
  181. timer->continuous = false;
  182. unsignal_pipe(timer);
  183. }
  184. ao2_unlock(timer);
  185. return 0;
  186. }
  187. static enum ast_timer_event pthread_timer_get_event(void *data)
  188. {
  189. struct pthread_timer *timer = data;
  190. enum ast_timer_event res = AST_TIMING_EVENT_EXPIRED;
  191. ao2_lock(timer);
  192. if (timer->continuous) {
  193. res = AST_TIMING_EVENT_CONTINUOUS;
  194. }
  195. ao2_unlock(timer);
  196. return res;
  197. }
  198. static unsigned int pthread_timer_get_max_rate(void *data)
  199. {
  200. return MAX_RATE;
  201. }
  202. static int pthread_timer_fd(void *data)
  203. {
  204. struct pthread_timer *timer = data;
  205. return timer->pipe[PIPE_READ];
  206. }
  207. static void pthread_timer_destructor(void *obj)
  208. {
  209. struct pthread_timer *timer = obj;
  210. if (timer->pipe[PIPE_READ] > -1) {
  211. close(timer->pipe[PIPE_READ]);
  212. timer->pipe[PIPE_READ] = -1;
  213. }
  214. if (timer->pipe[PIPE_WRITE] > -1) {
  215. close(timer->pipe[PIPE_WRITE]);
  216. timer->pipe[PIPE_WRITE] = -1;
  217. }
  218. }
  219. /*!
  220. * \note only PIPE_READ is guaranteed valid
  221. */
  222. static int pthread_timer_hash(const void *obj, const int flags)
  223. {
  224. const struct pthread_timer *timer = obj;
  225. return timer->pipe[PIPE_READ];
  226. }
  227. /*!
  228. * \note only PIPE_READ is guaranteed valid
  229. */
  230. static int pthread_timer_cmp(void *obj, void *arg, int flags)
  231. {
  232. struct pthread_timer *timer1 = obj, *timer2 = arg;
  233. return (timer1->pipe[PIPE_READ] == timer2->pipe[PIPE_READ]) ? CMP_MATCH | CMP_STOP : 0;
  234. }
  235. /*!
  236. * \retval 0 no timer tick needed
  237. * \retval non-zero write to the timing pipe needed
  238. */
  239. static int check_timer(struct pthread_timer *timer)
  240. {
  241. struct timeval now;
  242. if (timer->state == TIMER_STATE_IDLE) {
  243. return 0;
  244. }
  245. now = ast_tvnow();
  246. if (timer->tick_count < (ast_tvdiff_ms(now, timer->start) / timer->interval)) {
  247. timer->tick_count++;
  248. if (!timer->tick_count) {
  249. /* Handle overflow. */
  250. timer->start = now;
  251. }
  252. return 1;
  253. }
  254. return 0;
  255. }
  256. /*!
  257. * \internal
  258. * \pre timer is locked
  259. */
  260. static void ack_ticks(struct pthread_timer *timer, unsigned int quantity)
  261. {
  262. int pending_ticks = timer->pending_ticks;
  263. ast_assert(quantity);
  264. if (quantity > pending_ticks) {
  265. quantity = pending_ticks;
  266. }
  267. if (!quantity) {
  268. return;
  269. }
  270. timer->pending_ticks -= quantity;
  271. if ((0 == timer->pending_ticks) && !timer->continuous) {
  272. unsignal_pipe(timer);
  273. }
  274. }
  275. /*!
  276. * \internal
  277. * \pre timer is locked
  278. */
  279. static void signal_pipe(struct pthread_timer *timer)
  280. {
  281. ssize_t res;
  282. unsigned char x = 42;
  283. if (timer->pipe_signaled) {
  284. return;
  285. }
  286. res = write(timer->pipe[PIPE_WRITE], &x, 1);
  287. if (-1 == res) {
  288. ast_log(LOG_ERROR, "Error writing to timing pipe: %s\n",
  289. strerror(errno));
  290. } else {
  291. timer->pipe_signaled = true;
  292. }
  293. }
  294. /*!
  295. * \internal
  296. * \pre timer is locked
  297. */
  298. static void unsignal_pipe(struct pthread_timer *timer)
  299. {
  300. ssize_t res;
  301. unsigned long buffer;
  302. if (!timer->pipe_signaled) {
  303. return;
  304. }
  305. res = read(timer->pipe[PIPE_READ], &buffer, sizeof(buffer));
  306. if (-1 == res) {
  307. ast_log(LOG_ERROR, "Error reading from pipe: %s\n",
  308. strerror(errno));
  309. } else {
  310. timer->pipe_signaled = false;
  311. }
  312. }
  313. static int run_timer(void *obj, void *arg, int flags)
  314. {
  315. struct pthread_timer *timer = obj;
  316. if (timer->state == TIMER_STATE_IDLE) {
  317. return 0;
  318. }
  319. ao2_lock(timer);
  320. if (check_timer(timer)) {
  321. timer->pending_ticks++;
  322. signal_pipe(timer);
  323. }
  324. ao2_unlock(timer);
  325. return 0;
  326. }
  327. static void *do_timing(void *arg)
  328. {
  329. struct timeval next_wakeup = ast_tvnow();
  330. while (!timing_thread.stop) {
  331. struct timespec ts = { 0, };
  332. ao2_callback(pthread_timers, OBJ_NODATA, run_timer, NULL);
  333. next_wakeup = ast_tvadd(next_wakeup, ast_tv(0, 5000));
  334. ts.tv_sec = next_wakeup.tv_sec;
  335. ts.tv_nsec = next_wakeup.tv_usec * 1000;
  336. ast_mutex_lock(&timing_thread.lock);
  337. if (!timing_thread.stop) {
  338. if (ao2_container_count(pthread_timers)) {
  339. ast_cond_timedwait(&timing_thread.cond, &timing_thread.lock, &ts);
  340. } else {
  341. ast_cond_wait(&timing_thread.cond, &timing_thread.lock);
  342. }
  343. }
  344. ast_mutex_unlock(&timing_thread.lock);
  345. }
  346. return NULL;
  347. }
  348. static int init_timing_thread(void)
  349. {
  350. ast_mutex_init(&timing_thread.lock);
  351. ast_cond_init(&timing_thread.cond, NULL);
  352. if (ast_pthread_create_background(&timing_thread.thread, NULL, do_timing, NULL)) {
  353. ast_log(LOG_ERROR, "Unable to start timing thread.\n");
  354. return -1;
  355. }
  356. return 0;
  357. }
  358. static int load_module(void)
  359. {
  360. if (!(pthread_timers = ao2_container_alloc(PTHREAD_TIMER_BUCKETS,
  361. pthread_timer_hash, pthread_timer_cmp))) {
  362. return AST_MODULE_LOAD_DECLINE;
  363. }
  364. if (init_timing_thread()) {
  365. ao2_ref(pthread_timers, -1);
  366. pthread_timers = NULL;
  367. return AST_MODULE_LOAD_DECLINE;
  368. }
  369. return (timing_funcs_handle = ast_register_timing_interface(&pthread_timing)) ?
  370. AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
  371. }
  372. static int unload_module(void)
  373. {
  374. int res;
  375. ast_mutex_lock(&timing_thread.lock);
  376. timing_thread.stop = 1;
  377. ast_cond_signal(&timing_thread.cond);
  378. ast_mutex_unlock(&timing_thread.lock);
  379. pthread_join(timing_thread.thread, NULL);
  380. if (!(res = ast_unregister_timing_interface(timing_funcs_handle))) {
  381. ao2_ref(pthread_timers, -1);
  382. pthread_timers = NULL;
  383. }
  384. return res;
  385. }
  386. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "pthread Timing Interface",
  387. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  388. .load = load_module,
  389. .unload = unload_module,
  390. .load_pri = AST_MODPRI_TIMING,
  391. );