blob: 40b39b90ca10cb1303a870274676e700a5ebd2c9 [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>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020043#include <stdbool.h>
Harald Welteec8b4502010-02-20 20:34:29 +010044
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010045#include <osmocom/core/linuxlist.h>
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020046#include <osmocom/core/linuxrbtree.h>
Harald Welteec8b4502010-02-20 20:34:29 +010047
Neels Hofmeyr87e45502017-06-20 00:17:59 +020048/*! A structure representing a single instance of a timer */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020049struct osmo_timer_list {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050 struct rb_node node; /*!< rb-tree node header */
51 struct llist_head list; /*!< internal list header */
52 struct timeval timeout; /*!< expiration time */
53 unsigned int active : 1; /*!< is it active? */
Harald Welteec8b4502010-02-20 20:34:29 +010054
Neels Hofmeyr87e45502017-06-20 00:17:59 +020055 void (*cb)(void*); /*!< call-back called at timeout */
56 void *data; /*!< user data for callback */
Harald Welteec8b4502010-02-20 20:34:29 +010057};
58
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020059/*
Harald Welteec8b4502010-02-20 20:34:29 +010060 * timer management
61 */
Harald Weltebd598e32011-08-16 23:26:52 +020062
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +020063void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data);
64
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020065void osmo_timer_add(struct osmo_timer_list *timer);
Harald Weltebd598e32011-08-16 23:26:52 +020066
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020067void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds);
Harald Weltebd598e32011-08-16 23:26:52 +020068
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020069void osmo_timer_del(struct osmo_timer_list *timer);
Harald Weltebd598e32011-08-16 23:26:52 +020070
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020071int osmo_timer_pending(struct osmo_timer_list *timer);
Harald Welteec8b4502010-02-20 20:34:29 +010072
Harald Weltee30b6ac2012-07-13 12:21:04 +020073int osmo_timer_remaining(const struct osmo_timer_list *timer,
74 const struct timeval *now,
75 struct timeval *remaining);
Harald Weltebd598e32011-08-16 23:26:52 +020076/*
Harald Welteec8b4502010-02-20 20:34:29 +010077 * internal timer list management
78 */
Harald Welte7e820202011-07-16 10:15:16 +020079struct timeval *osmo_timers_nearest(void);
80void osmo_timers_prepare(void);
81int osmo_timers_update(void);
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020082int osmo_timers_check(void);
Harald Welteec8b4502010-02-20 20:34:29 +010083
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020084int osmo_gettimeofday(struct timeval *tv, struct timezone *tz);
85
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020086/*
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020087 * timer override
88 */
89
90extern bool osmo_gettimeofday_override;
91extern struct timeval osmo_gettimeofday_override_time;
92void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs);
93
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020094/*! @} */