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