res_timing_pthread.c 10.0 KB

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