Wed Oct 28 11:53:21 2009

Asterisk developer's documentation


timing.c File Reference

Timing source management. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/timing.h"
#include "asterisk/lock.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/time.h"
#include "asterisk/heap.h"
#include "asterisk/module.h"
#include "asterisk/poll-compat.h"

Include dependency graph for timing.c:

Go to the source code of this file.

Data Structures

struct  ast_timer
struct  timing_holder

Functions

void * _ast_register_timing_interface (struct ast_timing_interface *funcs, struct ast_module *mod)
void ast_timer_ack (const struct ast_timer *handle, unsigned int quantity)
 Acknowledge a timer event.
void ast_timer_close (struct ast_timer *handle)
 Close an opened timing handle.
int ast_timer_disable_continuous (const struct ast_timer *handle)
 Disable continuous mode.
int ast_timer_enable_continuous (const struct ast_timer *handle)
 Enable continuous mode.
int ast_timer_fd (const struct ast_timer *handle)
 Get a poll()-able file descriptor for a timer.
enum ast_timer_event ast_timer_get_event (const struct ast_timer *handle)
 Retrieve timing event.
unsigned int ast_timer_get_max_rate (const struct ast_timer *handle)
 Get maximum rate supported for a timer.
struct ast_timerast_timer_open (void)
 Open a timer.
int ast_timer_set_rate (const struct ast_timer *handle, unsigned int rate)
 Set the timing tick rate.
int ast_timing_init (void)
int ast_unregister_timing_interface (void *handle)
 Unregister a previously registered timing interface.
static int timing_holder_cmp (void *_h1, void *_h2)
static char * timing_test (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)

Variables

static struct ast_cli_entry cli_timing []
static struct ast_heaptiming_interfaces


Detailed Description

Timing source management.

Author:
Kevin P. Fleming <kpfleming@digium.com>

Russell Bryant <russell@digium.com>

Definition in file timing.c.


Function Documentation

void* _ast_register_timing_interface ( struct ast_timing_interface funcs,
struct ast_module mod 
)

Definition at line 71 of file timing.c.

References ast_calloc, ast_heap_push(), ast_heap_unlock(), ast_heap_wrlock(), timing_holder::iface, timing_holder::mod, ast_timing_interface::timer_ack, ast_timing_interface::timer_close, ast_timing_interface::timer_disable_continuous, ast_timing_interface::timer_enable_continuous, ast_timing_interface::timer_get_event, ast_timing_interface::timer_get_max_rate, ast_timing_interface::timer_open, and ast_timing_interface::timer_set_rate.

00073 {
00074    struct timing_holder *h;
00075 
00076    if (!funcs->timer_open ||
00077        !funcs->timer_close ||
00078        !funcs->timer_set_rate ||
00079        !funcs->timer_ack ||
00080        !funcs->timer_get_event ||
00081        !funcs->timer_get_max_rate ||
00082        !funcs->timer_enable_continuous ||
00083        !funcs->timer_disable_continuous) {
00084       return NULL;
00085    }
00086 
00087    if (!(h = ast_calloc(1, sizeof(*h)))) {
00088       return NULL;
00089    }
00090 
00091    h->iface = funcs;
00092    h->mod = mod;
00093 
00094    ast_heap_wrlock(timing_interfaces);
00095    ast_heap_push(timing_interfaces, h);
00096    ast_heap_unlock(timing_interfaces);
00097 
00098    return h;
00099 }

void ast_timer_ack ( const struct ast_timer handle,
unsigned int  quantity 
)

Acknowledge a timer event.

Parameters:
handle timer handle returned from timer_open()
quantity number of timer events to acknowledge
Note:
This function should only be called if timer_get_event() returned AST_TIMING_EVENT_EXPIRED.
Returns:
nothing
Since:
1.6.1

Definition at line 167 of file timing.c.

References ast_timer::fd, ast_timer::holder, timing_holder::iface, and ast_timing_interface::timer_ack.

Referenced by __ast_read(), timing_read(), and timing_test().

00168 {
00169    handle->holder->iface->timer_ack(handle->fd, quantity);
00170 }

void ast_timer_close ( struct ast_timer handle  ) 

Close an opened timing handle.

Parameters:
handle timer handle returned from timer_open()
Returns:
nothing
Since:
1.6.1

Definition at line 146 of file timing.c.

References ast_free, ast_module_unref(), ast_timer::fd, ast_timer::holder, timing_holder::iface, timing_holder::mod, and ast_timing_interface::timer_close.

Referenced by __ast_channel_alloc_ap(), __unload_module(), ast_channel_free(), load_module(), and timing_test().

00147 {
00148    handle->holder->iface->timer_close(handle->fd);
00149    ast_module_unref(handle->holder->mod);
00150    ast_free(handle);
00151 }

int ast_timer_disable_continuous ( const struct ast_timer handle  ) 

Disable continuous mode.

Parameters:
handle timer handle returned from timer_close()
Return values:
-1 failure, with errno set
0 success
Since:
1.6.1

Definition at line 181 of file timing.c.

References ast_timer::fd, ast_timer::holder, timing_holder::iface, and ast_timing_interface::timer_disable_continuous.

Referenced by __ast_read().

00182 {
00183    int res = -1;
00184 
00185    res = handle->holder->iface->timer_disable_continuous(handle->fd);
00186 
00187    return res;
00188 }

int ast_timer_enable_continuous ( const struct ast_timer handle  ) 

Enable continuous mode.

Parameters:
handle timer handle returned from timer_open()
Continuous mode causes poll() on the timer's fd to immediately return always until continuous mode is disabled.

Return values:
-1 failure, with errno set
0 success
Since:
1.6.1

Definition at line 172 of file timing.c.

References ast_timer::fd, ast_timer::holder, timing_holder::iface, and ast_timing_interface::timer_enable_continuous.

Referenced by __ast_queue_frame().

00173 {
00174    int res = -1;
00175 
00176    res = handle->holder->iface->timer_enable_continuous(handle->fd);
00177 
00178    return res;
00179 }

int ast_timer_fd ( const struct ast_timer handle  ) 

Get a poll()-able file descriptor for a timer.

Parameters:
handle timer handle returned from timer_open()
Returns:
file descriptor which can be used with poll() to wait for events
Since:
1.6.1

Definition at line 153 of file timing.c.

References ast_timer::fd.

Referenced by __ast_channel_alloc_ap(), network_thread(), and timing_test().

00154 {
00155    return handle->fd;
00156 }

enum ast_timer_event ast_timer_get_event ( const struct ast_timer handle  ) 

Retrieve timing event.

Parameters:
handle timer handle returned by timer_open()
After poll() indicates that there is input on the timer's fd, this will be called to find out what triggered it.

Returns:
which event triggered the timer
Since:
1.6.1

Definition at line 190 of file timing.c.

References ast_timer::fd, ast_timer::holder, timing_holder::iface, and ast_timing_interface::timer_get_event.

Referenced by __ast_read().

00191 {
00192    enum ast_timer_event res = -1;
00193 
00194    res = handle->holder->iface->timer_get_event(handle->fd);
00195 
00196    return res;
00197 }

unsigned int ast_timer_get_max_rate ( const struct ast_timer handle  ) 

Get maximum rate supported for a timer.

Parameters:
handle timer handle returned by timer_open()
Returns:
maximum rate supported by timer
Since:
1.6.1

Definition at line 199 of file timing.c.

References ast_timer::fd, ast_timer::holder, timing_holder::iface, and ast_timing_interface::timer_get_max_rate.

Referenced by ast_settimeout().

00200 {
00201    unsigned int res = 0;
00202 
00203    res = handle->holder->iface->timer_get_max_rate(handle->fd);
00204 
00205    return res;
00206 }

struct ast_timer* ast_timer_open ( void   )  [read]

Open a timer.

Return values:
NULL on error, with errno set
non-NULL timer handle on success
Since:
1.6.1

Definition at line 119 of file timing.c.

References ast_calloc, ast_heap_peek(), ast_heap_rdlock(), ast_heap_unlock(), ast_module_ref(), ast_timer::fd, ast_timer::holder, timing_holder::iface, timing_holder::mod, ast_timing_interface::timer_close, and ast_timing_interface::timer_open.

Referenced by __ast_channel_alloc_ap(), load_module(), and timing_test().

00120 {
00121    int fd = -1;
00122    struct timing_holder *h;
00123    struct ast_timer *t = NULL;
00124 
00125    ast_heap_rdlock(timing_interfaces);
00126 
00127    if ((h = ast_heap_peek(timing_interfaces, 1))) {
00128       fd = h->iface->timer_open();
00129       ast_module_ref(h->mod);
00130    }
00131 
00132    if (fd != -1) {
00133       if (!(t = ast_calloc(1, sizeof(*t)))) {
00134          h->iface->timer_close(fd);
00135       } else {
00136          t->fd = fd;
00137          t->holder = h;
00138       }
00139    }
00140 
00141    ast_heap_unlock(timing_interfaces);
00142 
00143    return t;
00144 }

int ast_timer_set_rate ( const struct ast_timer handle,
unsigned int  rate 
)

Set the timing tick rate.

Parameters:
handle timer handle returned from timer_open()
rate ticks per second, 0 turns the ticks off if needed
Use this function if you want the timer to show input at a certain rate. The other alternative use of a timer is the continuous mode.

Return values:
-1 error, with errno set
0 success
Since:
1.6.1

Definition at line 158 of file timing.c.

References ast_timer::fd, ast_timer::holder, timing_holder::iface, and ast_timing_interface::timer_set_rate.

Referenced by __ast_read(), ast_settimeout(), load_module(), and timing_test().

00159 {
00160    int res = -1;
00161 
00162    res = handle->holder->iface->timer_set_rate(handle->fd, rate);
00163 
00164    return res;
00165 }

int ast_timing_init ( void   ) 

Provided by timing.c

Definition at line 283 of file timing.c.

References ARRAY_LEN, ast_cli_register_multiple(), ast_heap_create(), and timing_holder_cmp().

Referenced by main().

00284 {
00285    if (!(timing_interfaces = ast_heap_create(2, timing_holder_cmp, 0))) {
00286       return -1;
00287    }
00288 
00289    return ast_cli_register_multiple(cli_timing, ARRAY_LEN(cli_timing));
00290 }

int ast_unregister_timing_interface ( void *  handle  ) 

Unregister a previously registered timing interface.

Parameters:
handle The handle returned from a prior successful call to ast_register_timing_interface().
Return values:
0 success
non-zero failure
Since:
1.6.1

Definition at line 101 of file timing.c.

References ast_free, ast_heap_remove(), ast_heap_unlock(), and ast_heap_wrlock().

Referenced by unload_module().

00102 {
00103    struct timing_holder *h = handle;
00104    int res = -1;
00105 
00106    ast_heap_wrlock(timing_interfaces);
00107    h = ast_heap_remove(timing_interfaces, h);
00108    ast_heap_unlock(timing_interfaces);
00109 
00110    if (h) {
00111       ast_free(h);
00112       h = NULL;
00113       res = 0;
00114    }
00115 
00116    return res;
00117 }

static int timing_holder_cmp ( void *  _h1,
void *  _h2 
) [static]

Definition at line 57 of file timing.c.

References timing_holder::iface, and ast_timing_interface::priority.

Referenced by ast_timing_init().

00058 {
00059    struct timing_holder *h1 = _h1;
00060    struct timing_holder *h2 = _h2;
00061 
00062    if (h1->iface->priority > h2->iface->priority) {
00063       return 1;
00064    } else if (h1->iface->priority == h2->iface->priority) {
00065       return 0;
00066    } else {
00067       return -1;
00068    }
00069 }

static char* timing_test ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 208 of file timing.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_poll, ast_timer_ack(), ast_timer_close(), ast_timer_fd(), ast_timer_open(), ast_timer_set_rate(), ast_tvdiff_ms(), ast_tvnow(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, errno, ast_cli_args::fd, ast_timer::holder, timing_holder::iface, ast_timing_interface::name, timer, and ast_cli_entry::usage.

00209 {
00210    struct ast_timer *timer;
00211    int count = 0;
00212    struct timeval start, end;
00213    unsigned int test_rate = 50;
00214 
00215    switch (cmd) {
00216    case CLI_INIT:
00217       e->command = "timing test";
00218       e->usage = "Usage: timing test <rate>\n"
00219                  "   Test a timer with a specified rate, 100/sec by default.\n"
00220                  "";
00221       return NULL;
00222    case CLI_GENERATE:
00223       return NULL;
00224    }
00225 
00226    if (a->argc != 2 && a->argc != 3) {
00227       return CLI_SHOWUSAGE;
00228    }
00229 
00230    if (a->argc == 3) {
00231       unsigned int rate;
00232       if (sscanf(a->argv[2], "%30u", &rate) == 1) {
00233          test_rate = rate;
00234       } else {
00235          ast_cli(a->fd, "Invalid rate '%s', using default of %u\n", a->argv[2], test_rate);  
00236       }
00237    }
00238 
00239    ast_cli(a->fd, "Attempting to test a timer with %u ticks per second.\n", test_rate);
00240 
00241    if (!(timer = ast_timer_open())) {
00242       ast_cli(a->fd, "Failed to open timing fd\n");
00243       return CLI_FAILURE;
00244    }
00245 
00246    ast_cli(a->fd, "Using the '%s' timing module for this test.\n", timer->holder->iface->name);
00247 
00248    start = ast_tvnow();
00249 
00250    ast_timer_set_rate(timer, test_rate);
00251 
00252    while (ast_tvdiff_ms((end = ast_tvnow()), start) < 1000) {
00253       int res;
00254       struct pollfd pfd = {
00255          .fd = ast_timer_fd(timer),
00256          .events = POLLIN | POLLPRI,
00257       };
00258 
00259       res = ast_poll(&pfd, 1, 100);
00260 
00261       if (res == 1) {
00262          count++;
00263          ast_timer_ack(timer, 1);
00264       } else if (!res) {
00265          ast_cli(a->fd, "poll() timed out!  This is bad.\n");
00266       } else if (errno != EAGAIN && errno != EINTR) {
00267          ast_cli(a->fd, "poll() returned error: %s\n", strerror(errno));
00268       }
00269    }
00270 
00271    ast_timer_close(timer);
00272 
00273    ast_cli(a->fd, "It has been %d milliseconds, and we got %d timer ticks\n", 
00274       ast_tvdiff_ms(end, start), count);
00275 
00276    return CLI_SUCCESS;
00277 }


Variable Documentation

struct ast_cli_entry cli_timing[] [static]

Initial value:

 {
   AST_CLI_DEFINE(timing_test, "Run a timing test"),
}

Definition at line 279 of file timing.c.

struct ast_heap* timing_interfaces [static]

Definition at line 50 of file timing.c.


Generated on Wed Oct 28 11:53:21 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6