blob: f7901d4f92cce1f2409682f96cc40e861da9229d [file] [log] [blame]
Ivan Kluchnikov65785622012-04-12 14:49:02 +04001/* 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. */
21static int nearest;
22static 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>
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +020036#include <bts.h>
37
Ivan Kluchnikov65785622012-04-12 14:49:02 +040038
39static struct rb_root timer_root = RB_ROOT;
40
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +020041/*
42 * TODO: make this depend on the BTS. This means that
43 * all time functions schedule based on the BTS they
44 * are scheduled on.
45 */
Philipp Maier7e8e3972018-04-10 13:31:45 +020046int get_current_fn()
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +020047{
48 return BTS::main_bts()->current_frame_number();
49}
50
Ivan Kluchnikov65785622012-04-12 14:49:02 +040051static void __add_gsm_timer(struct osmo_gsm_timer_list *timer)
52{
53 struct rb_node **new_node = &(timer_root.rb_node);
54 struct rb_node *parent = NULL;
55
56 while (*new_node) {
57 struct osmo_gsm_timer_list *this_timer;
58
59 this_timer = container_of(*new_node, struct osmo_gsm_timer_list, node);
60
61 parent = *new_node;
62 if (timer->fn < this_timer->fn)
63 new_node = &((*new_node)->rb_left);
64 else
65 new_node = &((*new_node)->rb_right);
66 }
67
68 rb_link_node(&timer->node, parent, new_node);
69 rb_insert_color(&timer->node, &timer_root);
70}
71
72/*! \brief add a new timer to the timer management
73 * \param[in] timer the timer that should be added
74 */
75void osmo_gsm_timer_add(struct osmo_gsm_timer_list *timer)
76{
77 osmo_gsm_timer_del(timer);
78 timer->active = 1;
79 INIT_LLIST_HEAD(&timer->list);
80 __add_gsm_timer(timer);
81}
82
83/*! \brief schedule a gsm timer at a given future relative time
84 * \param[in] timer the to-be-added timer
85 * \param[in] number of frames from now
86 *
87 * This function can be used to (re-)schedule a given timer at a
88 * specified number of frames in the future. It will
89 * internally add it to the timer management data structures, thus
90 * osmo_timer_add() is automatically called.
91 */
92void
93osmo_gsm_timer_schedule(struct osmo_gsm_timer_list *timer, int fn)
94{
95 int current_fn;
96
97 current_fn = get_current_fn();
98 timer->fn = current_fn + fn;
99 osmo_gsm_timer_add(timer);
100}
101
102/*! \brief delete a gsm timer from timer management
103 * \param[in] timer the to-be-deleted timer
104 *
105 * This function can be used to delete a previously added/scheduled
106 * timer from the timer management code.
107 */
108void osmo_gsm_timer_del(struct osmo_gsm_timer_list *timer)
109{
110 if (timer->active) {
111 timer->active = 0;
112 rb_erase(&timer->node, &timer_root);
113 /* make sure this is not already scheduled for removal. */
114 if (!llist_empty(&timer->list))
115 llist_del_init(&timer->list);
116 }
117}
118
119/*! \brief check if given timer is still pending
120 * \param[in] timer the to-be-checked timer
121 * \return 1 if pending, 0 otherwise
122 *
123 * This function can be used to determine whether a given timer
124 * has alredy expired (returns 0) or is still pending (returns 1)
125 */
126int osmo_gsm_timer_pending(struct osmo_gsm_timer_list *timer)
127{
128 return timer->active;
129}
130
131/*
132 * if we have a nearest frame number return the delta between the current
133 * FN and the FN of the nearest timer.
134 * If the nearest timer timed out return NULL and then we will
135 * dispatch everything after the select
136 */
137int *osmo_gsm_timers_nearest(void)
138{
139 /* nearest_p is exactly what we need already: NULL if nothing is
140 * waiting, {0,0} if we must dispatch immediately, and the correct
141 * delay if we need to wait */
142 return nearest_p;
143}
144
145static void update_nearest(int *cand, int *current)
146{
147 if (*cand != LONG_MAX) {
148 if (*cand > *current)
149 nearest = *cand - *current;
150 else {
151 /* loop again inmediately */
152 nearest = 0;
153 }
154 nearest_p = &nearest;
155 } else {
156 nearest_p = NULL;
157 }
158}
159
160/*
161 * Find the nearest FN and update s_nearest_time
162 */
163void osmo_gsm_timers_prepare(void)
164{
165 struct rb_node *node;
166 int current_fn;
167
168 current_fn = get_current_fn();
169
170 node = rb_first(&timer_root);
171 if (node) {
172 struct osmo_gsm_timer_list *this_timer;
173 this_timer = container_of(node, struct osmo_gsm_timer_list, node);
174 update_nearest(&this_timer->fn, &current_fn);
175 } else {
176 nearest_p = NULL;
177 }
178}
179
180/*
181 * fire all timers... and remove them
182 */
183int osmo_gsm_timers_update(void)
184{
185 int current_fn;
186 struct rb_node *node;
187 struct llist_head timer_eviction_list;
188 struct osmo_gsm_timer_list *this_timer;
189 int work = 0;
190
191 current_fn = get_current_fn();
192
193 INIT_LLIST_HEAD(&timer_eviction_list);
194 for (node = rb_first(&timer_root); node; node = rb_next(node)) {
195 this_timer = container_of(node, struct osmo_gsm_timer_list, node);
196
197 if (this_timer->fn > current_fn)
198 break;
199
200 llist_add(&this_timer->list, &timer_eviction_list);
201 }
202
203 /*
204 * The callbacks might mess with our list and in this case
205 * even llist_for_each_entry_safe is not safe to use. To allow
206 * osmo_gsm_timer_del to be called from within the callback we need
207 * to restart the iteration for each element scheduled for removal.
208 *
209 * The problematic scenario is the following: Given two timers A
210 * and B that have expired at the same time. Thus, they are both
211 * in the eviction list in this order: A, then B. If we remove
212 * timer B from the A's callback, we continue with B in the next
213 * iteration step, leading to an access-after-release.
214 */
215restart:
216 llist_for_each_entry(this_timer, &timer_eviction_list, list) {
217 osmo_gsm_timer_del(this_timer);
218 this_timer->cb(this_timer->data);
219 work = 1;
220 goto restart;
221 }
222
223 return work;
224}
225
226int osmo_gsm_timers_check(void)
227{
228 struct rb_node *node;
229 int i = 0;
230
231 for (node = rb_first(&timer_root); node; node = rb_next(node)) {
232 i++;
233 }
234 return i;
235}
236
237/*! }@ */
238