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 "common/list.h"
48 #include "olsr_types.h"
52 /* Some defs for juggling with timers */
53 #define MSEC_PER_SEC 1000
54 #define USEC_PER_SEC 1000000
55 #define NSEC_PER_USEC 1000
56 #define USEC_PER_MSEC 1000
58 #define TIMER_WHEEL_SLOTS 1024
59 #define TIMER_WHEEL_MASK (TIMER_WHEEL_SLOTS - 1)
61 typedef void (*timer_cb_func) (void *); /* callback function */
64 * Our timer implementation is a based on individual timers arranged in
65 * a double linked list hanging of hash containers called a timer wheel slot.
66 * For every timer a timer_entry is created and attached to the timer wheel slot.
67 * When the timer fires, the timer_cb function is called with the
69 * The implementation supports periodic and oneshot timers.
70 * For a periodic timer the timer_period field is set to non zero,
71 * which causes the timer to run forever until manually stopped.
74 struct list_node timer_list; /* Wheel membership */
75 uint32_t timer_clock; /* when timer shall fire (absolute time) */
76 unsigned int timer_period; /* set for periodical timers (relative time) */
77 struct olsr_cookie_info *timer_cookie; /* used for diag stuff */
78 uint8_t timer_jitter_pct; /* the jitter expressed in percent */
79 uint8_t timer_flags; /* misc flags */
80 unsigned int timer_random; /* cache random() result for performance reasons */
81 timer_cb_func timer_cb; /* callback function */
82 void *timer_cb_context; /* context pointer */
85 /* inline to recast from timer_list back to timer_entry */
86 LISTNODE2STRUCT(list2timer, struct timer_entry, timer_list);
88 #define OLSR_TIMER_ONESHOT 0 /* One shot timer */
89 #define OLSR_TIMER_PERIODIC 1 /* Periodic timer */
92 #define OLSR_TIMER_RUNNING ( 1 << 0) /* this timer is running */
95 void olsr_init_timers(void);
96 void olsr_flush_timers(void);
97 void olsr_set_timer (struct timer_entry **, unsigned int, uint8_t, bool, timer_cb_func, void *, struct olsr_cookie_info *);
98 struct timer_entry *olsr_start_timer (unsigned int, uint8_t, bool, timer_cb_func, void *, struct olsr_cookie_info *);
99 void olsr_change_timer(struct timer_entry *, unsigned int, uint8_t, bool);
100 void olsr_stop_timer (struct timer_entry *);
102 /* Printing timestamps */
103 const char *olsr_clock_string(uint32_t);
104 const char *olsr_wallclock_string(void);
106 /* Main scheduler loop */
107 void olsr_scheduler(void);
108 void olsr_scheduler_stop(void);
111 * Provides a timestamp s1 milliseconds in the future
113 #define GET_TIMESTAMP(s1) olsr_getTimestamp(s1)
115 /* Compute the time in milliseconds when a timestamp will expire. */
116 #define TIME_DUE(s1) olsr_getTimeDue(s1)
118 /* Returns TRUE if a timestamp is expired */
119 #define TIMED_OUT(s1) olsr_isTimedOut(s1)
122 extern uint32_t now_times; /* current idea of times(2) reported uptime */
125 #define SP_PR_READ 0x01
126 #define SP_PR_WRITE 0x02
128 #define SP_IMM_READ 0x04
129 #define SP_IMM_WRITE 0x08
132 typedef void (*socket_handler_func) (int fd, void *data, unsigned int flags);
135 struct olsr_socket_entry {
137 socket_handler_func process_immediate;
138 socket_handler_func process_pollrate;
141 struct list_node socket_node;
144 LISTNODE2STRUCT(list2socket, struct olsr_socket_entry, socket_node);
146 /* deletion safe macro for socket list traversal */
147 #define OLSR_FOR_ALL_SOCKETS(socket) \
149 struct list_node *_socket_node, *_next_socket_node; \
150 for (_socket_node = socket_head.next; \
151 _socket_node != &socket_head; \
152 _socket_node = _next_socket_node) { \
153 _next_socket_node = _socket_node->next; \
154 socket = list2socket(_socket_node);
155 #define OLSR_FOR_ALL_SOCKETS_END(socket) }}
157 uint32_t olsr_getTimestamp (uint32_t s);
158 int32_t olsr_getTimeDue (uint32_t s);
159 bool olsr_isTimedOut (uint32_t s);
161 void add_olsr_socket (int fd, socket_handler_func pf_pr, socket_handler_func pf_imm, void *data, unsigned int flags);
162 int remove_olsr_socket (int fd, socket_handler_func pf_pr, socket_handler_func pf_imm);
163 void olsr_flush_sockets(void);
164 void enable_olsr_socket (int fd, socket_handler_func pf_pr, socket_handler_func pf_imm, unsigned int flags);
165 void disable_olsr_socket (int fd, socket_handler_func pf_pr, socket_handler_func pf_imm, unsigned int flags);
168 * a wrapper around times(2). times(2) has the problem, that it may return -1
169 * in case of an err (e.g. EFAULT on the parameter) or immediately before an
170 * overrun (though it is not en error) just because the jiffies (or whatever
171 * the underlying kernel calls the smallest accountable time unit) are
172 * inherently "unsigned" (and always incremented).
176 #endif /* _OLSR_SCHEDULER */
181 * indent-tabs-mode: nil