2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2008 - 2009, Digium, Inc.
6 * Kevin P. Fleming <kpfleming@digium.com>
7 * Russell Bryant <russell@digium.com>
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.
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.
22 * \brief Timing source management
24 * \author Kevin P. Fleming <kpfleming@digium.com>
25 * \author Russell Bryant <russell@digium.com>
29 <support_level>core</support_level>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/_private.h"
38 #include "asterisk/timing.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/cli.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/time.h"
43 #include "asterisk/heap.h"
44 #include "asterisk/module.h"
45 #include "asterisk/poll-compat.h"
47 struct timing_holder {
48 /*! Do _not_ move this from the beginning of the struct. */
50 struct ast_module *mod;
51 struct ast_timing_interface *iface;
54 static struct ast_heap *timing_interfaces;
58 struct timing_holder *holder;
61 static int timing_holder_cmp(void *_h1, void *_h2)
63 struct timing_holder *h1 = _h1;
64 struct timing_holder *h2 = _h2;
66 if (h1->iface->priority > h2->iface->priority) {
68 } else if (h1->iface->priority == h2->iface->priority) {
75 void *_ast_register_timing_interface(struct ast_timing_interface *funcs,
76 struct ast_module *mod)
78 struct timing_holder *h;
80 if (!funcs->timer_open ||
81 !funcs->timer_close ||
82 !funcs->timer_set_rate ||
84 !funcs->timer_get_event ||
85 !funcs->timer_get_max_rate ||
86 !funcs->timer_enable_continuous ||
87 !funcs->timer_disable_continuous) {
91 if (!(h = ast_calloc(1, sizeof(*h)))) {
98 ast_heap_wrlock(timing_interfaces);
99 ast_heap_push(timing_interfaces, h);
100 ast_heap_unlock(timing_interfaces);
105 int ast_unregister_timing_interface(void *handle)
107 struct timing_holder *h = handle;
110 ast_heap_wrlock(timing_interfaces);
111 h = ast_heap_remove(timing_interfaces, h);
112 ast_heap_unlock(timing_interfaces);
123 struct ast_timer *ast_timer_open(void)
126 struct timing_holder *h;
127 struct ast_timer *t = NULL;
129 ast_heap_rdlock(timing_interfaces);
131 if ((h = ast_heap_peek(timing_interfaces, 1))) {
132 fd = h->iface->timer_open();
133 ast_module_ref(h->mod);
137 if (!(t = ast_calloc(1, sizeof(*t)))) {
138 h->iface->timer_close(fd);
145 ast_heap_unlock(timing_interfaces);
150 void ast_timer_close(struct ast_timer *handle)
152 handle->holder->iface->timer_close(handle->fd);
154 ast_module_unref(handle->holder->mod);
158 int ast_timer_fd(const struct ast_timer *handle)
163 int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate)
167 res = handle->holder->iface->timer_set_rate(handle->fd, rate);
172 int ast_timer_ack(const struct ast_timer *handle, unsigned int quantity)
176 res = handle->holder->iface->timer_ack(handle->fd, quantity);
181 int ast_timer_enable_continuous(const struct ast_timer *handle)
185 res = handle->holder->iface->timer_enable_continuous(handle->fd);
190 int ast_timer_disable_continuous(const struct ast_timer *handle)
194 res = handle->holder->iface->timer_disable_continuous(handle->fd);
199 enum ast_timer_event ast_timer_get_event(const struct ast_timer *handle)
201 enum ast_timer_event res = -1;
203 res = handle->holder->iface->timer_get_event(handle->fd);
208 unsigned int ast_timer_get_max_rate(const struct ast_timer *handle)
210 unsigned int res = 0;
212 res = handle->holder->iface->timer_get_max_rate(handle->fd);
217 const char *ast_timer_get_name(const struct ast_timer *handle)
219 return handle->holder->iface->name;
222 static char *timing_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
224 struct ast_timer *timer;
226 struct timeval start, end;
227 unsigned int test_rate = 50;
231 e->command = "timing test";
232 e->usage = "Usage: timing test <rate>\n"
233 " Test a timer with a specified rate, 50/sec by default.\n"
240 if (a->argc != 2 && a->argc != 3) {
241 return CLI_SHOWUSAGE;
246 if (sscanf(a->argv[2], "%30u", &rate) == 1) {
249 ast_cli(a->fd, "Invalid rate '%s', using default of %u\n", a->argv[2], test_rate);
253 ast_cli(a->fd, "Attempting to test a timer with %u ticks per second.\n", test_rate);
255 if (!(timer = ast_timer_open())) {
256 ast_cli(a->fd, "Failed to open timing fd\n");
260 ast_cli(a->fd, "Using the '%s' timing module for this test.\n", timer->holder->iface->name);
264 ast_timer_set_rate(timer, test_rate);
266 while (ast_tvdiff_ms((end = ast_tvnow()), start) < 1000) {
268 struct pollfd pfd = {
269 .fd = ast_timer_fd(timer),
270 .events = POLLIN | POLLPRI,
273 res = ast_poll(&pfd, 1, 100);
277 if (ast_timer_ack(timer, 1) < 0) {
278 ast_cli(a->fd, "Timer failed to acknowledge.\n");
279 ast_timer_close(timer);
283 ast_cli(a->fd, "poll() timed out! This is bad.\n");
284 } else if (errno != EAGAIN && errno != EINTR) {
285 ast_cli(a->fd, "poll() returned error: %s\n", strerror(errno));
289 ast_timer_close(timer);
292 ast_cli(a->fd, "It has been %" PRIi64 " milliseconds, and we got %d timer ticks\n",
293 ast_tvdiff_ms(end, start), count);
298 static struct ast_cli_entry cli_timing[] = {
299 AST_CLI_DEFINE(timing_test, "Run a timing test"),
302 static void timing_shutdown(void)
304 ast_cli_unregister_multiple(cli_timing, ARRAY_LEN(cli_timing));
306 ast_heap_destroy(timing_interfaces);
307 timing_interfaces = NULL;
310 int ast_timing_init(void)
312 if (!(timing_interfaces = ast_heap_create(2, timing_holder_cmp, 0))) {
316 ast_register_atexit(timing_shutdown);
318 return ast_cli_register_multiple(cli_timing, ARRAY_LEN(cli_timing));