blob: 16338dad5151ccebf6833fc53564fa3f03451bbe [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 *
Harald Welteec8b4502010-02-20 20:34:29 +010017 */
18
Harald Welteba6988b2011-08-17 12:46:48 +020019/*! \defgroup timer Osmocom timers
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020020 * Timer management:
21 * - Create a struct osmo_timer_list
22 * - Fill out timeout and use osmo_timer_add(), or
23 * use osmo_timer_schedule() to schedule a timer in
24 * x seconds and microseconds from now...
25 * - Use osmo_timer_del() to remove the timer
26 *
27 * Internally:
28 * - We hook into select.c to give a timeval of the
29 * nearest timer. On already passed timers we give
30 * it a 0 to immediately fire after the select
31 * - osmo_timers_update() will call the callbacks and
32 * remove the timers.
Harald Welteba6988b2011-08-17 12:46:48 +020033 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020034 * \file timer.h */
Harald Weltebd598e32011-08-16 23:26:52 +020035
Sylvain Munaut12ba7782014-06-16 10:13:40 +020036#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +010037
38#include <sys/time.h>
Pau Espin Pedrol87fade82018-02-26 19:42:22 +010039#include <time.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020040#include <stdbool.h>
Harald Welteec8b4502010-02-20 20:34:29 +010041
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010042#include <osmocom/core/linuxlist.h>
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020043#include <osmocom/core/linuxrbtree.h>
Pau Espin Pedrol2ca8ceb2018-12-09 01:26:55 +010044#include <osmocom/core/timer_compat.h>
Harald Welteec8b4502010-02-20 20:34:29 +010045
Max383c5632017-09-25 16:37:37 +020046/* convert absolute time (in seconds) to elapsed days/hours/minutes */
47#define OSMO_SEC2MIN(sec) ((sec % (60 * 60)) / 60)
48#define OSMO_SEC2HRS(sec) ((sec % (60 * 60 * 24)) / (60 * 60))
49#define OSMO_SEC2DAY(sec) ((sec % (60 * 60 * 24 * 365)) / (60 * 60 * 24)) /* we ignore leap year for simplicity */
50
Neels Hofmeyr87e45502017-06-20 00:17:59 +020051/*! A structure representing a single instance of a timer */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020052struct osmo_timer_list {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053 struct rb_node node; /*!< rb-tree node header */
54 struct llist_head list; /*!< internal list header */
55 struct timeval timeout; /*!< expiration time */
56 unsigned int active : 1; /*!< is it active? */
Harald Welteec8b4502010-02-20 20:34:29 +010057
Neels Hofmeyr87e45502017-06-20 00:17:59 +020058 void (*cb)(void*); /*!< call-back called at timeout */
59 void *data; /*!< user data for callback */
Harald Welteec8b4502010-02-20 20:34:29 +010060};
61
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020062/*
Harald Welteec8b4502010-02-20 20:34:29 +010063 * timer management
64 */
Harald Weltebd598e32011-08-16 23:26:52 +020065
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +020066void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data);
67
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020068void osmo_timer_add(struct osmo_timer_list *timer);
Harald Weltebd598e32011-08-16 23:26:52 +020069
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020070void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds);
Harald Weltebd598e32011-08-16 23:26:52 +020071
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020072void osmo_timer_del(struct osmo_timer_list *timer);
Harald Weltebd598e32011-08-16 23:26:52 +020073
Pau Espin Pedrola4886392021-04-29 16:36:53 +020074int osmo_timer_pending(const struct osmo_timer_list *timer);
Harald Welteec8b4502010-02-20 20:34:29 +010075
Harald Weltee30b6ac2012-07-13 12:21:04 +020076int osmo_timer_remaining(const struct osmo_timer_list *timer,
77 const struct timeval *now,
78 struct timeval *remaining);
Harald Weltebd598e32011-08-16 23:26:52 +020079/*
Harald Welteec8b4502010-02-20 20:34:29 +010080 * internal timer list management
81 */
Harald Welte7e820202011-07-16 10:15:16 +020082struct timeval *osmo_timers_nearest(void);
Harald Welteb904e422020-10-18 21:24:13 +020083int osmo_timers_nearest_ms(void);
Harald Welte7e820202011-07-16 10:15:16 +020084void osmo_timers_prepare(void);
85int osmo_timers_update(void);
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020086int osmo_timers_check(void);
Harald Welteec8b4502010-02-20 20:34:29 +010087
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020088int osmo_gettimeofday(struct timeval *tv, struct timezone *tz);
Pau Espin Pedrol87fade82018-02-26 19:42:22 +010089int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020090
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020091/*
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020092 * timer override
93 */
94
95extern bool osmo_gettimeofday_override;
96extern struct timeval osmo_gettimeofday_override_time;
97void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs);
98
Pau Espin Pedrol87fade82018-02-26 19:42:22 +010099void osmo_clock_override_enable(clockid_t clk_id, bool enable);
100void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs);
101struct timespec *osmo_clock_override_gettimespec(clockid_t clk_id);
102
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200103/*! @} */