res_timing_pthread.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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_flags(pthread_timers, timer, OBJ_NOLOCK);
  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_unlink(pthread_timers, timer);
  132. ao2_ref(timer, -1);
  133. }
  134. static int pthread_timer_set_rate(void *data, unsigned int rate)
  135. {
  136. struct pthread_timer *timer = data;
  137. if (rate > MAX_RATE) {
  138. ast_log(LOG_ERROR, "res_timing_pthread only supports timers at a "
  139. "max rate of %d / sec\n", MAX_RATE);
  140. errno = EINVAL;
  141. return -1;
  142. }
  143. ao2_lock(timer);
  144. if ((timer->rate = rate)) {
  145. timer->interval = roundf(1000.0 / ((float) rate));
  146. timer->start = ast_tvnow();
  147. timer->state = TIMER_STATE_TICKING;
  148. } else {
  149. timer->interval = 0;
  150. timer->start = ast_tv(0, 0);
  151. timer->state = TIMER_STATE_IDLE;
  152. }
  153. timer->tick_count = 0;
  154. ao2_unlock(timer);
  155. return 0;
  156. }
  157. static int pthread_timer_ack(void *data, unsigned int quantity)
  158. {
  159. struct pthread_timer *timer = data;
  160. ast_assert(quantity > 0);
  161. ao2_lock(timer);
  162. ack_ticks(timer, quantity);
  163. ao2_unlock(timer);
  164. return 0;
  165. }
  166. static int pthread_timer_enable_continuous(void *data)
  167. {
  168. struct pthread_timer *timer = data;
  169. ao2_lock(timer);
  170. if (!timer->continuous) {
  171. timer->continuous = true;
  172. signal_pipe(timer);
  173. }
  174. ao2_unlock(timer);
  175. return 0;
  176. }
  177. static int pthread_timer_disable_continuous(void *data)
  178. {
  179. struct pthread_timer *timer = data;
  180. ao2_lock(timer);
  181. if (timer->continuous) {
  182. timer->continuous = false;
  183. unsignal_pipe(timer);
  184. }
  185. ao2_unlock(timer);
  186. return 0;
  187. }
  188. static enum ast_timer_event pthread_timer_get_event(void *data)
  189. {
  190. struct pthread_timer *timer = data;
  191. enum ast_timer_event res = AST_TIMING_EVENT_EXPIRED;
  192. ao2_lock(timer);
  193. if (timer->continuous) {
  194. res = AST_TIMING_EVENT_CONTINUOUS;
  195. }
  196. ao2_unlock(timer);
  197. return res;
  198. }
  199. static unsigned int pthread_timer_get_max_rate(void *data)
  200. {
  201. return MAX_RATE;
  202. }
  203. static int pthread_timer_fd(void *data)
  204. {
  205. struct pthread_timer *timer = data;
  206. return timer->pipe[PIPE_READ];
  207. }
  208. static void pthread_timer_destructor(void *obj)
  209. {
  210. struct pthread_timer *timer = obj;
  211. if (timer->pipe[PIPE_READ] > -1) {
  212. close(timer->pipe[PIPE_READ]);
  213. timer->pipe[PIPE_READ] = -1;
  214. }
  215. if (timer->pipe[PIPE_WRITE] > -1) {
  216. close(timer->pipe[PIPE_WRITE]);
  217. timer->pipe[PIPE_WRITE] = -1;
  218. }
  219. }
  220. /*!
  221. * \note only PIPE_READ is guaranteed valid
  222. */
  223. static int pthread_timer_hash(const void *obj, const int flags)
  224. {
  225. const struct pthread_timer *timer = obj;
  226. return timer->pipe[PIPE_READ];
  227. }
  228. /*!
  229. * \note only PIPE_READ is guaranteed valid
  230. */
  231. static int pthread_timer_cmp(void *obj, void *arg, int flags)
  232. {
  233. struct pthread_timer *timer1 = obj, *timer2 = arg;
  234. return (timer1->pipe[PIPE_READ] == timer2->pipe[PIPE_READ]) ? CMP_MATCH | CMP_STOP : 0;
  235. }
  236. /*!
  237. * \retval 0 no timer tick needed
  238. * \retval non-zero write to the timing pipe needed
  239. */
  240. static int check_timer(struct pthread_timer *timer)
  241. {
  242. struct timeval now;
  243. if (timer->state == TIMER_STATE_IDLE) {
  244. return 0;
  245. }
  246. now = ast_tvnow();
  247. if (timer->tick_count < (ast_tvdiff_ms(now, timer->start) / timer->interval)) {
  248. timer->tick_count++;
  249. if (!timer->tick_count) {
  250. /* Handle overflow. */
  251. timer->start = now;
  252. }
  253. return 1;
  254. }
  255. return 0;
  256. }
  257. /*!
  258. * \internal
  259. * \pre timer is locked
  260. */
  261. static void ack_ticks(struct pthread_timer *timer, unsigned int quantity)
  262. {
  263. int pending_ticks = timer->pending_ticks;
  264. ast_assert(quantity);
  265. if (quantity > pending_ticks) {
  266. quantity = pending_ticks;
  267. }
  268. if (!quantity) {
  269. return;
  270. }
  271. timer->pending_ticks -= quantity;
  272. if ((0 == timer->pending_ticks) && !timer->continuous) {
  273. unsignal_pipe(timer);
  274. }
  275. }
  276. /*!
  277. * \internal
  278. * \pre timer is locked
  279. */
  280. static void signal_pipe(struct pthread_timer *timer)
  281. {
  282. ssize_t res;
  283. unsigned char x = 42;
  284. if (timer->pipe_signaled) {
  285. return;
  286. }
  287. res = write(timer->pipe[PIPE_WRITE], &x, 1);
  288. if (-1 == res) {
  289. ast_log(LOG_ERROR, "Error writing to timing pipe: %s\n",
  290. strerror(errno));
  291. } else {
  292. timer->pipe_signaled = true;
  293. }
  294. }
  295. /*!
  296. * \internal
  297. * \pre timer is locked
  298. */
  299. static void unsignal_pipe(struct pthread_timer *timer)
  300. {
  301. ssize_t res;
  302. unsigned long buffer;
  303. if (!timer->pipe_signaled) {
  304. return;
  305. }
  306. res = read(timer->pipe[PIPE_READ], &buffer, sizeof(buffer));
  307. if (-1 == res) {
  308. ast_log(LOG_ERROR, "Error reading from pipe: %s\n",
  309. strerror(errno));
  310. } else {
  311. timer->pipe_signaled = false;
  312. }
  313. }
  314. static int run_timer(void *obj, void *arg, int flags)
  315. {
  316. struct pthread_timer *timer = obj;
  317. if (timer->state == TIMER_STATE_IDLE) {
  318. return 0;
  319. }
  320. ao2_lock(timer);
  321. if (check_timer(timer)) {
  322. timer->pending_ticks++;
  323. signal_pipe(timer);
  324. }
  325. ao2_unlock(timer);
  326. return 0;
  327. }
  328. static void *do_timing(void *arg)
  329. {
  330. struct timeval next_wakeup = ast_tvnow();
  331. while (!timing_thread.stop) {
  332. struct timespec ts = { 0, };
  333. ao2_callback(pthread_timers, OBJ_NODATA, run_timer, NULL);
  334. next_wakeup = ast_tvadd(next_wakeup, ast_tv(0, 5000));
  335. ts.tv_sec = next_wakeup.tv_sec;
  336. ts.tv_nsec = next_wakeup.tv_usec * 1000;
  337. ast_mutex_lock(&timing_thread.lock);
  338. if (!timing_thread.stop) {
  339. if (ao2_container_count(pthread_timers)) {
  340. ast_cond_timedwait(&timing_thread.cond, &timing_thread.lock, &ts);
  341. } else {
  342. ast_cond_wait(&timing_thread.cond, &timing_thread.lock);
  343. }
  344. }
  345. ast_mutex_unlock(&timing_thread.lock);
  346. }
  347. return NULL;
  348. }
  349. static int init_timing_thread(void)
  350. {
  351. ast_mutex_init(&timing_thread.lock);
  352. ast_cond_init(&timing_thread.cond, NULL);
  353. if (ast_pthread_create_background(&timing_thread.thread, NULL, do_timing, NULL)) {
  354. ast_log(LOG_ERROR, "Unable to start timing thread.\n");
  355. return -1;
  356. }
  357. return 0;
  358. }
  359. static int load_module(void)
  360. {
  361. if (!(pthread_timers = ao2_container_alloc(PTHREAD_TIMER_BUCKETS,
  362. pthread_timer_hash, pthread_timer_cmp))) {
  363. return AST_MODULE_LOAD_DECLINE;
  364. }
  365. if (init_timing_thread()) {
  366. ao2_ref(pthread_timers, -1);
  367. pthread_timers = NULL;
  368. return AST_MODULE_LOAD_DECLINE;
  369. }
  370. return (timing_funcs_handle = ast_register_timing_interface(&pthread_timing)) ?
  371. AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
  372. }
  373. static int unload_module(void)
  374. {
  375. int res;
  376. ast_mutex_lock(&timing_thread.lock);
  377. timing_thread.stop = 1;
  378. ast_cond_signal(&timing_thread.cond);
  379. ast_mutex_unlock(&timing_thread.lock);
  380. pthread_join(timing_thread.thread, NULL);
  381. if (!(res = ast_unregister_timing_interface(timing_funcs_handle))) {
  382. ao2_ref(pthread_timers, -1);
  383. pthread_timers = NULL;
  384. }
  385. return res;
  386. }
  387. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "pthread Timing Interface",
  388. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  389. .load = load_module,
  390. .unload = unload_module,
  391. .load_pri = AST_MODPRI_TIMING,
  392. );