]>
Commit | Line | Data |
---|---|---|
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 | * | |
896014f4 DL |
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 | |
cbea8737 DS |
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 | ||
d62a17ae | 27 | DEFINE_MTYPE_STATIC(LIB, TIMER_WHEEL, "Timer Wheel") |
cbea8737 DS |
28 | DEFINE_MTYPE_STATIC(LIB, TIMER_WHEEL_LIST, "Timer Wheel Slot List") |
29 | ||
30 | static int debug_timer_wheel = 0; | |
31 | ||
c2cfa843 DS |
32 | static int wheel_timer_thread(struct thread *t); |
33 | ||
34 | static int wheel_timer_thread_helper(struct thread *t) | |
cbea8737 | 35 | { |
d62a17ae | 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; | |
cbea8737 | 41 | |
d62a17ae | 42 | wheel = THREAD_ARG(t); |
43 | THREAD_OFF(wheel->timer); | |
cbea8737 | 44 | |
d62a17ae | 45 | wheel->curr_slot += wheel->slots_to_skip; |
cbea8737 | 46 | |
d62a17ae | 47 | curr_slot = wheel->curr_slot % wheel->slots; |
cbea8737 | 48 | |
d62a17ae | 49 | if (debug_timer_wheel) |
50 | zlog_debug("%s: Wheel Slot: %lld(%lld) count: %d", | |
51 | __PRETTY_FUNCTION__, wheel->curr_slot, curr_slot, | |
52 | listcount(wheel->wheel_slot_lists[curr_slot])); | |
cbea8737 | 53 | |
d62a17ae | 54 | for (ALL_LIST_ELEMENTS(wheel->wheel_slot_lists[curr_slot], node, |
55 | nextnode, data)) | |
56 | (*wheel->slot_run)(data); | |
cbea8737 | 57 | |
d62a17ae | 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++; | |
cbea8737 | 62 | |
d62a17ae | 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); | |
cbea8737 | 66 | |
d62a17ae | 67 | return 0; |
cbea8737 DS |
68 | } |
69 | ||
c2cfa843 DS |
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_name(wheel->master, wheel_timer_thread_helper, | |
77 | wheel, 0, wheel->name); | |
78 | ||
79 | return 0; | |
80 | } | |
81 | ||
d62a17ae | 82 | struct timer_wheel *wheel_init(struct thread_master *master, int period, |
83 | size_t slots, unsigned int (*slot_key)(void *), | |
c2cfa843 DS |
84 | void (*slot_run)(void *), |
85 | const char *run_name) | |
cbea8737 | 86 | { |
d62a17ae | 87 | struct timer_wheel *wheel; |
88 | size_t i; | |
cbea8737 | 89 | |
d62a17ae | 90 | wheel = XCALLOC(MTYPE_TIMER_WHEEL, sizeof(struct timer_wheel)); |
cbea8737 | 91 | |
c2cfa843 | 92 | wheel->name = XSTRDUP(MTYPE_TIMER_WHEEL, run_name); |
d62a17ae | 93 | wheel->slot_key = slot_key; |
94 | wheel->slot_run = slot_run; | |
cbea8737 | 95 | |
d62a17ae | 96 | wheel->period = period; |
97 | wheel->slots = slots; | |
98 | wheel->curr_slot = 0; | |
99 | wheel->master = master; | |
100 | wheel->nexttime = period / slots; | |
cbea8737 | 101 | |
d62a17ae | 102 | wheel->wheel_slot_lists = XCALLOC(MTYPE_TIMER_WHEEL_LIST, |
103 | slots * sizeof(struct listnode *)); | |
104 | for (i = 0; i < slots; i++) | |
105 | wheel->wheel_slot_lists[i] = list_new(); | |
cbea8737 | 106 | |
d62a17ae | 107 | thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel, |
108 | wheel->nexttime, &wheel->timer); | |
cbea8737 | 109 | |
d62a17ae | 110 | return wheel; |
cbea8737 DS |
111 | } |
112 | ||
d62a17ae | 113 | void wheel_delete(struct timer_wheel *wheel) |
cbea8737 | 114 | { |
d62a17ae | 115 | int i; |
cbea8737 | 116 | |
d62a17ae | 117 | for (i = 0; i < wheel->slots; i++) { |
6a154c88 | 118 | list_delete(&wheel->wheel_slot_lists[i]); |
d62a17ae | 119 | } |
cbea8737 | 120 | |
d62a17ae | 121 | THREAD_OFF(wheel->timer); |
122 | XFREE(MTYPE_TIMER_WHEEL_LIST, wheel->wheel_slot_lists); | |
c2cfa843 | 123 | XFREE(MTYPE_TIMER_WHEEL, wheel->name); |
d62a17ae | 124 | XFREE(MTYPE_TIMER_WHEEL, wheel); |
cbea8737 DS |
125 | } |
126 | ||
d62a17ae | 127 | int wheel_stop(struct timer_wheel *wheel) |
cbea8737 | 128 | { |
d62a17ae | 129 | THREAD_OFF(wheel->timer); |
130 | return 0; | |
cbea8737 DS |
131 | } |
132 | ||
d62a17ae | 133 | int wheel_start(struct timer_wheel *wheel) |
cbea8737 | 134 | { |
d62a17ae | 135 | if (!wheel->timer) |
136 | thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel, | |
137 | wheel->nexttime, &wheel->timer); | |
cbea8737 | 138 | |
d62a17ae | 139 | return 0; |
cbea8737 DS |
140 | } |
141 | ||
d62a17ae | 142 | int wheel_add_item(struct timer_wheel *wheel, void *item) |
cbea8737 | 143 | { |
d62a17ae | 144 | long long slot; |
cbea8737 | 145 | |
d62a17ae | 146 | slot = (*wheel->slot_key)(item); |
cbea8737 | 147 | |
d62a17ae | 148 | if (debug_timer_wheel) |
149 | zlog_debug("%s: Inserting %p: %lld %lld", __PRETTY_FUNCTION__, | |
150 | item, slot, slot % wheel->slots); | |
151 | listnode_add(wheel->wheel_slot_lists[slot % wheel->slots], item); | |
cbea8737 | 152 | |
d62a17ae | 153 | return 0; |
cbea8737 DS |
154 | } |
155 | ||
d62a17ae | 156 | int wheel_remove_item(struct timer_wheel *wheel, void *item) |
cbea8737 | 157 | { |
d62a17ae | 158 | long long slot; |
cbea8737 | 159 | |
d62a17ae | 160 | slot = (*wheel->slot_key)(item); |
cbea8737 | 161 | |
d62a17ae | 162 | if (debug_timer_wheel) |
163 | zlog_debug("%s: Removing %p: %lld %lld", __PRETTY_FUNCTION__, | |
164 | item, slot, slot % wheel->slots); | |
165 | listnode_delete(wheel->wheel_slot_lists[slot % wheel->slots], item); | |
cbea8737 | 166 | |
d62a17ae | 167 | return 0; |
cbea8737 | 168 | } |