Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Functions | Variables
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"

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)
 
int ast_timer_ack (const struct ast_timer *handle, unsigned int quantity)
 Acknowledge a timer event. More...
 
void ast_timer_close (struct ast_timer *handle)
 Close an opened timing handle. More...
 
int ast_timer_disable_continuous (const struct ast_timer *handle)
 Disable continuous mode. More...
 
int ast_timer_enable_continuous (const struct ast_timer *handle)
 Enable continuous mode. More...
 
int ast_timer_fd (const struct ast_timer *handle)
 Get a poll()-able file descriptor for a timer. More...
 
enum ast_timer_event ast_timer_get_event (const struct ast_timer *handle)
 Retrieve timing event. More...
 
unsigned int ast_timer_get_max_rate (const struct ast_timer *handle)
 Get maximum rate supported for a timer. More...
 
const char * ast_timer_get_name (const struct ast_timer *handle)
 Get name of timer in use. More...
 
struct ast_timerast_timer_open (void)
 Open a timer. More...
 
int ast_timer_set_rate (const struct ast_timer *handle, unsigned int rate)
 Set the timing tick rate. More...
 
int ast_timing_init (void)
 
int ast_unregister_timing_interface (void *handle)
 Unregister a previously registered timing interface. More...
 
static int timing_holder_cmp (void *_h1, void *_h2)
 
static void timing_shutdown (void)
 
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 kpfle.nosp@m.ming.nosp@m.@digi.nosp@m.um.c.nosp@m.om
Russell Bryant russe.nosp@m.ll@d.nosp@m.igium.nosp@m..com

Definition in file timing.c.

Function Documentation

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

Acknowledge a timer event.

Parameters
handletimer handle returned from timer_open()
quantitynumber of timer events to acknowledge
Note
This function should only be called if timer_get_event() returned AST_TIMING_EVENT_EXPIRED.
Return values
-1failure, with errno set
0success
Since
10.5.2

Definition at line 171 of file timing.c.

Referenced by __ast_read(), snoop_read(), softmix_mixing_loop(), and spandsp_fax_read().

172 {
173  return handle->holder->iface->timer_ack(handle->data, quantity);
174 }
void ast_timer_close ( struct ast_timer handle)

Close an opened timing handle.

Parameters
handletimer handle returned from timer_open()
Since
1.6.1

Definition at line 154 of file timing.c.

References ast_module_unref.

Referenced by ast_channel_destructor(), load_module(), and snoop_destroy().

155 {
156  handle->holder->iface->timer_close(handle->data);
157  ast_module_unref(handle->holder->mod);
158  ast_free(handle);
159 }
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:483
int ast_timer_disable_continuous ( const struct ast_timer handle)

Disable continuous mode.

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

Definition at line 181 of file timing.c.

Referenced by __ast_read().

182 {
183  return handle->holder->iface->timer_disable_continuous(handle->data);
184 }
int ast_timer_enable_continuous ( const struct ast_timer handle)

Enable continuous mode.

Parameters
handletimer 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
-1failure, with errno set
0success
Since
1.6.1

Definition at line 176 of file timing.c.

177 {
178  return handle->holder->iface->timer_enable_continuous(handle->data);
179 }
int ast_timer_fd ( const struct ast_timer handle)

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

Parameters
handletimer 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 161 of file timing.c.

Referenced by __ast_channel_alloc_ap(), softmix_mixing_loop(), spandsp_fax_new(), and stasis_app_control_snoop().

162 {
163  return handle->holder->iface->timer_fd(handle->data);
164 }
enum ast_timer_event ast_timer_get_event ( const struct ast_timer handle)

Retrieve timing event.

Parameters
handletimer 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 186 of file timing.c.

Referenced by __ast_read().

187 {
188  return handle->holder->iface->timer_get_event(handle->data);
189 }
unsigned int ast_timer_get_max_rate ( const struct ast_timer handle)

Get maximum rate supported for a timer.

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

Definition at line 191 of file timing.c.

192 {
193  return handle->holder->iface->timer_get_max_rate(handle->data);
194 }
const char* ast_timer_get_name ( const struct ast_timer handle)

Get name of timer in use.

Parameters
handletimer handle returned by timer_open()
Returns
name of timer
Since
1.6.2

Definition at line 196 of file timing.c.

Referenced by __ast_channel_alloc_ap().

197 {
198  return handle->holder->iface->name;
199 }
struct ast_timer* ast_timer_open ( void  )

Open a timer.

Return values
NULLon error, with errno set
non-NULLtimer handle on success
Since
1.6.1

Definition at line 122 of file timing.c.

References ast_calloc, ast_heap_peek(), ast_module_running_ref, and ast_module_unref.

Referenced by __ast_channel_alloc_ap(), load_module(), softmix_bridge_create(), spandsp_fax_new(), and stasis_app_control_snoop().

123 {
124  void *data = NULL;
125  struct timing_holder *h;
126  struct ast_timer *t = NULL;
127  int idx = 1;
128 
129  ast_heap_rdlock(timing_interfaces);
130 
131  while ((h = ast_heap_peek(timing_interfaces, idx))) {
132  if (ast_module_running_ref(h->mod)) {
133  data = h->iface->timer_open();
134  break;
135  }
136  idx++;
137  }
138 
139  if (data) {
140  if (!(t = ast_calloc(1, sizeof(*t)))) {
141  h->iface->timer_close(data);
142  ast_module_unref(h->mod);
143  } else {
144  t->data = data;
145  t->holder = h;
146  }
147  }
148 
149  ast_heap_unlock(timing_interfaces);
150 
151  return t;
152 }
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:483
#define ast_module_running_ref(mod)
Hold a reference to the module if it is running.
Definition: module.h:469
void * ast_heap_peek(struct ast_heap *h, unsigned int index)
Peek at an element on a heap.
Definition: heap.c:267
int ast_timer_set_rate ( const struct ast_timer handle,
unsigned int  rate 
)

Set the timing tick rate.

Parameters
handletimer handle returned from timer_open()
rateticks 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
-1error, with errno set
0success
Since
1.6.1

Definition at line 166 of file timing.c.

Referenced by __ast_read(), ast_deactivate_generator(), load_module(), set_config(), softmix_mixing_loop(), and stasis_app_control_snoop().

167 {
168  return handle->holder->iface->timer_set_rate(handle->data, rate);
169 }
int ast_timing_init ( void  )

Provided by timing.c

Definition at line 289 of file timing.c.

References ast_cli_register_multiple, ast_heap_create, and ast_register_cleanup().

290 {
291  if (!(timing_interfaces = ast_heap_create(2, timing_holder_cmp, 0))) {
292  return -1;
293  }
294 
295  ast_register_cleanup(timing_shutdown);
296 
297  return ast_cli_register_multiple(cli_timing, ARRAY_LEN(cli_timing));
298 }
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
#define ast_heap_create(init_height, cmp_fn, index_offset)
Create a max heap.
Definition: heap.h:100
int ast_unregister_timing_interface ( void *  handle)

Unregister a previously registered timing interface.

Parameters
handleThe handle returned from a prior successful call to ast_register_timing_interface().
Return values
0success
non-zerofailure
Since
1.6.1

Definition at line 104 of file timing.c.

References ast_heap_remove().

105 {
106  struct timing_holder *h = handle;
107  int res = -1;
108 
109  ast_heap_wrlock(timing_interfaces);
110  h = ast_heap_remove(timing_interfaces, h);
111  ast_heap_unlock(timing_interfaces);
112 
113  if (h) {
114  ast_free(h);
115  h = NULL;
116  res = 0;
117  }
118 
119  return res;
120 }
void * ast_heap_remove(struct ast_heap *h, void *elm)
Remove a specific element from a heap.
Definition: heap.c:251

Variable Documentation

struct ast_cli_entry cli_timing[]
static
Initial value:
= {
{ .handler = timing_test , .summary = "Run a timing test" ,},
}

Definition at line 277 of file timing.c.