Holger Freyther | 5f75598 | 2008-12-27 09:42:59 +0000 | [diff] [blame] | 1 | /* |
Harald Welte | b8f04ea | 2009-01-02 23:57:22 +0000 | [diff] [blame] | 2 | * (C) 2008,2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Freyther | 5f75598 | 2008-12-27 09:42:59 +0000 | [diff] [blame] | 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 | |
| 25 | static LLIST_HEAD(timer_list); |
| 26 | static struct timeval s_nearest_time; |
| 27 | static 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 | |
| 34 | void 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 | |
Harald Welte | b8f04ea | 2009-01-02 23:57:22 +0000 | [diff] [blame] | 46 | timer->in_list = 1; |
Holger Freyther | 5f75598 | 2008-12-27 09:42:59 +0000 | [diff] [blame] | 47 | llist_add(&timer->entry, &timer_list); |
| 48 | } |
| 49 | |
| 50 | void schedule_timer(struct timer_list *timer, int seconds, int microseconds) |
| 51 | { |
| 52 | struct timeval current_time; |
| 53 | |
| 54 | gettimeofday(¤t_time, NULL); |
| 55 | unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec; |
| 56 | currentTime += seconds * MICRO_SECONDS + microseconds; |
| 57 | timer->timeout.tv_sec = currentTime / MICRO_SECONDS; |
| 58 | timer->timeout.tv_usec = currentTime % MICRO_SECONDS; |
| 59 | add_timer(timer); |
| 60 | } |
| 61 | |
| 62 | void del_timer(struct timer_list *timer) |
| 63 | { |
Harald Welte | b8f04ea | 2009-01-02 23:57:22 +0000 | [diff] [blame] | 64 | if (timer->in_list) { |
Holger Freyther | 5f75598 | 2008-12-27 09:42:59 +0000 | [diff] [blame] | 65 | timer->active = 0; |
Harald Welte | b8f04ea | 2009-01-02 23:57:22 +0000 | [diff] [blame] | 66 | timer->in_list = 0; |
Holger Freyther | 5f75598 | 2008-12-27 09:42:59 +0000 | [diff] [blame] | 67 | llist_del(&timer->entry); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | int timer_pending(struct timer_list *timer) |
| 72 | { |
| 73 | return timer->active; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * if we have a nearest time return the delta between the current |
| 78 | * time and the time of the nearest timer. |
| 79 | * If the nearest timer timed out return NULL and then we will |
| 80 | * dispatch everything after the select |
| 81 | */ |
| 82 | struct timeval *nearest_timer() |
| 83 | { |
| 84 | struct timeval current_time; |
| 85 | |
| 86 | if (s_nearest_time.tv_sec == 0 && s_nearest_time.tv_usec == 0) |
| 87 | return NULL; |
| 88 | |
| 89 | if (gettimeofday(¤t_time, NULL) == -1) |
| 90 | return NULL; |
| 91 | |
| 92 | unsigned long long nearestTime = s_nearest_time.tv_sec * MICRO_SECONDS + s_nearest_time.tv_usec; |
| 93 | unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec; |
| 94 | |
| 95 | if (nearestTime < currentTime) { |
| 96 | s_select_time.tv_sec = 0; |
| 97 | s_select_time.tv_usec = 0; |
| 98 | } else { |
| 99 | s_select_time.tv_sec = (nearestTime - currentTime) / MICRO_SECONDS; |
| 100 | s_select_time.tv_usec = (nearestTime - currentTime) % MICRO_SECONDS; |
| 101 | } |
| 102 | |
| 103 | return &s_select_time; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Find the nearest time and update s_nearest_time |
| 108 | */ |
| 109 | void prepare_timers() |
| 110 | { |
| 111 | struct timer_list *timer, *nearest_timer = NULL; |
| 112 | llist_for_each_entry(timer, &timer_list, entry) { |
| 113 | if (!nearest_timer || TIME_SMALLER(timer->timeout, nearest_timer->timeout)) { |
| 114 | nearest_timer = timer; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (nearest_timer) { |
| 119 | s_nearest_time = nearest_timer->timeout; |
| 120 | } else { |
| 121 | memset(&s_nearest_time, 0, sizeof(struct timeval)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * fire all timers... and remove them |
| 127 | */ |
| 128 | void update_timers() |
| 129 | { |
| 130 | struct timeval current_time; |
| 131 | struct timer_list *timer, *tmp; |
| 132 | |
| 133 | gettimeofday(¤t_time, NULL); |
| 134 | |
| 135 | /* |
| 136 | * The callbacks might mess with our list and in this case |
| 137 | * even llist_for_each_entry_safe is not safe to use. To allow |
| 138 | * del_timer, add_timer, schedule_timer to be called from within |
| 139 | * the callback we jump through some loops. |
| 140 | * |
| 141 | * First we set the handled flag of each active timer to zero, |
| 142 | * then we iterate over the list and execute the callbacks. As the |
| 143 | * list might have been changed (specially the next) from within |
| 144 | * the callback we have to start over again. Once every callback |
| 145 | * is dispatched we will remove the non-active from the list. |
| 146 | * |
| 147 | * TODO: If this is a performance issue we can poison a global |
| 148 | * variable in add_timer and del_timer and only then restart. |
| 149 | */ |
| 150 | llist_for_each_entry(timer, &timer_list, entry) { |
| 151 | timer->handled = 0; |
| 152 | } |
| 153 | |
| 154 | restart: |
| 155 | llist_for_each_entry(timer, &timer_list, entry) { |
| 156 | if (!timer->handled && TIME_SMALLER(timer->timeout, current_time)) { |
| 157 | timer->handled = 1; |
| 158 | timer->active = 0; |
| 159 | (*timer->cb)(timer->data); |
| 160 | goto restart; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | llist_for_each_entry_safe(timer, tmp, &timer_list, entry) { |
| 165 | timer->handled = 0; |
| 166 | if (!timer->active) { |
| 167 | llist_del(&timer->entry); |
| 168 | } |
| 169 | } |
| 170 | } |