3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2004-2009, 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.
43 #ifndef _OLSR_SCHEDULER
44 #define _OLSR_SCHEDULER
46 #include "olsr_time.h"
47 #include "common/list.h"
48 #include "common/avl.h"
50 #include "olsr_types.h"
54 #define TIMER_WHEEL_SLOTS 1024
55 #define TIMER_WHEEL_MASK (TIMER_WHEEL_SLOTS - 1)
57 typedef void (*timer_cb_func) (void *); /* callback function */
59 struct olsr_timer_info {
60 /* node of timerinfo tree */
63 /* name of this timer class */
66 /* callback function */
67 timer_cb_func callback;
69 /* true if this is a class of periodic timers */
72 /* Stats, resource usage */
75 /* Stats, resource churn */
79 #define OLSR_FOR_ALL_TIMERS(ti, iterator) avl_for_each_element_safe(&timerinfo_tree, ti, node, iterator)
82 * Our timer implementation is a based on individual timers arranged in
83 * a double linked list hanging of hash containers called a timer wheel slot.
84 * For every timer a timer_entry is created and attached to the timer wheel slot.
85 * When the timer fires, the timer_cb function is called with the
87 * The implementation supports periodic and oneshot timers.
88 * For a periodic timer the timer_period field is set to non zero,
89 * which causes the timer to run forever until manually stopped.
92 /* Wheel membership */
93 struct list_entity timer_list;
95 /* backpointer to timer info */
96 struct olsr_timer_info *timer_info;
98 /* when timer shall fire (absolute internal timerstamp) */
101 /* timeperiod between two timer events for periodical timers */
102 uint32_t timer_period; /* set for periodical timers (relative time) */
104 /* the jitter expressed in percent */
105 uint8_t timer_jitter_pct;
107 /* true if timer is running at the moment */
110 /* true if timer is in callback at the moment */
111 bool timer_in_callback;
113 /* cache random() result for performance reasons */
114 unsigned int timer_random;
116 /* context pointer */
117 void *timer_cb_context;
121 extern struct avl_tree EXPORT(timerinfo_tree);
123 void olsr_init_timers(void);
124 void olsr_flush_timers(void);
125 struct olsr_timer_info *olsr_alloc_timerinfo(const char *name, timer_cb_func callback, bool periodic);
127 void EXPORT(olsr_set_timer) (struct timer_entry **, uint32_t, uint8_t,
128 void *, struct olsr_timer_info *);
129 struct timer_entry *EXPORT(olsr_start_timer) (uint32_t, uint8_t,
130 void *, struct olsr_timer_info *);
131 void olsr_change_timer(struct timer_entry *, uint32_t, uint8_t);
132 void EXPORT(olsr_stop_timer) (struct timer_entry *);
134 /* Printing timestamps */
135 const char *olsr_clock_string(uint32_t);
136 const char *olsr_wallclock_string(void);
138 /* Main scheduler loop */
139 void olsr_scheduler(void);
142 * Provides a timestamp s1 milliseconds in the future
144 #define GET_TIMESTAMP(s1) olsr_getTimestamp(s1)
146 /* Compute the time in milliseconds when a timestamp will expire. */
147 #define TIME_DUE(s1) olsr_getTimeDue(s1)
149 /* Returns TRUE if a timestamp is expired */
150 #define TIMED_OUT(s1) olsr_isTimedOut(s1)
153 extern uint32_t EXPORT(now_times); /* current idea of times(2) reported uptime */
156 #define SP_PR_READ 0x01
157 #define SP_PR_WRITE 0x02
159 #define SP_IMM_READ 0x04
160 #define SP_IMM_WRITE 0x08
163 typedef void (*socket_handler_func) (int fd, void *data, unsigned int flags);
166 struct olsr_socket_entry {
168 socket_handler_func process_immediate;
169 socket_handler_func process_pollrate;
172 struct list_entity socket_node;
175 /* deletion safe macro for socket list traversal */
177 #define OLSR_FOR_ALL_SOCKETS(socket, iterator) list_for_each_element_safe(&socket_head, socket, socket_node, iterator)
179 uint32_t EXPORT(olsr_getTimestamp) (uint32_t s);
180 int32_t EXPORT(olsr_getTimeDue) (uint32_t s);
181 bool EXPORT(olsr_isTimedOut) (uint32_t s);
183 void EXPORT(add_olsr_socket) (int fd, socket_handler_func pf_pr, socket_handler_func pf_imm, void *data, unsigned int flags);
184 int EXPORT(remove_olsr_socket) (int fd, socket_handler_func pf_pr, socket_handler_func pf_imm);
185 void olsr_flush_sockets(void);
186 void EXPORT(enable_olsr_socket) (int fd, socket_handler_func pf_pr, socket_handler_func pf_imm, unsigned int flags);
187 void EXPORT(disable_olsr_socket) (int fd, socket_handler_func pf_pr, socket_handler_func pf_imm, unsigned int flags);
190 * a wrapper around times(2). times(2) has the problem, that it may return -1
191 * in case of an err (e.g. EFAULT on the parameter) or immediately before an
192 * overrun (though it is not en error) just because the jiffies (or whatever
193 * the underlying kernel calls the smallest accountable time unit) are
194 * inherently "unsigned" (and always incremented).
203 * indent-tabs-mode: nil