2010-02-20 19:34:29 +00:00
|
|
|
/*
|
|
|
|
* (C) 2008,2009 by Holger Hans Peter Freyther <zecke@selfish.org>
|
2011-09-26 09:45:03 +00:00
|
|
|
* (C) 2011 by Harald Welte <laforge@gnumonks.org>
|
2010-02-20 19:34:29 +00:00
|
|
|
* All Rights Reserved
|
|
|
|
*
|
2011-09-26 09:45:03 +00:00
|
|
|
* Authors: Holger Hans Peter Freyther <zecke@selfish.org>
|
|
|
|
* Harald Welte <laforge@gnumonks.org>
|
|
|
|
* Pablo Neira Ayuso <pablo@gnumonks.org>
|
|
|
|
*
|
2010-02-20 19:34:29 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-09-26 09:45:03 +00:00
|
|
|
/* These store the amount of time that we wait until next timer expires. */
|
|
|
|
static struct timeval nearest;
|
|
|
|
static struct timeval *nearest_p;
|
|
|
|
|
2011-08-17 10:46:48 +00:00
|
|
|
/*! \addtogroup timer
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file timer.c
|
|
|
|
*/
|
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2011-09-26 09:45:03 +00:00
|
|
|
#include <limits.h>
|
2011-03-22 15:36:13 +00:00
|
|
|
#include <osmocom/core/timer.h>
|
2011-09-26 09:45:03 +00:00
|
|
|
#include <osmocom/core/linuxlist.h>
|
|
|
|
|
|
|
|
static struct rb_root timer_root = RB_ROOT;
|
|
|
|
|
|
|
|
static void __add_timer(struct osmo_timer_list *timer)
|
|
|
|
{
|
|
|
|
struct rb_node **new = &(timer_root.rb_node);
|
|
|
|
struct rb_node *parent = NULL;
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-09-26 09:45:03 +00:00
|
|
|
while (*new) {
|
|
|
|
struct osmo_timer_list *this;
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-09-26 09:45:03 +00:00
|
|
|
this = container_of(*new, struct osmo_timer_list, node);
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-09-26 09:45:03 +00:00
|
|
|
parent = *new;
|
|
|
|
if (timercmp(&timer->timeout, &this->timeout, <))
|
|
|
|
new = &((*new)->rb_left);
|
|
|
|
else
|
|
|
|
new = &((*new)->rb_right);
|
|
|
|
}
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-09-26 09:45:03 +00:00
|
|
|
rb_link_node(&timer->node, parent, new);
|
|
|
|
rb_insert_color(&timer->node, &timer_root);
|
|
|
|
}
|
2011-08-17 10:46:48 +00:00
|
|
|
|
|
|
|
/*! \brief add a new timer to the timer management
|
|
|
|
* \param[in] timer the timer that should be added
|
|
|
|
*/
|
2011-05-07 10:42:28 +00:00
|
|
|
void osmo_timer_add(struct osmo_timer_list *timer)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
|
|
|
timer->active = 1;
|
2011-09-26 09:45:03 +00:00
|
|
|
INIT_LLIST_HEAD(&timer->list);
|
|
|
|
__add_timer(timer);
|
2010-02-20 19:34:29 +00:00
|
|
|
}
|
|
|
|
|
2011-08-17 10:46:48 +00:00
|
|
|
/*! \brief schedule a timer at a given future relative time
|
|
|
|
* \param[in] timer the to-be-added timer
|
|
|
|
* \param[in] seconds number of seconds from now
|
|
|
|
* \param[in] microseconds number of microseconds from now
|
|
|
|
*
|
|
|
|
* This function can be used to (re-)schedule a given timer at a
|
|
|
|
* specified number of seconds+microseconds in the future. It will
|
|
|
|
* internally add it to the timer management data structures, thus
|
|
|
|
* osmo_timer_add() is automatically called.
|
|
|
|
*/
|
2011-05-07 10:42:28 +00:00
|
|
|
void
|
|
|
|
osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
|
|
|
struct timeval current_time;
|
|
|
|
|
|
|
|
gettimeofday(¤t_time, NULL);
|
2011-09-26 09:45:03 +00:00
|
|
|
timer->timeout.tv_sec = seconds;
|
|
|
|
timer->timeout.tv_usec = microseconds;
|
|
|
|
timeradd(&timer->timeout, ¤t_time, &timer->timeout);
|
2011-05-07 10:42:28 +00:00
|
|
|
osmo_timer_add(timer);
|
2010-02-20 19:34:29 +00:00
|
|
|
}
|
|
|
|
|
2011-08-17 10:46:48 +00:00
|
|
|
/*! \brief delete a timer from timer management
|
|
|
|
* \param[in] timer the to-be-deleted timer
|
|
|
|
*
|
|
|
|
* This function can be used to delete a previously added/scheduled
|
|
|
|
* timer from the timer management code.
|
|
|
|
*/
|
2011-05-07 10:42:28 +00:00
|
|
|
void osmo_timer_del(struct osmo_timer_list *timer)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
2011-09-26 09:45:03 +00:00
|
|
|
if (timer->active) {
|
2010-02-20 19:34:29 +00:00
|
|
|
timer->active = 0;
|
2011-09-26 09:45:03 +00:00
|
|
|
rb_erase(&timer->node, &timer_root);
|
|
|
|
/* make sure this is not already scheduled for removal. */
|
|
|
|
if (!llist_empty(&timer->list))
|
|
|
|
llist_del_init(&timer->list);
|
2010-02-20 19:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-17 10:46:48 +00:00
|
|
|
/*! \brief check if given timer is still pending
|
|
|
|
* \param[in] timer the to-be-checked timer
|
|
|
|
* \return 1 if pending, 0 otherwise
|
|
|
|
*
|
|
|
|
* This function can be used to determine whether a given timer
|
|
|
|
* has alredy expired (returns 0) or is still pending (returns 1)
|
|
|
|
*/
|
2011-05-07 10:42:28 +00:00
|
|
|
int osmo_timer_pending(struct osmo_timer_list *timer)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
|
|
|
return timer->active;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if we have a nearest time return the delta between the current
|
|
|
|
* time and the time of the nearest timer.
|
|
|
|
* If the nearest timer timed out return NULL and then we will
|
|
|
|
* dispatch everything after the select
|
|
|
|
*/
|
2011-07-16 08:15:16 +00:00
|
|
|
struct timeval *osmo_timers_nearest(void)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
2011-10-17 21:03:57 +00:00
|
|
|
if (nearest_p != NULL)
|
2011-09-26 09:45:03 +00:00
|
|
|
return nearest_p;
|
|
|
|
else
|
2011-10-17 21:03:57 +00:00
|
|
|
return NULL;
|
2011-09-26 09:45:03 +00:00
|
|
|
}
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-09-26 09:45:03 +00:00
|
|
|
static void update_nearest(struct timeval *cand, struct timeval *current)
|
|
|
|
{
|
|
|
|
if (cand->tv_sec != LONG_MAX) {
|
|
|
|
if (timercmp(cand, current, >))
|
|
|
|
timersub(cand, current, &nearest);
|
|
|
|
else {
|
|
|
|
/* loop again inmediately */
|
|
|
|
nearest.tv_sec = 0;
|
|
|
|
nearest.tv_usec = 0;
|
|
|
|
}
|
|
|
|
nearest_p = &nearest;
|
2010-02-20 19:34:29 +00:00
|
|
|
} else {
|
2011-09-26 09:45:03 +00:00
|
|
|
nearest_p = NULL;
|
2010-02-20 19:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the nearest time and update s_nearest_time
|
|
|
|
*/
|
2011-07-16 08:15:16 +00:00
|
|
|
void osmo_timers_prepare(void)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
2011-09-26 09:45:03 +00:00
|
|
|
struct rb_node *node;
|
|
|
|
struct timeval current;
|
|
|
|
|
|
|
|
gettimeofday(¤t, NULL);
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-09-26 09:45:03 +00:00
|
|
|
node = rb_first(&timer_root);
|
|
|
|
if (node) {
|
|
|
|
struct osmo_timer_list *this;
|
|
|
|
this = container_of(node, struct osmo_timer_list, node);
|
|
|
|
update_nearest(&this->timeout, ¤t);
|
2010-02-20 19:34:29 +00:00
|
|
|
} else {
|
2011-09-26 09:45:03 +00:00
|
|
|
nearest_p = NULL;
|
2010-02-20 19:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fire all timers... and remove them
|
|
|
|
*/
|
2011-07-16 08:15:16 +00:00
|
|
|
int osmo_timers_update(void)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
|
|
|
struct timeval current_time;
|
2011-09-26 09:45:03 +00:00
|
|
|
struct rb_node *node;
|
|
|
|
struct llist_head timer_eviction_list;
|
|
|
|
struct osmo_timer_list *this;
|
2010-02-20 19:34:29 +00:00
|
|
|
int work = 0;
|
|
|
|
|
|
|
|
gettimeofday(¤t_time, NULL);
|
|
|
|
|
2011-09-26 09:45:03 +00:00
|
|
|
INIT_LLIST_HEAD(&timer_eviction_list);
|
|
|
|
for (node = rb_first(&timer_root); node; node = rb_next(node)) {
|
|
|
|
this = container_of(node, struct osmo_timer_list, node);
|
|
|
|
|
|
|
|
if (timercmp(&this->timeout, ¤t_time, >))
|
|
|
|
break;
|
|
|
|
|
|
|
|
llist_add(&this->list, &timer_eviction_list);
|
|
|
|
}
|
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
/*
|
|
|
|
* The callbacks might mess with our list and in this case
|
|
|
|
* even llist_for_each_entry_safe is not safe to use. To allow
|
2011-09-26 09:45:03 +00:00
|
|
|
* osmo_timer_del to be called from within the callback we need
|
|
|
|
* to restart the iteration for each element scheduled for removal.
|
2010-02-20 19:34:29 +00:00
|
|
|
*
|
2011-09-26 09:45:03 +00:00
|
|
|
* The problematic scenario is the following: Given two timers A
|
|
|
|
* and B that have expired at the same time. Thus, they are both
|
|
|
|
* in the eviction list in this order: A, then B. If we remove
|
|
|
|
* timer B from the A's callback, we continue with B in the next
|
|
|
|
* iteration step, leading to an access-after-release.
|
2010-02-20 19:34:29 +00:00
|
|
|
*/
|
|
|
|
restart:
|
2011-09-26 09:45:03 +00:00
|
|
|
llist_for_each_entry(this, &timer_eviction_list, list) {
|
|
|
|
osmo_timer_del(this);
|
|
|
|
this->cb(this->data);
|
|
|
|
work = 1;
|
|
|
|
goto restart;
|
2010-02-20 19:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return work;
|
|
|
|
}
|
|
|
|
|
2011-05-07 10:42:28 +00:00
|
|
|
int osmo_timers_check(void)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
2011-09-26 09:45:03 +00:00
|
|
|
struct rb_node *node;
|
2010-02-20 19:34:29 +00:00
|
|
|
int i = 0;
|
|
|
|
|
2011-09-26 09:45:03 +00:00
|
|
|
for (node = rb_first(&timer_root); node; node = rb_next(node)) {
|
2010-02-20 19:34:29 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2011-08-17 10:46:48 +00:00
|
|
|
|
|
|
|
/*! }@ */
|