blob: 158ee76b729356659c302d5ba13cd16372d3bbb4 [file] [log] [blame]
Holger Freyther5f755982008-12-27 09:42:59 +00001/*
2 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <assert.h>
22#include <string.h>
23#include <openbsc/timer.h>
24
25static LLIST_HEAD(timer_list);
26static struct timeval s_nearest_time;
27static struct timeval s_select_time;
28
29#define MICRO_SECONDS 1000000LL
30
31#define TIME_SMALLER(left, right) \
32 (left.tv_sec*MICRO_SECONDS+left.tv_usec) <= (right.tv_sec*MICRO_SECONDS+right.tv_usec)
33
34void add_timer(struct timer_list *timer)
35{
36 struct timer_list *list_timer;
37
38 /* TODO: Optimize and remember the closest item... */
39 timer->active = 1;
40
41 /* this might be called from within update_timers */
42 llist_for_each_entry(list_timer, &timer_list, entry)
43 if (timer == list_timer)
44 return;
45
46 llist_add(&timer->entry, &timer_list);
47}
48
49void schedule_timer(struct timer_list *timer, int seconds, int microseconds)
50{
51 struct timeval current_time;
52
53 gettimeofday(&current_time, NULL);
54 unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec;
55 currentTime += seconds * MICRO_SECONDS + microseconds;
56 timer->timeout.tv_sec = currentTime / MICRO_SECONDS;
57 timer->timeout.tv_usec = currentTime % MICRO_SECONDS;
58 add_timer(timer);
59}
60
61void del_timer(struct timer_list *timer)
62{
63 if (timer_pending(timer)) {
64 timer->active = 0;
65 llist_del(&timer->entry);
66 }
67}
68
69int timer_pending(struct timer_list *timer)
70{
71 return timer->active;
72}
73
74/*
75 * if we have a nearest time return the delta between the current
76 * time and the time of the nearest timer.
77 * If the nearest timer timed out return NULL and then we will
78 * dispatch everything after the select
79 */
80struct timeval *nearest_timer()
81{
82 struct timeval current_time;
83
84 if (s_nearest_time.tv_sec == 0 && s_nearest_time.tv_usec == 0)
85 return NULL;
86
87 if (gettimeofday(&current_time, NULL) == -1)
88 return NULL;
89
90 unsigned long long nearestTime = s_nearest_time.tv_sec * MICRO_SECONDS + s_nearest_time.tv_usec;
91 unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec;
92
93 if (nearestTime < currentTime) {
94 s_select_time.tv_sec = 0;
95 s_select_time.tv_usec = 0;
96 } else {
97 s_select_time.tv_sec = (nearestTime - currentTime) / MICRO_SECONDS;
98 s_select_time.tv_usec = (nearestTime - currentTime) % MICRO_SECONDS;
99 }
100
101 return &s_select_time;
102}
103
104/*
105 * Find the nearest time and update s_nearest_time
106 */
107void prepare_timers()
108{
109 struct timer_list *timer, *nearest_timer = NULL;
110 llist_for_each_entry(timer, &timer_list, entry) {
111 if (!nearest_timer || TIME_SMALLER(timer->timeout, nearest_timer->timeout)) {
112 nearest_timer = timer;
113 }
114 }
115
116 if (nearest_timer) {
117 s_nearest_time = nearest_timer->timeout;
118 } else {
119 memset(&s_nearest_time, 0, sizeof(struct timeval));
120 }
121}
122
123/*
124 * fire all timers... and remove them
125 */
126void update_timers()
127{
128 struct timeval current_time;
129 struct timer_list *timer, *tmp;
130
131 gettimeofday(&current_time, NULL);
132
133 /*
134 * The callbacks might mess with our list and in this case
135 * even llist_for_each_entry_safe is not safe to use. To allow
136 * del_timer, add_timer, schedule_timer to be called from within
137 * the callback we jump through some loops.
138 *
139 * First we set the handled flag of each active timer to zero,
140 * then we iterate over the list and execute the callbacks. As the
141 * list might have been changed (specially the next) from within
142 * the callback we have to start over again. Once every callback
143 * is dispatched we will remove the non-active from the list.
144 *
145 * TODO: If this is a performance issue we can poison a global
146 * variable in add_timer and del_timer and only then restart.
147 */
148 llist_for_each_entry(timer, &timer_list, entry) {
149 timer->handled = 0;
150 }
151
152restart:
153 llist_for_each_entry(timer, &timer_list, entry) {
154 if (!timer->handled && TIME_SMALLER(timer->timeout, current_time)) {
155 timer->handled = 1;
156 timer->active = 0;
157 (*timer->cb)(timer->data);
158 goto restart;
159 }
160 }
161
162 llist_for_each_entry_safe(timer, tmp, &timer_list, entry) {
163 timer->handled = 0;
164 if (!timer->active) {
165 llist_del(&timer->entry);
166 }
167 }
168}