blob: a039ac82db3a5606531873c07b5f7eb6727ef654 [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
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020022 * Timer management:
23 * - Create a struct osmo_timer_list
24 * - Fill out timeout and use osmo_timer_add(), or
25 * use osmo_timer_schedule() to schedule a timer in
26 * x seconds and microseconds from now...
27 * - Use osmo_timer_del() to remove the timer
28 *
29 * Internally:
30 * - We hook into select.c to give a timeval of the
31 * nearest timer. On already passed timers we give
32 * it a 0 to immediately fire after the select
33 * - osmo_timers_update() will call the callbacks and
34 * remove the timers.
Harald Welteba6988b2011-08-17 12:46:48 +020035 * @{
36 */
37
Harald Weltebd598e32011-08-16 23:26:52 +020038/*! \file timer.h
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020039 * Osmocom timer handling routines.
Harald Weltebd598e32011-08-16 23:26:52 +020040 */
41
Sylvain Munaut12ba7782014-06-16 10:13:40 +020042#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +010043
44#include <sys/time.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020045#include <stdbool.h>
Harald Welteec8b4502010-02-20 20:34:29 +010046
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010047#include <osmocom/core/linuxlist.h>
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020048#include <osmocom/core/linuxrbtree.h>
Harald Welteec8b4502010-02-20 20:34:29 +010049
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050/*! A structure representing a single instance of a timer */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020051struct osmo_timer_list {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052 struct rb_node node; /*!< rb-tree node header */
53 struct llist_head list; /*!< internal list header */
54 struct timeval timeout; /*!< expiration time */
55 unsigned int active : 1; /*!< is it active? */
Harald Welteec8b4502010-02-20 20:34:29 +010056
Neels Hofmeyr87e45502017-06-20 00:17:59 +020057 void (*cb)(void*); /*!< call-back called at timeout */
58 void *data; /*!< user data for callback */
Harald Welteec8b4502010-02-20 20:34:29 +010059};
60
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020061/*
Harald Welteec8b4502010-02-20 20:34:29 +010062 * timer management
63 */
Harald Weltebd598e32011-08-16 23:26:52 +020064
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +020065void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data);
66
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020067void osmo_timer_add(struct osmo_timer_list *timer);
Harald Weltebd598e32011-08-16 23:26:52 +020068
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020069void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds);
Harald Weltebd598e32011-08-16 23:26:52 +020070
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020071void osmo_timer_del(struct osmo_timer_list *timer);
Harald Weltebd598e32011-08-16 23:26:52 +020072
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020073int osmo_timer_pending(struct osmo_timer_list *timer);
Harald Welteec8b4502010-02-20 20:34:29 +010074
Harald Weltee30b6ac2012-07-13 12:21:04 +020075int osmo_timer_remaining(const struct osmo_timer_list *timer,
76 const struct timeval *now,
77 struct timeval *remaining);
Harald Weltebd598e32011-08-16 23:26:52 +020078/*
Harald Welteec8b4502010-02-20 20:34:29 +010079 * internal timer list management
80 */
Harald Welte7e820202011-07-16 10:15:16 +020081struct timeval *osmo_timers_nearest(void);
82void osmo_timers_prepare(void);
83int osmo_timers_update(void);
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020084int osmo_timers_check(void);
Harald Welteec8b4502010-02-20 20:34:29 +010085
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020086int osmo_gettimeofday(struct timeval *tv, struct timezone *tz);
87
Neels Hofmeyr33370cb2017-06-20 04:32:34 +020088/*
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020089 * timer override
90 */
91
92extern bool osmo_gettimeofday_override;
93extern struct timeval osmo_gettimeofday_override_time;
94void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs);
95
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020096/*! @} */