Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2008,2009 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> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 23 | #include <osmocom/core/timer.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 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 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 34 | void osmo_timer_add(struct osmo_timer_list *timer) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | { |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 36 | struct osmo_timer_list *list_timer; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 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 | timer->in_list = 1; |
| 47 | llist_add(&timer->entry, &timer_list); |
| 48 | } |
| 49 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 50 | void |
| 51 | osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 52 | { |
| 53 | struct timeval current_time; |
| 54 | |
| 55 | gettimeofday(¤t_time, NULL); |
| 56 | unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec; |
| 57 | currentTime += seconds * MICRO_SECONDS + microseconds; |
| 58 | timer->timeout.tv_sec = currentTime / MICRO_SECONDS; |
| 59 | timer->timeout.tv_usec = currentTime % MICRO_SECONDS; |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 60 | osmo_timer_add(timer); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 63 | void osmo_timer_del(struct osmo_timer_list *timer) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 64 | { |
| 65 | if (timer->in_list) { |
| 66 | timer->active = 0; |
| 67 | timer->in_list = 0; |
| 68 | llist_del(&timer->entry); |
| 69 | } |
| 70 | } |
| 71 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 72 | int osmo_timer_pending(struct osmo_timer_list *timer) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 73 | { |
| 74 | return timer->active; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * if we have a nearest time return the delta between the current |
| 79 | * time and the time of the nearest timer. |
| 80 | * If the nearest timer timed out return NULL and then we will |
| 81 | * dispatch everything after the select |
| 82 | */ |
Harald Welte | 7e82020 | 2011-07-16 10:15:16 +0200 | [diff] [blame] | 83 | struct timeval *osmo_timers_nearest(void) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 84 | { |
| 85 | struct timeval current_time; |
| 86 | |
| 87 | if (s_nearest_time.tv_sec == 0 && s_nearest_time.tv_usec == 0) |
| 88 | return NULL; |
| 89 | |
| 90 | if (gettimeofday(¤t_time, NULL) == -1) |
| 91 | return NULL; |
| 92 | |
| 93 | unsigned long long nearestTime = s_nearest_time.tv_sec * MICRO_SECONDS + s_nearest_time.tv_usec; |
| 94 | unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec; |
| 95 | |
| 96 | if (nearestTime < currentTime) { |
| 97 | s_select_time.tv_sec = 0; |
| 98 | s_select_time.tv_usec = 0; |
| 99 | } else { |
| 100 | s_select_time.tv_sec = (nearestTime - currentTime) / MICRO_SECONDS; |
| 101 | s_select_time.tv_usec = (nearestTime - currentTime) % MICRO_SECONDS; |
| 102 | } |
| 103 | |
| 104 | return &s_select_time; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Find the nearest time and update s_nearest_time |
| 109 | */ |
Harald Welte | 7e82020 | 2011-07-16 10:15:16 +0200 | [diff] [blame] | 110 | void osmo_timers_prepare(void) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 111 | { |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 112 | struct osmo_timer_list *timer, *nearest_timer = NULL; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 113 | llist_for_each_entry(timer, &timer_list, entry) { |
| 114 | if (!nearest_timer || TIME_SMALLER(timer->timeout, nearest_timer->timeout)) { |
| 115 | nearest_timer = timer; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (nearest_timer) { |
| 120 | s_nearest_time = nearest_timer->timeout; |
| 121 | } else { |
| 122 | memset(&s_nearest_time, 0, sizeof(struct timeval)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * fire all timers... and remove them |
| 128 | */ |
Harald Welte | 7e82020 | 2011-07-16 10:15:16 +0200 | [diff] [blame] | 129 | int osmo_timers_update(void) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 130 | { |
| 131 | struct timeval current_time; |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 132 | struct osmo_timer_list *timer, *tmp; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 133 | int work = 0; |
| 134 | |
| 135 | gettimeofday(¤t_time, NULL); |
| 136 | |
| 137 | /* |
| 138 | * The callbacks might mess with our list and in this case |
| 139 | * even llist_for_each_entry_safe is not safe to use. To allow |
| 140 | * del_timer, add_timer, schedule_timer to be called from within |
| 141 | * the callback we jump through some loops. |
| 142 | * |
| 143 | * First we set the handled flag of each active timer to zero, |
| 144 | * then we iterate over the list and execute the callbacks. As the |
| 145 | * list might have been changed (specially the next) from within |
| 146 | * the callback we have to start over again. Once every callback |
| 147 | * is dispatched we will remove the non-active from the list. |
| 148 | * |
| 149 | * TODO: If this is a performance issue we can poison a global |
| 150 | * variable in add_timer and del_timer and only then restart. |
| 151 | */ |
| 152 | llist_for_each_entry(timer, &timer_list, entry) { |
| 153 | timer->handled = 0; |
| 154 | } |
| 155 | |
| 156 | restart: |
| 157 | llist_for_each_entry(timer, &timer_list, entry) { |
| 158 | if (!timer->handled && TIME_SMALLER(timer->timeout, current_time)) { |
| 159 | timer->handled = 1; |
| 160 | timer->active = 0; |
| 161 | (*timer->cb)(timer->data); |
| 162 | work = 1; |
| 163 | goto restart; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | llist_for_each_entry_safe(timer, tmp, &timer_list, entry) { |
| 168 | timer->handled = 0; |
| 169 | if (!timer->active) { |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 170 | osmo_timer_del(timer); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | return work; |
| 175 | } |
| 176 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 177 | int osmo_timers_check(void) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 178 | { |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 179 | struct osmo_timer_list *timer; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 180 | int i = 0; |
| 181 | |
| 182 | llist_for_each_entry(timer, &timer_list, entry) { |
| 183 | i++; |
| 184 | } |
| 185 | return i; |
| 186 | } |