]> git.proxmox.com Git - mirror_frr.git/blob - lib/wheel.c
Merge pull request #7980 from gromit1811/fix_ospf6_lsa_str_oob
[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 static int wheel_timer_thread_helper(struct thread *t)
35 {
36 struct listnode *node, *nextnode;
37 unsigned long long curr_slot;
38 unsigned int slots_to_skip = 1;
39 struct timer_wheel *wheel;
40 void *data;
41
42 wheel = THREAD_ARG(t);
43 THREAD_OFF(wheel->timer);
44
45 wheel->curr_slot += wheel->slots_to_skip;
46
47 curr_slot = wheel->curr_slot % wheel->slots;
48
49 if (debug_timer_wheel)
50 zlog_debug("%s: Wheel Slot: %lld(%lld) count: %d", __func__,
51 wheel->curr_slot, curr_slot,
52 listcount(wheel->wheel_slot_lists[curr_slot]));
53
54 for (ALL_LIST_ELEMENTS(wheel->wheel_slot_lists[curr_slot], node,
55 nextnode, data))
56 (*wheel->slot_run)(data);
57
58 while (list_isempty(wheel->wheel_slot_lists[(curr_slot + slots_to_skip)
59 % wheel->slots])
60 && (curr_slot + slots_to_skip) % wheel->slots != curr_slot)
61 slots_to_skip++;
62
63 wheel->slots_to_skip = slots_to_skip;
64 thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
65 wheel->nexttime * slots_to_skip, &wheel->timer);
66
67 return 0;
68 }
69
70 static int wheel_timer_thread(struct thread *t)
71 {
72 struct timer_wheel *wheel;
73
74 wheel = THREAD_ARG(t);
75
76 thread_execute(wheel->master, wheel_timer_thread_helper, wheel, 0);
77
78 return 0;
79 }
80
81 struct timer_wheel *wheel_init(struct thread_master *master, int period,
82 size_t slots, unsigned int (*slot_key)(const void *),
83 void (*slot_run)(void *),
84 const char *run_name)
85 {
86 struct timer_wheel *wheel;
87 size_t i;
88
89 wheel = XCALLOC(MTYPE_TIMER_WHEEL, sizeof(struct timer_wheel));
90
91 wheel->name = XSTRDUP(MTYPE_TIMER_WHEEL, run_name);
92 wheel->slot_key = slot_key;
93 wheel->slot_run = slot_run;
94
95 wheel->period = period;
96 wheel->slots = slots;
97 wheel->curr_slot = 0;
98 wheel->master = master;
99 wheel->nexttime = period / slots;
100
101 wheel->wheel_slot_lists = XCALLOC(MTYPE_TIMER_WHEEL_LIST,
102 slots * sizeof(struct listnode *));
103 for (i = 0; i < slots; i++)
104 wheel->wheel_slot_lists[i] = list_new();
105
106 thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
107 wheel->nexttime, &wheel->timer);
108
109 return wheel;
110 }
111
112 void wheel_delete(struct timer_wheel *wheel)
113 {
114 int i;
115
116 for (i = 0; i < wheel->slots; i++) {
117 list_delete(&wheel->wheel_slot_lists[i]);
118 }
119
120 THREAD_OFF(wheel->timer);
121 XFREE(MTYPE_TIMER_WHEEL_LIST, wheel->wheel_slot_lists);
122 XFREE(MTYPE_TIMER_WHEEL, wheel->name);
123 XFREE(MTYPE_TIMER_WHEEL, wheel);
124 }
125
126 int wheel_stop(struct timer_wheel *wheel)
127 {
128 THREAD_OFF(wheel->timer);
129 return 0;
130 }
131
132 int wheel_start(struct timer_wheel *wheel)
133 {
134 if (!wheel->timer)
135 thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
136 wheel->nexttime, &wheel->timer);
137
138 return 0;
139 }
140
141 int wheel_add_item(struct timer_wheel *wheel, void *item)
142 {
143 long long slot;
144
145 slot = (*wheel->slot_key)(item);
146
147 if (debug_timer_wheel)
148 zlog_debug("%s: Inserting %p: %lld %lld", __func__, item, slot,
149 slot % wheel->slots);
150 listnode_add(wheel->wheel_slot_lists[slot % wheel->slots], item);
151
152 return 0;
153 }
154
155 int wheel_remove_item(struct timer_wheel *wheel, void *item)
156 {
157 long long slot;
158
159 slot = (*wheel->slot_key)(item);
160
161 if (debug_timer_wheel)
162 zlog_debug("%s: Removing %p: %lld %lld", __func__, item, slot,
163 slot % wheel->slots);
164 listnode_delete(wheel->wheel_slot_lists[slot % wheel->slots], item);
165
166 return 0;
167 }