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