Wed Apr 6 11:29:47 2011

Asterisk developer's documentation


res_timing_timerfd.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  * Mark Michelson <mmichelson@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 Mark Michelson <mmichelson@digium.com>
00022  *
00023  * \brief timerfd timing interface
00024  */
00025 
00026 /*** MODULEINFO
00027    <depend>timerfd</depend>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 #include <sys/timerfd.h>
00033 
00034 #include "asterisk/module.h"
00035 #include "asterisk/astobj2.h"
00036 #include "asterisk/timing.h"
00037 #include "asterisk/logger.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/time.h"
00040 
00041 static void *timing_funcs_handle;
00042 
00043 static int timerfd_timer_open(void);
00044 static void timerfd_timer_close(int handle);
00045 static int timerfd_timer_set_rate(int handle, unsigned int rate);
00046 static void timerfd_timer_ack(int handle, unsigned int quantity);
00047 static int timerfd_timer_enable_continuous(int handle);
00048 static int timerfd_timer_disable_continuous(int handle);
00049 static enum ast_timer_event timerfd_timer_get_event(int handle);
00050 static unsigned int timerfd_timer_get_max_rate(int handle);
00051 
00052 static struct ast_timing_interface timerfd_timing = {
00053    .name = "timerfd",
00054    .priority = 200,
00055    .timer_open = timerfd_timer_open,
00056    .timer_close = timerfd_timer_close,
00057    .timer_set_rate = timerfd_timer_set_rate,
00058    .timer_ack = timerfd_timer_ack,
00059    .timer_enable_continuous = timerfd_timer_enable_continuous,
00060    .timer_disable_continuous = timerfd_timer_disable_continuous,
00061    .timer_get_event = timerfd_timer_get_event,
00062    .timer_get_max_rate = timerfd_timer_get_max_rate,
00063 };
00064 
00065 static struct ao2_container *timerfd_timers;
00066 
00067 #define TIMERFD_TIMER_BUCKETS 563
00068 #define TIMERFD_MAX_RATE 1000
00069 
00070 struct timerfd_timer {
00071    int handle;
00072    struct itimerspec saved_timer;
00073    unsigned int is_continuous:1;
00074 };
00075 
00076 static int timerfd_timer_hash(const void *obj, const int flags)
00077 {
00078    const struct timerfd_timer *timer = obj;
00079 
00080    return timer->handle;
00081 }
00082 
00083 static int timerfd_timer_cmp(void *obj, void *args, int flags)
00084 {
00085    struct timerfd_timer *timer1 = obj, *timer2 = args;
00086    return timer1->handle == timer2->handle ? CMP_MATCH | CMP_STOP : 0;
00087 }
00088 
00089 static void timer_destroy(void *obj)
00090 {
00091    struct timerfd_timer *timer = obj;
00092    close(timer->handle);
00093 }
00094 
00095 static int timerfd_timer_open(void)
00096 {
00097    struct timerfd_timer *timer;
00098    int handle;
00099 
00100    if (!(timer = ao2_alloc(sizeof(*timer), timer_destroy))) {
00101       ast_log(LOG_ERROR, "Could not allocate memory for timerfd_timer structure\n");
00102       return -1;
00103    }
00104    if ((handle = timerfd_create(CLOCK_MONOTONIC, 0)) < 0) {
00105       ast_log(LOG_ERROR, "Failed to create timerfd timer: %s\n", strerror(errno));
00106       ao2_ref(timer, -1);
00107       return -1;
00108    }
00109 
00110    timer->handle = handle;
00111    ao2_link(timerfd_timers, timer);
00112    /* Get rid of the reference from the allocation */
00113    ao2_ref(timer, -1);
00114    return handle;
00115 }
00116 
00117 static void timerfd_timer_close(int handle)
00118 {
00119    struct timerfd_timer *our_timer, find_helper = {
00120       .handle = handle,
00121    };
00122 
00123    if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
00124       ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
00125       return;
00126    }
00127 
00128    ao2_unlink(timerfd_timers, our_timer);
00129    ao2_ref(our_timer, -1);
00130 }
00131 
00132 static int timerfd_timer_set_rate(int handle, unsigned int rate)
00133 {
00134    struct timerfd_timer *our_timer, find_helper = {
00135       .handle = handle,
00136    };
00137    int res = 0;
00138 
00139    if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
00140       ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
00141       return -1;
00142    }
00143    ao2_lock(our_timer);
00144 
00145    our_timer->saved_timer.it_value.tv_sec = 0;
00146    our_timer->saved_timer.it_value.tv_nsec = rate ? (long) (1000000000 / rate) : 0L;
00147    our_timer->saved_timer.it_interval.tv_sec = our_timer->saved_timer.it_value.tv_sec;
00148    our_timer->saved_timer.it_interval.tv_nsec = our_timer->saved_timer.it_value.tv_nsec;
00149 
00150    if (!our_timer->is_continuous) {
00151       res = timerfd_settime(handle, 0, &our_timer->saved_timer, NULL);
00152    }
00153 
00154    ao2_unlock(our_timer);
00155    ao2_ref(our_timer, -1);
00156 
00157    return res;
00158 }
00159 
00160 static void timerfd_timer_ack(int handle, unsigned int quantity)
00161 {
00162    uint64_t expirations;
00163    int read_result = 0;
00164 
00165    do {
00166       read_result = read(handle, &expirations, sizeof(expirations));
00167       if (read_result == -1) {
00168          if (errno == EINTR) {
00169             continue;
00170          } else {
00171             ast_log(LOG_ERROR, "Read error: %s\n", strerror(errno));
00172             break;
00173          }
00174       }
00175    } while (read_result != sizeof(expirations));
00176 
00177    if (expirations != quantity) {
00178       ast_debug(2, "Expected to acknowledge %u ticks but got %llu instead\n", quantity, (unsigned long long) expirations);
00179    }
00180 }
00181 
00182 static int timerfd_timer_enable_continuous(int handle)
00183 {
00184    int res;
00185    struct itimerspec continuous_timer = {
00186       .it_value.tv_nsec = 1L,
00187    };
00188    struct timerfd_timer *our_timer, find_helper = {
00189       .handle = handle,
00190    };
00191 
00192    if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
00193       ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
00194       return -1;
00195    }
00196    ao2_lock(our_timer);
00197 
00198    if (our_timer->is_continuous) {
00199       /*It's already in continous mode, no need to do
00200        * anything further
00201        */
00202       ao2_unlock(our_timer);
00203       ao2_ref(our_timer, -1);
00204       return 0;
00205    }
00206 
00207    res = timerfd_settime(handle, 0, &continuous_timer, &our_timer->saved_timer);
00208    our_timer->is_continuous = 1;
00209    ao2_unlock(our_timer);
00210    ao2_ref(our_timer, -1);
00211    return res;
00212 }
00213 
00214 static int timerfd_timer_disable_continuous(int handle)
00215 {
00216    int res;
00217    struct timerfd_timer *our_timer, find_helper = {
00218       .handle = handle,
00219    };
00220 
00221    if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
00222       ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
00223       return -1;
00224    }
00225    ao2_lock(our_timer);
00226 
00227    if(!our_timer->is_continuous) {
00228       /* No reason to do anything if we're not
00229        * in continuous mode
00230        */
00231       ao2_unlock(our_timer);
00232       ao2_ref(our_timer, -1);
00233       return 0;
00234    }
00235 
00236    res = timerfd_settime(handle, 0, &our_timer->saved_timer, NULL);
00237    our_timer->is_continuous = 0;
00238    memset(&our_timer->saved_timer, 0, sizeof(our_timer->saved_timer));
00239    ao2_unlock(our_timer);
00240    ao2_ref(our_timer, -1);
00241    return res;
00242 }
00243 
00244 static enum ast_timer_event timerfd_timer_get_event(int handle)
00245 {
00246    enum ast_timer_event res;
00247    struct timerfd_timer *our_timer, find_helper = {
00248       .handle = handle,
00249    };
00250 
00251    if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
00252       ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
00253       return -1;
00254    }
00255    ao2_lock(our_timer);
00256 
00257    if (our_timer->is_continuous) {
00258       res = AST_TIMING_EVENT_CONTINUOUS;
00259    } else {
00260       res = AST_TIMING_EVENT_EXPIRED;
00261    }
00262 
00263    ao2_unlock(our_timer);
00264    ao2_ref(our_timer, -1);
00265    return res;
00266 }
00267 
00268 static unsigned int timerfd_timer_get_max_rate(int handle)
00269 {
00270    return TIMERFD_MAX_RATE;
00271 }
00272 
00273 static int load_module(void)
00274 {
00275    int fd;
00276 
00277    /* Make sure we support the necessary clock type */
00278    if ((fd = timerfd_create(CLOCK_MONOTONIC, 0)) < 0) {
00279       ast_log(LOG_ERROR, "timerfd_create() not supported by the kernel.  Not loading.\n");
00280       return AST_MODULE_LOAD_DECLINE;
00281    }
00282 
00283    close(fd);
00284 
00285    if (!(timerfd_timers = ao2_container_alloc(TIMERFD_TIMER_BUCKETS, timerfd_timer_hash, timerfd_timer_cmp))) {
00286       return AST_MODULE_LOAD_DECLINE;
00287    }
00288 
00289    if (!(timing_funcs_handle = ast_register_timing_interface(&timerfd_timing))) {
00290       ao2_ref(timerfd_timers, -1);
00291       return AST_MODULE_LOAD_DECLINE;
00292    }
00293 
00294    return AST_MODULE_LOAD_SUCCESS;
00295 }
00296 
00297 static int unload_module(void)
00298 {
00299    int res;
00300 
00301    if (!(res = ast_unregister_timing_interface(timing_funcs_handle))) {
00302       ao2_ref(timerfd_timers, -1);
00303       timerfd_timers = NULL;
00304    }
00305 
00306    return res;
00307 }
00308 
00309 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Timerfd Timing Interface",
00310       .load = load_module,
00311       .unload = unload_module,
00312       .load_pri = AST_MODPRI_CHANNEL_DEPEND,
00313       );

Generated on Wed Apr 6 11:29:47 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7