Fix build issues on Mac OSX.
[asterisk/asterisk.git] / main / timing.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008 - 2009, Digium, Inc.
5  *
6  * Kevin P. Fleming <kpfleming@digium.com>
7  * Russell Bryant <russell@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \brief Timing source management
23  *
24  * \author Kevin P. Fleming <kpfleming@digium.com>
25  * \author Russell Bryant <russell@digium.com>
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/_private.h"
33
34 #include "asterisk/timing.h"
35 #include "asterisk/lock.h"
36 #include "asterisk/cli.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/time.h"
39 #include "asterisk/heap.h"
40 #include "asterisk/module.h"
41
42 struct timing_holder {
43         /*! Do _not_ move this from the beginning of the struct. */
44         ssize_t __heap_index;
45         struct ast_module *mod;
46         struct ast_timing_interface *iface;
47 };
48
49 static struct ast_heap *timing_interfaces;
50
51 static int timing_holder_cmp(void *_h1, void *_h2)
52 {
53         struct timing_holder *h1 = _h1;
54         struct timing_holder *h2 = _h2;
55
56         if (h1->iface->priority > h2->iface->priority) {
57                 return 1;
58         } else if (h1->iface->priority == h2->iface->priority) {
59                 return 0;
60         } else {
61                 return -1;
62         }
63 }
64
65 void *_ast_register_timing_interface(struct ast_timing_interface *funcs, 
66                 struct ast_module *mod)
67 {
68         struct timing_holder *h;
69
70         if (!funcs->timer_open ||
71             !funcs->timer_close ||
72                 !funcs->timer_set_rate ||
73             !funcs->timer_ack ||
74             !funcs->timer_get_event ||
75                 !funcs->timer_get_max_rate ||
76             !funcs->timer_enable_continuous ||
77             !funcs->timer_disable_continuous) {
78                 return NULL;
79         }
80
81         if (!(h = ast_calloc(1, sizeof(*h)))) {
82                 return NULL;
83         }
84
85         h->iface = funcs;
86         h->mod = mod;
87
88         ast_heap_wrlock(timing_interfaces);
89         ast_heap_push(timing_interfaces, h);
90         ast_heap_unlock(timing_interfaces);
91
92         return h;
93 }
94
95 int ast_unregister_timing_interface(void *handle)
96 {
97         struct timing_holder *h = handle;
98         int res = -1;
99
100         ast_heap_wrlock(timing_interfaces);
101         h = ast_heap_remove(timing_interfaces, h);
102         ast_heap_unlock(timing_interfaces);
103
104         if (h) {
105                 ast_free(h);
106                 h = NULL;
107                 res = 0;
108         }
109
110         return res;
111 }
112
113 int ast_timer_open(void)
114 {
115         int fd = -1;
116         struct timing_holder *h;
117
118         ast_heap_rdlock(timing_interfaces);
119
120         if ((h = ast_heap_peek(timing_interfaces, 1))) {
121                 fd = h->iface->timer_open();
122                 ast_module_ref(h->mod);
123         }
124
125         ast_heap_unlock(timing_interfaces);
126
127         return fd;
128 }
129
130 void ast_timer_close(int timer)
131 {
132         struct timing_holder *h;
133
134         ast_heap_rdlock(timing_interfaces);
135
136         if ((h = ast_heap_peek(timing_interfaces, 1))) {
137                 h->iface->timer_close(timer);
138                 ast_module_unref(h->mod);
139         }
140
141         ast_heap_unlock(timing_interfaces);
142 }
143
144 int ast_timer_set_rate(int handle, unsigned int rate)
145 {
146         struct timing_holder *h;
147         int res = -1;
148
149         ast_heap_rdlock(timing_interfaces);
150
151         if ((h = ast_heap_peek(timing_interfaces, 1))) {
152                 res = h->iface->timer_set_rate(handle, rate);
153         }
154
155         ast_heap_unlock(timing_interfaces);
156
157         return res;
158 }
159
160 void ast_timer_ack(int handle, unsigned int quantity)
161 {
162         struct timing_holder *h;
163
164         ast_heap_rdlock(timing_interfaces);
165
166         if ((h = ast_heap_peek(timing_interfaces, 1))) {
167                 h->iface->timer_ack(handle, quantity);
168         }
169
170         ast_heap_unlock(timing_interfaces);
171 }
172
173 int ast_timer_enable_continuous(int handle)
174 {
175         struct timing_holder *h;
176         int res = -1;
177
178         ast_heap_rdlock(timing_interfaces);
179
180         if ((h = ast_heap_peek(timing_interfaces, 1))) {
181                 res = h->iface->timer_enable_continuous(handle);
182         }
183
184         ast_heap_unlock(timing_interfaces);
185
186         return res;
187 }
188
189 int ast_timer_disable_continuous(int handle)
190 {
191         struct timing_holder *h;
192         int res = -1;
193
194         ast_heap_rdlock(timing_interfaces);
195
196         if ((h = ast_heap_peek(timing_interfaces, 1))) {
197                 res = h->iface->timer_disable_continuous(handle);
198         }
199
200         ast_heap_unlock(timing_interfaces);
201
202         return res;
203 }
204
205 enum ast_timer_event ast_timer_get_event(int handle)
206 {
207         struct timing_holder *h;
208         enum ast_timer_event res = -1;
209
210         ast_heap_rdlock(timing_interfaces);
211
212         if ((h = ast_heap_peek(timing_interfaces, 1))) {
213                 res = h->iface->timer_get_event(handle);
214         }
215
216         ast_heap_unlock(timing_interfaces);
217
218         return res;
219 }
220
221 unsigned int ast_timer_get_max_rate(int handle)
222 {
223         struct timing_holder *h;
224         unsigned int res = 0;
225
226         ast_heap_rdlock(timing_interfaces);
227
228         if ((h = ast_heap_peek(timing_interfaces, 1))) {
229                 res = h->iface->timer_get_max_rate(handle);
230         }
231
232         ast_heap_unlock(timing_interfaces);
233
234         return res;
235 }
236
237 static char *timing_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
238 {
239         int fd, count = 0;
240         struct timeval start, end;
241         unsigned int test_rate = 50;
242         struct timing_holder *h;
243
244         switch (cmd) {
245         case CLI_INIT:
246                 e->command = "timing test";
247                 e->usage = "Usage: timing test <rate>\n"
248                            "   Test a timer with a specified rate, 100/sec by default.\n"
249                            "";
250                 return NULL;
251         case CLI_GENERATE:
252                 return NULL;
253         }
254
255         if (a->argc != 2 && a->argc != 3) {
256                 return CLI_SHOWUSAGE;
257         }
258
259         if (a->argc == 3) {
260                 unsigned int rate;
261                 if (sscanf(a->argv[2], "%u", &rate) == 1) {
262                         test_rate = rate;
263                 } else {
264                         ast_cli(a->fd, "Invalid rate '%s', using default of %u\n", a->argv[2], test_rate);      
265                 }
266         }
267
268         ast_cli(a->fd, "Attempting to test a timer with %u ticks per second.\n", test_rate);
269
270         if ((fd = ast_timer_open()) == -1) {
271                 ast_cli(a->fd, "Failed to open timing fd\n");
272                 return CLI_FAILURE;
273         }
274
275         ast_heap_rdlock(timing_interfaces);
276         if ((h = ast_heap_peek(timing_interfaces, 1))) {
277                 ast_cli(a->fd, "Using the '%s' timing module for this test.\n", h->iface->name);
278                 h = NULL;
279         }
280         ast_heap_unlock(timing_interfaces);
281
282         start = ast_tvnow();
283
284         ast_timer_set_rate(fd, test_rate);
285
286         while (ast_tvdiff_ms((end = ast_tvnow()), start) < 1000) {
287                 int res;
288                 struct pollfd pfd = {
289                         .fd = fd,
290                         .events = POLLIN | POLLPRI,
291                 };
292
293                 res = poll(&pfd, 1, 100);
294
295                 if (res == 1) {
296                         count++;
297                         ast_timer_ack(fd, 1);
298                 } else if (!res) {
299                         ast_cli(a->fd, "poll() timed out!  This is bad.\n");
300                 } else if (errno != EAGAIN && errno != EINTR) {
301                         ast_cli(a->fd, "poll() returned error: %s\n", strerror(errno));
302                 }
303         }
304
305         ast_timer_close(fd);
306
307         ast_cli(a->fd, "It has been %d milliseconds, and we got %d timer ticks\n", 
308                 ast_tvdiff_ms(end, start), count);
309
310         return CLI_SUCCESS;
311 }
312
313 static struct ast_cli_entry cli_timing[] = {
314         AST_CLI_DEFINE(timing_test, "Run a timing test"),
315 };
316
317 int ast_timing_init(void)
318 {
319         if (!(timing_interfaces = ast_heap_create(2, timing_holder_cmp, 0))) {
320                 return -1;
321         }
322
323         return ast_cli_register_multiple(cli_timing, ARRAY_LEN(cli_timing));
324 }