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 |
Neels Hofmeyr | 33370cb | 2017-06-20 04:32:34 +0200 | [diff] [blame^] | 22 | * Timer management: |
| 23 | * - Create a struct osmo_timer_list |
| 24 | * - Fill out timeout and use osmo_timer_add(), or |
| 25 | * use osmo_timer_schedule() to schedule a timer in |
| 26 | * x seconds and microseconds from now... |
| 27 | * - Use osmo_timer_del() to remove the timer |
| 28 | * |
| 29 | * Internally: |
| 30 | * - We hook into select.c to give a timeval of the |
| 31 | * nearest timer. On already passed timers we give |
| 32 | * it a 0 to immediately fire after the select |
| 33 | * - osmo_timers_update() will call the callbacks and |
| 34 | * remove the timers. |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 35 | * @{ |
| 36 | */ |
| 37 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 38 | /*! \file timer.h |
Neels Hofmeyr | 33370cb | 2017-06-20 04:32:34 +0200 | [diff] [blame^] | 39 | * Osmocom timer handling routines. |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 40 | */ |
| 41 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 42 | #pragma once |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 43 | |
| 44 | #include <sys/time.h> |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 45 | #include <stdbool.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 46 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 47 | #include <osmocom/core/linuxlist.h> |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 48 | #include <osmocom/core/linuxrbtree.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 49 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 50 | /*! A structure representing a single instance of a timer */ |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 51 | struct osmo_timer_list { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 52 | struct rb_node node; /*!< rb-tree node header */ |
| 53 | struct llist_head list; /*!< internal list header */ |
| 54 | struct timeval timeout; /*!< expiration time */ |
| 55 | unsigned int active : 1; /*!< is it active? */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 56 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 57 | void (*cb)(void*); /*!< call-back called at timeout */ |
| 58 | void *data; /*!< user data for callback */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
Neels Hofmeyr | 33370cb | 2017-06-20 04:32:34 +0200 | [diff] [blame^] | 61 | /* |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 62 | * timer management |
| 63 | */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 64 | |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 65 | void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data); |
| 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 | |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 86 | int osmo_gettimeofday(struct timeval *tv, struct timezone *tz); |
| 87 | |
Neels Hofmeyr | 33370cb | 2017-06-20 04:32:34 +0200 | [diff] [blame^] | 88 | /* |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 89 | * timer override |
| 90 | */ |
| 91 | |
| 92 | extern bool osmo_gettimeofday_override; |
| 93 | extern struct timeval osmo_gettimeofday_override_time; |
| 94 | void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs); |
| 95 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 96 | /*! @} */ |