]> git.proxmox.com Git - mirror_frr.git/blame - lib/wheel.c
debianpkg: Remove -werror from Ubuntu 14.04 and 12.04 build to skip warnings from...
[mirror_frr.git] / lib / wheel.c
CommitLineData
cbea8737
DS
1/*
2 * Timer Wheel
3 * Copyright (C) 2016 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
21#include "zebra.h"
22#include "linklist.h"
23#include "thread.h"
24#include "memory.h"
25#include "wheel.h"
26#include "log.h"
27
ac4d0be5 28DEFINE_MTYPE_STATIC(LIB, TIMER_WHEEL, "Timer Wheel")
cbea8737
DS
29DEFINE_MTYPE_STATIC(LIB, TIMER_WHEEL_LIST, "Timer Wheel Slot List")
30
31static int debug_timer_wheel = 0;
32
ac4d0be5 33static int wheel_timer_thread(struct thread *t)
cbea8737 34{
ac4d0be5 35 struct listnode *node, *nextnode;
36 unsigned long long curr_slot;
37 unsigned int slots_to_skip = 1;
38 struct timer_wheel *wheel;
39 void *data;
cbea8737 40
ac4d0be5 41 wheel = THREAD_ARG(t);
42 THREAD_OFF(wheel->timer);
cbea8737 43
ac4d0be5 44 wheel->curr_slot += wheel->slots_to_skip;
cbea8737 45
ac4d0be5 46 curr_slot = wheel->curr_slot % wheel->slots;
cbea8737 47
ac4d0be5 48 if (debug_timer_wheel)
49 zlog_debug("%s: Wheel Slot: %lld(%lld) count: %d",
50 __PRETTY_FUNCTION__, wheel->curr_slot, curr_slot,
51 listcount(wheel->wheel_slot_lists[curr_slot]));
cbea8737 52
ac4d0be5 53 for (ALL_LIST_ELEMENTS(wheel->wheel_slot_lists[curr_slot], node,
54 nextnode, data))
55 (*wheel->slot_run)(data);
cbea8737 56
ac4d0be5 57 while (list_isempty(wheel->wheel_slot_lists[(curr_slot + slots_to_skip)
58 % wheel->slots])
59 && (curr_slot + slots_to_skip) % wheel->slots != curr_slot)
60 slots_to_skip++;
cbea8737 61
ac4d0be5 62 wheel->slots_to_skip = slots_to_skip;
63 THREAD_TIMER_MSEC_ON(wheel->master, wheel->timer, wheel_timer_thread,
64 wheel, wheel->nexttime * slots_to_skip);
cbea8737 65
ac4d0be5 66 return 0;
cbea8737
DS
67}
68
ac4d0be5 69struct timer_wheel *wheel_init(struct thread_master *master, int period,
70 size_t slots, unsigned int (*slot_key)(void *),
71 void (*slot_run)(void *))
cbea8737 72{
ac4d0be5 73 struct timer_wheel *wheel;
74 size_t i;
cbea8737 75
ac4d0be5 76 wheel = XCALLOC(MTYPE_TIMER_WHEEL, sizeof(struct timer_wheel));
cbea8737 77
ac4d0be5 78 wheel->slot_key = slot_key;
79 wheel->slot_run = slot_run;
cbea8737 80
ac4d0be5 81 wheel->period = period;
82 wheel->slots = slots;
83 wheel->curr_slot = 0;
84 wheel->master = master;
85 wheel->nexttime = period / slots;
cbea8737 86
ac4d0be5 87 wheel->wheel_slot_lists = XCALLOC(MTYPE_TIMER_WHEEL_LIST,
88 slots * sizeof(struct listnode *));
89 for (i = 0; i < slots; i++)
90 wheel->wheel_slot_lists[i] = list_new();
cbea8737 91
ac4d0be5 92 THREAD_TIMER_MSEC_ON(wheel->master, wheel->timer, wheel_timer_thread,
93 wheel, wheel->nexttime);
cbea8737 94
ac4d0be5 95 return wheel;
cbea8737
DS
96}
97
ac4d0be5 98void wheel_delete(struct timer_wheel *wheel)
cbea8737 99{
ac4d0be5 100 int i;
cbea8737 101
ac4d0be5 102 for (i = 0; i < wheel->slots; i++) {
103 list_delete(wheel->wheel_slot_lists[i]);
104 }
cbea8737 105
ac4d0be5 106 THREAD_OFF(wheel->timer);
107 XFREE(MTYPE_TIMER_WHEEL_LIST, wheel->wheel_slot_lists);
108 XFREE(MTYPE_TIMER_WHEEL, wheel);
cbea8737
DS
109}
110
ac4d0be5 111int wheel_stop(struct timer_wheel *wheel)
cbea8737 112{
ac4d0be5 113 THREAD_OFF(wheel->timer);
114 return 0;
cbea8737
DS
115}
116
ac4d0be5 117int wheel_start(struct timer_wheel *wheel)
cbea8737 118{
ac4d0be5 119 if (!wheel->timer)
120 THREAD_TIMER_MSEC_ON(wheel->master, wheel->timer,
121 wheel_timer_thread, wheel,
122 wheel->nexttime);
cbea8737 123
ac4d0be5 124 return 0;
cbea8737
DS
125}
126
ac4d0be5 127int wheel_add_item(struct timer_wheel *wheel, void *item)
cbea8737 128{
ac4d0be5 129 long long slot;
cbea8737 130
ac4d0be5 131 slot = (*wheel->slot_key)(item);
cbea8737 132
ac4d0be5 133 if (debug_timer_wheel)
134 zlog_debug("%s: Inserting %p: %lld %lld", __PRETTY_FUNCTION__,
135 item, slot, slot % wheel->slots);
136 listnode_add(wheel->wheel_slot_lists[slot % wheel->slots], item);
cbea8737 137
ac4d0be5 138 return 0;
cbea8737
DS
139}
140
ac4d0be5 141int wheel_remove_item(struct timer_wheel *wheel, void *item)
cbea8737 142{
ac4d0be5 143 long long slot;
cbea8737 144
ac4d0be5 145 slot = (*wheel->slot_key)(item);
cbea8737 146
ac4d0be5 147 if (debug_timer_wheel)
148 zlog_debug("%s: Removing %p: %lld %lld", __PRETTY_FUNCTION__,
149 item, slot, slot % wheel->slots);
150 listnode_delete(wheel->wheel_slot_lists[slot % wheel->slots], item);
cbea8737 151
ac4d0be5 152 return 0;
cbea8737 153}