blob: 217f6521c85801943bb37b69074a2654f7f52a2f [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/*
2 * (C) 2008,2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +02003 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
Harald Welteec8b4502010-02-20 20:34:29 +01004 * All Rights Reserved
5 *
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +02006 * Authors: Holger Hans Peter Freyther <zecke@selfish.org>
7 * Harald Welte <laforge@gnumonks.org>
8 * Pablo Neira Ayuso <pablo@gnumonks.org>
9 *
Harald Welteec8b4502010-02-20 20:34:29 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020026/* These store the amount of time that we wait until next timer expires. */
27static struct timeval nearest;
28static struct timeval *nearest_p;
29
Harald Welteba6988b2011-08-17 12:46:48 +020030/*! \addtogroup timer
31 * @{
32 */
33
34/*! \file timer.c
35 */
36
Harald Welteec8b4502010-02-20 20:34:29 +010037#include <assert.h>
38#include <string.h>
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020039#include <limits.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010040#include <osmocom/core/timer.h>
Sylvain Munaut07f11032011-10-21 21:55:29 +020041#include <osmocom/core/timer_compat.h>
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020042#include <osmocom/core/linuxlist.h>
Harald Welteec8b4502010-02-20 20:34:29 +010043
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020044static struct rb_root timer_root = RB_ROOT;
Harald Welteec8b4502010-02-20 20:34:29 +010045
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020046static void __add_timer(struct osmo_timer_list *timer)
47{
48 struct rb_node **new = &(timer_root.rb_node);
49 struct rb_node *parent = NULL;
Harald Welteec8b4502010-02-20 20:34:29 +010050
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020051 while (*new) {
52 struct osmo_timer_list *this;
Harald Welteec8b4502010-02-20 20:34:29 +010053
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020054 this = container_of(*new, struct osmo_timer_list, node);
55
56 parent = *new;
57 if (timercmp(&timer->timeout, &this->timeout, <))
58 new = &((*new)->rb_left);
59 else
60 new = &((*new)->rb_right);
61 }
62
63 rb_link_node(&timer->node, parent, new);
64 rb_insert_color(&timer->node, &timer_root);
65}
Harald Welteba6988b2011-08-17 12:46:48 +020066
67/*! \brief add a new timer to the timer management
68 * \param[in] timer the timer that should be added
69 */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020070void osmo_timer_add(struct osmo_timer_list *timer)
Harald Welteec8b4502010-02-20 20:34:29 +010071{
Harald Welteec8b4502010-02-20 20:34:29 +010072 timer->active = 1;
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020073 INIT_LLIST_HEAD(&timer->list);
74 __add_timer(timer);
Harald Welteec8b4502010-02-20 20:34:29 +010075}
76
Harald Welteba6988b2011-08-17 12:46:48 +020077/*! \brief schedule a timer at a given future relative time
78 * \param[in] timer the to-be-added timer
79 * \param[in] seconds number of seconds from now
80 * \param[in] microseconds number of microseconds from now
81 *
82 * This function can be used to (re-)schedule a given timer at a
83 * specified number of seconds+microseconds in the future. It will
84 * internally add it to the timer management data structures, thus
85 * osmo_timer_add() is automatically called.
86 */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020087void
88osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds)
Harald Welteec8b4502010-02-20 20:34:29 +010089{
90 struct timeval current_time;
91
92 gettimeofday(&current_time, NULL);
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020093 timer->timeout.tv_sec = seconds;
94 timer->timeout.tv_usec = microseconds;
95 timeradd(&timer->timeout, &current_time, &timer->timeout);
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020096 osmo_timer_add(timer);
Harald Welteec8b4502010-02-20 20:34:29 +010097}
98
Harald Welteba6988b2011-08-17 12:46:48 +020099/*! \brief delete a timer from timer management
100 * \param[in] timer the to-be-deleted timer
101 *
102 * This function can be used to delete a previously added/scheduled
103 * timer from the timer management code.
104 */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200105void osmo_timer_del(struct osmo_timer_list *timer)
Harald Welteec8b4502010-02-20 20:34:29 +0100106{
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200107 if (timer->active) {
Harald Welteec8b4502010-02-20 20:34:29 +0100108 timer->active = 0;
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200109 rb_erase(&timer->node, &timer_root);
110 /* make sure this is not already scheduled for removal. */
111 if (!llist_empty(&timer->list))
112 llist_del_init(&timer->list);
Harald Welteec8b4502010-02-20 20:34:29 +0100113 }
114}
115
Harald Welteba6988b2011-08-17 12:46:48 +0200116/*! \brief check if given timer is still pending
117 * \param[in] timer the to-be-checked timer
118 * \return 1 if pending, 0 otherwise
119 *
120 * This function can be used to determine whether a given timer
121 * has alredy expired (returns 0) or is still pending (returns 1)
122 */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200123int osmo_timer_pending(struct osmo_timer_list *timer)
Harald Welteec8b4502010-02-20 20:34:29 +0100124{
125 return timer->active;
126}
127
128/*
129 * if we have a nearest time return the delta between the current
130 * time and the time of the nearest timer.
131 * If the nearest timer timed out return NULL and then we will
132 * dispatch everything after the select
133 */
Harald Welte7e820202011-07-16 10:15:16 +0200134struct timeval *osmo_timers_nearest(void)
Harald Welteec8b4502010-02-20 20:34:29 +0100135{
Sylvain Munaut0061ded2011-10-18 20:11:03 +0200136 /* nearest_p is exactly what we need already: NULL if nothing is
137 * waiting, {0,0} if we must dispatch immediately, and the correct
138 * delay if we need to wait */
139 return nearest_p;
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200140}
Harald Welteec8b4502010-02-20 20:34:29 +0100141
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200142static void update_nearest(struct timeval *cand, struct timeval *current)
143{
144 if (cand->tv_sec != LONG_MAX) {
145 if (timercmp(cand, current, >))
146 timersub(cand, current, &nearest);
147 else {
148 /* loop again inmediately */
149 nearest.tv_sec = 0;
150 nearest.tv_usec = 0;
151 }
152 nearest_p = &nearest;
Harald Welteec8b4502010-02-20 20:34:29 +0100153 } else {
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200154 nearest_p = NULL;
Harald Welteec8b4502010-02-20 20:34:29 +0100155 }
Harald Welteec8b4502010-02-20 20:34:29 +0100156}
157
158/*
159 * Find the nearest time and update s_nearest_time
160 */
Harald Welte7e820202011-07-16 10:15:16 +0200161void osmo_timers_prepare(void)
Harald Welteec8b4502010-02-20 20:34:29 +0100162{
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200163 struct rb_node *node;
164 struct timeval current;
Harald Welteec8b4502010-02-20 20:34:29 +0100165
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200166 gettimeofday(&current, NULL);
167
168 node = rb_first(&timer_root);
169 if (node) {
170 struct osmo_timer_list *this;
171 this = container_of(node, struct osmo_timer_list, node);
172 update_nearest(&this->timeout, &current);
Harald Welteec8b4502010-02-20 20:34:29 +0100173 } else {
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200174 nearest_p = NULL;
Harald Welteec8b4502010-02-20 20:34:29 +0100175 }
176}
177
178/*
179 * fire all timers... and remove them
180 */
Harald Welte7e820202011-07-16 10:15:16 +0200181int osmo_timers_update(void)
Harald Welteec8b4502010-02-20 20:34:29 +0100182{
183 struct timeval current_time;
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200184 struct rb_node *node;
185 struct llist_head timer_eviction_list;
186 struct osmo_timer_list *this;
Harald Welteec8b4502010-02-20 20:34:29 +0100187 int work = 0;
188
189 gettimeofday(&current_time, NULL);
190
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200191 INIT_LLIST_HEAD(&timer_eviction_list);
192 for (node = rb_first(&timer_root); node; node = rb_next(node)) {
193 this = container_of(node, struct osmo_timer_list, node);
194
195 if (timercmp(&this->timeout, &current_time, >))
196 break;
197
198 llist_add(&this->list, &timer_eviction_list);
199 }
200
Harald Welteec8b4502010-02-20 20:34:29 +0100201 /*
202 * The callbacks might mess with our list and in this case
203 * even llist_for_each_entry_safe is not safe to use. To allow
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200204 * osmo_timer_del to be called from within the callback we need
205 * to restart the iteration for each element scheduled for removal.
Harald Welteec8b4502010-02-20 20:34:29 +0100206 *
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200207 * The problematic scenario is the following: Given two timers A
208 * and B that have expired at the same time. Thus, they are both
209 * in the eviction list in this order: A, then B. If we remove
210 * timer B from the A's callback, we continue with B in the next
211 * iteration step, leading to an access-after-release.
Harald Welteec8b4502010-02-20 20:34:29 +0100212 */
Harald Welteec8b4502010-02-20 20:34:29 +0100213restart:
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200214 llist_for_each_entry(this, &timer_eviction_list, list) {
215 osmo_timer_del(this);
216 this->cb(this->data);
217 work = 1;
218 goto restart;
Harald Welteec8b4502010-02-20 20:34:29 +0100219 }
220
221 return work;
222}
223
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200224int osmo_timers_check(void)
Harald Welteec8b4502010-02-20 20:34:29 +0100225{
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200226 struct rb_node *node;
Harald Welteec8b4502010-02-20 20:34:29 +0100227 int i = 0;
228
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200229 for (node = rb_first(&timer_root); node; node = rb_next(node)) {
Harald Welteec8b4502010-02-20 20:34:29 +0100230 i++;
231 }
232 return i;
233}
Harald Welteba6988b2011-08-17 12:46:48 +0200234
235/*! }@ */