The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1#pragma once
2
3/*
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19/** Timer lists with event callbacks
20 *
21 * @file src/lib/util/event.h
22 *
23 * @copyright 2025 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 */
25RCSIDH(timer_h, "$Id: b57afbaa66f82bb1d24303a7804e42862b132aed $")
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#include <freeradius-devel/util/time.h>
32#include <freeradius-devel/util/misc.h>
33#include <freeradius-devel/util/talloc.h>
34
35/*
36 * Allow public and private versions of the same structures
37 */
38#ifdef _CONST
39# error _CONST can only be defined in the local header
40#endif
41#ifndef _TIMER_PRIVATE
43# define _CONST const
44#else
45# define _CONST
46#endif
47
48/** Alternative time source, useful for testing
49 *
50 * @return the current time in nanoseconds past the epoch.
51 */
53
54/** Public event timer list structure
55 *
56 * Make the current list time, and time source available, but nothing else.
57 *
58 * This allows us to access these values without the cost of a function call.
59 */
61 fr_event_time_source_t _CONST time; //!< Time source this list uses to get the current time
62 ///< when calculating deltas (fr_timer_in).
63};
64
65/** An opaque timer handle
66 */
67typedef struct fr_timer_s fr_timer_t;
68
69/** Called when a timer event fires
70 *
71 * @param[in] tl timer list event was inserted into.
72 * @param[in] now The current time.
73 * @param[in] uctx User ctx passed to #fr_timer_in or #fr_timer_at.
74 */
75typedef void (*fr_timer_cb_t)(fr_timer_list_t *tl, fr_time_t now, void *uctx);
76
78 TALLOC_CTX *ctx, fr_timer_list_t *tl, fr_timer_t **ev,
81#define fr_timer_at(...) _fr_timer_at(NDEBUG_LOCATION_EXP __VA_ARGS__)
82
84 TALLOC_CTX *ctx, fr_timer_list_t *tl, fr_timer_t **ev,
85 fr_time_delta_t delta, bool free_on_fire, fr_timer_cb_t callback, void const *uctx)
87#define fr_timer_in(...) _fr_timer_in(NDEBUG_LOCATION_EXP __VA_ARGS__)
88
89int fr_timer_disarm(fr_timer_t *ev); /* disarms but does not free */
90
91#define FR_TIMER_DISARM(_ev) \
92 do { \
93 if (likely((_ev) != NULL) && unlikely(fr_timer_disarm(_ev) < 0)) { \
94 fr_assert_msg(0, "Failed to disarm timer %p", (_ev)); \
95 } \
96 } while (0)
97
98#define FR_TIMER_DISARM_RETURN(_ev) \
99 if ((likely(((_ev)) != NULL) && unlikely(!fr_cond_assert_msg(fr_timer_disarm(_ev) == 0, "Failed to disarm timer %p", (_ev))))) return -1;
100
101int fr_timer_delete(fr_timer_t **ev_p) CC_HINT(nonnull); /* disarms AND frees */
102
103#define FR_TIMER_DELETE(_ev_p) \
104 do { \
105 if ((likely((*(_ev_p)) != NULL) && unlikely(fr_timer_delete(_ev_p) < 0))) { \
106 fr_assert_msg(0, "Failed to delete timer %p", *(_ev_p)); \
107 } \
108 } while (0)
109
110#define FR_TIMER_DELETE_RETURN(_ev_p) \
111 if ((likely((*(_ev_p)) != NULL) && unlikely(!fr_cond_assert_msg(fr_timer_delete(_ev_p) == 0, "Failed to delete timer %p", *(_ev_p))))) return -1;
112
114
116
118
119/* Wrapper to avoid overhead of function call on NULL */
120static inline bool fr_timer_armed(fr_timer_t *ev) { return ev && _fr_timer_armed(ev); }
121
123
125
127
128
130
132
134
136
138
140
142
144
146
148 fr_timer_cb_t callback, size_t node_offset, size_t time_offset) CC_HINT(nonnull);
149
150#ifdef WITH_EVENT_DEBUG
151void fr_timer_report(fr_timer_list_t *tl, fr_time_t now, void *uctx);
152void fr_timer_dump(fr_timer_list_t *tl);
153#endif
154
155#undef _CONST
156
157#ifdef __cplusplus
158}
159#endif
#define RCSIDH(h, id)
Definition build.h:486
#define NDEBUG_LOCATION_ARGS
Pass caller information to the function.
Definition build.h:263
#define NDEBUG_LOCATION_NONNULL(_num)
Definition build.h:267
int8_t(* fr_cmp_t)(void const *a, void const *b)
Definition misc.h:38
struct fr_time_s fr_time_t
"server local" time.
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
void const * uctx
Context pointer to pass to the callback.
Definition timer.c:88
fr_timer_cb_t callback
Callback to execute when the timer fires.
Definition timer.c:87
fr_time_t when
When this timer should fire.
Definition timer.c:85
bool free_on_fire
Whether to free the event when it fires.
Definition timer.c:100
fr_timer_list_t * tl
The event list this timer is part of.
Definition timer.c:102
An event timer list.
Definition timer.c:50
A timer event.
Definition timer.c:84
int fr_timer_list_run(fr_timer_list_t *tl, fr_time_t *when)
Execute any pending events in the event loop.
Definition timer.c:938
int _fr_timer_in(NDEBUG_LOCATION_ARGS TALLOC_CTX *ctx, fr_timer_list_t *tl, fr_timer_t **ev, fr_time_delta_t delta, bool free_on_fire, fr_timer_cb_t callback, void const *uctx)
int fr_timer_list_disarm(fr_timer_list_t *tl)
Disarm a timer list.
Definition timer.c:1101
fr_time_t fr_timer_when(fr_timer_t *ev)
Internal timestamp representing when the timer should fire.
Definition timer.c:719
uint64_t fr_timer_list_num_events(fr_timer_list_t *tl)
Return number of pending events.
Definition timer.c:1149
int _fr_timer_at(NDEBUG_LOCATION_ARGS TALLOC_CTX *ctx, fr_timer_list_t *tl, fr_timer_t **ev, fr_time_t when, bool free_on_fire, fr_timer_cb_t callback, void const *uctx)
int fr_timer_uctx_insert(fr_timer_list_t *tl, void *uctx)
Insert a uctx into a shared timer, and update the timer.
Definition timer.c:1344
fr_time_t fr_timer_list_when(fr_timer_list_t *tl)
Return the time of the next event.
Definition timer.c:1186
fr_timer_list_t * fr_timer_list_ordered_alloc(TALLOC_CTX *ctx, fr_timer_list_t *parent)
Allocate a new sorted event timer list.
Definition timer.c:1287
bool _fr_timer_armed(fr_timer_t *ev)
Check if a timer event is armed.
Definition timer.c:742
fr_time_delta_t fr_timer_remaining(fr_timer_t *ev)
Return time delta between now and when the timer should fire.
Definition timer.c:729
int fr_timer_list_force_run(fr_timer_list_t *tl)
Forcibly run all events in an event loop.
Definition timer.c:920
fr_timer_list_t * fr_timer_list_shared_alloc(TALLOC_CTX *ctx, fr_timer_list_t *parent, fr_cmp_t cmp, fr_timer_cb_t callback, size_t node_offset, size_t time_offset)
Allocate a new shared event timer list.
Definition timer.c:1308
fr_timer_list_t * fr_timer_list_lst_alloc(TALLOC_CTX *ctx, fr_timer_list_t *parent)
Allocate a new lst based timer list.
Definition timer.c:1261
int fr_timer_disarm(fr_timer_t *ev)
Remove an event from the event list, but don't free the memory.
Definition timer.c:655
#define _CONST
Definition timer.h:43
int fr_timer_uctx_remove(fr_timer_list_t *tl, void *uctx)
Remove a uctx from a shared timer.
Definition timer.c:1367
void fr_timer_list_set_time_func(fr_timer_list_t *tl, fr_event_time_source_t func)
Override event list time source.
Definition timer.c:1200
fr_time_t(* fr_event_time_source_t)(void)
Alternative time source, useful for testing.
Definition timer.h:52
void(* fr_timer_cb_t)(fr_timer_list_t *tl, fr_time_t now, void *uctx)
Called when a timer event fires.
Definition timer.h:75
static bool fr_timer_armed(fr_timer_t *ev)
Definition timer.h:120
void * fr_timer_uctx_peek(fr_timer_list_t *tl)
Definition timer.c:1377
int fr_timer_list_arm(fr_timer_list_t *tl)
Arm (or re-arm) a timer list.
Definition timer.c:1122
int fr_timer_delete(fr_timer_t **ev_p)
Delete a timer event and free its memory.
Definition timer.c:692
fr_event_time_source_t _CONST time
Time source this list uses to get the current time when calculating deltas (fr_timer_in).
Definition timer.h:61
Public event timer list structure.
Definition timer.h:60
fr_time_delta_t time_offset
static fr_slen_t parent
Definition pair.h:845
int nonnull(2, 5))