blob: 30f558b41e6b43184a8f576f62b96ee06afb855e [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
Harald Welteec8b4502010-02-20 20:34:29 +010029#ifndef TIMER_H
30#define TIMER_H
31
32#include <sys/time.h>
33
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
41 * use schedule_timer to schedule a timer in
42 * x seconds and microseconds from now...
43 * - Use del_timer to remove the timer
44 *
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
49 * - update_timers will call the callbacks and remove
50 * the timers.
51 *
52 */
Harald Weltebd598e32011-08-16 23:26:52 +020053/*! \brief A structure representing a single instance of a timer */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020054struct osmo_timer_list {
Pablo Neira Ayuso066c9122011-09-26 11:45:03 +020055 struct rb_node node; /*!< \brief rb-tree node header */
56 struct llist_head list; /*!< \brief internal list header */
Harald Weltebd598e32011-08-16 23:26:52 +020057 struct timeval timeout; /*!< \brief expiration time */
58 unsigned int active : 1; /*!< \brief is it active? */
Harald Welteec8b4502010-02-20 20:34:29 +010059
Harald Weltebd598e32011-08-16 23:26:52 +020060 void (*cb)(void*); /*!< \brief call-back called at timeout */
61 void *data; /*!< \brief 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 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
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020074int osmo_timer_pending(struct osmo_timer_list *timer);
Harald Welteec8b4502010-02-20 20:34:29 +010075
76
Harald Weltebd598e32011-08-16 23:26:52 +020077/*
Harald Welteec8b4502010-02-20 20:34:29 +010078 * internal timer list management
79 */
Harald Welte7e820202011-07-16 10:15:16 +020080struct timeval *osmo_timers_nearest(void);
81void osmo_timers_prepare(void);
82int osmo_timers_update(void);
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020083int osmo_timers_check(void);
Harald Welteec8b4502010-02-20 20:34:29 +010084
Harald Welteba6988b2011-08-17 12:46:48 +020085/*! }@ */
86
Harald Welteec8b4502010-02-20 20:34:29 +010087#endif