blob: bf46c24a6d7b6568ffce368c74b0954431ccfdcf [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
Harald Welteba6988b2011-08-17 12:46:48 +020027/*! \addtogroup timer
28 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020029 * Osmocom timer abstraction; modelled after linux kernel timers
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020030 *
31 * \file timer.c */
Harald Welteba6988b2011-08-17 12:46:48 +020032
Harald Welteec8b4502010-02-20 20:34:29 +010033#include <assert.h>
34#include <string.h>
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020035#include <limits.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010036#include <osmocom/core/timer.h>
Sylvain Munaut07f11032011-10-21 21:55:29 +020037#include <osmocom/core/timer_compat.h>
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020038#include <osmocom/core/linuxlist.h>
Harald Welteec8b4502010-02-20 20:34:29 +010039
Holger Hans Peter Freyther3f838b72015-08-20 18:17:15 +000040/* These store the amount of time that we wait until next timer expires. */
41static struct timeval nearest;
42static struct timeval *nearest_p;
43
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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020067/*! set up timer callback and data
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +020068 * \param[in] timer the timer that should be added
69 * \param[in] callback function to be called when timer expires
70 * \param[in] pointer to data that passed to the callback function
71 */
72void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data),
73 void *data)
74{
75 timer->cb = cb;
76 timer->data = data;
77}
78
Neels Hofmeyr87e45502017-06-20 00:17:59 +020079/*! add a new timer to the timer management
Harald Welteba6988b2011-08-17 12:46:48 +020080 * \param[in] timer the timer that should be added
81 */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020082void osmo_timer_add(struct osmo_timer_list *timer)
Harald Welteec8b4502010-02-20 20:34:29 +010083{
Pablo Neira Ayusoa71b8ea2011-11-13 10:11:31 +010084 osmo_timer_del(timer);
85 timer->active = 1;
86 INIT_LLIST_HEAD(&timer->list);
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020087 __add_timer(timer);
Harald Welteec8b4502010-02-20 20:34:29 +010088}
89
Neels Hofmeyr87e45502017-06-20 00:17:59 +020090/*! schedule a timer at a given future relative time
Harald Welteba6988b2011-08-17 12:46:48 +020091 * \param[in] timer the to-be-added timer
92 * \param[in] seconds number of seconds from now
93 * \param[in] microseconds number of microseconds from now
94 *
95 * This function can be used to (re-)schedule a given timer at a
96 * specified number of seconds+microseconds in the future. It will
97 * internally add it to the timer management data structures, thus
98 * osmo_timer_add() is automatically called.
99 */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200100void
101osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds)
Harald Welteec8b4502010-02-20 20:34:29 +0100102{
103 struct timeval current_time;
104
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200105 osmo_gettimeofday(&current_time, NULL);
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200106 timer->timeout.tv_sec = seconds;
107 timer->timeout.tv_usec = microseconds;
108 timeradd(&timer->timeout, &current_time, &timer->timeout);
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200109 osmo_timer_add(timer);
Harald Welteec8b4502010-02-20 20:34:29 +0100110}
111
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200112/*! delete a timer from timer management
Harald Welteba6988b2011-08-17 12:46:48 +0200113 * \param[in] timer the to-be-deleted timer
114 *
115 * This function can be used to delete a previously added/scheduled
116 * timer from the timer management code.
117 */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200118void osmo_timer_del(struct osmo_timer_list *timer)
Harald Welteec8b4502010-02-20 20:34:29 +0100119{
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200120 if (timer->active) {
Harald Welteec8b4502010-02-20 20:34:29 +0100121 timer->active = 0;
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200122 rb_erase(&timer->node, &timer_root);
123 /* make sure this is not already scheduled for removal. */
124 if (!llist_empty(&timer->list))
125 llist_del_init(&timer->list);
Harald Welteec8b4502010-02-20 20:34:29 +0100126 }
127}
128
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129/*! check if given timer is still pending
Harald Welteba6988b2011-08-17 12:46:48 +0200130 * \param[in] timer the to-be-checked timer
131 * \return 1 if pending, 0 otherwise
132 *
133 * This function can be used to determine whether a given timer
134 * has alredy expired (returns 0) or is still pending (returns 1)
135 */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200136int osmo_timer_pending(struct osmo_timer_list *timer)
Harald Welteec8b4502010-02-20 20:34:29 +0100137{
138 return timer->active;
139}
140
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200141/*! compute the remaining time of a timer
Harald Weltee30b6ac2012-07-13 12:21:04 +0200142 * \param[in] timer the to-be-checked timer
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100143 * \param[in] now the current time (NULL if not known)
Harald Weltee30b6ac2012-07-13 12:21:04 +0200144 * \param[out] remaining remaining time until timer fires
145 * \return 0 if timer has not expired yet, -1 if it has
146 *
147 * This function can be used to determine the amount of time
148 * remaining until the expiration of the timer.
149 */
150int osmo_timer_remaining(const struct osmo_timer_list *timer,
151 const struct timeval *now,
152 struct timeval *remaining)
153{
154 struct timeval current_time;
155
Holger Hans Peter Freyther8e5435a2014-05-23 08:37:02 +0200156 if (!now)
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200157 osmo_gettimeofday(&current_time, NULL);
Holger Hans Peter Freyther8e5435a2014-05-23 08:37:02 +0200158 else
159 current_time = *now;
Harald Weltee30b6ac2012-07-13 12:21:04 +0200160
161 timersub(&timer->timeout, &current_time, remaining);
162
163 if (remaining->tv_sec < 0)
164 return -1;
165
166 return 0;
167}
168
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200169/*! Determine time between now and the nearest timer
Harald Weltede6e4982012-12-06 21:25:27 +0100170 * \returns pointer to timeval of nearest timer, NULL if there is none
171 *
Harald Welteec8b4502010-02-20 20:34:29 +0100172 * if we have a nearest time return the delta between the current
173 * time and the time of the nearest timer.
174 * If the nearest timer timed out return NULL and then we will
175 * dispatch everything after the select
176 */
Harald Welte7e820202011-07-16 10:15:16 +0200177struct timeval *osmo_timers_nearest(void)
Harald Welteec8b4502010-02-20 20:34:29 +0100178{
Sylvain Munaut0061ded2011-10-18 20:11:03 +0200179 /* nearest_p is exactly what we need already: NULL if nothing is
180 * waiting, {0,0} if we must dispatch immediately, and the correct
181 * delay if we need to wait */
182 return nearest_p;
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200183}
Harald Welteec8b4502010-02-20 20:34:29 +0100184
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200185static void update_nearest(struct timeval *cand, struct timeval *current)
186{
187 if (cand->tv_sec != LONG_MAX) {
188 if (timercmp(cand, current, >))
189 timersub(cand, current, &nearest);
190 else {
191 /* loop again inmediately */
Harald Welte00476022017-05-15 14:29:13 +0200192 timerclear(&nearest);
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200193 }
194 nearest_p = &nearest;
Harald Welteec8b4502010-02-20 20:34:29 +0100195 } else {
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200196 nearest_p = NULL;
Harald Welteec8b4502010-02-20 20:34:29 +0100197 }
Harald Welteec8b4502010-02-20 20:34:29 +0100198}
199
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200200/*! Find the nearest time and update nearest_p */
Harald Welte7e820202011-07-16 10:15:16 +0200201void osmo_timers_prepare(void)
Harald Welteec8b4502010-02-20 20:34:29 +0100202{
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200203 struct rb_node *node;
204 struct timeval current;
Harald Welteec8b4502010-02-20 20:34:29 +0100205
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200206 osmo_gettimeofday(&current, NULL);
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200207
208 node = rb_first(&timer_root);
209 if (node) {
210 struct osmo_timer_list *this;
211 this = container_of(node, struct osmo_timer_list, node);
212 update_nearest(&this->timeout, &current);
Harald Welteec8b4502010-02-20 20:34:29 +0100213 } else {
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200214 nearest_p = NULL;
Harald Welteec8b4502010-02-20 20:34:29 +0100215 }
216}
217
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200218/*! fire all timers... and remove them */
Harald Welte7e820202011-07-16 10:15:16 +0200219int osmo_timers_update(void)
Harald Welteec8b4502010-02-20 20:34:29 +0100220{
221 struct timeval current_time;
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200222 struct rb_node *node;
223 struct llist_head timer_eviction_list;
224 struct osmo_timer_list *this;
Harald Welteec8b4502010-02-20 20:34:29 +0100225 int work = 0;
226
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200227 osmo_gettimeofday(&current_time, NULL);
Harald Welteec8b4502010-02-20 20:34:29 +0100228
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200229 INIT_LLIST_HEAD(&timer_eviction_list);
230 for (node = rb_first(&timer_root); node; node = rb_next(node)) {
231 this = container_of(node, struct osmo_timer_list, node);
232
233 if (timercmp(&this->timeout, &current_time, >))
234 break;
235
236 llist_add(&this->list, &timer_eviction_list);
237 }
238
Harald Welteec8b4502010-02-20 20:34:29 +0100239 /*
240 * The callbacks might mess with our list and in this case
241 * even llist_for_each_entry_safe is not safe to use. To allow
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200242 * osmo_timer_del to be called from within the callback we need
243 * to restart the iteration for each element scheduled for removal.
Harald Welteec8b4502010-02-20 20:34:29 +0100244 *
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200245 * The problematic scenario is the following: Given two timers A
246 * and B that have expired at the same time. Thus, they are both
247 * in the eviction list in this order: A, then B. If we remove
248 * timer B from the A's callback, we continue with B in the next
249 * iteration step, leading to an access-after-release.
Harald Welteec8b4502010-02-20 20:34:29 +0100250 */
Harald Welteec8b4502010-02-20 20:34:29 +0100251restart:
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200252 llist_for_each_entry(this, &timer_eviction_list, list) {
253 osmo_timer_del(this);
Alexander Couzensec9bd522016-11-28 23:22:14 +0100254 if (this->cb)
255 this->cb(this->data);
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200256 work = 1;
257 goto restart;
Harald Welteec8b4502010-02-20 20:34:29 +0100258 }
259
260 return work;
261}
262
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200263/*! Check how many timers we have in the system
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200264 * \returns number of \ref osmo_timer_list registered */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200265int osmo_timers_check(void)
Harald Welteec8b4502010-02-20 20:34:29 +0100266{
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200267 struct rb_node *node;
Harald Welteec8b4502010-02-20 20:34:29 +0100268 int i = 0;
269
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +0200270 for (node = rb_first(&timer_root); node; node = rb_next(node)) {
Harald Welteec8b4502010-02-20 20:34:29 +0100271 i++;
272 }
273 return i;
274}
Harald Welteba6988b2011-08-17 12:46:48 +0200275
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200276/*! @} */