libosmocore  0.12.0
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 <time.h>
44 #include <stdbool.h>
45 
46 #include <osmocom/core/linuxlist.h>
48 
49 /* convert absolute time (in seconds) to elapsed days/hours/minutes */
50 #define OSMO_SEC2MIN(sec) ((sec % (60 * 60)) / 60)
51 #define OSMO_SEC2HRS(sec) ((sec % (60 * 60 * 24)) / (60 * 60))
52 #define OSMO_SEC2DAY(sec) ((sec % (60 * 60 * 24 * 365)) / (60 * 60 * 24)) /* we ignore leap year for simplicity */
53 
56  struct rb_node node;
57  struct llist_head list;
58  struct timeval timeout;
59  unsigned int active : 1;
61  void (*cb)(void*);
62  void *data;
63 };
64 
65 /*
66  * timer management
67  */
68 
69 void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data);
70 
71 void osmo_timer_add(struct osmo_timer_list *timer);
72 
73 void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds);
74 
75 void osmo_timer_del(struct osmo_timer_list *timer);
76 
77 int osmo_timer_pending(struct osmo_timer_list *timer);
78 
79 int osmo_timer_remaining(const struct osmo_timer_list *timer,
80  const struct timeval *now,
81  struct timeval *remaining);
82 /*
83  * internal timer list management
84  */
85 struct timeval *osmo_timers_nearest(void);
86 void osmo_timers_prepare(void);
87 int osmo_timers_update(void);
88 int osmo_timers_check(void);
89 
90 int osmo_gettimeofday(struct timeval *tv, struct timezone *tz);
91 int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp);
92 
93 /*
94  * timer override
95  */
96 
97 extern bool osmo_gettimeofday_override;
98 extern struct timeval osmo_gettimeofday_override_time;
99 void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs);
100 
101 void osmo_clock_override_enable(clockid_t clk_id, bool enable);
102 void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs);
103 struct timespec *osmo_clock_override_gettimespec(clockid_t clk_id);
104 
osmo_gettimeofday
int osmo_gettimeofday(struct timeval *tv, struct timezone *tz)
shim around gettimeofday to be able to set the time manually.
Definition: timer_gettimeofday.c:60
osmo_timer_list
A structure representing a single instance of a timer.
Definition: timer.h:55
osmo_timer_add
void osmo_timer_add(struct osmo_timer_list *timer)
add a new timer to the timer management
Definition: timer.c:84
osmo_clock_gettime
int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp)
osmo_timers_update
int osmo_timers_update(void)
fire all timers...
Definition: timer.c:221
osmo_gettimeofday_override_add
void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs)
convenience function to advance the fake time.
Definition: timer_gettimeofday.c:72
osmo_timer_list::timeout
struct timeval timeout
expiration time
Definition: timer.h:58
osmo_clock_override_add
void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs)
osmo_timer_list::data
void * data
user data for callback
Definition: timer.h:62
osmo_timer_schedule
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:103
osmo_clock_override_enable
void osmo_clock_override_enable(clockid_t clk_id, bool enable)
osmo_gettimeofday_override
bool osmo_gettimeofday_override
Definition: timer_gettimeofday.c:48
osmo_timers_prepare
void osmo_timers_prepare(void)
Find the nearest time and update nearest_p.
Definition: timer.c:203
osmo_timers_nearest
struct timeval * osmo_timers_nearest(void)
Determine time between now and the nearest timer.
Definition: timer.c:179
llist_head
(double) linked list header structure
Definition: linuxlist.h:46
osmo_timer_list::list
struct llist_head list
internal list header
Definition: timer.h:57
osmo_timer_pending
int osmo_timer_pending(struct osmo_timer_list *timer)
check if given timer is still pending
Definition: timer.c:138
osmo_timer_list::node
struct rb_node node
rb-tree node header
Definition: timer.h:56
osmo_timer_setup
void osmo_timer_setup(struct osmo_timer_list *timer, void(*cb)(void *data), void *data)
set up timer callback and data
Definition: timer.c:74
osmo_gettimeofday_override_time
struct timeval osmo_gettimeofday_override_time
Definition: timer_gettimeofday.c:49
osmo_clock_override_gettimespec
struct timespec * osmo_clock_override_gettimespec(clockid_t clk_id)
linuxrbtree.h
osmo_timer_list::cb
void(* cb)(void *)
call-back called at timeout
Definition: timer.h:61
osmo_timer_del
void osmo_timer_del(struct osmo_timer_list *timer)
delete a timer from timer management
Definition: timer.c:120
rb_node
Definition: linuxrbtree.h:100
osmo_timer_list::active
unsigned int active
is it active?
Definition: timer.h:59
osmo_timers_check
int osmo_timers_check(void)
Check how many timers we have in the system.
Definition: timer.c:267
linuxlist.h
osmo_timer_remaining
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:152