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> |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 43 | #include <stdbool.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 44 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 45 | #include <osmocom/core/linuxlist.h> |
Pablo Neira Ayuso | 066c912 | 2011-09-26 11:45:03 +0200 | [diff] [blame] | 46 | #include <osmocom/core/linuxrbtree.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 47 | |
Max | 383c563 | 2017-09-25 16:37:37 +0200 | [diff] [blame] | 48 | /* convert absolute time (in seconds) to elapsed days/hours/minutes */ |
| 49 | #define OSMO_SEC2MIN(sec) ((sec % (60 * 60)) / 60) |
| 50 | #define OSMO_SEC2HRS(sec) ((sec % (60 * 60 * 24)) / (60 * 60)) |
| 51 | #define OSMO_SEC2DAY(sec) ((sec % (60 * 60 * 24 * 365)) / (60 * 60 * 24)) /* we ignore leap year for simplicity */ |
| 52 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 53 | /*! A structure representing a single instance of a timer */ |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 54 | struct osmo_timer_list { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 55 | struct rb_node node; /*!< rb-tree node header */ |
| 56 | struct llist_head list; /*!< internal list header */ |
| 57 | struct timeval timeout; /*!< expiration time */ |
| 58 | unsigned int active : 1; /*!< is it active? */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 59 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 60 | void (*cb)(void*); /*!< call-back called at timeout */ |
| 61 | void *data; /*!< user data for callback */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
Neels Hofmeyr | 33370cb | 2017-06-20 04:32:34 +0200 | [diff] [blame] | 64 | /* |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 65 | * timer management |
| 66 | */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 67 | |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 68 | void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data); |
| 69 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 70 | void osmo_timer_add(struct osmo_timer_list *timer); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 71 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 72 | 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] | 73 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 74 | void osmo_timer_del(struct osmo_timer_list *timer); |
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 | int osmo_timer_pending(struct osmo_timer_list *timer); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 77 | |
Harald Welte | e30b6ac | 2012-07-13 12:21:04 +0200 | [diff] [blame] | 78 | int osmo_timer_remaining(const struct osmo_timer_list *timer, |
| 79 | const struct timeval *now, |
| 80 | struct timeval *remaining); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 81 | /* |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 82 | * internal timer list management |
| 83 | */ |
Harald Welte | 7e82020 | 2011-07-16 10:15:16 +0200 | [diff] [blame] | 84 | struct timeval *osmo_timers_nearest(void); |
| 85 | void osmo_timers_prepare(void); |
| 86 | int osmo_timers_update(void); |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 87 | int osmo_timers_check(void); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 88 | |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 89 | int osmo_gettimeofday(struct timeval *tv, struct timezone *tz); |
| 90 | |
Neels Hofmeyr | 33370cb | 2017-06-20 04:32:34 +0200 | [diff] [blame] | 91 | /* |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 92 | * timer override |
| 93 | */ |
| 94 | |
| 95 | extern bool osmo_gettimeofday_override; |
| 96 | extern struct timeval osmo_gettimeofday_override_time; |
| 97 | void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs); |
| 98 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 99 | /*! @} */ |