Fri Jan 18 19:56:24 2013

Asterisk developer's documentation


res_timing_pthread.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2008, Digium, Inc.
00005  *
00006  * Russell Bryant <russell@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*!
00020  * \file
00021  * \author Russell Bryant <russell@digium.com>
00022  *
00023  * \brief pthread timing interface
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>extended</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 375893 $");
00033 
00034 #include <math.h>
00035 #include <sys/select.h>
00036 
00037 #include "asterisk/module.h"
00038 #include "asterisk/timing.h"
00039 #include "asterisk/utils.h"
00040 #include "asterisk/astobj2.h"
00041 #include "asterisk/time.h"
00042 #include "asterisk/lock.h"
00043 #include "asterisk/poll-compat.h"
00044 
00045 static void *timing_funcs_handle;
00046 
00047 static int pthread_timer_open(void);
00048 static void pthread_timer_close(int handle);
00049 static int pthread_timer_set_rate(int handle, unsigned int rate);
00050 static int pthread_timer_ack(int handle, unsigned int quantity);
00051 static int pthread_timer_enable_continuous(int handle);
00052 static int pthread_timer_disable_continuous(int handle);
00053 static enum ast_timer_event pthread_timer_get_event(int handle);
00054 static unsigned int pthread_timer_get_max_rate(int handle);
00055 
00056 static struct ast_timing_interface pthread_timing = {
00057    .name = "pthread",
00058    .priority = 0, /* use this as a last resort */
00059    .timer_open = pthread_timer_open,
00060    .timer_close = pthread_timer_close,
00061    .timer_set_rate = pthread_timer_set_rate,
00062    .timer_ack = pthread_timer_ack,
00063    .timer_enable_continuous = pthread_timer_enable_continuous,
00064    .timer_disable_continuous = pthread_timer_disable_continuous,
00065    .timer_get_event = pthread_timer_get_event,
00066    .timer_get_max_rate = pthread_timer_get_max_rate,
00067 };
00068 
00069 /* 1 tick / 10 ms */
00070 #define MAX_RATE 100
00071 
00072 static struct ao2_container *pthread_timers;
00073 #define PTHREAD_TIMER_BUCKETS 563
00074 
00075 enum {
00076    PIPE_READ =  0,
00077    PIPE_WRITE = 1
00078 };
00079 
00080 enum pthread_timer_state {
00081    TIMER_STATE_IDLE,
00082    TIMER_STATE_TICKING,
00083 };
00084 
00085 struct pthread_timer {
00086    int pipe[2];
00087    enum pthread_timer_state state;
00088    unsigned int rate;
00089    /*! Interval in ms for current rate */
00090    unsigned int interval;
00091    unsigned int tick_count;
00092    unsigned int pending_ticks;
00093    struct timeval start;
00094    unsigned int continuous:1;
00095 };
00096 
00097 static void pthread_timer_destructor(void *obj);
00098 static struct pthread_timer *find_timer(int handle, int unlinkobj);
00099 static void write_byte(struct pthread_timer *timer);
00100 static int read_pipe(struct pthread_timer *timer, unsigned int num);
00101 
00102 /*!
00103  * \brief Data for the timing thread
00104  */
00105 static struct {
00106    pthread_t thread;
00107    ast_mutex_t lock;
00108    ast_cond_t cond;
00109    unsigned int stop:1;
00110 } timing_thread;
00111 
00112 static int pthread_timer_open(void)
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 }
00145 
00146 static void pthread_timer_close(int handle)
00147 {
00148    struct pthread_timer *timer;
00149 
00150    if (!(timer = find_timer(handle, 1))) {
00151       return;
00152    }
00153 
00154    ao2_ref(timer, -1);
00155 }
00156 
00157 static int pthread_timer_set_rate(int handle, unsigned int rate)
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 }
00192 
00193 static int pthread_timer_ack(int handle, unsigned int quantity)
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 }
00212 
00213 static int pthread_timer_enable_continuous(int handle)
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 }
00233 
00234 static int pthread_timer_disable_continuous(int handle)
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 }
00259 
00260 static enum ast_timer_event pthread_timer_get_event(int handle)
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 }
00279 
00280 static unsigned int pthread_timer_get_max_rate(int handle)
00281 {
00282    return MAX_RATE;
00283 }
00284 
00285 static struct pthread_timer *find_timer(int handle, int unlinkobj)
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 }
00304 
00305 static void pthread_timer_destructor(void *obj)
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 }
00319 
00320 /*!
00321  * \note only PIPE_READ is guaranteed valid
00322  */
00323 static int pthread_timer_hash(const void *obj, const int flags)
00324 {
00325    const struct pthread_timer *timer = obj;
00326 
00327    return timer->pipe[PIPE_READ];
00328 }
00329 
00330 /*!
00331  * \note only PIPE_READ is guaranteed valid
00332  */
00333 static int pthread_timer_cmp(void *obj, void *arg, int flags)
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 }
00339 
00340 /*!
00341  * \retval 0 no timer tick needed
00342  * \retval non-zero write to the timing pipe needed
00343  */
00344 static int check_timer(struct pthread_timer *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 }
00365 
00366 /*!
00367  * \internal
00368  * \pre timer is locked
00369  * \retval 0 if nothing to read or read success
00370  * \retval -1 on error
00371  */
00372 static int read_pipe(struct pthread_timer *timer, unsigned int quantity)
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 }
00423 
00424 /*!
00425  * \internal
00426  * \pre timer is locked
00427  */
00428 static void write_byte(struct pthread_timer *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 }
00444 
00445 static int run_timer(void *obj, void *arg, int flags)
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 }
00461 
00462 static void *do_timing(void *arg)
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 }
00489 
00490 static int init_timing_thread(void)
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 }
00502 
00503 static int load_module(void)
00504 {
00505    if (!(pthread_timers = ao2_container_alloc(PTHREAD_TIMER_BUCKETS,
00506       pthread_timer_hash, pthread_timer_cmp))) {
00507       return AST_MODULE_LOAD_DECLINE;
00508    }
00509 
00510    if (init_timing_thread()) {
00511       ao2_ref(pthread_timers, -1);
00512       pthread_timers = NULL;
00513       return AST_MODULE_LOAD_DECLINE;
00514    }
00515 
00516    return (timing_funcs_handle = ast_register_timing_interface(&pthread_timing)) ?
00517       AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
00518 }
00519 
00520 static int unload_module(void)
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 }
00537 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "pthread Timing Interface",
00538       .load = load_module,
00539       .unload = unload_module,
00540       .load_pri = AST_MODPRI_TIMING,
00541       );

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