blob: 28785dbd63d3bc67ca13e1f1bf1546f41ec3c1f7 [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/*
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 Welteba6988b2011-08-17 12:46:48 +020021/*! \defgroup timer Osmocom timers
22 * @{
23 */
24
Harald Weltebd598e32011-08-16 23:26:52 +020025/*! \file timer.h
Neels Hofmeyr87e45502017-06-20 00:17:59 +020026 * Osmocom timer handling routines
Harald Weltebd598e32011-08-16 23:26:52 +020027 */
28
Sylvain Munaut12ba7782014-06-16 10:13:40 +020029#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +010030
31#include <sys/time.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020032#include <stdbool.h>
Harald Welteec8b4502010-02-20 20:34:29 +010033
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010034#include <osmocom/core/linuxlist.h>
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020035#include <osmocom/core/linuxrbtree.h>
Harald Welteec8b4502010-02-20 20:34:29 +010036
37/**
38 * Timer management:
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020039 * - Create a struct osmo_timer_list
Harald Welteec8b4502010-02-20 20:34:29 +010040 * - Fill out timeout and use add_timer or
Neels Hofmeyr667e83d2015-11-02 20:18:11 +010041 * use osmo_timer_schedule to schedule a timer in
Harald Welteec8b4502010-02-20 20:34:29 +010042 * x seconds and microseconds from now...
Neels Hofmeyr667e83d2015-11-02 20:18:11 +010043 * - Use osmo_timer_del to remove the timer
Harald Welteec8b4502010-02-20 20:34:29 +010044 *
45 * Internally:
46 * - We hook into select.c to give a timeval of the
47 * nearest timer. On already passed timers we give
48 * it a 0 to immediately fire after the select
Neels Hofmeyr667e83d2015-11-02 20:18:11 +010049 * - osmo_timers_update will call the callbacks and
50 * remove the timers.
Harald Welteec8b4502010-02-20 20:34:29 +010051 *
52 */
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053/*! A structure representing a single instance of a timer */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020054struct osmo_timer_list {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020055 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 Welteec8b4502010-02-20 20:34:29 +010059
Neels Hofmeyr87e45502017-06-20 00:17:59 +020060 void (*cb)(void*); /*!< call-back called at timeout */
61 void *data; /*!< user data for callback */
Harald Welteec8b4502010-02-20 20:34:29 +010062};
63
64/**
65 * timer management
66 */
Harald Weltebd598e32011-08-16 23:26:52 +020067
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +020068void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data);
69
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020070void osmo_timer_add(struct osmo_timer_list *timer);
Harald Weltebd598e32011-08-16 23:26:52 +020071
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020072void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds);
Harald Weltebd598e32011-08-16 23:26:52 +020073
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020074void osmo_timer_del(struct osmo_timer_list *timer);
Harald Weltebd598e32011-08-16 23:26:52 +020075
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020076int osmo_timer_pending(struct osmo_timer_list *timer);
Harald Welteec8b4502010-02-20 20:34:29 +010077
Harald Weltee30b6ac2012-07-13 12:21:04 +020078int osmo_timer_remaining(const struct osmo_timer_list *timer,
79 const struct timeval *now,
80 struct timeval *remaining);
Harald Weltebd598e32011-08-16 23:26:52 +020081/*
Harald Welteec8b4502010-02-20 20:34:29 +010082 * internal timer list management
83 */
Harald Welte7e820202011-07-16 10:15:16 +020084struct timeval *osmo_timers_nearest(void);
85void osmo_timers_prepare(void);
86int osmo_timers_update(void);
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020087int osmo_timers_check(void);
Harald Welteec8b4502010-02-20 20:34:29 +010088
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020089int osmo_gettimeofday(struct timeval *tv, struct timezone *tz);
90
91/**
92 * 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
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020099/*! @} */