res_timing_pthread.c 10 KB

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