res/res_pjsip/pjsip_options.c: Eliminate excessive RAII_VAR usage.
[asterisk/asterisk.git] / res / res_timing_timerfd.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@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 Mark Michelson <mmichelson@digium.com>
22  *
23  * \brief timerfd timing interface
24  */
25
26 /*** MODULEINFO
27         <depend>timerfd</depend>
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 #include <sys/timerfd.h>
34
35 #include "asterisk/module.h"
36 #include "asterisk/astobj2.h"
37 #include "asterisk/timing.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/time.h"
41
42 static void *timing_funcs_handle;
43
44 static void *timerfd_timer_open(void);
45 static void timerfd_timer_close(void *data);
46 static int timerfd_timer_set_rate(void *data, unsigned int rate);
47 static int timerfd_timer_ack(void *data, unsigned int quantity);
48 static int timerfd_timer_enable_continuous(void *data);
49 static int timerfd_timer_disable_continuous(void *data);
50 static enum ast_timer_event timerfd_timer_get_event(void *data);
51 static unsigned int timerfd_timer_get_max_rate(void *data);
52 static int timerfd_timer_fd(void *data);
53
54 static struct ast_timing_interface timerfd_timing = {
55         .name = "timerfd",
56         .priority = 200,
57         .timer_open = timerfd_timer_open,
58         .timer_close = timerfd_timer_close,
59         .timer_set_rate = timerfd_timer_set_rate,
60         .timer_ack = timerfd_timer_ack,
61         .timer_enable_continuous = timerfd_timer_enable_continuous,
62         .timer_disable_continuous = timerfd_timer_disable_continuous,
63         .timer_get_event = timerfd_timer_get_event,
64         .timer_get_max_rate = timerfd_timer_get_max_rate,
65         .timer_fd = timerfd_timer_fd,
66 };
67
68 #define TIMERFD_MAX_RATE 1000
69
70 struct timerfd_timer {
71         int fd;
72         struct itimerspec saved_timer;
73         unsigned int is_continuous:1;
74 };
75
76 static void timer_destroy(void *obj)
77 {
78         struct timerfd_timer *timer = obj;
79
80         close(timer->fd);
81 }
82
83 static void *timerfd_timer_open(void)
84 {
85         struct timerfd_timer *timer;
86
87         if (!(timer = ao2_alloc(sizeof(*timer), timer_destroy))) {
88                 ast_log(LOG_ERROR, "Could not allocate memory for timerfd_timer structure\n");
89                 return NULL;
90         }
91         if ((timer->fd = timerfd_create(CLOCK_MONOTONIC, 0)) < 0) {
92                 ast_log(LOG_ERROR, "Failed to create timerfd timer: %s\n", strerror(errno));
93                 ao2_ref(timer, -1);
94                 return NULL;
95         }
96
97         return timer;
98 }
99
100 static void timerfd_timer_close(void *data)
101 {
102         ao2_ref(data, -1);
103 }
104
105 static int timerfd_timer_set_rate(void *data, unsigned int rate)
106 {
107         struct timerfd_timer *timer = data;
108         int res = 0;
109
110         ao2_lock(timer);
111
112         timer->saved_timer.it_value.tv_sec = 0;
113         timer->saved_timer.it_value.tv_nsec = rate ? (long) (1000000000 / rate) : 0L;
114         timer->saved_timer.it_interval.tv_sec = timer->saved_timer.it_value.tv_sec;
115         timer->saved_timer.it_interval.tv_nsec = timer->saved_timer.it_value.tv_nsec;
116
117         if (!timer->is_continuous) {
118                 res = timerfd_settime(timer->fd, 0, &timer->saved_timer, NULL);
119         }
120
121         ao2_unlock(timer);
122
123         return res;
124 }
125
126 static int timerfd_timer_ack(void *data, unsigned int quantity)
127 {
128         struct timerfd_timer *timer = data;
129         uint64_t expirations;
130         int read_result = 0;
131         int res = 0;
132
133         ao2_lock(timer);
134
135         do {
136                 struct itimerspec timer_status;
137
138                 if (timerfd_gettime(timer->fd, &timer_status)) {
139                         ast_log(LOG_ERROR, "Call to timerfd_gettime() using handle %d error: %s\n", timer->fd, strerror(errno));
140                         expirations = 0;
141                         res = -1;
142                         break;
143                 }
144
145                 if (timer_status.it_value.tv_sec == 0 && timer_status.it_value.tv_nsec == 0) {
146                         ast_debug(1, "Avoiding read on disarmed timerfd %d\n", timer->fd);
147                         expirations = 0;
148                         break;
149                 }
150
151                 read_result = read(timer->fd, &expirations, sizeof(expirations));
152                 if (read_result == -1) {
153                         if (errno == EINTR || errno == EAGAIN) {
154                                 continue;
155                         } else {
156                                 ast_log(LOG_ERROR, "Read error: %s\n", strerror(errno));
157                                 res = -1;
158                                 break;
159                         }
160                 }
161         } while (read_result != sizeof(expirations));
162
163         ao2_unlock(timer);
164
165         if (expirations != quantity) {
166                 ast_debug(2, "Expected to acknowledge %u ticks but got %llu instead\n", quantity, (unsigned long long) expirations);
167         }
168
169         return res;
170 }
171
172 static int timerfd_timer_enable_continuous(void *data)
173 {
174         struct timerfd_timer *timer = data;
175         int res;
176         static const struct itimerspec continuous_timer = {
177                 .it_value.tv_nsec = 1L,
178         };
179
180         ao2_lock(timer);
181
182         if (timer->is_continuous) {
183                 /*It's already in continous mode, no need to do
184                  * anything further
185                  */
186                 ao2_unlock(timer);
187                 return 0;
188         }
189
190         res = timerfd_settime(timer->fd, 0, &continuous_timer, &timer->saved_timer);
191         timer->is_continuous = 1;
192         ao2_unlock(timer);
193
194         return res;
195 }
196
197 static int timerfd_timer_disable_continuous(void *data)
198 {
199         struct timerfd_timer *timer = data;
200         int res;
201
202         ao2_lock(timer);
203
204         if (!timer->is_continuous) {
205                 /* No reason to do anything if we're not
206                  * in continuous mode
207                  */
208                 ao2_unlock(timer);
209                 return 0;
210         }
211
212         res = timerfd_settime(timer->fd, 0, &timer->saved_timer, NULL);
213         timer->is_continuous = 0;
214         memset(&timer->saved_timer, 0, sizeof(timer->saved_timer));
215         ao2_unlock(timer);
216
217         return res;
218 }
219
220 static enum ast_timer_event timerfd_timer_get_event(void *data)
221 {
222         struct timerfd_timer *timer = data;
223         enum ast_timer_event res;
224
225         ao2_lock(timer);
226
227         if (timer->is_continuous) {
228                 res = AST_TIMING_EVENT_CONTINUOUS;
229         } else {
230                 res = AST_TIMING_EVENT_EXPIRED;
231         }
232
233         ao2_unlock(timer);
234
235         return res;
236 }
237
238 static unsigned int timerfd_timer_get_max_rate(void *data)
239 {
240         return TIMERFD_MAX_RATE;
241 }
242
243 static int timerfd_timer_fd(void *data)
244 {
245         struct timerfd_timer *timer = data;
246
247         return timer->fd;
248 }
249
250 static int load_module(void)
251 {
252         int fd;
253
254         /* Make sure we support the necessary clock type */
255         if ((fd = timerfd_create(CLOCK_MONOTONIC, 0)) < 0) {
256                 ast_log(LOG_ERROR, "timerfd_create() not supported by the kernel.  Not loading.\n");
257                 return AST_MODULE_LOAD_DECLINE;
258         }
259
260         close(fd);
261
262         if (!(timing_funcs_handle = ast_register_timing_interface(&timerfd_timing))) {
263                 return AST_MODULE_LOAD_DECLINE;
264         }
265
266         return AST_MODULE_LOAD_SUCCESS;
267 }
268
269 static int unload_module(void)
270 {
271         return ast_unregister_timing_interface(timing_funcs_handle);
272 }
273
274 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Timerfd Timing Interface",
275                 .support_level = AST_MODULE_SUPPORT_CORE,
276                 .load = load_module,
277                 .unload = unload_module,
278                 .load_pri = AST_MODPRI_TIMING,
279                 );