res_timing_pthread.c 11 KB

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