pthread timing interface More...
#include "asterisk.h"#include <math.h>#include <sys/select.h>#include "asterisk/module.h"#include "asterisk/timing.h"#include "asterisk/utils.h"#include "asterisk/astobj2.h"#include "asterisk/time.h"#include "asterisk/lock.h"#include "asterisk/poll-compat.h"
Go to the source code of this file.
Data Structures | |
| struct | pthread_timer |
Defines | |
| #define | MAX_RATE 100 |
| #define | PTHREAD_TIMER_BUCKETS 563 |
Enumerations | |
| enum | { PIPE_READ = 0, PIPE_WRITE = 1 } |
| enum | pthread_timer_state { TIMER_STATE_IDLE, TIMER_STATE_TICKING } |
Functions | |
| static void | __reg_module (void) |
| static void | __unreg_module (void) |
| ASTERISK_FILE_VERSION (__FILE__,"$Revision: 375893 $") | |
| static int | check_timer (struct pthread_timer *timer) |
| static void * | do_timing (void *arg) |
| static struct pthread_timer * | find_timer (int handle, int unlinkobj) |
| static int | init_timing_thread (void) |
| static int | load_module (void) |
| static int | pthread_timer_ack (int handle, unsigned int quantity) |
| static void | pthread_timer_close (int handle) |
| static int | pthread_timer_cmp (void *obj, void *arg, int flags) |
| static void | pthread_timer_destructor (void *obj) |
| static int | pthread_timer_disable_continuous (int handle) |
| static int | pthread_timer_enable_continuous (int handle) |
| static enum ast_timer_event | pthread_timer_get_event (int handle) |
| static unsigned int | pthread_timer_get_max_rate (int handle) |
| static int | pthread_timer_hash (const void *obj, const int flags) |
| static int | pthread_timer_open (void) |
| static int | pthread_timer_set_rate (int handle, unsigned int rate) |
| static int | read_pipe (struct pthread_timer *timer, unsigned int num) |
| static int | run_timer (void *obj, void *arg, int flags) |
| static int | unload_module (void) |
| static void | write_byte (struct pthread_timer *timer) |
Variables | |
| static struct ast_module_info | __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "pthread Timing Interface" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "95089850e3c922fa176f9bd274fd8109" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_TIMING, } |
| static struct ast_module_info * | ast_module_info = &__mod_info |
| static struct ao2_container * | pthread_timers |
| static struct ast_timing_interface | pthread_timing |
| static void * | timing_funcs_handle |
| struct { | |
| ast_cond_t cond | |
| ast_mutex_t lock | |
| unsigned int stop:1 | |
| pthread_t thread | |
| } | timing_thread |
| Data for the timing thread. | |
pthread timing interface
Definition in file res_timing_pthread.c.
| #define MAX_RATE 100 |
Definition at line 70 of file res_timing_pthread.c.
Referenced by pthread_timer_set_rate(), and pthread_timer_get_max_rate().
| #define PTHREAD_TIMER_BUCKETS 563 |
Definition at line 73 of file res_timing_pthread.c.
Referenced by load_module().
| anonymous enum |
Definition at line 75 of file res_timing_pthread.c.
{
PIPE_READ = 0,
PIPE_WRITE = 1
};
| enum pthread_timer_state |
Definition at line 80 of file res_timing_pthread.c.
| static void __reg_module | ( | void | ) | [static] |
Definition at line 541 of file res_timing_pthread.c.
| static void __unreg_module | ( | void | ) | [static] |
Definition at line 541 of file res_timing_pthread.c.
| ASTERISK_FILE_VERSION | ( | __FILE__ | , |
| "$Revision: 375893 $" | |||
| ) |
| static int check_timer | ( | struct pthread_timer * | timer | ) | [static] |
| 0 | no timer tick needed |
| non-zero | write to the timing pipe needed |
Definition at line 344 of file res_timing_pthread.c.
References pthread_timer::state, TIMER_STATE_IDLE, ast_tvnow(), pthread_timer::tick_count, ast_tvdiff_ms(), pthread_timer::start, and pthread_timer::interval.
Referenced by run_timer().
{
struct timeval now;
if (timer->state == TIMER_STATE_IDLE) {
return 0;
}
now = ast_tvnow();
if (timer->tick_count < (ast_tvdiff_ms(now, timer->start) / timer->interval)) {
timer->tick_count++;
if (!timer->tick_count) {
/* Handle overflow. */
timer->start = now;
}
return 1;
}
return 0;
}
| static void* do_timing | ( | void * | arg | ) | [static] |
Definition at line 462 of file res_timing_pthread.c.
References ast_tvnow(), timing_thread, ao2_callback, OBJ_NODATA, run_timer(), ast_tvadd(), ast_tv(), ast_mutex_lock, ao2_container_count(), ast_cond_timedwait, ast_cond_wait, and ast_mutex_unlock.
Referenced by init_timing_thread().
{
struct timeval next_wakeup = ast_tvnow();
while (!timing_thread.stop) {
struct timespec ts = { 0, };
ao2_callback(pthread_timers, OBJ_NODATA, run_timer, NULL);
next_wakeup = ast_tvadd(next_wakeup, ast_tv(0, 5000));
ts.tv_sec = next_wakeup.tv_sec;
ts.tv_nsec = next_wakeup.tv_usec * 1000;
ast_mutex_lock(&timing_thread.lock);
if (!timing_thread.stop) {
if (ao2_container_count(pthread_timers)) {
ast_cond_timedwait(&timing_thread.cond, &timing_thread.lock, &ts);
} else {
ast_cond_wait(&timing_thread.cond, &timing_thread.lock);
}
}
ast_mutex_unlock(&timing_thread.lock);
}
return NULL;
}
| static struct pthread_timer * find_timer | ( | int | handle, |
| int | unlinkobj | ||
| ) | [static, read] |
Definition at line 285 of file res_timing_pthread.c.
References timer, OBJ_POINTER, pthread_timer::pipe, PIPE_READ, OBJ_UNLINK, ao2_find, and ast_assert.
Referenced by pthread_timer_close(), pthread_timer_set_rate(), pthread_timer_ack(), pthread_timer_enable_continuous(), pthread_timer_disable_continuous(), and pthread_timer_get_event().
{
struct pthread_timer *timer;
struct pthread_timer tmp_timer;
int flags = OBJ_POINTER;
tmp_timer.pipe[PIPE_READ] = handle;
if (unlinkobj) {
flags |= OBJ_UNLINK;
}
if (!(timer = ao2_find(pthread_timers, &tmp_timer, flags))) {
ast_assert(timer != NULL);
return NULL;
}
return timer;
}
| static int init_timing_thread | ( | void | ) | [static] |
Definition at line 490 of file res_timing_pthread.c.
References ast_mutex_init, timing_thread, ast_cond_init, ast_pthread_create_background, do_timing(), ast_log(), and LOG_ERROR.
Referenced by load_module().
{
ast_mutex_init(&timing_thread.lock);
ast_cond_init(&timing_thread.cond, NULL);
if (ast_pthread_create_background(&timing_thread.thread, NULL, do_timing, NULL)) {
ast_log(LOG_ERROR, "Unable to start timing thread.\n");
return -1;
}
return 0;
}
| static int load_module | ( | void | ) | [static] |
Definition at line 503 of file res_timing_pthread.c.
References ao2_container_alloc, PTHREAD_TIMER_BUCKETS, pthread_timer_hash(), pthread_timer_cmp(), AST_MODULE_LOAD_DECLINE, init_timing_thread(), ao2_ref, timing_funcs_handle, ast_register_timing_interface, and AST_MODULE_LOAD_SUCCESS.
{
if (!(pthread_timers = ao2_container_alloc(PTHREAD_TIMER_BUCKETS,
pthread_timer_hash, pthread_timer_cmp))) {
return AST_MODULE_LOAD_DECLINE;
}
if (init_timing_thread()) {
ao2_ref(pthread_timers, -1);
pthread_timers = NULL;
return AST_MODULE_LOAD_DECLINE;
}
return (timing_funcs_handle = ast_register_timing_interface(&pthread_timing)) ?
AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
}
| static int pthread_timer_ack | ( | int | handle, |
| unsigned int | quantity | ||
| ) | [static] |
Definition at line 193 of file res_timing_pthread.c.
References timer, ast_assert, find_timer(), ao2_lock, read_pipe(), ao2_unlock, and ao2_ref.
{
struct pthread_timer *timer;
int res;
ast_assert(quantity > 0);
if (!(timer = find_timer(handle, 0))) {
return -1;
}
ao2_lock(timer);
res = read_pipe(timer, quantity);
ao2_unlock(timer);
ao2_ref(timer, -1);
return res;
}
| static void pthread_timer_close | ( | int | handle | ) | [static] |
Definition at line 146 of file res_timing_pthread.c.
References timer, find_timer(), and ao2_ref.
{
struct pthread_timer *timer;
if (!(timer = find_timer(handle, 1))) {
return;
}
ao2_ref(timer, -1);
}
| static int pthread_timer_cmp | ( | void * | obj, |
| void * | arg, | ||
| int | flags | ||
| ) | [static] |
Definition at line 333 of file res_timing_pthread.c.
References pthread_timer::pipe, PIPE_READ, CMP_MATCH, and CMP_STOP.
Referenced by load_module().
| static void pthread_timer_destructor | ( | void * | obj | ) | [static] |
Definition at line 305 of file res_timing_pthread.c.
References timer, pthread_timer::pipe, PIPE_READ, and PIPE_WRITE.
Referenced by pthread_timer_open().
{
struct pthread_timer *timer = obj;
if (timer->pipe[PIPE_READ] > -1) {
close(timer->pipe[PIPE_READ]);
timer->pipe[PIPE_READ] = -1;
}
if (timer->pipe[PIPE_WRITE] > -1) {
close(timer->pipe[PIPE_WRITE]);
timer->pipe[PIPE_WRITE] = -1;
}
}
| static int pthread_timer_disable_continuous | ( | int | handle | ) | [static] |
Definition at line 234 of file res_timing_pthread.c.
References timer, find_timer(), errno, ao2_lock, pthread_timer::continuous, read_pipe(), ao2_unlock, and ao2_ref.
{
struct pthread_timer *timer;
if (!(timer = find_timer(handle, 0))) {
errno = EINVAL;
return -1;
}
ao2_lock(timer);
if (timer->continuous) {
timer->continuous = 0;
if (read_pipe(timer, 1) != 0) {
/* Let the errno from read_pipe propagate up */
ao2_unlock(timer);
ao2_ref(timer, -1);
return -1;
}
}
ao2_unlock(timer);
ao2_ref(timer, -1);
return 0;
}
| static int pthread_timer_enable_continuous | ( | int | handle | ) | [static] |
Definition at line 213 of file res_timing_pthread.c.
References timer, find_timer(), errno, ao2_lock, pthread_timer::continuous, write_byte(), ao2_unlock, and ao2_ref.
{
struct pthread_timer *timer;
if (!(timer = find_timer(handle, 0))) {
errno = EINVAL;
return -1;
}
ao2_lock(timer);
if (!timer->continuous) {
timer->continuous = 1;
write_byte(timer);
}
ao2_unlock(timer);
ao2_ref(timer, -1);
return 0;
}
| static enum ast_timer_event pthread_timer_get_event | ( | int | handle | ) | [static] |
Definition at line 260 of file res_timing_pthread.c.
References timer, AST_TIMING_EVENT_EXPIRED, find_timer(), ao2_lock, pthread_timer::continuous, pthread_timer::pending_ticks, AST_TIMING_EVENT_CONTINUOUS, ao2_unlock, and ao2_ref.
{
struct pthread_timer *timer;
enum ast_timer_event res = AST_TIMING_EVENT_EXPIRED;
if (!(timer = find_timer(handle, 0))) {
return res;
}
ao2_lock(timer);
if (timer->continuous && timer->pending_ticks == 1) {
res = AST_TIMING_EVENT_CONTINUOUS;
}
ao2_unlock(timer);
ao2_ref(timer, -1);
return res;
}
| static unsigned int pthread_timer_get_max_rate | ( | int | handle | ) | [static] |
| static int pthread_timer_hash | ( | const void * | obj, |
| const int | flags | ||
| ) | [static] |
Definition at line 323 of file res_timing_pthread.c.
References timer, pthread_timer::pipe, and PIPE_READ.
Referenced by load_module().
{
const struct pthread_timer *timer = obj;
return timer->pipe[PIPE_READ];
}
| static int pthread_timer_open | ( | void | ) | [static] |
Definition at line 112 of file res_timing_pthread.c.
References timer, ao2_alloc, pthread_timer_destructor(), errno, pthread_timer::pipe, PIPE_READ, PIPE_WRITE, pthread_timer::state, TIMER_STATE_IDLE, ao2_ref, ao2_lock, ao2_container_count(), ast_mutex_lock, timing_thread, ast_cond_signal, ast_mutex_unlock, ao2_link, and ao2_unlock.
{
struct pthread_timer *timer;
int fd;
if (!(timer = ao2_alloc(sizeof(*timer), pthread_timer_destructor))) {
errno = ENOMEM;
return -1;
}
timer->pipe[PIPE_READ] = timer->pipe[PIPE_WRITE] = -1;
timer->state = TIMER_STATE_IDLE;
if (pipe(timer->pipe)) {
ao2_ref(timer, -1);
return -1;
}
ao2_lock(pthread_timers);
if (!ao2_container_count(pthread_timers)) {
ast_mutex_lock(&timing_thread.lock);
ast_cond_signal(&timing_thread.cond);
ast_mutex_unlock(&timing_thread.lock);
}
ao2_link(pthread_timers, timer);
ao2_unlock(pthread_timers);
fd = timer->pipe[PIPE_READ];
ao2_ref(timer, -1);
return fd;
}
| static int pthread_timer_set_rate | ( | int | handle, |
| unsigned int | rate | ||
| ) | [static] |
Definition at line 157 of file res_timing_pthread.c.
References timer, find_timer(), errno, MAX_RATE, ast_log(), LOG_ERROR, ao2_lock, pthread_timer::rate, pthread_timer::interval, pthread_timer::start, ast_tvnow(), pthread_timer::state, TIMER_STATE_TICKING, ast_tv(), TIMER_STATE_IDLE, pthread_timer::tick_count, ao2_unlock, and ao2_ref.
{
struct pthread_timer *timer;
if (!(timer = find_timer(handle, 0))) {
errno = EINVAL;
return -1;
}
if (rate > MAX_RATE) {
ast_log(LOG_ERROR, "res_timing_pthread only supports timers at a "
"max rate of %d / sec\n", MAX_RATE);
errno = EINVAL;
return -1;
}
ao2_lock(timer);
if ((timer->rate = rate)) {
timer->interval = roundf(1000.0 / ((float) rate));
timer->start = ast_tvnow();
timer->state = TIMER_STATE_TICKING;
} else {
timer->interval = 0;
timer->start = ast_tv(0, 0);
timer->state = TIMER_STATE_IDLE;
}
timer->tick_count = 0;
ao2_unlock(timer);
ao2_ref(timer, -1);
return 0;
}
| static int read_pipe | ( | struct pthread_timer * | timer, |
| unsigned int | num | ||
| ) | [static] |
Definition at line 372 of file res_timing_pthread.c.
References pthread_timer::pipe, PIPE_READ, pthread_timer::pending_ticks, ast_assert, pthread_timer::continuous, ast_poll, ast_debug, errno, ast_log(), and LOG_ERROR.
Referenced by pthread_timer_ack(), and pthread_timer_disable_continuous().
{
int rd_fd = timer->pipe[PIPE_READ];
int pending_ticks = timer->pending_ticks;
ast_assert(quantity);
if (timer->continuous && pending_ticks) {
pending_ticks--;
}
if (quantity > pending_ticks) {
quantity = pending_ticks;
}
if (!quantity) {
return 0;
}
do {
unsigned char buf[1024];
ssize_t res;
struct pollfd pfd = {
.fd = rd_fd,
.events = POLLIN,
};
if (ast_poll(&pfd, 1, 0) != 1) {
ast_debug(1, "Reading not available on timing pipe, "
"quantity: %u\n", quantity);
return -1;
}
res = read(rd_fd, buf,
(quantity < sizeof(buf)) ? quantity : sizeof(buf));
if (res == -1) {
if (errno == EAGAIN) {
continue;
}
ast_log(LOG_ERROR, "read failed on timing pipe: %s\n",
strerror(errno));
return -1;
}
quantity -= res;
timer->pending_ticks -= res;
} while (quantity);
return 0;
}
| static int run_timer | ( | void * | obj, |
| void * | arg, | ||
| int | flags | ||
| ) | [static] |
Definition at line 445 of file res_timing_pthread.c.
References timer, pthread_timer::state, TIMER_STATE_IDLE, ao2_lock, check_timer(), write_byte(), and ao2_unlock.
Referenced by do_timing().
{
struct pthread_timer *timer = obj;
if (timer->state == TIMER_STATE_IDLE) {
return 0;
}
ao2_lock(timer);
if (check_timer(timer)) {
write_byte(timer);
}
ao2_unlock(timer);
return 0;
}
| static int unload_module | ( | void | ) | [static] |
Definition at line 520 of file res_timing_pthread.c.
References ast_mutex_lock, timing_thread, ast_cond_signal, ast_mutex_unlock, ast_unregister_timing_interface(), timing_funcs_handle, and ao2_ref.
{
int res;
ast_mutex_lock(&timing_thread.lock);
timing_thread.stop = 1;
ast_cond_signal(&timing_thread.cond);
ast_mutex_unlock(&timing_thread.lock);
pthread_join(timing_thread.thread, NULL);
if (!(res = ast_unregister_timing_interface(timing_funcs_handle))) {
ao2_ref(pthread_timers, -1);
pthread_timers = NULL;
}
return res;
}
| static void write_byte | ( | struct pthread_timer * | timer | ) | [static] |
Definition at line 428 of file res_timing_pthread.c.
References pthread_timer::pipe, PIPE_WRITE, errno, ast_log(), LOG_ERROR, and pthread_timer::pending_ticks.
Referenced by pthread_timer_enable_continuous(), and run_timer().
{
ssize_t res;
unsigned char x = 42;
do {
res = write(timer->pipe[PIPE_WRITE], &x, 1);
} while (res == -1 && errno == EAGAIN);
if (res == -1) {
ast_log(LOG_ERROR, "Error writing to timing pipe: %s\n",
strerror(errno));
} else {
timer->pending_ticks++;
}
}
struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "pthread Timing Interface" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "95089850e3c922fa176f9bd274fd8109" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_TIMING, } [static] |
Definition at line 541 of file res_timing_pthread.c.
struct ast_module_info* ast_module_info = &__mod_info [static] |
Definition at line 541 of file res_timing_pthread.c.
Definition at line 108 of file res_timing_pthread.c.
Definition at line 107 of file res_timing_pthread.c.
struct ao2_container* pthread_timers [static] |
Definition at line 72 of file res_timing_pthread.c.
struct ast_timing_interface pthread_timing [static] |
Definition at line 56 of file res_timing_pthread.c.
| unsigned int stop |
Definition at line 109 of file res_timing_pthread.c.
| pthread_t thread |
Definition at line 106 of file res_timing_pthread.c.
void* timing_funcs_handle [static] |
Definition at line 45 of file res_timing_pthread.c.
Referenced by load_module(), and unload_module().
struct { ... } timing_thread [static] |
Data for the timing thread.
Referenced by pthread_timer_open(), do_timing(), init_timing_thread(), and unload_module().