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.
42 #include "scheduler.h"
46 #include "olsr_cookie.h"
48 #include "mpr_selector_set.h"
50 #include <sys/times.h>
56 #define close(x) closesocket(x)
59 /* Timer data, global. Externed in scheduler.h */
60 uint32_t now_times; /* relative time compared to startup (in milliseconds */
61 struct timeval first_tv; /* timevalue during startup */
62 struct timeval last_tv; /* timevalue used for last olsr_times() calculation */
64 /* Hashed root of all timers */
65 static struct list_node timer_wheel[TIMER_WHEEL_SLOTS];
66 static uint32_t timer_last_run; /* remember the last timeslot walk */
68 /* Memory cookie for the block based memory manager */
69 static struct olsr_cookie_info *timer_mem_cookie = NULL;
71 /* Head of all OLSR used sockets */
72 static struct list_node socket_head = { &socket_head, &socket_head };
75 static void walk_timers(uint32_t *);
76 static void poll_sockets(void);
77 static uint32_t calc_jitter(unsigned int rel_time, uint8_t jitter_pct, unsigned int random_val);
80 * A wrapper around times(2). Note, that this function has some
81 * portability problems, so do not rely on absolute values returned.
82 * Under Linux, uclibc and libc directly call the sys_times() located
83 * in kernel/sys.c and will only return an error if the tms_buf is
92 if (gettimeofday(&tv, NULL) != 0) {
93 olsr_exit("OS clock is not working, have to shut down OLSR", 1);
96 /* test if time jumped backward or more than 60 seconds forward */
97 if (tv.tv_sec < last_tv.tv_sec || (tv.tv_sec == last_tv.tv_sec && tv.tv_usec < last_tv.tv_usec)
98 || tv.tv_sec - last_tv.tv_sec > 60) {
99 OLSR_PRINTF(1, "Time jump (%d.%06d to %d.%06d)\n",
100 (int32_t) (last_tv.tv_sec), (int32_t) (last_tv.tv_usec), (int32_t) (tv.tv_sec), (int32_t) (tv.tv_usec));
102 t = (last_tv.tv_sec - first_tv.tv_sec) * 1000 + (last_tv.tv_usec - first_tv.tv_usec) / 1000;
103 t++; /* advance time by one millisecond */
106 first_tv.tv_sec -= (t / 1000);
107 first_tv.tv_usec -= ((t % 1000) * 1000);
109 if (first_tv.tv_usec < 0) {
111 first_tv.tv_usec += 1000000;
117 return (tv.tv_sec - first_tv.tv_sec) * 1000 + (tv.tv_usec - first_tv.tv_usec) / 1000;
121 * Returns a timestamp s seconds in the future
124 olsr_getTimestamp(uint32_t s)
126 return now_times + s;
130 * Returns the number of milliseconds until the timestamp will happen
134 olsr_getTimeDue(uint32_t s)
138 diff = s - now_times;
141 if (diff > (1u << 31)) {
142 return -(int32_t) (0xffffffff - diff);
144 return (int32_t) (diff);
147 diff = now_times - s;
149 if (diff > (1u << 31)) {
150 return (int32_t) (0xffffffff - diff);
152 return -(int32_t) (diff);
156 olsr_isTimedOut(uint32_t s)
159 return s - now_times > (1u << 31);
162 return now_times - s <= (1u << 31);
166 * Add a socket and handler to the socketset
167 * beeing used in the main select(2) loop
170 *@param fd the socket
171 *@param pf_pr the processing function
172 *@param pf_imm the (immediate) processing function
173 *@param data the data pointer for the processing function
174 *@param flags the flags for the processing function
177 add_olsr_socket(int fd, socket_handler_func pf_pr, socket_handler_func pf_imm, void *data, unsigned int flags)
179 struct olsr_socket_entry *new_entry;
181 if (fd < 0 || (pf_pr == NULL && pf_imm == NULL)) {
182 OLSR_PRINTF(1, "Bogus socket entry - not registering...");
185 OLSR_PRINTF(3, "Adding OLSR socket entry %d\n", fd);
187 new_entry = olsr_malloc(sizeof(*new_entry), "Socket entry");
190 new_entry->process_immediate = pf_imm;
191 new_entry->process_pollrate = pf_pr;
192 new_entry->data = data;
193 new_entry->flags = flags;
196 list_node_init(&new_entry->socket_node);
197 list_add_before(&socket_head, &new_entry->socket_node);
201 * Remove a socket and handler to the socketset
202 * beeing used in the main select(2) loop
205 *@param fd the socket
206 *@param pf_pr the processing function
207 *@param pf_imm the (immediate) processing function
210 remove_olsr_socket(int fd, socket_handler_func pf_pr, socket_handler_func pf_imm)
212 struct olsr_socket_entry *entry;
214 if (fd < 0 || (pf_pr == NULL && pf_imm == NULL)) {
215 OLSR_PRINTF(1, "Bogus socket entry - not processing...");
218 OLSR_PRINTF(3, "Removing OLSR socket entry %d\n", fd);
220 OLSR_FOR_ALL_SOCKETS(entry) {
221 if (entry->fd == fd && entry->process_immediate == pf_imm && entry->process_pollrate == pf_pr) {
222 /* just mark this node as "deleted", it will be cleared later at the end of handle_fds() */
223 entry->process_immediate = NULL;
224 entry->process_pollrate = NULL;
229 OLSR_FOR_ALL_SOCKETS_END(entry);
234 enable_olsr_socket(int fd, socket_handler_func pf_pr, socket_handler_func pf_imm, unsigned int flags)
236 struct olsr_socket_entry *entry;
238 OLSR_FOR_ALL_SOCKETS(entry) {
239 if (entry->fd == fd && entry->process_immediate == pf_imm && entry->process_pollrate == pf_pr) {
240 entry->flags |= flags;
243 OLSR_FOR_ALL_SOCKETS_END(entry);
247 disable_olsr_socket(int fd, socket_handler_func pf_pr, socket_handler_func pf_imm, unsigned int flags)
249 struct olsr_socket_entry *entry;
251 OLSR_FOR_ALL_SOCKETS(entry) {
252 if (entry->fd == fd && entry->process_immediate == pf_imm && entry->process_pollrate == pf_pr) {
253 entry->flags &= ~flags;
256 OLSR_FOR_ALL_SOCKETS_END(entry);
260 * Close and free all sockets.
263 olsr_flush_sockets(void)
265 struct olsr_socket_entry *entry;
267 OLSR_FOR_ALL_SOCKETS(entry) {
269 list_remove(&entry->socket_node);
271 } OLSR_FOR_ALL_SOCKETS_END(entry);
278 struct olsr_socket_entry *entry;
280 struct timeval tvp = { 0, 0 };
281 int hfd = 0, fdsets = 0;
283 /* If there are no registered sockets we
284 * do not call select(2)
286 if (list_is_empty(&socket_head)) {
293 /* Adding file-descriptors to FD set */
294 OLSR_FOR_ALL_SOCKETS(entry) {
295 if (entry->process_pollrate == NULL) {
298 if ((entry->flags & SP_PR_READ) != 0) {
299 fdsets |= SP_PR_READ;
300 FD_SET((unsigned int)entry->fd, &ibits); /* And we cast here since we get a warning on Win32 */
302 if ((entry->flags & SP_PR_WRITE) != 0) {
303 fdsets |= SP_PR_WRITE;
304 FD_SET((unsigned int)entry->fd, &obits); /* And we cast here since we get a warning on Win32 */
306 if ((entry->flags & (SP_PR_READ | SP_PR_WRITE)) != 0 && entry->fd >= hfd) {
310 OLSR_FOR_ALL_SOCKETS_END(entry);
312 /* Running select on the FD set */
314 n = olsr_select(hfd, fdsets & SP_PR_READ ? &ibits : NULL, fdsets & SP_PR_WRITE ? &obits : NULL, NULL, &tvp);
315 } while (n == -1 && errno == EINTR);
320 if (n == -1) { /* Did something go wrong? */
321 OLSR_PRINTF(1, "select error: %s", strerror(errno));
325 /* Update time since this is much used by the parsing functions */
326 now_times = olsr_times();
327 OLSR_FOR_ALL_SOCKETS(entry) {
329 if (entry->process_pollrate == NULL) {
333 if (FD_ISSET(entry->fd, &ibits)) {
336 if (FD_ISSET(entry->fd, &obits)) {
337 flags |= SP_PR_WRITE;
340 entry->process_pollrate(entry->fd, entry->data, flags);
343 OLSR_FOR_ALL_SOCKETS_END(entry);
347 handle_fds(uint32_t next_interval)
349 struct olsr_socket_entry *entry;
353 /* calculate the first timeout */
354 now_times = olsr_times();
356 remaining = TIME_DUE(next_interval);
357 if (remaining <= 0) {
358 /* we are already over the interval */
359 if (list_is_empty(&socket_head)) {
360 /* If there are no registered sockets we do not call select(2) */
366 /* we need an absolute time - milliseconds */
367 tvp.tv_sec = remaining / MSEC_PER_SEC;
368 tvp.tv_usec = (remaining % MSEC_PER_SEC) * USEC_PER_MSEC;
371 /* do at least one select */
374 int n, hfd = 0, fdsets = 0;
378 /* Adding file-descriptors to FD set */
379 OLSR_FOR_ALL_SOCKETS(entry) {
380 if (entry->process_immediate == NULL) {
383 if ((entry->flags & SP_IMM_READ) != 0) {
384 fdsets |= SP_IMM_READ;
385 FD_SET((unsigned int)entry->fd, &ibits); /* And we cast here since we get a warning on Win32 */
387 if ((entry->flags & SP_IMM_WRITE) != 0) {
388 fdsets |= SP_IMM_WRITE;
389 FD_SET((unsigned int)entry->fd, &obits); /* And we cast here since we get a warning on Win32 */
391 if ((entry->flags & (SP_IMM_READ | SP_IMM_WRITE)) != 0 && entry->fd >= hfd) {
395 OLSR_FOR_ALL_SOCKETS_END(entry);
397 if (hfd == 0 && (long)remaining <= 0) {
398 /* we are over the interval and we have no fd's. Skip the select() etc. */
403 n = olsr_select(hfd, fdsets & SP_IMM_READ ? &ibits : NULL, fdsets & SP_IMM_WRITE ? &obits : NULL, NULL, &tvp);
404 } while (n == -1 && errno == EINTR);
406 if (n == 0) { /* timeout! */
409 if (n == -1) { /* Did something go wrong? */
410 OLSR_PRINTF(1, "select error: %s", strerror(errno));
414 /* Update time since this is much used by the parsing functions */
415 now_times = olsr_times();
416 OLSR_FOR_ALL_SOCKETS(entry) {
418 if (entry->process_immediate == NULL) {
422 if (FD_ISSET(entry->fd, &ibits)) {
423 flags |= SP_IMM_READ;
425 if (FD_ISSET(entry->fd, &obits)) {
426 flags |= SP_IMM_WRITE;
429 entry->process_immediate(entry->fd, entry->data, flags);
432 OLSR_FOR_ALL_SOCKETS_END(entry);
434 /* calculate the next timeout */
435 remaining = TIME_DUE(next_interval);
436 if (remaining <= 0) {
437 /* we are already over the interval */
440 /* we need an absolute time - milliseconds */
441 tvp.tv_sec = remaining / MSEC_PER_SEC;
442 tvp.tv_usec = (remaining % MSEC_PER_SEC) * USEC_PER_MSEC;
445 OLSR_FOR_ALL_SOCKETS(entry) {
446 if (entry->process_immediate == NULL && entry->process_pollrate == NULL) {
447 /* clean up socket handler */
448 list_remove(&entry->socket_node);
451 } OLSR_FOR_ALL_SOCKETS_END(entry);
455 * Main scheduler event loop. Polls at every
456 * sched_poll_interval and calls all functions
457 * that are timed out or that are triggered.
458 * Also calls the olsr_process_changes()
459 * function at every poll.
463 void __attribute__ ((noreturn))
466 OLSR_PRINTF(1, "Scheduler started - polling every %d ms\n", (int)(olsr_cnf->pollrate*1000));
468 /* Main scheduler loop */
470 uint32_t next_interval;
473 * Update the global timestamp. We are using a non-wallclock timer here
474 * to avoid any undesired side effects if the system clock changes.
476 now_times = olsr_times();
477 next_interval = GET_TIMESTAMP(olsr_cnf->pollrate * 1000);
479 /* Read incoming data */
483 walk_timers(&timer_last_run);
486 olsr_process_changes();
488 /* Check for changes in topology */
490 increase_local_ansn();
491 OLSR_PRINTF(3, "ANSN UPDATED %d\n\n", get_local_ansn());
492 link_changes = false;
495 /* Read incoming data and handle it immediiately */
496 handle_fds(next_interval);
499 if (olsr_win32_end_request) {
500 olsr_win32_end_flag = true;
507 * Decrement a relative timer by a random number range.
509 * @param rel_time the relative timer expressed in units of milliseconds.
510 * @param jitter_pct the jitter in percent
511 * @param random_val cached result of random() at system init.
512 * @return the absolute timer in system clock tick units
515 calc_jitter(unsigned int rel_time, uint8_t jitter_pct, unsigned int random_val)
517 unsigned int jitter_time;
520 * No jitter or, jitter larger than 99% does not make sense.
521 * Also protect against overflows resulting from > 25 bit timers.
523 if (jitter_pct == 0 || jitter_pct > 99 || rel_time > (1 << 24)) {
524 return GET_TIMESTAMP(rel_time);
528 * Play some tricks to avoid overflows with integer arithmetic.
530 jitter_time = (jitter_pct * rel_time) / 100;
531 jitter_time = random_val / (1 + RAND_MAX / (jitter_time + 1));
533 OLSR_PRINTF(3, "TIMER: jitter %u%% rel_time %ums to %ums\n", jitter_pct, rel_time, rel_time - jitter_time);
535 return GET_TIMESTAMP(rel_time - jitter_time);
539 * Init datastructures for maintaining timers.
542 olsr_init_timers(void)
546 OLSR_PRINTF(3, "Initializing scheduler.\n");
548 /* Grab initial timestamp */
549 if (gettimeofday(&first_tv, NULL)) {
550 olsr_exit("OS clock is not working, have to shut down OLSR", 1);
553 now_times = olsr_times();
555 for (idx = 0; idx < TIMER_WHEEL_SLOTS; idx++) {
556 list_head_init(&timer_wheel[idx]);
560 * Reset the last timer run.
562 timer_last_run = now_times;
564 /* Allocate a cookie for the block based memeory manager. */
565 timer_mem_cookie = olsr_alloc_cookie("timer_entry", OLSR_COOKIE_TYPE_MEMORY);
566 olsr_cookie_set_memory_size(timer_mem_cookie, sizeof(struct timer_entry));
570 * Walk through the timer list and check if any timer is ready to fire.
571 * Callback the provided function with the context pointer.
574 walk_timers(uint32_t * last_run)
576 unsigned int total_timers_walked = 0, total_timers_fired = 0;
577 unsigned int wheel_slot_walks = 0;
580 * Check the required wheel slots since the last time a timer walk was invoked,
581 * or check *all* the wheel slots, whatever is less work.
582 * The latter is meant as a safety belt if the scheduler falls behind.
584 while ((*last_run <= now_times) && (wheel_slot_walks < TIMER_WHEEL_SLOTS)) {
585 struct list_node tmp_head_node;
586 /* keep some statistics */
587 unsigned int timers_walked = 0, timers_fired = 0;
589 /* Get the hash slot for this clocktick */
590 struct list_node *const timer_head_node = &timer_wheel[*last_run & TIMER_WHEEL_MASK];
592 /* Walk all entries hanging off this hash bucket. We treat this basically as a stack
593 * so that we always know if and where the next element is.
595 list_head_init(&tmp_head_node);
596 while (!list_is_empty(timer_head_node)) {
597 /* the top element */
598 struct list_node *const timer_node = timer_head_node->next;
599 struct timer_entry *const timer = list2timer(timer_node);
602 * Dequeue and insert to a temporary list.
603 * We do this to avoid loosing our walking context when
604 * multiple timers fire.
606 list_remove(timer_node);
607 list_add_after(&tmp_head_node, timer_node);
610 /* Ready to fire ? */
611 if (TIMED_OUT(timer->timer_clock)) {
613 OLSR_PRINTF(7, "TIMER: fire %s timer %p, ctx %p, "
614 "at clocktick %u (%s)\n",
615 timer->timer_cookie->ci_name,
616 timer, timer->timer_cb_context, (unsigned int)*last_run, olsr_wallclock_string());
618 /* This timer is expired, call into the provided callback function */
619 timer->timer_cb(timer->timer_cb_context);
621 /* Only act on actually running timers */
622 if (timer->timer_flags & OLSR_TIMER_RUNNING) {
624 * Don't restart the periodic timer if the callback function has
627 if (timer->timer_period) {
628 /* For periodical timers, rehash the random number and restart */
629 timer->timer_random = random();
630 olsr_change_timer(timer, timer->timer_period, timer->timer_jitter_pct, OLSR_TIMER_PERIODIC);
632 /* Singleshot timers are stopped */
633 olsr_stop_timer(timer);
642 * Now merge the temporary list back to the old bucket.
644 list_merge(timer_head_node, &tmp_head_node);
646 /* keep some statistics */
647 total_timers_walked += timers_walked;
648 total_timers_fired += timers_fired;
650 /* Increment the time slot and wheel slot walk iteration */
655 OLSR_PRINTF(7, "TIMER: processed %4u/%d clockwheel slots, "
656 "timers walked %4u/%u, timers fired %u\n",
657 wheel_slot_walks, TIMER_WHEEL_SLOTS, total_timers_walked, timer_mem_cookie->ci_usage, total_timers_fired);
660 * If the scheduler has slipped and we have walked all wheel slots,
661 * reset the last timer run.
663 *last_run = now_times;
667 * Stop and delete all timers.
670 olsr_flush_timers(void)
672 struct list_node *timer_head_node;
673 unsigned int wheel_slot = 0;
675 for (wheel_slot = 0; wheel_slot < TIMER_WHEEL_SLOTS; wheel_slot++) {
676 timer_head_node = &timer_wheel[wheel_slot & TIMER_WHEEL_MASK];
678 /* Kill all entries hanging off this hash bucket. */
679 while (!list_is_empty(timer_head_node)) {
680 olsr_stop_timer(list2timer(timer_head_node->next));
686 * Returns the difference between gmt and local time in seconds.
687 * Use gmtime() and localtime() to keep things simple.
689 * taken and slightly modified from www.tcpdump.org.
692 olsr_get_timezone(void)
694 #define OLSR_TIMEZONE_UNINITIALIZED -1
695 static int time_diff = OLSR_TIMEZONE_UNINITIALIZED;
696 if (time_diff == OLSR_TIMEZONE_UNINITIALIZED) {
698 const time_t t = time(NULL);
699 const struct tm gmt = *gmtime(&t);
700 const struct tm *loc = localtime(&t);
702 time_diff = (loc->tm_hour - gmt.tm_hour) * 60 * 60 + (loc->tm_min - gmt.tm_min) * 60;
705 * If the year or julian day is different, we span 00:00 GMT
706 * and must add or subtract a day. Check the year first to
707 * avoid problems when the julian day wraps.
709 dir = loc->tm_year - gmt.tm_year;
711 dir = loc->tm_yday - gmt.tm_yday;
714 time_diff += dir * 24 * 60 * 60;
720 * Format an absolute wallclock system time string.
721 * May be called upto 4 times in a single printf() statement.
722 * Displays microsecond resolution.
724 * @return buffer to a formatted system time string.
727 olsr_wallclock_string(void)
729 static char buf[sizeof("00:00:00.000000")];
733 gettimeofday(&now, NULL);
735 sec = (int)now.tv_sec + olsr_get_timezone();
736 usec = (int)now.tv_usec;
738 snprintf(buf, sizeof(buf), "%02d:%02d:%02d.%06d", (sec % 86400) / 3600, (sec % 3600) / 60, sec % 60, usec);
744 * Format an relative non-wallclock system time string.
745 * May be called upto 4 times in a single printf() statement.
746 * Displays millisecond resolution.
748 * @param clk absolute time expressed in clockticks
749 * @return buffer to a formatted system time string.
752 olsr_clock_string(uint32_t clk)
754 static char buf[sizeof("00:00:00.000")];
756 /* On most systems a clocktick is a 10ms quantity. */
757 unsigned int msec = clk % 1000;
758 unsigned int sec = clk / 1000;
760 snprintf(buf, sizeof(buf), "%02u:%02u:%02u.%03u", sec / 3600, (sec % 3600) / 60, (sec % 60), (msec % MSEC_PER_SEC));
768 * @param rel_time relative time expressed in milliseconds
769 * @param jitter_pct jitter expressed in percent
770 * @param periodical true for a repeating timer, false for a one-shot timer
771 * @param cb_func timer callback function
772 * @param context context for the callback function
773 * @param ci timer cookie
774 * @return a pointer to the created entry
777 olsr_start_timer(unsigned int rel_time,
778 uint8_t jitter_pct, bool periodical, timer_cb_func cb_func, void *context, struct olsr_cookie_info *ci)
780 struct timer_entry *timer;
787 timer = olsr_cookie_malloc(timer_mem_cookie);
790 * Compute random numbers only once.
792 if (!timer->timer_random) {
793 timer->timer_random = random();
797 timer->timer_clock = calc_jitter(rel_time, jitter_pct, timer->timer_random);
798 timer->timer_cb = cb_func;
799 timer->timer_cb_context = context;
800 timer->timer_jitter_pct = jitter_pct;
801 timer->timer_flags = OLSR_TIMER_RUNNING;
803 /* The cookie is used for debugging to traceback the originator */
804 timer->timer_cookie = ci;
805 olsr_cookie_usage_incr(ci->ci_id);
807 /* Singleshot or periodical timer ? */
808 timer->timer_period = periodical ? rel_time : 0;
811 * Now insert in the respective timer_wheel slot.
813 list_add_before(&timer_wheel[timer->timer_clock & TIMER_WHEEL_MASK], &timer->timer_list);
815 OLSR_PRINTF(7, "TIMER: start %s timer %p firing in %s, ctx %p\n",
816 ci->ci_name, timer, olsr_clock_string(timer->timer_clock), context);
824 * @param timer the timer_entry that shall be removed
827 olsr_stop_timer(struct timer_entry *timer)
829 /* It's okay to get a NULL here */
834 assert(timer->timer_cookie); /* we want timer cookies everywhere */
836 OLSR_PRINTF(7, "TIMER: stop %s timer %p, ctx %p\n",
837 timer->timer_cookie->ci_name, timer, timer->timer_cb_context);
841 * Carve out of the existing wheel_slot and free.
843 list_remove(&timer->timer_list);
844 timer->timer_flags &= ~OLSR_TIMER_RUNNING;
845 olsr_cookie_usage_decr(timer->timer_cookie->ci_id);
847 olsr_cookie_free(timer_mem_cookie, timer);
851 * Change a timer_entry.
853 * @param timer timer_entry to be changed.
854 * @param rel_time new relative time expressed in units of milliseconds.
855 * @param jitter_pct new jitter expressed in percent.
856 * @param periodical true for a repeating timer, false for a one-shot timer
859 olsr_change_timer(struct timer_entry *timer, unsigned int rel_time, uint8_t jitter_pct, bool periodical)
866 assert(timer->timer_cookie); /* we want timer cookies everywhere */
868 /* Singleshot or periodical timer ? */
869 timer->timer_period = periodical ? rel_time : 0;
871 timer->timer_clock = calc_jitter(rel_time, jitter_pct, timer->timer_random);
872 timer->timer_jitter_pct = jitter_pct;
875 * Changes are easy: Remove timer from the exisiting timer_wheel slot
876 * and reinsert into the new slot.
878 list_remove(&timer->timer_list);
879 list_add_before(&timer_wheel[timer->timer_clock & TIMER_WHEEL_MASK], &timer->timer_list);
881 OLSR_PRINTF(7, "TIMER: change %s timer %p, firing to %s, ctx %p\n",
882 timer->timer_cookie->ci_name, timer, olsr_clock_string(timer->timer_clock), timer->timer_cb_context);
886 * This is the one stop shop for all sort of timer manipulation.
887 * Depending on the paseed in parameters a new timer is started,
888 * or an existing timer is started or an existing timer is
892 olsr_set_timer(struct timer_entry **timer_ptr,
893 unsigned int rel_time,
894 uint8_t jitter_pct, bool periodical, timer_cb_func cb_func, void *context, struct olsr_cookie_info *cookie)
897 cookie = def_timer_ci;
901 /* No good future time provided, kill it. */
902 olsr_stop_timer(*timer_ptr);
905 else if ((*timer_ptr) == NULL) {
906 /* No timer running, kick it. */
907 *timer_ptr = olsr_start_timer(rel_time, jitter_pct, periodical, cb_func, context, cookie);
910 olsr_change_timer(*timer_ptr, rel_time, jitter_pct, periodical);
917 * indent-tabs-mode: nil