]> git.proxmox.com Git - mirror_frr.git/blob - lib/wheel.c
Merge branch 'stable/3.0' into tmp-3.0-master-merge
[mirror_frr.git] / lib / wheel.c
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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "zebra.h"
21 #include "linklist.h"
22 #include "thread.h"
23 #include "memory.h"
24 #include "wheel.h"
25 #include "log.h"
26
27 DEFINE_MTYPE_STATIC(LIB, TIMER_WHEEL, "Timer Wheel")
28 DEFINE_MTYPE_STATIC(LIB, TIMER_WHEEL_LIST, "Timer Wheel Slot List")
29
30 static int debug_timer_wheel = 0;
31
32 static int wheel_timer_thread(struct thread *t)
33 {
34 struct listnode *node, *nextnode;
35 unsigned long long curr_slot;
36 unsigned int slots_to_skip = 1;
37 struct timer_wheel *wheel;
38 void *data;
39
40 wheel = THREAD_ARG(t);
41 THREAD_OFF(wheel->timer);
42
43 wheel->curr_slot += wheel->slots_to_skip;
44
45 curr_slot = wheel->curr_slot % wheel->slots;
46
47 if (debug_timer_wheel)
48 zlog_debug("%s: Wheel Slot: %lld(%lld) count: %d",
49 __PRETTY_FUNCTION__, wheel->curr_slot, curr_slot,
50 listcount(wheel->wheel_slot_lists[curr_slot]));
51
52 for (ALL_LIST_ELEMENTS(wheel->wheel_slot_lists[curr_slot], node,
53 nextnode, data))
54 (*wheel->slot_run)(data);
55
56 while (list_isempty(wheel->wheel_slot_lists[(curr_slot + slots_to_skip)
57 % wheel->slots])
58 && (curr_slot + slots_to_skip) % wheel->slots != curr_slot)
59 slots_to_skip++;
60
61 wheel->slots_to_skip = slots_to_skip;
62 thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
63 wheel->nexttime * slots_to_skip, &wheel->timer);
64
65 return 0;
66 }
67
68 struct timer_wheel *wheel_init(struct thread_master *master, int period,
69 size_t slots, unsigned int (*slot_key)(void *),
70 void (*slot_run)(void *))
71 {
72 struct timer_wheel *wheel;
73 size_t i;
74
75 wheel = XCALLOC(MTYPE_TIMER_WHEEL, sizeof(struct timer_wheel));
76
77 wheel->slot_key = slot_key;
78 wheel->slot_run = slot_run;
79
80 wheel->period = period;
81 wheel->slots = slots;
82 wheel->curr_slot = 0;
83 wheel->master = master;
84 wheel->nexttime = period / slots;
85
86 wheel->wheel_slot_lists = XCALLOC(MTYPE_TIMER_WHEEL_LIST,
87 slots * sizeof(struct listnode *));
88 for (i = 0; i < slots; i++)
89 wheel->wheel_slot_lists[i] = list_new();
90
91 thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
92 wheel->nexttime, &wheel->timer);
93
94 return wheel;
95 }
96
97 void wheel_delete(struct timer_wheel *wheel)
98 {
99 int i;
100
101 for (i = 0; i < wheel->slots; i++) {
102 list_delete(wheel->wheel_slot_lists[i]);
103 }
104
105 THREAD_OFF(wheel->timer);
106 XFREE(MTYPE_TIMER_WHEEL_LIST, wheel->wheel_slot_lists);
107 XFREE(MTYPE_TIMER_WHEEL, wheel);
108 }
109
110 int wheel_stop(struct timer_wheel *wheel)
111 {
112 THREAD_OFF(wheel->timer);
113 return 0;
114 }
115
116 int wheel_start(struct timer_wheel *wheel)
117 {
118 if (!wheel->timer)
119 thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
120 wheel->nexttime, &wheel->timer);
121
122 return 0;
123 }
124
125 int wheel_add_item(struct timer_wheel *wheel, void *item)
126 {
127 long long slot;
128
129 slot = (*wheel->slot_key)(item);
130
131 if (debug_timer_wheel)
132 zlog_debug("%s: Inserting %p: %lld %lld", __PRETTY_FUNCTION__,
133 item, slot, slot % wheel->slots);
134 listnode_add(wheel->wheel_slot_lists[slot % wheel->slots], item);
135
136 return 0;
137 }
138
139 int wheel_remove_item(struct timer_wheel *wheel, void *item)
140 {
141 long long slot;
142
143 slot = (*wheel->slot_key)(item);
144
145 if (debug_timer_wheel)
146 zlog_debug("%s: Removing %p: %lld %lld", __PRETTY_FUNCTION__,
147 item, slot, slot % wheel->slots);
148 listnode_delete(wheel->wheel_slot_lists[slot % wheel->slots], item);
149
150 return 0;
151 }