libosmocore  0.10.2
Osmocom core library
timer.h
Go to the documentation of this file.
1 
3 /*
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 
40 #pragma once
41 
42 #include <sys/time.h>
43 #include <stdbool.h>
44 
45 #include <osmocom/core/linuxlist.h>
47 
48 /* convert absolute time (in seconds) to elapsed days/hours/minutes */
49 #define OSMO_SEC2MIN(sec) ((sec % (60 * 60)) / 60)
50 #define OSMO_SEC2HRS(sec) ((sec % (60 * 60 * 24)) / (60 * 60))
51 #define OSMO_SEC2DAY(sec) ((sec % (60 * 60 * 24 * 365)) / (60 * 60 * 24)) /* we ignore leap year for simplicity */
52 
55  struct rb_node node;
56  struct llist_head list;
57  struct timeval timeout;
58  unsigned int active : 1;
60  void (*cb)(void*);
61  void *data;
62 };
63 
64 /*
65  * timer management
66  */
67 
68 void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data);
69 
70 void osmo_timer_add(struct osmo_timer_list *timer);
71 
72 void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds);
73 
74 void osmo_timer_del(struct osmo_timer_list *timer);
75 
76 int osmo_timer_pending(struct osmo_timer_list *timer);
77 
78 int osmo_timer_remaining(const struct osmo_timer_list *timer,
79  const struct timeval *now,
80  struct timeval *remaining);
81 /*
82  * internal timer list management
83  */
84 struct timeval *osmo_timers_nearest(void);
85 void osmo_timers_prepare(void);
86 int osmo_timers_update(void);
87 int osmo_timers_check(void);
88 
89 int osmo_gettimeofday(struct timeval *tv, struct timezone *tz);
90 
91 /*
92  * timer override
93  */
94 
95 extern bool osmo_gettimeofday_override;
96 extern struct timeval osmo_gettimeofday_override_time;
97 void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs);
98 
void osmo_timer_del(struct osmo_timer_list *timer)
delete a timer from timer management
Definition: timer.c:118
void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs)
convenience function to advance the fake time.
Definition: timer_gettimeofday.c:55
struct timeval timeout
expiration time
Definition: timer.h:57
void * data
user data for callback
Definition: timer.h:61
struct llist_head list
internal list header
Definition: timer.h:56
void osmo_timer_add(struct osmo_timer_list *timer)
add a new timer to the timer management
Definition: timer.c:82
void osmo_timers_prepare(void)
Find the nearest time and update nearest_p.
Definition: timer.c:201
bool osmo_gettimeofday_override
Definition: timer_gettimeofday.c:31
int osmo_timers_update(void)
fire all timers...
Definition: timer.c:219
struct rb_node node
rb-tree node header
Definition: timer.h:55
void osmo_timer_setup(struct osmo_timer_list *timer, void(*cb)(void *data), void *data)
set up timer callback and data
Definition: timer.c:72
void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds)
schedule a timer at a given future relative time
Definition: timer.c:101
Simple doubly linked list implementation.
void(* cb)(void *)
call-back called at timeout
Definition: timer.h:60
(double) linked list header structure
Definition: linuxlist.h:46
struct timeval * osmo_timers_nearest(void)
Determine time between now and the nearest timer.
Definition: timer.c:177
unsigned int active
is it active?
Definition: timer.h:58
int osmo_timer_remaining(const struct osmo_timer_list *timer, const struct timeval *now, struct timeval *remaining)
compute the remaining time of a timer
Definition: timer.c:150
struct timeval osmo_gettimeofday_override_time
Definition: timer_gettimeofday.c:32
int osmo_timers_check(void)
Check how many timers we have in the system.
Definition: timer.c:265
int osmo_gettimeofday(struct timeval *tv, struct timezone *tz)
shim around gettimeofday to be able to set the time manually.
Definition: timer_gettimeofday.c:43
A structure representing a single instance of a timer.
Definition: timer.h:54
int osmo_timer_pending(struct osmo_timer_list *timer)
check if given timer is still pending
Definition: timer.c:136
Definition: linuxrbtree.h:99