blob: 1979766263475abe4fcfc4798c73941a94b1494f [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file timer.h
2 * Osmocom timer handling routines. */
Harald Welteec8b4502010-02-20 20:34:29 +01003/*
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 Welteba6988b2011-08-17 12:46:48 +020023/*! \defgroup timer Osmocom timers
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020024 * 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 Welteba6988b2011-08-17 12:46:48 +020037 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020038 * \file timer.h */
Harald Weltebd598e32011-08-16 23:26:52 +020039
Sylvain Munaut12ba7782014-06-16 10:13:40 +020040#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +010041
42#include <sys/time.h>
Pau Espin Pedrol87fade82018-02-26 19:42:22 +010043#include <time.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020044#include <stdbool.h>
Harald Welteec8b4502010-02-20 20:34:29 +010045
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010046#include <osmocom/core/linuxlist.h>
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020047#include <osmocom/core/linuxrbtree.h>
Pau Espin Pedrol2ca8ceb2018-12-09 01:26:55 +010048#include <osmocom/core/timer_compat.h>
Harald Welteec8b4502010-02-20 20:34:29 +010049
Max383c5632017-09-25 16:37:37 +020050/* 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 Hofmeyr87e45502017-06-20 00:17:59 +020055/*! A structure representing a single instance of a timer */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020056struct osmo_timer_list {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020057 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 Welteec8b4502010-02-20 20:34:29 +010061
Neels Hofmeyr87e45502017-06-20 00:17:59 +020062 void (*cb)(void*); /*!< call-back called at timeout */
63 void *data; /*!< user data for callback */
Harald Welteec8b4502010-02-20 20:34:29 +010064};
65
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020066/*
Harald Welteec8b4502010-02-20 20:34:29 +010067 * timer management
68 */
Harald Weltebd598e32011-08-16 23:26:52 +020069
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +020070void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data);
71
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020072void osmo_timer_add(struct osmo_timer_list *timer);
Harald Weltebd598e32011-08-16 23:26:52 +020073
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020074void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds);
Harald Weltebd598e32011-08-16 23:26:52 +020075
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020076void osmo_timer_del(struct osmo_timer_list *timer);
Harald Weltebd598e32011-08-16 23:26:52 +020077
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020078int osmo_timer_pending(struct osmo_timer_list *timer);
Harald Welteec8b4502010-02-20 20:34:29 +010079
Harald Weltee30b6ac2012-07-13 12:21:04 +020080int osmo_timer_remaining(const struct osmo_timer_list *timer,
81 const struct timeval *now,
82 struct timeval *remaining);
Harald Weltebd598e32011-08-16 23:26:52 +020083/*
Harald Welteec8b4502010-02-20 20:34:29 +010084 * internal timer list management
85 */
Harald Welte7e820202011-07-16 10:15:16 +020086struct timeval *osmo_timers_nearest(void);
87void osmo_timers_prepare(void);
88int osmo_timers_update(void);
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020089int osmo_timers_check(void);
Harald Welteec8b4502010-02-20 20:34:29 +010090
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020091int osmo_gettimeofday(struct timeval *tv, struct timezone *tz);
Pau Espin Pedrol87fade82018-02-26 19:42:22 +010092int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020093
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020094/*
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020095 * timer override
96 */
97
98extern bool osmo_gettimeofday_override;
99extern struct timeval osmo_gettimeofday_override_time;
100void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs);
101
Pau Espin Pedrol87fade82018-02-26 19:42:22 +0100102void osmo_clock_override_enable(clockid_t clk_id, bool enable);
103void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs);
104struct timespec *osmo_clock_override_gettimespec(clockid_t clk_id);
105
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200106/*! @} */