blob: cefe520fe72cbc39d1d37148304d815318d1dbb5 [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{
Vadim Yanitskiy4590b912020-01-25 04:39:27 +0700147 if (*cand > *current)
148 nearest = *cand - *current;
149 else {
150 /* loop again inmediately */
151 nearest = 0;
Ivan Kluchnikov65785622012-04-12 14:49:02 +0400152 }
Vadim Yanitskiy4590b912020-01-25 04:39:27 +0700153
154 nearest_p = &nearest;
Ivan Kluchnikov65785622012-04-12 14:49:02 +0400155}
156
157/*
158 * Find the nearest FN and update s_nearest_time
159 */
160void osmo_gsm_timers_prepare(void)
161{
162 struct rb_node *node;
163 int current_fn;
164
165 current_fn = get_current_fn();
166
167 node = rb_first(&timer_root);
168 if (node) {
169 struct osmo_gsm_timer_list *this_timer;
170 this_timer = container_of(node, struct osmo_gsm_timer_list, node);
171 update_nearest(&this_timer->fn, &current_fn);
172 } else {
173 nearest_p = NULL;
174 }
175}
176
177/*
178 * fire all timers... and remove them
179 */
180int osmo_gsm_timers_update(void)
181{
182 int current_fn;
183 struct rb_node *node;
184 struct llist_head timer_eviction_list;
185 struct osmo_gsm_timer_list *this_timer;
186 int work = 0;
187
188 current_fn = get_current_fn();
189
190 INIT_LLIST_HEAD(&timer_eviction_list);
191 for (node = rb_first(&timer_root); node; node = rb_next(node)) {
192 this_timer = container_of(node, struct osmo_gsm_timer_list, node);
193
194 if (this_timer->fn > current_fn)
195 break;
196
197 llist_add(&this_timer->list, &timer_eviction_list);
198 }
199
200 /*
201 * The callbacks might mess with our list and in this case
202 * even llist_for_each_entry_safe is not safe to use. To allow
203 * osmo_gsm_timer_del to be called from within the callback we need
204 * to restart the iteration for each element scheduled for removal.
205 *
206 * The problematic scenario is the following: Given two timers A
207 * and B that have expired at the same time. Thus, they are both
208 * in the eviction list in this order: A, then B. If we remove
209 * timer B from the A's callback, we continue with B in the next
210 * iteration step, leading to an access-after-release.
211 */
212restart:
213 llist_for_each_entry(this_timer, &timer_eviction_list, list) {
214 osmo_gsm_timer_del(this_timer);
215 this_timer->cb(this_timer->data);
216 work = 1;
217 goto restart;
218 }
219
220 return work;
221}
222
223int osmo_gsm_timers_check(void)
224{
225 struct rb_node *node;
226 int i = 0;
227
228 for (node = rb_first(&timer_root); node; node = rb_next(node)) {
229 i++;
230 }
231 return i;
232}
233
234/*! }@ */
235