blob: 326a0c8862546d584009d4c9ebf7e230856db8a3 [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 Weltebd598e32011-08-16 23:26:52 +020021/*! \file timer.h
22 * \brief Osmocom timer handling routines
23 */
24
Harald Welteec8b4502010-02-20 20:34:29 +010025#ifndef TIMER_H
26#define TIMER_H
27
28#include <sys/time.h>
29
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010030#include <osmocom/core/linuxlist.h>
Harald Welteec8b4502010-02-20 20:34:29 +010031
32/**
33 * Timer management:
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020034 * - Create a struct osmo_timer_list
Harald Welteec8b4502010-02-20 20:34:29 +010035 * - Fill out timeout and use add_timer or
36 * use schedule_timer to schedule a timer in
37 * x seconds and microseconds from now...
38 * - Use del_timer to remove the timer
39 *
40 * Internally:
41 * - We hook into select.c to give a timeval of the
42 * nearest timer. On already passed timers we give
43 * it a 0 to immediately fire after the select
44 * - update_timers will call the callbacks and remove
45 * the timers.
46 *
47 */
Harald Weltebd598e32011-08-16 23:26:52 +020048/*! \brief A structure representing a single instance of a timer */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020049struct osmo_timer_list {
Harald Weltebd598e32011-08-16 23:26:52 +020050 struct llist_head entry; /*!< \brief linked list header */
51 struct timeval timeout; /*!< \brief expiration time */
52 unsigned int active : 1; /*!< \brief is it active? */
53 unsigned int handled : 1; /*!< \brief did we already handle it */
54 unsigned int in_list : 1; /*!< \brief is it in the global list? */
Harald Welteec8b4502010-02-20 20:34:29 +010055
Harald Weltebd598e32011-08-16 23:26:52 +020056 void (*cb)(void*); /*!< \brief call-back called at timeout */
57 void *data; /*!< \brief user data for callback */
Harald Welteec8b4502010-02-20 20:34:29 +010058};
59
60/**
61 * timer management
62 */
Harald Weltebd598e32011-08-16 23:26:52 +020063
64/*! \brief add a new timer to the timer management
65 * \param[in] timer the timer that should be added
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
69/*! \brief schedule a timer at a given future relative time
70 * \param[in] timer the to-be-added timer
71 * \param[in] seconds number of seconds from now
72 * \param[in] microseconds number of microseconds from now
73 *
74 * This function can be used to (re-)schedule a given timer at a
75 * specified number of seconds+microseconds in the future. It will
76 * internally add it to the timer management data structures, thus
77 * osmo_timer_add() is automatically called.
78 */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020079void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds);
Harald Weltebd598e32011-08-16 23:26:52 +020080
81/*! \brief delete a timer from timer management
82 * \param[in] timer the to-be-deleted timer
83 *
84 * This function can be used to delete a previously added/scheduled
85 * timer from the timer management code.
86 */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020087void osmo_timer_del(struct osmo_timer_list *timer);
Harald Weltebd598e32011-08-16 23:26:52 +020088
89/*! \brief check if given timer is still pending
90 * \param[in] timer the to-be-checked timer
91 * \return 1 if pending, 0 otherwise
92 *
93 * This function can be used to determine whether a given timer
94 * has alredy expired (returns 0) or is still pending (returns 1)
95 */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020096int osmo_timer_pending(struct osmo_timer_list *timer);
Harald Welteec8b4502010-02-20 20:34:29 +010097
98
Harald Weltebd598e32011-08-16 23:26:52 +020099/*
Harald Welteec8b4502010-02-20 20:34:29 +0100100 * internal timer list management
101 */
Harald Welte7e820202011-07-16 10:15:16 +0200102struct timeval *osmo_timers_nearest(void);
103void osmo_timers_prepare(void);
104int osmo_timers_update(void);
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200105int osmo_timers_check(void);
Harald Welteec8b4502010-02-20 20:34:29 +0100106
107#endif