3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2004-2013, the olsr.org team - see HISTORY file
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
45 #include "common/common_types.h"
46 #include "common/list.h"
47 #include "common/avl.h"
49 #include "subsystems/oonf_clock.h"
51 /* prototype for timer callback */
52 typedef void (*timer_cb_func) (void *);
54 struct oonf_timer_entry;
57 * This struct defines a class of timers which have the same
58 * type (periodic/non-periodic) and callback.
60 struct oonf_timer_info {
61 /* _node of timerinfo list */
62 struct list_entity _node;
64 /* name of this timer class */
67 /* callback function */
68 timer_cb_func callback;
70 /* true if this is a class of periodic timers */
73 /* Stats, resource usage */
76 /* Stats, resource churn */
79 /* pointer to timer currently in callback */
80 struct oonf_timer_entry *_timer_in_callback;
82 /* set to true if the current running timer has been stopped */
87 * Our timer implementation is a based on individual timers arranged in
88 * a double linked list hanging in a hierarchical list of timer slots.
90 * When an event is triggered, its callback is called with cb_context
93 struct oonf_timer_entry {
95 struct avl_node _node;
97 /* backpointer to timer info */
98 struct oonf_timer_info *info;
100 /* the jitter expressed in percent */
103 /* context pointer */
106 /* timeperiod between two timer events for periodical timers */
109 /* cache random() result for performance reasons */
110 unsigned int _random;
112 /* absolute timestamp when timer will fire */
116 #define LOG_TIMER oonf_timer_subsystem.logging
117 EXPORT extern struct oonf_subsystem oonf_timer_subsystem;
120 EXPORT extern struct list_entity oonf_timer_info_list;
122 EXPORT void oonf_timer_walk(void);
124 EXPORT void oonf_timer_add(struct oonf_timer_info *ti);
125 EXPORT void oonf_timer_remove(struct oonf_timer_info *);
127 EXPORT void oonf_timer_set(struct oonf_timer_entry *timer, uint64_t rel_time);
128 EXPORT void oonf_timer_start(struct oonf_timer_entry *timer, uint64_t rel_time);
129 EXPORT void oonf_timer_stop(struct oonf_timer_entry *);
131 EXPORT uint64_t oonf_timer_getNextEvent(void);
134 * @param timer pointer to timer
135 * @return true if the timer is running, false otherwise
138 oonf_timer_is_active(const struct oonf_timer_entry *timer) {
139 return timer->_clock != 0ull;
143 * @param timer pointer to timer
144 * @return interval between timer events in milliseconds
146 static INLINE uint64_t
147 oonf_timer_get_period(const struct oonf_timer_entry *timer) {
148 return timer->_period;
152 * @param timer pointer to timer
153 * @return number of milliseconds until timer fires
155 static INLINE int64_t
156 oonf_timer_get_due(const struct oonf_timer_entry *timer) {
157 return oonf_clock_get_relative(timer->_clock);
161 #endif /* OONF_TIMER_H_ */