res_timing_pthread.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 <math.h>
  30. #include <sys/select.h>
  31. #include "asterisk/module.h"
  32. #include "asterisk/timing.h"
  33. #include "asterisk/utils.h"
  34. #include "asterisk/astobj2.h"
  35. #include "asterisk/time.h"
  36. #include "asterisk/lock.h"
  37. #include "asterisk/poll-compat.h"
  38. static void *timing_funcs_handle;
  39. static int pthread_timer_open(void);
  40. static void pthread_timer_close(int handle);
  41. static int pthread_timer_set_rate(int handle, unsigned int rate);
  42. static int pthread_timer_ack(int handle, unsigned int quantity);
  43. static int pthread_timer_enable_continuous(int handle);
  44. static int pthread_timer_disable_continuous(int handle);
  45. static enum ast_timer_event pthread_timer_get_event(int handle);
  46. static unsigned int pthread_timer_get_max_rate(int handle);
  47. static struct ast_timing_interface pthread_timing = {
  48. .name = "pthread",
  49. .priority = 0, /* use this as a last resort */
  50. .timer_open = pthread_timer_open,
  51. .timer_close = pthread_timer_close,
  52. .timer_set_rate = pthread_timer_set_rate,
  53. .timer_ack = pthread_timer_ack,
  54. .timer_enable_continuous = pthread_timer_enable_continuous,
  55. .timer_disable_continuous = pthread_timer_disable_continuous,
  56. .timer_get_event = pthread_timer_get_event,
  57. .timer_get_max_rate = pthread_timer_get_max_rate,
  58. };
  59. /* 1 tick / 10 ms */
  60. #define MAX_RATE 100
  61. static struct ao2_container *pthread_timers;
  62. #define PTHREAD_TIMER_BUCKETS 563
  63. enum {
  64. PIPE_READ = 0,
  65. PIPE_WRITE = 1
  66. };
  67. enum pthread_timer_state {
  68. TIMER_STATE_IDLE,
  69. TIMER_STATE_TICKING,
  70. };
  71. struct pthread_timer {
  72. int pipe[2];
  73. enum pthread_timer_state state;
  74. unsigned int rate;
  75. /*! Interval in ms for current rate */
  76. unsigned int interval;
  77. unsigned int tick_count;
  78. unsigned int pending_ticks;
  79. struct timeval start;
  80. unsigned int continuous:1;
  81. };
  82. static void pthread_timer_destructor(void *obj);
  83. static struct pthread_timer *find_timer(int handle, int unlinkobj);
  84. static void write_byte(struct pthread_timer *timer);
  85. static int read_pipe(struct pthread_timer *timer, unsigned int num);
  86. /*!
  87. * \brief Data for the timing thread
  88. */
  89. static struct {
  90. pthread_t thread;
  91. ast_mutex_t lock;
  92. ast_cond_t cond;
  93. unsigned int stop:1;
  94. } timing_thread;
  95. static int pthread_timer_open(void)
  96. {
  97. struct pthread_timer *timer;
  98. int fd;
  99. if (!(timer = ao2_alloc(sizeof(*timer), pthread_timer_destructor))) {
  100. errno = ENOMEM;
  101. return -1;
  102. }
  103. timer->pipe[PIPE_READ] = timer->pipe[PIPE_WRITE] = -1;
  104. timer->state = TIMER_STATE_IDLE;
  105. if (pipe(timer->pipe)) {
  106. ao2_ref(timer, -1);
  107. return -1;
  108. }
  109. ao2_lock(pthread_timers);
  110. if (!ao2_container_count(pthread_timers)) {
  111. ast_mutex_lock(&timing_thread.lock);
  112. ast_cond_signal(&timing_thread.cond);
  113. ast_mutex_unlock(&timing_thread.lock);
  114. }
  115. ao2_link(pthread_timers, timer);
  116. ao2_unlock(pthread_timers);
  117. fd = timer->pipe[PIPE_READ];
  118. ao2_ref(timer, -1);
  119. return fd;
  120. }
  121. static void pthread_timer_close(int handle)
  122. {
  123. struct pthread_timer *timer;
  124. if (!(timer = find_timer(handle, 1))) {
  125. return;
  126. }
  127. ao2_ref(timer, -1);
  128. }
  129. static int pthread_timer_set_rate(int handle, unsigned int rate)
  130. {
  131. struct pthread_timer *timer;
  132. if (!(timer = find_timer(handle, 0))) {
  133. errno = EINVAL;
  134. return -1;
  135. }
  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. ao2_ref(timer, -1);
  155. return 0;
  156. }
  157. static int pthread_timer_ack(int handle, unsigned int quantity)
  158. {
  159. struct pthread_timer *timer;
  160. int res;
  161. ast_assert(quantity > 0);
  162. if (!(timer = find_timer(handle, 0))) {
  163. return -1;
  164. }
  165. ao2_lock(timer);
  166. res = read_pipe(timer, quantity);
  167. ao2_unlock(timer);
  168. ao2_ref(timer, -1);
  169. return res;
  170. }
  171. static int pthread_timer_enable_continuous(int handle)
  172. {
  173. struct pthread_timer *timer;
  174. if (!(timer = find_timer(handle, 0))) {
  175. errno = EINVAL;
  176. return -1;
  177. }
  178. ao2_lock(timer);
  179. if (!timer->continuous) {
  180. timer->continuous = 1;
  181. write_byte(timer);
  182. }
  183. ao2_unlock(timer);
  184. ao2_ref(timer, -1);
  185. return 0;
  186. }
  187. static int pthread_timer_disable_continuous(int handle)
  188. {
  189. struct pthread_timer *timer;
  190. if (!(timer = find_timer(handle, 0))) {
  191. errno = EINVAL;
  192. return -1;
  193. }
  194. ao2_lock(timer);
  195. if (timer->continuous) {
  196. timer->continuous = 0;
  197. if (read_pipe(timer, 1) != 0) {
  198. /* Let the errno from read_pipe propagate up */
  199. ao2_unlock(timer);
  200. ao2_ref(timer, -1);
  201. return -1;
  202. }
  203. }
  204. ao2_unlock(timer);
  205. ao2_ref(timer, -1);
  206. return 0;
  207. }
  208. static enum ast_timer_event pthread_timer_get_event(int handle)
  209. {
  210. struct pthread_timer *timer;
  211. enum ast_timer_event res = AST_TIMING_EVENT_EXPIRED;
  212. if (!(timer = find_timer(handle, 0))) {
  213. return res;
  214. }
  215. ao2_lock(timer);
  216. if (timer->continuous && timer->pending_ticks == 1) {
  217. res = AST_TIMING_EVENT_CONTINUOUS;
  218. }
  219. ao2_unlock(timer);
  220. ao2_ref(timer, -1);
  221. return res;
  222. }
  223. static unsigned int pthread_timer_get_max_rate(int handle)
  224. {
  225. return MAX_RATE;
  226. }
  227. static struct pthread_timer *find_timer(int handle, int unlinkobj)
  228. {
  229. struct pthread_timer *timer;
  230. struct pthread_timer tmp_timer;
  231. int flags = OBJ_POINTER;
  232. tmp_timer.pipe[PIPE_READ] = handle;
  233. if (unlinkobj) {
  234. flags |= OBJ_UNLINK;
  235. }
  236. if (!(timer = ao2_find(pthread_timers, &tmp_timer, flags))) {
  237. ast_assert(timer != NULL);
  238. return NULL;
  239. }
  240. return timer;
  241. }
  242. static void pthread_timer_destructor(void *obj)
  243. {
  244. struct pthread_timer *timer = obj;
  245. if (timer->pipe[PIPE_READ] > -1) {
  246. close(timer->pipe[PIPE_READ]);
  247. timer->pipe[PIPE_READ] = -1;
  248. }
  249. if (timer->pipe[PIPE_WRITE] > -1) {
  250. close(timer->pipe[PIPE_WRITE]);
  251. timer->pipe[PIPE_WRITE] = -1;
  252. }
  253. }
  254. /*!
  255. * \note only PIPE_READ is guaranteed valid
  256. */
  257. static int pthread_timer_hash(const void *obj, const int flags)
  258. {
  259. const struct pthread_timer *timer = obj;
  260. return timer->pipe[PIPE_READ];
  261. }
  262. /*!
  263. * \note only PIPE_READ is guaranteed valid
  264. */
  265. static int pthread_timer_cmp(void *obj, void *arg, int flags)
  266. {
  267. struct pthread_timer *timer1 = obj, *timer2 = arg;
  268. return (timer1->pipe[PIPE_READ] == timer2->pipe[PIPE_READ]) ? CMP_MATCH | CMP_STOP : 0;
  269. }
  270. /*!
  271. * \retval 0 no timer tick needed
  272. * \retval non-zero write to the timing pipe needed
  273. */
  274. static int check_timer(struct pthread_timer *timer)
  275. {
  276. struct timeval now;
  277. if (timer->state == TIMER_STATE_IDLE) {
  278. return 0;
  279. }
  280. now = ast_tvnow();
  281. if (timer->tick_count < (ast_tvdiff_ms(now, timer->start) / timer->interval)) {
  282. timer->tick_count++;
  283. if (!timer->tick_count) {
  284. /* Handle overflow. */
  285. timer->start = now;
  286. }
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. /*!
  292. * \internal
  293. * \pre timer is locked
  294. * \retval 0 if nothing to read or read success
  295. * \retval -1 on error
  296. */
  297. static int read_pipe(struct pthread_timer *timer, unsigned int quantity)
  298. {
  299. int rd_fd = timer->pipe[PIPE_READ];
  300. int pending_ticks = timer->pending_ticks;
  301. ast_assert(quantity);
  302. if (timer->continuous && pending_ticks) {
  303. pending_ticks--;
  304. }
  305. if (quantity > pending_ticks) {
  306. quantity = pending_ticks;
  307. }
  308. if (!quantity) {
  309. return 0;
  310. }
  311. do {
  312. unsigned char buf[1024];
  313. ssize_t res;
  314. struct pollfd pfd = {
  315. .fd = rd_fd,
  316. .events = POLLIN,
  317. };
  318. if (ast_poll(&pfd, 1, 0) != 1) {
  319. ast_debug(1, "Reading not available on timing pipe, "
  320. "quantity: %u\n", quantity);
  321. return -1;
  322. }
  323. res = read(rd_fd, buf,
  324. (quantity < sizeof(buf)) ? quantity : sizeof(buf));
  325. if (res == -1) {
  326. if (errno == EAGAIN) {
  327. continue;
  328. }
  329. ast_log(LOG_ERROR, "read failed on timing pipe: %s\n",
  330. strerror(errno));
  331. return -1;
  332. }
  333. quantity -= res;
  334. timer->pending_ticks -= res;
  335. } while (quantity);
  336. return 0;
  337. }
  338. /*!
  339. * \internal
  340. * \pre timer is locked
  341. */
  342. static void write_byte(struct pthread_timer *timer)
  343. {
  344. ssize_t res;
  345. unsigned char x = 42;
  346. do {
  347. res = write(timer->pipe[PIPE_WRITE], &x, 1);
  348. } while (res == -1 && errno == EAGAIN);
  349. if (res == -1) {
  350. ast_log(LOG_ERROR, "Error writing to timing pipe: %s\n",
  351. strerror(errno));
  352. } else {
  353. timer->pending_ticks++;
  354. }
  355. }
  356. static int run_timer(void *obj, void *arg, int flags)
  357. {
  358. struct pthread_timer *timer = obj;
  359. if (timer->state == TIMER_STATE_IDLE) {
  360. return 0;
  361. }
  362. ao2_lock(timer);
  363. if (check_timer(timer)) {
  364. write_byte(timer);
  365. }
  366. ao2_unlock(timer);
  367. return 0;
  368. }
  369. static void *do_timing(void *arg)
  370. {
  371. struct timeval next_wakeup = ast_tvnow();
  372. while (!timing_thread.stop) {
  373. struct timespec ts = { 0, };
  374. ao2_callback(pthread_timers, OBJ_NODATA, run_timer, NULL);
  375. next_wakeup = ast_tvadd(next_wakeup, ast_tv(0, 5000));
  376. ts.tv_sec = next_wakeup.tv_sec;
  377. ts.tv_nsec = next_wakeup.tv_usec * 1000;
  378. ast_mutex_lock(&timing_thread.lock);
  379. if (!timing_thread.stop) {
  380. if (ao2_container_count(pthread_timers)) {
  381. ast_cond_timedwait(&timing_thread.cond, &timing_thread.lock, &ts);
  382. } else {
  383. ast_cond_wait(&timing_thread.cond, &timing_thread.lock);
  384. }
  385. }
  386. ast_mutex_unlock(&timing_thread.lock);
  387. }
  388. return NULL;
  389. }
  390. static int init_timing_thread(void)
  391. {
  392. ast_mutex_init(&timing_thread.lock);
  393. ast_cond_init(&timing_thread.cond, NULL);
  394. if (ast_pthread_create_background(&timing_thread.thread, NULL, do_timing, NULL)) {
  395. ast_log(LOG_ERROR, "Unable to start timing thread.\n");
  396. return -1;
  397. }
  398. return 0;
  399. }
  400. static int load_module(void)
  401. {
  402. if (!(pthread_timers = ao2_container_alloc(PTHREAD_TIMER_BUCKETS,
  403. pthread_timer_hash, pthread_timer_cmp))) {
  404. return AST_MODULE_LOAD_DECLINE;
  405. }
  406. if (init_timing_thread()) {
  407. ao2_ref(pthread_timers, -1);
  408. pthread_timers = NULL;
  409. return AST_MODULE_LOAD_DECLINE;
  410. }
  411. return (timing_funcs_handle = ast_register_timing_interface(&pthread_timing)) ?
  412. AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
  413. }
  414. static int unload_module(void)
  415. {
  416. int res;
  417. ast_mutex_lock(&timing_thread.lock);
  418. timing_thread.stop = 1;
  419. ast_cond_signal(&timing_thread.cond);
  420. ast_mutex_unlock(&timing_thread.lock);
  421. pthread_join(timing_thread.thread, NULL);
  422. if (!(res = ast_unregister_timing_interface(timing_funcs_handle))) {
  423. ao2_ref(pthread_timers, -1);
  424. pthread_timers = NULL;
  425. }
  426. return res;
  427. }
  428. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "pthread Timing Interface",
  429. .load = load_module,
  430. .unload = unload_module,
  431. .load_pri = AST_MODPRI_TIMING,
  432. );