libosmocore 1.13.1.1-afb4 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 */
18
36#pragma once
37
38#include <sys/time.h>
39#include <time.h>
40#include <stdbool.h>
41
42#include <osmocom/core/linuxlist.h>
43#include <osmocom/core/linuxrbtree.h>
44#include <osmocom/core/timer_compat.h>
45
46/* convert absolute time (in seconds) to elapsed days/hours/minutes */
47#define OSMO_SEC2MIN(sec) ((sec % (60 * 60)) / 60)
48#define OSMO_SEC2HRS(sec) ((sec % (60 * 60 * 24)) / (60 * 60))
49#define OSMO_SEC2DAY(sec) ((sec % (60 * 60 * 24 * 365)) / (60 * 60 * 24)) /* we ignore leap year for simplicity */
50
52 struct osmo_timer_list {
53 struct rb_node node;
54 struct llist_head list;
55 struct timeval timeout;
56 unsigned int active : 1;
58 void (*cb)(void*);
59 void *data;
60};
61
62/*
63 * timer management
64 */
65
66 void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data);
67
68 void osmo_timer_add(struct osmo_timer_list *timer);
69
70 void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds);
71
72 void osmo_timer_del(struct osmo_timer_list *timer);
73
74 int osmo_timer_pending(const struct osmo_timer_list *timer);
75
76 int osmo_timer_remaining(const struct osmo_timer_list *timer,
77 const struct timeval *now,
78 struct timeval *remaining);
79/*
80 * internal timer list management
81 */
82 struct timeval *osmo_timers_nearest(void);
83 int osmo_timers_nearest_ms(void);
84 void osmo_timers_prepare(void);
85 int osmo_timers_update(void);
86 int osmo_timers_check(void);
87
88 int osmo_gettimeofday(struct timeval *tv, struct timezone *tz);
89 int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp);
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
99 void osmo_clock_override_enable(clockid_t clk_id, bool enable);
100 void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs);
101 struct timespec *osmo_clock_override_gettimespec(clockid_t clk_id);
102
data
uint8_t data[0]
osmo_clock_override_gettimespec
struct timespec * osmo_clock_override_gettimespec(clockid_t clk_id)
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:149
osmo_timer_del
void osmo_timer_del(struct osmo_timer_list *timer)
delete a timer from timer management
Definition: timer.c:117
osmo_clock_override_enable
void osmo_clock_override_enable(clockid_t clk_id, bool enable)
osmo_timers_prepare
void osmo_timers_prepare(void)
Find the nearest time and update nearest_p.
Definition: timer.c:225
osmo_timers_nearest
struct timeval * osmo_timers_nearest(void)
Determine time between now and the nearest timer.
Definition: timer.c:176
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:71
osmo_clock_override_add
void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs)
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:57
osmo_timer_add
void osmo_timer_add(struct osmo_timer_list *timer)
add a new timer to the timer management
Definition: timer.c:81
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:100
osmo_gettimeofday_override_time
struct timeval osmo_gettimeofday_override_time
Definition: timer_gettimeofday.c:46
osmo_gettimeofday_override
bool osmo_gettimeofday_override
Definition: timer_gettimeofday.c:45
osmo_clock_gettime
int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp)
osmo_timer_pending
int osmo_timer_pending(const struct osmo_timer_list *timer)
check if given timer is still pending
Definition: timer.c:135
osmo_timers_check
int osmo_timers_check(void)
Check how many timers we have in the system.
Definition: timer.c:289
osmo_timers_nearest_ms
int osmo_timers_nearest_ms(void)
Determine time between now and the nearest timer in milliseconds.
Definition: timer.c:187
osmo_timers_update
int osmo_timers_update(void)
fire all timers... and remove them
Definition: timer.c:243
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:69
linuxlist.h
Simple doubly linked list implementation.
linuxrbtree.h
llist_head
(double) linked list header structure
Definition: linuxlist.h:46
osmo_timer_list
A structure representing a single instance of a timer.
Definition: timer.h:52
osmo_timer_list::list
struct llist_head list
internal list header
Definition: timer.h:54
osmo_timer_list::active
unsigned int active
is it active?
Definition: timer.h:56
osmo_timer_list::node
struct rb_node node
rb-tree node header
Definition: timer.h:53
osmo_timer_list::cb
void(* cb)(void *)
call-back called at timeout
Definition: timer.h:58
osmo_timer_list::data
void * data
user data for callback
Definition: timer.h:59
osmo_timer_list::timeout
struct timeval timeout
expiration time
Definition: timer.h:55
rb_node
Definition: linuxrbtree.h:95
timer_compat.h
Compatibility header with some helpers.