blob: d7343576325423b030c917fbff5d768cd9547a0d [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
26 * \brief Osmocom timer handling routines
27 */
28
Sylvain Munaut12ba7782014-06-16 10:13:40 +020029#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +010030
31#include <sys/time.h>
32
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010033#include <osmocom/core/linuxlist.h>
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020034#include <osmocom/core/linuxrbtree.h>
Harald Welteec8b4502010-02-20 20:34:29 +010035
36/**
37 * Timer management:
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020038 * - Create a struct osmo_timer_list
Harald Welteec8b4502010-02-20 20:34:29 +010039 * - Fill out timeout and use add_timer or
40 * use schedule_timer to schedule a timer in
41 * x seconds and microseconds from now...
42 * - Use del_timer to remove the timer
43 *
44 * Internally:
45 * - We hook into select.c to give a timeval of the
46 * nearest timer. On already passed timers we give
47 * it a 0 to immediately fire after the select
48 * - update_timers will call the callbacks and remove
49 * the timers.
50 *
51 */
Harald Weltebd598e32011-08-16 23:26:52 +020052/*! \brief A structure representing a single instance of a timer */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020053struct osmo_timer_list {
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020054 struct rb_node node; /*!< \brief rb-tree node header */
55 struct llist_head list; /*!< \brief internal list header */
Harald Weltebd598e32011-08-16 23:26:52 +020056 struct timeval timeout; /*!< \brief expiration time */
57 unsigned int active : 1; /*!< \brief is it active? */
Harald Welteec8b4502010-02-20 20:34:29 +010058
Harald Weltebd598e32011-08-16 23:26:52 +020059 void (*cb)(void*); /*!< \brief call-back called at timeout */
60 void *data; /*!< \brief user data for callback */
Harald Welteec8b4502010-02-20 20:34:29 +010061};
62
63/**
64 * timer management
65 */
Harald Weltebd598e32011-08-16 23:26:52 +020066
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
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020086/*! @} */