Fri Jan 18 19:59:21 2013

Asterisk developer's documentation


res_timing_pthread.c File Reference

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"
Include dependency graph for res_timing_pthread.c:

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.

Detailed Description

pthread timing interface

Author:
Russell Bryant <russell@digium.com>

Definition in file res_timing_pthread.c.


Define Documentation

#define MAX_RATE   100

Definition at line 70 of file res_timing_pthread.c.

Referenced by pthread_timer_get_max_rate(), and pthread_timer_set_rate().

#define PTHREAD_TIMER_BUCKETS   563

Definition at line 73 of file res_timing_pthread.c.

Referenced by load_module().


Enumeration Type Documentation

anonymous enum
Enumerator:
PIPE_READ 
PIPE_WRITE 

Definition at line 75 of file res_timing_pthread.c.

00075      {
00076    PIPE_READ =  0,
00077    PIPE_WRITE = 1
00078 };

Enumerator:
TIMER_STATE_IDLE 
TIMER_STATE_TICKING 

Definition at line 80 of file res_timing_pthread.c.

00080                          {
00081    TIMER_STATE_IDLE,
00082    TIMER_STATE_TICKING,
00083 };


Function Documentation

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]
Return values:
0 no timer tick needed
non-zero write to the timing pipe needed

Definition at line 344 of file res_timing_pthread.c.

References ast_tvdiff_ms(), ast_tvnow(), pthread_timer::interval, pthread_timer::start, pthread_timer::state, pthread_timer::tick_count, and TIMER_STATE_IDLE.

Referenced by run_timer().

00345 {
00346    struct timeval now;
00347 
00348    if (timer->state == TIMER_STATE_IDLE) {
00349       return 0;
00350    }
00351 
00352    now = ast_tvnow();
00353 
00354    if (timer->tick_count < (ast_tvdiff_ms(now, timer->start) / timer->interval)) {
00355       timer->tick_count++;
00356       if (!timer->tick_count) {
00357          /* Handle overflow. */
00358          timer->start = now;
00359       }
00360       return 1;
00361    }
00362 
00363    return 0;
00364 }

static void* do_timing ( void *  arg  )  [static]

Definition at line 462 of file res_timing_pthread.c.

References ao2_callback, ao2_container_count(), ast_cond_timedwait, ast_cond_wait, ast_mutex_lock, ast_mutex_unlock, ast_tv(), ast_tvadd(), ast_tvnow(), OBJ_NODATA, run_timer(), and timing_thread.

Referenced by init_timing_thread().

00463 {
00464    struct timeval next_wakeup = ast_tvnow();
00465 
00466    while (!timing_thread.stop) {
00467       struct timespec ts = { 0, };
00468 
00469       ao2_callback(pthread_timers, OBJ_NODATA, run_timer, NULL);
00470 
00471       next_wakeup = ast_tvadd(next_wakeup, ast_tv(0, 5000));
00472 
00473       ts.tv_sec = next_wakeup.tv_sec;
00474       ts.tv_nsec = next_wakeup.tv_usec * 1000;
00475 
00476       ast_mutex_lock(&timing_thread.lock);
00477       if (!timing_thread.stop) {
00478          if (ao2_container_count(pthread_timers)) {
00479             ast_cond_timedwait(&timing_thread.cond, &timing_thread.lock, &ts);
00480          } else {
00481             ast_cond_wait(&timing_thread.cond, &timing_thread.lock);
00482          }
00483       }
00484       ast_mutex_unlock(&timing_thread.lock);
00485    }
00486 
00487    return NULL;
00488 }

static struct pthread_timer * find_timer ( int  handle,
int  unlinkobj 
) [static, read]

Definition at line 285 of file res_timing_pthread.c.

References ao2_find, ast_assert, OBJ_POINTER, OBJ_UNLINK, pthread_timer::pipe, PIPE_READ, and timer.

Referenced by pthread_timer_ack(), pthread_timer_close(), pthread_timer_disable_continuous(), pthread_timer_enable_continuous(), pthread_timer_get_event(), and pthread_timer_set_rate().

00286 {
00287    struct pthread_timer *timer;
00288    struct pthread_timer tmp_timer;
00289    int flags = OBJ_POINTER;
00290 
00291    tmp_timer.pipe[PIPE_READ] = handle;
00292 
00293    if (unlinkobj) {
00294       flags |= OBJ_UNLINK;
00295    }
00296 
00297    if (!(timer = ao2_find(pthread_timers, &tmp_timer, flags))) {
00298       ast_assert(timer != NULL);
00299       return NULL;
00300    }
00301 
00302    return timer;
00303 }

static int init_timing_thread ( void   )  [static]

Definition at line 490 of file res_timing_pthread.c.

References ast_cond_init, ast_log(), ast_mutex_init, ast_pthread_create_background, do_timing(), LOG_ERROR, and timing_thread.

Referenced by load_module().

00491 {
00492    ast_mutex_init(&timing_thread.lock);
00493    ast_cond_init(&timing_thread.cond, NULL);
00494 
00495    if (ast_pthread_create_background(&timing_thread.thread, NULL, do_timing, NULL)) {
00496       ast_log(LOG_ERROR, "Unable to start timing thread.\n");
00497       return -1;
00498    }
00499 
00500    return 0;
00501 }

static int load_module ( void   )  [static]
static int pthread_timer_ack ( int  handle,
unsigned int  quantity 
) [static]

Definition at line 193 of file res_timing_pthread.c.

References ao2_lock, ao2_ref, ao2_unlock, ast_assert, find_timer(), read_pipe(), and timer.

00194 {
00195    struct pthread_timer *timer;
00196    int res;
00197 
00198    ast_assert(quantity > 0);
00199 
00200    if (!(timer = find_timer(handle, 0))) {
00201       return -1;
00202    }
00203 
00204    ao2_lock(timer);
00205    res = read_pipe(timer, quantity);
00206    ao2_unlock(timer);
00207 
00208    ao2_ref(timer, -1);
00209 
00210    return res;
00211 }

static void pthread_timer_close ( int  handle  )  [static]

Definition at line 146 of file res_timing_pthread.c.

References ao2_ref, find_timer(), and timer.

00147 {
00148    struct pthread_timer *timer;
00149 
00150    if (!(timer = find_timer(handle, 1))) {
00151       return;
00152    }
00153 
00154    ao2_ref(timer, -1);
00155 }

static int pthread_timer_cmp ( void *  obj,
void *  arg,
int  flags 
) [static]
Note:
only PIPE_READ is guaranteed valid

Definition at line 333 of file res_timing_pthread.c.

References CMP_MATCH, CMP_STOP, pthread_timer::pipe, and PIPE_READ.

Referenced by load_module().

00334 {
00335    struct pthread_timer *timer1 = obj, *timer2 = arg;
00336 
00337    return (timer1->pipe[PIPE_READ] == timer2->pipe[PIPE_READ]) ? CMP_MATCH | CMP_STOP : 0;
00338 }

static void pthread_timer_destructor ( void *  obj  )  [static]

Definition at line 305 of file res_timing_pthread.c.

References pthread_timer::pipe, PIPE_READ, PIPE_WRITE, and timer.

Referenced by pthread_timer_open().

00306 {
00307    struct pthread_timer *timer = obj;
00308 
00309    if (timer->pipe[PIPE_READ] > -1) {
00310       close(timer->pipe[PIPE_READ]);
00311       timer->pipe[PIPE_READ] = -1;
00312    }
00313 
00314    if (timer->pipe[PIPE_WRITE] > -1) {
00315       close(timer->pipe[PIPE_WRITE]);
00316       timer->pipe[PIPE_WRITE] = -1;
00317    }
00318 }

static int pthread_timer_disable_continuous ( int  handle  )  [static]

Definition at line 234 of file res_timing_pthread.c.

References ao2_lock, ao2_ref, ao2_unlock, pthread_timer::continuous, errno, find_timer(), read_pipe(), and timer.

00235 {
00236    struct pthread_timer *timer;
00237 
00238    if (!(timer = find_timer(handle, 0))) {
00239       errno = EINVAL;
00240       return -1;
00241    }
00242 
00243    ao2_lock(timer);
00244    if (timer->continuous) {
00245       timer->continuous = 0;
00246       if (read_pipe(timer, 1) != 0) {
00247          /* Let the errno from read_pipe propagate up */
00248          ao2_unlock(timer);
00249          ao2_ref(timer, -1);
00250          return -1;
00251       }
00252    }
00253    ao2_unlock(timer);
00254 
00255    ao2_ref(timer, -1);
00256 
00257    return 0;
00258 }

static int pthread_timer_enable_continuous ( int  handle  )  [static]

Definition at line 213 of file res_timing_pthread.c.

References ao2_lock, ao2_ref, ao2_unlock, pthread_timer::continuous, errno, find_timer(), timer, and write_byte().

00214 {
00215    struct pthread_timer *timer;
00216 
00217    if (!(timer = find_timer(handle, 0))) {
00218       errno = EINVAL;
00219       return -1;
00220    }
00221 
00222    ao2_lock(timer);
00223    if (!timer->continuous) {
00224       timer->continuous = 1;
00225       write_byte(timer);
00226    }
00227    ao2_unlock(timer);
00228 
00229    ao2_ref(timer, -1);
00230 
00231    return 0;
00232 }

static enum ast_timer_event pthread_timer_get_event ( int  handle  )  [static]

Definition at line 260 of file res_timing_pthread.c.

References ao2_lock, ao2_ref, ao2_unlock, AST_TIMING_EVENT_CONTINUOUS, AST_TIMING_EVENT_EXPIRED, pthread_timer::continuous, find_timer(), pthread_timer::pending_ticks, and timer.

00261 {
00262    struct pthread_timer *timer;
00263    enum ast_timer_event res = AST_TIMING_EVENT_EXPIRED;
00264 
00265    if (!(timer = find_timer(handle, 0))) {
00266       return res;
00267    }
00268 
00269    ao2_lock(timer);
00270    if (timer->continuous && timer->pending_ticks == 1) {
00271       res = AST_TIMING_EVENT_CONTINUOUS;
00272    }
00273    ao2_unlock(timer);
00274 
00275    ao2_ref(timer, -1);
00276 
00277    return res;
00278 }

static unsigned int pthread_timer_get_max_rate ( int  handle  )  [static]

Definition at line 280 of file res_timing_pthread.c.

References MAX_RATE.

00281 {
00282    return MAX_RATE;
00283 }

static int pthread_timer_hash ( const void *  obj,
const int  flags 
) [static]
Note:
only PIPE_READ is guaranteed valid

Definition at line 323 of file res_timing_pthread.c.

References pthread_timer::pipe, PIPE_READ, and timer.

Referenced by load_module().

00324 {
00325    const struct pthread_timer *timer = obj;
00326 
00327    return timer->pipe[PIPE_READ];
00328 }

static int pthread_timer_open ( void   )  [static]

Definition at line 112 of file res_timing_pthread.c.

References ao2_alloc, ao2_container_count(), ao2_link, ao2_lock, ao2_ref, ao2_unlock, ast_cond_signal, ast_mutex_lock, ast_mutex_unlock, errno, pthread_timer::pipe, PIPE_READ, PIPE_WRITE, pthread_timer_destructor(), pthread_timer::state, timer, TIMER_STATE_IDLE, and timing_thread.

00113 {
00114    struct pthread_timer *timer;
00115    int fd;
00116 
00117    if (!(timer = ao2_alloc(sizeof(*timer), pthread_timer_destructor))) {
00118       errno = ENOMEM;
00119       return -1;
00120    }
00121 
00122    timer->pipe[PIPE_READ] = timer->pipe[PIPE_WRITE] = -1;
00123    timer->state = TIMER_STATE_IDLE;
00124 
00125    if (pipe(timer->pipe)) {
00126       ao2_ref(timer, -1);
00127       return -1;
00128    }
00129 
00130    ao2_lock(pthread_timers);
00131    if (!ao2_container_count(pthread_timers)) {
00132       ast_mutex_lock(&timing_thread.lock);
00133       ast_cond_signal(&timing_thread.cond);
00134       ast_mutex_unlock(&timing_thread.lock);
00135    }
00136    ao2_link(pthread_timers, timer);
00137    ao2_unlock(pthread_timers);
00138 
00139    fd = timer->pipe[PIPE_READ];
00140 
00141    ao2_ref(timer, -1);
00142 
00143    return fd;
00144 }

static int pthread_timer_set_rate ( int  handle,
unsigned int  rate 
) [static]

Definition at line 157 of file res_timing_pthread.c.

References ao2_lock, ao2_ref, ao2_unlock, ast_log(), ast_tv(), ast_tvnow(), errno, find_timer(), pthread_timer::interval, LOG_ERROR, MAX_RATE, pthread_timer::rate, pthread_timer::start, pthread_timer::state, pthread_timer::tick_count, timer, TIMER_STATE_IDLE, and TIMER_STATE_TICKING.

00158 {
00159    struct pthread_timer *timer;
00160 
00161    if (!(timer = find_timer(handle, 0))) {
00162       errno = EINVAL;
00163       return -1;
00164    }
00165 
00166    if (rate > MAX_RATE) {
00167       ast_log(LOG_ERROR, "res_timing_pthread only supports timers at a "
00168             "max rate of %d / sec\n", MAX_RATE);
00169       errno = EINVAL;
00170       return -1;
00171    }
00172 
00173    ao2_lock(timer);
00174 
00175    if ((timer->rate = rate)) {
00176       timer->interval = roundf(1000.0 / ((float) rate));
00177       timer->start = ast_tvnow();
00178       timer->state = TIMER_STATE_TICKING;
00179    } else {
00180       timer->interval = 0;
00181       timer->start = ast_tv(0, 0);
00182       timer->state = TIMER_STATE_IDLE;
00183    }
00184    timer->tick_count = 0;
00185 
00186    ao2_unlock(timer);
00187 
00188    ao2_ref(timer, -1);
00189 
00190    return 0;
00191 }

static int read_pipe ( struct pthread_timer *  timer,
unsigned int  num 
) [static]

Definition at line 372 of file res_timing_pthread.c.

References ast_assert, ast_debug, ast_log(), ast_poll, pthread_timer::continuous, errno, LOG_ERROR, pthread_timer::pending_ticks, pthread_timer::pipe, and PIPE_READ.

Referenced by pthread_timer_ack(), and pthread_timer_disable_continuous().

00373 {
00374    int rd_fd = timer->pipe[PIPE_READ];
00375    int pending_ticks = timer->pending_ticks;
00376 
00377    ast_assert(quantity);
00378 
00379    if (timer->continuous && pending_ticks) {
00380       pending_ticks--;
00381    }
00382 
00383    if (quantity > pending_ticks) {
00384       quantity = pending_ticks;
00385    }
00386 
00387    if (!quantity) {
00388       return 0;
00389    }
00390 
00391    do {
00392       unsigned char buf[1024];
00393       ssize_t res;
00394       struct pollfd pfd = {
00395          .fd = rd_fd,
00396          .events = POLLIN,
00397       };
00398 
00399       if (ast_poll(&pfd, 1, 0) != 1) {
00400          ast_debug(1, "Reading not available on timing pipe, "
00401                "quantity: %u\n", quantity);
00402          return -1;
00403       }
00404 
00405       res = read(rd_fd, buf,
00406          (quantity < sizeof(buf)) ? quantity : sizeof(buf));
00407 
00408       if (res == -1) {
00409          if (errno == EAGAIN) {
00410             continue;
00411          }
00412          ast_log(LOG_ERROR, "read failed on timing pipe: %s\n",
00413                strerror(errno));
00414          return -1;
00415       }
00416 
00417       quantity -= res;
00418       timer->pending_ticks -= res;
00419    } while (quantity);
00420 
00421    return 0;
00422 }

static int run_timer ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 445 of file res_timing_pthread.c.

References ao2_lock, ao2_unlock, check_timer(), pthread_timer::state, timer, TIMER_STATE_IDLE, and write_byte().

Referenced by do_timing().

00446 {
00447    struct pthread_timer *timer = obj;
00448 
00449    if (timer->state == TIMER_STATE_IDLE) {
00450       return 0;
00451    }
00452 
00453    ao2_lock(timer);
00454    if (check_timer(timer)) {
00455       write_byte(timer);
00456    }
00457    ao2_unlock(timer);
00458 
00459    return 0;
00460 }

static int unload_module ( void   )  [static]

Definition at line 520 of file res_timing_pthread.c.

References ao2_ref, ast_cond_signal, ast_mutex_lock, ast_mutex_unlock, ast_unregister_timing_interface(), timing_funcs_handle, and timing_thread.

00521 {
00522    int res;
00523 
00524    ast_mutex_lock(&timing_thread.lock);
00525    timing_thread.stop = 1;
00526    ast_cond_signal(&timing_thread.cond);
00527    ast_mutex_unlock(&timing_thread.lock);
00528    pthread_join(timing_thread.thread, NULL);
00529 
00530    if (!(res = ast_unregister_timing_interface(timing_funcs_handle))) {
00531       ao2_ref(pthread_timers, -1);
00532       pthread_timers = NULL;
00533    }
00534 
00535    return res;
00536 }

static void write_byte ( struct pthread_timer *  timer  )  [static]

Definition at line 428 of file res_timing_pthread.c.

References ast_log(), errno, LOG_ERROR, pthread_timer::pending_ticks, pthread_timer::pipe, and PIPE_WRITE.

Referenced by pthread_timer_enable_continuous(), and run_timer().

00429 {
00430    ssize_t res;
00431    unsigned char x = 42;
00432 
00433    do {
00434       res = write(timer->pipe[PIPE_WRITE], &x, 1);
00435    } while (res == -1 && errno == EAGAIN);
00436 
00437    if (res == -1) {
00438       ast_log(LOG_ERROR, "Error writing to timing pipe: %s\n",
00439             strerror(errno));
00440    } else {
00441       timer->pending_ticks++;
00442    }
00443 }


Variable Documentation

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.

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.

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 do_timing(), init_timing_thread(), pthread_timer_open(), and unload_module().


Generated on 18 Jan 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1