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