Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2008,2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 3 | * (C) 2011 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 6 | * Authors: Holger Hans Peter Freyther <zecke@selfish.org> |
| 7 | * Harald Welte <laforge@gnumonks.org> |
| 8 | * Pablo Neira Ayuso <pablo@gnumonks.org> |
| 9 | * |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 10 | * 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 Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 26 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 27 | /*! \addtogroup timer |
| 28 | * @{ |
| 29 | */ |
| 30 | |
| 31 | /*! \file timer.c |
| 32 | */ |
| 33 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 34 | #include <assert.h> |
| 35 | #include <string.h> |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 36 | #include <limits.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 37 | #include <osmocom/core/timer.h> |
Sylvain Munaut | 07f1103 | 2011-10-21 21:55:29 +0200 | [diff] [blame] | 38 | #include <osmocom/core/timer_compat.h> |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 39 | #include <osmocom/core/linuxlist.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 40 | |
Holger Hans Peter Freyther | 3f838b7 | 2015-08-20 18:17:15 +0000 | [diff] [blame] | 41 | /* These store the amount of time that we wait until next timer expires. */ |
| 42 | static struct timeval nearest; |
| 43 | static struct timeval *nearest_p; |
| 44 | |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 45 | static struct rb_root timer_root = RB_ROOT; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 46 | |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 47 | static void __add_timer(struct osmo_timer_list *timer) |
| 48 | { |
| 49 | struct rb_node **new = &(timer_root.rb_node); |
| 50 | struct rb_node *parent = NULL; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 51 | |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 52 | while (*new) { |
| 53 | struct osmo_timer_list *this; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 54 | |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 55 | this = container_of(*new, struct osmo_timer_list, node); |
| 56 | |
| 57 | parent = *new; |
| 58 | if (timercmp(&timer->timeout, &this->timeout, <)) |
| 59 | new = &((*new)->rb_left); |
| 60 | else |
| 61 | new = &((*new)->rb_right); |
| 62 | } |
| 63 | |
| 64 | rb_link_node(&timer->node, parent, new); |
| 65 | rb_insert_color(&timer->node, &timer_root); |
| 66 | } |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 67 | |
| 68 | /*! \brief add a new timer to the timer management |
| 69 | * \param[in] timer the timer that should be added |
| 70 | */ |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 71 | void osmo_timer_add(struct osmo_timer_list *timer) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 72 | { |
Pablo Neira Ayuso | a71b8ea | 2011-11-13 10:11:31 +0100 | [diff] [blame] | 73 | osmo_timer_del(timer); |
| 74 | timer->active = 1; |
| 75 | INIT_LLIST_HEAD(&timer->list); |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 76 | __add_timer(timer); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 79 | /*! \brief schedule a timer at a given future relative time |
| 80 | * \param[in] timer the to-be-added timer |
| 81 | * \param[in] seconds number of seconds from now |
| 82 | * \param[in] microseconds number of microseconds from now |
| 83 | * |
| 84 | * This function can be used to (re-)schedule a given timer at a |
| 85 | * specified number of seconds+microseconds in the future. It will |
| 86 | * internally add it to the timer management data structures, thus |
| 87 | * osmo_timer_add() is automatically called. |
| 88 | */ |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 89 | void |
| 90 | osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 91 | { |
| 92 | struct timeval current_time; |
| 93 | |
| 94 | gettimeofday(¤t_time, NULL); |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 95 | timer->timeout.tv_sec = seconds; |
| 96 | timer->timeout.tv_usec = microseconds; |
| 97 | timeradd(&timer->timeout, ¤t_time, &timer->timeout); |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 98 | osmo_timer_add(timer); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 101 | /*! \brief delete a timer from timer management |
| 102 | * \param[in] timer the to-be-deleted timer |
| 103 | * |
| 104 | * This function can be used to delete a previously added/scheduled |
| 105 | * timer from the timer management code. |
| 106 | */ |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 107 | void osmo_timer_del(struct osmo_timer_list *timer) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 108 | { |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 109 | if (timer->active) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 110 | timer->active = 0; |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 111 | rb_erase(&timer->node, &timer_root); |
| 112 | /* make sure this is not already scheduled for removal. */ |
| 113 | if (!llist_empty(&timer->list)) |
| 114 | llist_del_init(&timer->list); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 118 | /*! \brief check if given timer is still pending |
| 119 | * \param[in] timer the to-be-checked timer |
| 120 | * \return 1 if pending, 0 otherwise |
| 121 | * |
| 122 | * This function can be used to determine whether a given timer |
| 123 | * has alredy expired (returns 0) or is still pending (returns 1) |
| 124 | */ |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 125 | int osmo_timer_pending(struct osmo_timer_list *timer) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 126 | { |
| 127 | return timer->active; |
| 128 | } |
| 129 | |
Harald Welte | e30b6ac | 2012-07-13 12:21:04 +0200 | [diff] [blame] | 130 | /*! \brief compute the remaining time of a timer |
| 131 | * \param[in] timer the to-be-checked timer |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 132 | * \param[in] now the current time (NULL if not known) |
Harald Welte | e30b6ac | 2012-07-13 12:21:04 +0200 | [diff] [blame] | 133 | * \param[out] remaining remaining time until timer fires |
| 134 | * \return 0 if timer has not expired yet, -1 if it has |
| 135 | * |
| 136 | * This function can be used to determine the amount of time |
| 137 | * remaining until the expiration of the timer. |
| 138 | */ |
| 139 | int osmo_timer_remaining(const struct osmo_timer_list *timer, |
| 140 | const struct timeval *now, |
| 141 | struct timeval *remaining) |
| 142 | { |
| 143 | struct timeval current_time; |
| 144 | |
Holger Hans Peter Freyther | 8e5435a | 2014-05-23 08:37:02 +0200 | [diff] [blame] | 145 | if (!now) |
Harald Welte | e30b6ac | 2012-07-13 12:21:04 +0200 | [diff] [blame] | 146 | gettimeofday(¤t_time, NULL); |
Holger Hans Peter Freyther | 8e5435a | 2014-05-23 08:37:02 +0200 | [diff] [blame] | 147 | else |
| 148 | current_time = *now; |
Harald Welte | e30b6ac | 2012-07-13 12:21:04 +0200 | [diff] [blame] | 149 | |
| 150 | timersub(&timer->timeout, ¤t_time, remaining); |
| 151 | |
| 152 | if (remaining->tv_sec < 0) |
| 153 | return -1; |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 158 | /*! \brief Determine time between now and the nearest timer |
| 159 | * \returns pointer to timeval of nearest timer, NULL if there is none |
| 160 | * |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 161 | * if we have a nearest time return the delta between the current |
| 162 | * time and the time of the nearest timer. |
| 163 | * If the nearest timer timed out return NULL and then we will |
| 164 | * dispatch everything after the select |
| 165 | */ |
Harald Welte | 7e82020 | 2011-07-16 10:15:16 +0200 | [diff] [blame] | 166 | struct timeval *osmo_timers_nearest(void) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 167 | { |
Sylvain Munaut | 0061ded | 2011-10-18 20:11:03 +0200 | [diff] [blame] | 168 | /* nearest_p is exactly what we need already: NULL if nothing is |
| 169 | * waiting, {0,0} if we must dispatch immediately, and the correct |
| 170 | * delay if we need to wait */ |
| 171 | return nearest_p; |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 172 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 173 | |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 174 | static void update_nearest(struct timeval *cand, struct timeval *current) |
| 175 | { |
| 176 | if (cand->tv_sec != LONG_MAX) { |
| 177 | if (timercmp(cand, current, >)) |
| 178 | timersub(cand, current, &nearest); |
| 179 | else { |
| 180 | /* loop again inmediately */ |
| 181 | nearest.tv_sec = 0; |
| 182 | nearest.tv_usec = 0; |
| 183 | } |
| 184 | nearest_p = &nearest; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 185 | } else { |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 186 | nearest_p = NULL; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 187 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 190 | /*! \brief Find the nearest time and update nearest_p */ |
Harald Welte | 7e82020 | 2011-07-16 10:15:16 +0200 | [diff] [blame] | 191 | void osmo_timers_prepare(void) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 192 | { |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 193 | struct rb_node *node; |
| 194 | struct timeval current; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 195 | |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 196 | gettimeofday(¤t, NULL); |
| 197 | |
| 198 | node = rb_first(&timer_root); |
| 199 | if (node) { |
| 200 | struct osmo_timer_list *this; |
| 201 | this = container_of(node, struct osmo_timer_list, node); |
| 202 | update_nearest(&this->timeout, ¤t); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 203 | } else { |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 204 | nearest_p = NULL; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 208 | /*! \brief fire all timers... and remove them */ |
Harald Welte | 7e82020 | 2011-07-16 10:15:16 +0200 | [diff] [blame] | 209 | int osmo_timers_update(void) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 210 | { |
| 211 | struct timeval current_time; |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 212 | struct rb_node *node; |
| 213 | struct llist_head timer_eviction_list; |
| 214 | struct osmo_timer_list *this; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 215 | int work = 0; |
| 216 | |
| 217 | gettimeofday(¤t_time, NULL); |
| 218 | |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 219 | INIT_LLIST_HEAD(&timer_eviction_list); |
| 220 | for (node = rb_first(&timer_root); node; node = rb_next(node)) { |
| 221 | this = container_of(node, struct osmo_timer_list, node); |
| 222 | |
| 223 | if (timercmp(&this->timeout, ¤t_time, >)) |
| 224 | break; |
| 225 | |
| 226 | llist_add(&this->list, &timer_eviction_list); |
| 227 | } |
| 228 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 229 | /* |
| 230 | * The callbacks might mess with our list and in this case |
| 231 | * even llist_for_each_entry_safe is not safe to use. To allow |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 232 | * osmo_timer_del to be called from within the callback we need |
| 233 | * to restart the iteration for each element scheduled for removal. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 234 | * |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 235 | * The problematic scenario is the following: Given two timers A |
| 236 | * and B that have expired at the same time. Thus, they are both |
| 237 | * in the eviction list in this order: A, then B. If we remove |
| 238 | * timer B from the A's callback, we continue with B in the next |
| 239 | * iteration step, leading to an access-after-release. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 240 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 241 | restart: |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 242 | llist_for_each_entry(this, &timer_eviction_list, list) { |
| 243 | osmo_timer_del(this); |
| 244 | this->cb(this->data); |
| 245 | work = 1; |
| 246 | goto restart; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return work; |
| 250 | } |
| 251 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 252 | int osmo_timers_check(void) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 253 | { |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 254 | struct rb_node *node; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 255 | int i = 0; |
| 256 | |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 257 | for (node = rb_first(&timer_root); node; node = rb_next(node)) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 258 | i++; |
| 259 | } |
| 260 | return i; |
| 261 | } |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 262 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 263 | /*! @} */ |