Ivan Kluchnikov | 6578562 | 2012-04-12 14:49:02 +0400 | [diff] [blame] | 1 | /* gsm_timer.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012 Ivan Klyuchnikov |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | /* These store the amount of frame number that we wait until next timer expires. */ |
| 21 | static int nearest; |
| 22 | static int *nearest_p; |
| 23 | |
| 24 | /*! \addtogroup gsm_timer |
| 25 | * @{ |
| 26 | */ |
| 27 | |
| 28 | /*! \file gsm_timer.cpp |
| 29 | */ |
| 30 | |
| 31 | #include <assert.h> |
| 32 | #include <string.h> |
| 33 | #include <limits.h> |
| 34 | #include <gsm_timer.h> |
| 35 | #include <pcu_l1_if.h> |
| 36 | |
| 37 | static struct rb_root timer_root = RB_ROOT; |
| 38 | |
| 39 | static void __add_gsm_timer(struct osmo_gsm_timer_list *timer) |
| 40 | { |
| 41 | struct rb_node **new_node = &(timer_root.rb_node); |
| 42 | struct rb_node *parent = NULL; |
| 43 | |
| 44 | while (*new_node) { |
| 45 | struct osmo_gsm_timer_list *this_timer; |
| 46 | |
| 47 | this_timer = container_of(*new_node, struct osmo_gsm_timer_list, node); |
| 48 | |
| 49 | parent = *new_node; |
| 50 | if (timer->fn < this_timer->fn) |
| 51 | new_node = &((*new_node)->rb_left); |
| 52 | else |
| 53 | new_node = &((*new_node)->rb_right); |
| 54 | } |
| 55 | |
| 56 | rb_link_node(&timer->node, parent, new_node); |
| 57 | rb_insert_color(&timer->node, &timer_root); |
| 58 | } |
| 59 | |
| 60 | /*! \brief add a new timer to the timer management |
| 61 | * \param[in] timer the timer that should be added |
| 62 | */ |
| 63 | void osmo_gsm_timer_add(struct osmo_gsm_timer_list *timer) |
| 64 | { |
| 65 | osmo_gsm_timer_del(timer); |
| 66 | timer->active = 1; |
| 67 | INIT_LLIST_HEAD(&timer->list); |
| 68 | __add_gsm_timer(timer); |
| 69 | } |
| 70 | |
| 71 | /*! \brief schedule a gsm timer at a given future relative time |
| 72 | * \param[in] timer the to-be-added timer |
| 73 | * \param[in] number of frames from now |
| 74 | * |
| 75 | * This function can be used to (re-)schedule a given timer at a |
| 76 | * specified number of frames in the future. It will |
| 77 | * internally add it to the timer management data structures, thus |
| 78 | * osmo_timer_add() is automatically called. |
| 79 | */ |
| 80 | void |
| 81 | osmo_gsm_timer_schedule(struct osmo_gsm_timer_list *timer, int fn) |
| 82 | { |
| 83 | int current_fn; |
| 84 | |
| 85 | current_fn = get_current_fn(); |
| 86 | timer->fn = current_fn + fn; |
| 87 | osmo_gsm_timer_add(timer); |
| 88 | } |
| 89 | |
| 90 | /*! \brief delete a gsm timer from timer management |
| 91 | * \param[in] timer the to-be-deleted timer |
| 92 | * |
| 93 | * This function can be used to delete a previously added/scheduled |
| 94 | * timer from the timer management code. |
| 95 | */ |
| 96 | void osmo_gsm_timer_del(struct osmo_gsm_timer_list *timer) |
| 97 | { |
| 98 | if (timer->active) { |
| 99 | timer->active = 0; |
| 100 | rb_erase(&timer->node, &timer_root); |
| 101 | /* make sure this is not already scheduled for removal. */ |
| 102 | if (!llist_empty(&timer->list)) |
| 103 | llist_del_init(&timer->list); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /*! \brief check if given timer is still pending |
| 108 | * \param[in] timer the to-be-checked timer |
| 109 | * \return 1 if pending, 0 otherwise |
| 110 | * |
| 111 | * This function can be used to determine whether a given timer |
| 112 | * has alredy expired (returns 0) or is still pending (returns 1) |
| 113 | */ |
| 114 | int osmo_gsm_timer_pending(struct osmo_gsm_timer_list *timer) |
| 115 | { |
| 116 | return timer->active; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * if we have a nearest frame number return the delta between the current |
| 121 | * FN and the FN of the nearest timer. |
| 122 | * If the nearest timer timed out return NULL and then we will |
| 123 | * dispatch everything after the select |
| 124 | */ |
| 125 | int *osmo_gsm_timers_nearest(void) |
| 126 | { |
| 127 | /* nearest_p is exactly what we need already: NULL if nothing is |
| 128 | * waiting, {0,0} if we must dispatch immediately, and the correct |
| 129 | * delay if we need to wait */ |
| 130 | return nearest_p; |
| 131 | } |
| 132 | |
| 133 | static void update_nearest(int *cand, int *current) |
| 134 | { |
| 135 | if (*cand != LONG_MAX) { |
| 136 | if (*cand > *current) |
| 137 | nearest = *cand - *current; |
| 138 | else { |
| 139 | /* loop again inmediately */ |
| 140 | nearest = 0; |
| 141 | } |
| 142 | nearest_p = &nearest; |
| 143 | } else { |
| 144 | nearest_p = NULL; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Find the nearest FN and update s_nearest_time |
| 150 | */ |
| 151 | void osmo_gsm_timers_prepare(void) |
| 152 | { |
| 153 | struct rb_node *node; |
| 154 | int current_fn; |
| 155 | |
| 156 | current_fn = get_current_fn(); |
| 157 | |
| 158 | node = rb_first(&timer_root); |
| 159 | if (node) { |
| 160 | struct osmo_gsm_timer_list *this_timer; |
| 161 | this_timer = container_of(node, struct osmo_gsm_timer_list, node); |
| 162 | update_nearest(&this_timer->fn, ¤t_fn); |
| 163 | } else { |
| 164 | nearest_p = NULL; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * fire all timers... and remove them |
| 170 | */ |
| 171 | int osmo_gsm_timers_update(void) |
| 172 | { |
| 173 | int current_fn; |
| 174 | struct rb_node *node; |
| 175 | struct llist_head timer_eviction_list; |
| 176 | struct osmo_gsm_timer_list *this_timer; |
| 177 | int work = 0; |
| 178 | |
| 179 | current_fn = get_current_fn(); |
| 180 | |
| 181 | INIT_LLIST_HEAD(&timer_eviction_list); |
| 182 | for (node = rb_first(&timer_root); node; node = rb_next(node)) { |
| 183 | this_timer = container_of(node, struct osmo_gsm_timer_list, node); |
| 184 | |
| 185 | if (this_timer->fn > current_fn) |
| 186 | break; |
| 187 | |
| 188 | llist_add(&this_timer->list, &timer_eviction_list); |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * The callbacks might mess with our list and in this case |
| 193 | * even llist_for_each_entry_safe is not safe to use. To allow |
| 194 | * osmo_gsm_timer_del to be called from within the callback we need |
| 195 | * to restart the iteration for each element scheduled for removal. |
| 196 | * |
| 197 | * The problematic scenario is the following: Given two timers A |
| 198 | * and B that have expired at the same time. Thus, they are both |
| 199 | * in the eviction list in this order: A, then B. If we remove |
| 200 | * timer B from the A's callback, we continue with B in the next |
| 201 | * iteration step, leading to an access-after-release. |
| 202 | */ |
| 203 | restart: |
| 204 | llist_for_each_entry(this_timer, &timer_eviction_list, list) { |
| 205 | osmo_gsm_timer_del(this_timer); |
| 206 | this_timer->cb(this_timer->data); |
| 207 | work = 1; |
| 208 | goto restart; |
| 209 | } |
| 210 | |
| 211 | return work; |
| 212 | } |
| 213 | |
| 214 | int osmo_gsm_timers_check(void) |
| 215 | { |
| 216 | struct rb_node *node; |
| 217 | int i = 0; |
| 218 | |
| 219 | for (node = rb_first(&timer_root); node; node = rb_next(node)) { |
| 220 | i++; |
| 221 | } |
| 222 | return i; |
| 223 | } |
| 224 | |
| 225 | /*! }@ */ |
| 226 | |