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 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 21 | /*! \defgroup timer Osmocom timers |
| 22 | * @{ |
| 23 | */ |
| 24 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 25 | /*! \file timer.h |
| 26 | * \brief Osmocom timer handling routines |
| 27 | */ |
| 28 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 29 | #pragma once |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 30 | |
| 31 | #include <sys/time.h> |
| 32 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 33 | #include <osmocom/core/linuxlist.h> |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 34 | #include <osmocom/core/linuxrbtree.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Timer management: |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 38 | * - Create a struct osmo_timer_list |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 39 | * - Fill out timeout and use add_timer or |
Neels Hofmeyr | 667e83d | 2015-11-02 20:18:11 +0100 | [diff] [blame] | 40 | * use osmo_timer_schedule to schedule a timer in |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 41 | * x seconds and microseconds from now... |
Neels Hofmeyr | 667e83d | 2015-11-02 20:18:11 +0100 | [diff] [blame] | 42 | * - Use osmo_timer_del to remove the timer |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 43 | * |
| 44 | * Internally: |
| 45 | * - We hook into select.c to give a timeval of the |
| 46 | * nearest timer. On already passed timers we give |
| 47 | * it a 0 to immediately fire after the select |
Neels Hofmeyr | 667e83d | 2015-11-02 20:18:11 +0100 | [diff] [blame] | 48 | * - osmo_timers_update will call the callbacks and |
| 49 | * remove the timers. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 50 | * |
| 51 | */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 52 | /*! \brief A structure representing a single instance of a timer */ |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 53 | struct osmo_timer_list { |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 54 | struct rb_node node; /*!< \brief rb-tree node header */ |
| 55 | struct llist_head list; /*!< \brief internal list header */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 56 | struct timeval timeout; /*!< \brief expiration time */ |
| 57 | unsigned int active : 1; /*!< \brief is it active? */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 58 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 59 | void (*cb)(void*); /*!< \brief call-back called at timeout */ |
| 60 | void *data; /*!< \brief user data for callback */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * timer management |
| 65 | */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 66 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 67 | void osmo_timer_add(struct osmo_timer_list *timer); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 68 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 69 | void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 70 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 71 | void osmo_timer_del(struct osmo_timer_list *timer); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 72 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 73 | int osmo_timer_pending(struct osmo_timer_list *timer); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 74 | |
Harald Welte | e30b6ac | 2012-07-13 12:21:04 +0200 | [diff] [blame] | 75 | int osmo_timer_remaining(const struct osmo_timer_list *timer, |
| 76 | const struct timeval *now, |
| 77 | struct timeval *remaining); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 78 | /* |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 79 | * internal timer list management |
| 80 | */ |
Harald Welte | 7e82020 | 2011-07-16 10:15:16 +0200 | [diff] [blame] | 81 | struct timeval *osmo_timers_nearest(void); |
| 82 | void osmo_timers_prepare(void); |
| 83 | int osmo_timers_update(void); |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 84 | int osmo_timers_check(void); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 85 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 86 | /*! @} */ |