Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file timer.h |
| 2 | * Osmocom timer handling routines. */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 3 | /* |
| 4 | * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 23 | /*! \defgroup timer Osmocom timers |
Neels Hofmeyr | 33370cb | 2017-06-20 04:32:34 +0200 | [diff] [blame] | 24 | * Timer management: |
| 25 | * - Create a struct osmo_timer_list |
| 26 | * - Fill out timeout and use osmo_timer_add(), or |
| 27 | * use osmo_timer_schedule() to schedule a timer in |
| 28 | * x seconds and microseconds from now... |
| 29 | * - Use osmo_timer_del() to remove the timer |
| 30 | * |
| 31 | * Internally: |
| 32 | * - We hook into select.c to give a timeval of the |
| 33 | * nearest timer. On already passed timers we give |
| 34 | * it a 0 to immediately fire after the select |
| 35 | * - osmo_timers_update() will call the callbacks and |
| 36 | * remove the timers. |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 37 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 38 | * \file timer.h */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 39 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 40 | #pragma once |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 41 | |
| 42 | #include <sys/time.h> |
Pau Espin Pedrol | 87fade8 | 2018-02-26 19:42:22 +0100 | [diff] [blame] | 43 | #include <time.h> |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 44 | #include <stdbool.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 45 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 46 | #include <osmocom/core/linuxlist.h> |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 47 | #include <osmocom/core/linuxrbtree.h> |
Pau Espin Pedrol | 2ca8ceb | 2018-12-09 01:26:55 +0100 | [diff] [blame] | 48 | #include <osmocom/core/timer_compat.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 49 | |
Max | 383c563 | 2017-09-25 16:37:37 +0200 | [diff] [blame] | 50 | /* convert absolute time (in seconds) to elapsed days/hours/minutes */ |
| 51 | #define OSMO_SEC2MIN(sec) ((sec % (60 * 60)) / 60) |
| 52 | #define OSMO_SEC2HRS(sec) ((sec % (60 * 60 * 24)) / (60 * 60)) |
| 53 | #define OSMO_SEC2DAY(sec) ((sec % (60 * 60 * 24 * 365)) / (60 * 60 * 24)) /* we ignore leap year for simplicity */ |
| 54 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 55 | /*! A structure representing a single instance of a timer */ |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 56 | struct osmo_timer_list { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 57 | struct rb_node node; /*!< rb-tree node header */ |
| 58 | struct llist_head list; /*!< internal list header */ |
| 59 | struct timeval timeout; /*!< expiration time */ |
| 60 | unsigned int active : 1; /*!< is it active? */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 61 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 62 | void (*cb)(void*); /*!< call-back called at timeout */ |
| 63 | void *data; /*!< user data for callback */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
Neels Hofmeyr | 33370cb | 2017-06-20 04:32:34 +0200 | [diff] [blame] | 66 | /* |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 67 | * timer management |
| 68 | */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 69 | |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 70 | void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data); |
| 71 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 72 | void osmo_timer_add(struct osmo_timer_list *timer); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 73 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 74 | 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] | 75 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 76 | void osmo_timer_del(struct osmo_timer_list *timer); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 77 | |
Pau Espin Pedrol | a488639 | 2021-04-29 16:36:53 +0200 | [diff] [blame] | 78 | int osmo_timer_pending(const struct osmo_timer_list *timer); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 79 | |
Harald Welte | e30b6ac | 2012-07-13 12:21:04 +0200 | [diff] [blame] | 80 | int osmo_timer_remaining(const struct osmo_timer_list *timer, |
| 81 | const struct timeval *now, |
| 82 | struct timeval *remaining); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 83 | /* |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 84 | * internal timer list management |
| 85 | */ |
Harald Welte | 7e82020 | 2011-07-16 10:15:16 +0200 | [diff] [blame] | 86 | struct timeval *osmo_timers_nearest(void); |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 87 | int osmo_timers_nearest_ms(void); |
Harald Welte | 7e82020 | 2011-07-16 10:15:16 +0200 | [diff] [blame] | 88 | void osmo_timers_prepare(void); |
| 89 | int osmo_timers_update(void); |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 90 | int osmo_timers_check(void); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 91 | |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 92 | int osmo_gettimeofday(struct timeval *tv, struct timezone *tz); |
Pau Espin Pedrol | 87fade8 | 2018-02-26 19:42:22 +0100 | [diff] [blame] | 93 | int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp); |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 94 | |
Neels Hofmeyr | 33370cb | 2017-06-20 04:32:34 +0200 | [diff] [blame] | 95 | /* |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 96 | * timer override |
| 97 | */ |
| 98 | |
| 99 | extern bool osmo_gettimeofday_override; |
| 100 | extern struct timeval osmo_gettimeofday_override_time; |
| 101 | void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs); |
| 102 | |
Pau Espin Pedrol | 87fade8 | 2018-02-26 19:42:22 +0100 | [diff] [blame] | 103 | void osmo_clock_override_enable(clockid_t clk_id, bool enable); |
| 104 | void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs); |
| 105 | struct timespec *osmo_clock_override_gettimespec(clockid_t clk_id); |
| 106 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 107 | /*! @} */ |