libosmocore  1.5.1
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>
49 
50 /* convert absolute time (in seconds) to elapsed days/hours/minutes */
51 #define OSMO_SEC2MIN(sec) ((sec % (60 * 60)) / 60)
52 #define OSMO_SEC2HRS(sec) ((sec % (60 * 60 * 24)) / (60 * 60))
53 #define OSMO_SEC2DAY(sec) ((sec % (60 * 60 * 24 * 365)) / (60 * 60 * 24)) /* we ignore leap year for simplicity */
54 
57  struct rb_node node;
58  struct llist_head list;
59  struct timeval timeout;
60  unsigned int active : 1;
62  void (*cb)(void*);
63  void *data;
64 };
65 
66 /*
67  * timer management
68  */
69 
70 void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data);
71 
72 void osmo_timer_add(struct osmo_timer_list *timer);
73 
74 void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds);
75 
76 void osmo_timer_del(struct osmo_timer_list *timer);
77 
78 int osmo_timer_pending(struct osmo_timer_list *timer);
79 
80 int osmo_timer_remaining(const struct osmo_timer_list *timer,
81  const struct timeval *now,
82  struct timeval *remaining);
83 /*
84  * internal timer list management
85  */
86 struct timeval *osmo_timers_nearest(void);
87 int osmo_timers_nearest_ms(void);
88 void osmo_timers_prepare(void);
89 int osmo_timers_update(void);
90 int osmo_timers_check(void);
91 
92 int osmo_gettimeofday(struct timeval *tv, struct timezone *tz);
93 int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp);
94 
95 /*
96  * timer override
97  */
98 
99 extern bool osmo_gettimeofday_override;
100 extern struct timeval osmo_gettimeofday_override_time;
101 void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs);
102 
103 void osmo_clock_override_enable(clockid_t clk_id, bool enable);
104 void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs);
105 struct timespec *osmo_clock_override_gettimespec(clockid_t clk_id);
106 
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:56
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_nearest_ms
int osmo_timers_nearest_ms(void)
Determine time between now and the nearest timer in milliseconds.
Definition: timer.c:190
osmo_timers_update
int osmo_timers_update(void)
fire all timers...
Definition: timer.c:237
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:59
data
uint8_t data[0]
osmo_clock_override_add
void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs)
timer_compat.h
osmo_timer_list::data
void * data
user data for callback
Definition: timer.h:63
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:219
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:58
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:57
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:62
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:60
osmo_timers_check
int osmo_timers_check(void)
Check how many timers we have in the system.
Definition: timer.c:283
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