- Make res_timing_pthread allow a max rate of 100/sec instead of 50/sec
[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 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
29
30 #include <math.h>
31 #include <sys/select.h>
32
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
40 static void *timing_funcs_handle;
41
42 static int pthread_timer_open(void);
43 static void pthread_timer_close(int handle);
44 static int pthread_timer_set_rate(int handle, unsigned int rate);
45 static void pthread_timer_ack(int handle, unsigned int quantity);
46 static int pthread_timer_enable_continuous(int handle);
47 static int pthread_timer_disable_continuous(int handle);
48 static enum ast_timing_event pthread_timer_get_event(int handle);
49
50 static struct ast_timing_functions pthread_timing_functions = {
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 };
59
60 /* 1 tick / 10 ms */
61 #define MAX_RATE 100
62
63 static struct ao2_container *pthread_timers;
64 #define PTHREAD_TIMER_BUCKETS 563
65
66 enum {
67         PIPE_READ =  0,
68         PIPE_WRITE = 1
69 };
70
71 enum pthread_timer_state {
72         TIMER_STATE_IDLE,
73         TIMER_STATE_TICKING,
74         TIMER_STATE_CONTINUOUS,
75 };
76
77 struct pthread_timer {
78         int pipe[2];
79         enum pthread_timer_state state;
80         unsigned int rate;
81         /*! Interval in ms for current rate */
82         unsigned int interval;
83         unsigned int tick_count;
84         struct timeval start;
85 };
86
87 static void pthread_timer_destructor(void *obj);
88 static struct pthread_timer *find_timer(int handle, int unlink);
89 static void write_byte(int wr_fd);
90 static void read_pipe(int rd_fd, unsigned int num, int clear);
91
92 /*!
93  * \brief Data for the timing thread
94  */
95 static struct {
96         pthread_t thread;
97         ast_mutex_t lock;
98         ast_cond_t cond;
99         unsigned int stop:1;
100 } timing_thread;
101
102 static int pthread_timer_open(void)
103 {
104         struct pthread_timer *timer;
105
106         if (!(timer = ao2_alloc(sizeof(*timer), pthread_timer_destructor))) {
107                 errno = ENOMEM;
108                 return -1;
109         }
110
111         timer->pipe[PIPE_READ] = timer->pipe[PIPE_WRITE] = -1;
112         timer->state = TIMER_STATE_IDLE;
113
114         if (pipe(timer->pipe)) {
115                 ao2_ref(timer, -1);
116                 return -1;
117         }
118
119         ao2_lock(pthread_timers);
120         if (!ao2_container_count(pthread_timers)) {
121                 ast_mutex_lock(&timing_thread.lock);
122                 ast_cond_signal(&timing_thread.cond);
123                 ast_mutex_unlock(&timing_thread.lock);
124         }
125         ao2_link(pthread_timers, timer);
126         ao2_unlock(pthread_timers);
127
128         return timer->pipe[PIPE_READ];
129 }
130
131 static void pthread_timer_close(int handle)
132 {
133         struct pthread_timer *timer;
134
135         if (!(timer = find_timer(handle, 1))) {
136                 return;
137         }
138
139         ao2_ref(timer, -1);
140 }
141
142 static int pthread_timer_set_rate(int handle, unsigned int rate)
143 {
144         struct pthread_timer *timer;
145
146         if (!(timer = find_timer(handle, 0))) {
147                 errno = EINVAL;
148                 return -1;
149         }
150
151         if (rate > MAX_RATE) {
152                 ast_log(LOG_ERROR, "res_timing_pthread only supports timers at a max rate of %d / sec\n",
153                         MAX_RATE);
154                 errno = EINVAL;
155                 return -1;
156         }
157
158         ao2_lock(timer);
159         timer->rate = rate;
160         timer->state = rate ? TIMER_STATE_TICKING : TIMER_STATE_IDLE;
161         timer->interval = rate ? roundf(1000.0 / ((float) rate)) : 0;
162         timer->start = rate ? ast_tvnow() : ast_tv(0, 0);
163         timer->tick_count = 0;
164         ao2_unlock(timer);
165
166         ao2_ref(timer, -1);
167
168         return 0;
169 }
170
171 static void pthread_timer_ack(int handle, unsigned int quantity)
172 {
173         struct pthread_timer *timer;
174
175         ast_assert(quantity > 0);
176
177         if (!(timer = find_timer(handle, 0))) {
178                 return;
179         }
180
181         if (timer->state == TIMER_STATE_CONTINUOUS) {
182                 /* Leave the pipe alone, please! */
183                 return;
184         }
185
186         read_pipe(timer->pipe[PIPE_READ], quantity, 0);
187
188         ao2_ref(timer, -1);
189 }
190
191 static int pthread_timer_enable_continuous(int handle)
192 {
193         struct pthread_timer *timer;
194
195         if (!(timer = find_timer(handle, 0))) {
196                 errno = EINVAL;
197                 return -1;
198         }
199
200         ao2_lock(timer);
201         timer->state = TIMER_STATE_CONTINUOUS;
202         write_byte(timer->pipe[PIPE_WRITE]);
203         ao2_unlock(timer);
204
205         ao2_ref(timer, -1);
206
207         return 0;
208 }
209
210 static int pthread_timer_disable_continuous(int handle)
211 {
212         struct pthread_timer *timer;
213
214         if (!(timer = find_timer(handle, 0))) {
215                 errno = EINVAL;
216                 return -1;
217         }
218
219         ao2_lock(timer);
220         timer->state = timer->rate ? TIMER_STATE_TICKING : TIMER_STATE_IDLE;
221         read_pipe(timer->pipe[PIPE_READ], 0, 1);
222         ao2_unlock(timer);
223
224         ao2_ref(timer, -1);
225
226         return 0;
227 }
228
229 static enum ast_timing_event pthread_timer_get_event(int handle)
230 {
231         struct pthread_timer *timer;
232         enum ast_timing_event res = AST_TIMING_EVENT_EXPIRED;
233
234         if (!(timer = find_timer(handle, 0))) {
235                 return res;
236         }
237
238         if (timer->state == TIMER_STATE_CONTINUOUS) {
239                 res = AST_TIMING_EVENT_CONTINUOUS;
240         }
241
242         ao2_ref(timer, -1);
243
244         return res;
245 }
246
247 static struct pthread_timer *find_timer(int handle, int unlink)
248 {
249         struct pthread_timer *timer;
250         struct pthread_timer tmp_timer;
251         int flags = OBJ_POINTER;
252
253         tmp_timer.pipe[PIPE_READ] = handle;
254
255         if (unlink) {
256                 flags |= OBJ_UNLINK;
257         }
258
259         if (!(timer = ao2_find(pthread_timers, &tmp_timer, flags))) {
260                 ast_assert(timer != NULL);
261                 return NULL;
262         }
263
264         return timer;
265 }
266
267 static void pthread_timer_destructor(void *obj)
268 {
269         struct pthread_timer *timer = obj;
270
271         if (timer->pipe[PIPE_READ] > -1) {
272                 close(timer->pipe[PIPE_READ]);
273                 timer->pipe[PIPE_READ] = -1;
274         }
275
276         if (timer->pipe[PIPE_WRITE] > -1) {
277                 close(timer->pipe[PIPE_WRITE]);
278                 timer->pipe[PIPE_WRITE] = -1;
279         }
280 }
281
282 /*!
283  * \note only PIPE_READ is guaranteed valid 
284  */
285 static int pthread_timer_hash(const void *obj, const int flags)
286 {
287         const struct pthread_timer *timer = obj;
288
289         return timer->pipe[PIPE_READ];
290 }
291
292 /*!
293  * \note only PIPE_READ is guaranteed valid 
294  */
295 static int pthread_timer_cmp(void *obj, void *arg, int flags)
296 {
297         struct pthread_timer *timer1 = obj, *timer2 = arg;
298
299         return (timer1->pipe[PIPE_READ] == timer2->pipe[PIPE_READ]) ? CMP_MATCH : 0;
300 }
301
302 /*!
303  * \retval 0 no timer tick needed
304  * \retval non-zero write to the timing pipe needed
305  */
306 static int check_timer(struct pthread_timer *timer)
307 {
308         struct timeval now;
309
310         if (timer->state == TIMER_STATE_IDLE || timer->state == TIMER_STATE_CONTINUOUS) {
311                 return 0;       
312         }
313         
314         now = ast_tvnow();
315
316         if (timer->tick_count < (ast_tvdiff_ms(now, timer->start) / timer->interval)) {
317                 timer->tick_count++;
318                 if (!timer->tick_count) {
319                         timer->start = now;
320                 }
321                 return 1;
322         }
323
324         return 0;
325 }
326
327 static void read_pipe(int rd_fd, unsigned int quantity, int clear)
328 {
329
330         ast_assert(quantity || clear);
331
332         if (!quantity && clear) {
333                 quantity = 1;
334         }
335
336         do {
337                 unsigned char buf[1024];
338                 ssize_t res;
339                 fd_set rfds;
340                 struct timeval tv = {
341                         .tv_sec = 0,
342                 };
343
344                 /* Make sure there is data to read */
345                 FD_ZERO(&rfds);
346                 FD_SET(rd_fd, &rfds);
347
348                 if (select(rd_fd + 1, &rfds, NULL, NULL, &tv) != 1) {
349                         break;
350                 }
351
352                 res = read(rd_fd, buf, 
353                         (quantity < sizeof(buf)) ? quantity : sizeof(buf));
354
355                 if (res == -1) {
356                         if (errno == EAGAIN) {
357                                 continue;
358                         }
359                         ast_log(LOG_ERROR, "read failed on timing pipe: %s\n", strerror(errno));
360                         break;
361                 }
362
363                 if (clear) {
364                         continue;
365                 }
366
367                 quantity -= res;
368         } while (quantity);
369 }
370
371 static void write_byte(int wr_fd)
372 {
373         do {
374                 ssize_t res;
375                 unsigned char x = 42;
376
377                 res = write(wr_fd, &x, 1); 
378
379                 if (res == -1) {
380                         if (errno == EAGAIN) {
381                                 continue;
382                         }
383                         ast_log(LOG_ERROR, "Error writing to timing pipe: %s\n", strerror(errno));
384                 }
385         } while (0);
386 }
387
388 static int run_timer(void *obj, void *arg, int flags)
389 {
390         struct pthread_timer *timer = obj;
391
392         if (timer->state == TIMER_STATE_IDLE) {
393                 return 0;
394         }
395
396         ao2_lock(timer);
397
398         if (check_timer(timer)) {
399                 write_byte(timer->pipe[PIPE_WRITE]);
400         }
401         
402         ao2_unlock(timer);
403
404         return 0;
405 }
406
407 static void *do_timing(void *arg)
408 {
409         struct timeval next_wakeup = ast_tvnow();
410
411         while (!timing_thread.stop) {
412                 struct timespec ts = { 0, };
413
414                 ao2_callback(pthread_timers, OBJ_NODATA, run_timer, NULL);
415
416                 next_wakeup = ast_tvadd(next_wakeup, ast_tv(0, 5000));
417
418                 ts.tv_sec = next_wakeup.tv_sec;
419                 ts.tv_nsec = next_wakeup.tv_usec * 1000;
420
421                 ast_mutex_lock(&timing_thread.lock);
422                 if (!timing_thread.stop) {
423                         if (ao2_container_count(pthread_timers)) {
424                                 ast_cond_timedwait(&timing_thread.cond, &timing_thread.lock, &ts);
425                         } else {
426                                 ast_cond_wait(&timing_thread.cond, &timing_thread.lock);
427                         }
428                 }
429                 ast_mutex_unlock(&timing_thread.lock);
430         }
431
432         return NULL;
433 }
434
435 static int init_timing_thread(void)
436 {
437         ast_mutex_init(&timing_thread.lock);
438         ast_cond_init(&timing_thread.cond, NULL);
439
440         if (ast_pthread_create_background(&timing_thread.thread, NULL, do_timing, NULL)) {
441                 ast_log(LOG_ERROR, "Unable to start timing thread.\n");
442                 return -1;
443         }
444
445         return 0;
446 }
447
448 static int load_module(void)
449 {
450         if (!(pthread_timers = ao2_container_alloc(PTHREAD_TIMER_BUCKETS, 
451                 pthread_timer_hash, pthread_timer_cmp))) {
452                 return AST_MODULE_LOAD_DECLINE;
453         }
454
455         if (init_timing_thread()) {
456                 ao2_ref(pthread_timers, -1);
457                 pthread_timers = NULL;
458                 return AST_MODULE_LOAD_DECLINE;
459         }
460
461         return (timing_funcs_handle = ast_install_timing_functions(&pthread_timing_functions)) ?
462                 AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
463 }
464
465 static int unload_module(void)
466 {
467 #if 0
468         /* XXX code to stop the timing thread ... */
469
470         ast_uninstall_timing_functions(timing_funcs_handle);
471         ao2_ref(pthread_timers, -1);
472 #endif
473
474         /* This module can not currently be unloaded.  No use count handling is being done. */
475
476         return -1;
477 }
478
479 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "pthread Timing Interface");