]>
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 | ||
d62a17ae | 32 | static int wheel_timer_thread(struct thread *t) |
cbea8737 | 33 | { |
d62a17ae | 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; | |
cbea8737 | 39 | |
d62a17ae | 40 | wheel = THREAD_ARG(t); |
41 | THREAD_OFF(wheel->timer); | |
cbea8737 | 42 | |
d62a17ae | 43 | wheel->curr_slot += wheel->slots_to_skip; |
cbea8737 | 44 | |
d62a17ae | 45 | curr_slot = wheel->curr_slot % wheel->slots; |
cbea8737 | 46 | |
d62a17ae | 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])); | |
cbea8737 | 51 | |
d62a17ae | 52 | for (ALL_LIST_ELEMENTS(wheel->wheel_slot_lists[curr_slot], node, |
53 | nextnode, data)) | |
54 | (*wheel->slot_run)(data); | |
cbea8737 | 55 | |
d62a17ae | 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++; | |
cbea8737 | 60 | |
d62a17ae | 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); | |
cbea8737 | 64 | |
d62a17ae | 65 | return 0; |
cbea8737 DS |
66 | } |
67 | ||
d62a17ae | 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 *)) | |
cbea8737 | 71 | { |
d62a17ae | 72 | struct timer_wheel *wheel; |
73 | size_t i; | |
cbea8737 | 74 | |
d62a17ae | 75 | wheel = XCALLOC(MTYPE_TIMER_WHEEL, sizeof(struct timer_wheel)); |
cbea8737 | 76 | |
d62a17ae | 77 | wheel->slot_key = slot_key; |
78 | wheel->slot_run = slot_run; | |
cbea8737 | 79 | |
d62a17ae | 80 | wheel->period = period; |
81 | wheel->slots = slots; | |
82 | wheel->curr_slot = 0; | |
83 | wheel->master = master; | |
84 | wheel->nexttime = period / slots; | |
cbea8737 | 85 | |
d62a17ae | 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(); | |
cbea8737 | 90 | |
d62a17ae | 91 | thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel, |
92 | wheel->nexttime, &wheel->timer); | |
cbea8737 | 93 | |
d62a17ae | 94 | return wheel; |
cbea8737 DS |
95 | } |
96 | ||
d62a17ae | 97 | void wheel_delete(struct timer_wheel *wheel) |
cbea8737 | 98 | { |
d62a17ae | 99 | int i; |
cbea8737 | 100 | |
d62a17ae | 101 | for (i = 0; i < wheel->slots; i++) { |
affe9e99 | 102 | list_delete_and_null(&wheel->wheel_slot_lists[i]); |
d62a17ae | 103 | } |
cbea8737 | 104 | |
d62a17ae | 105 | THREAD_OFF(wheel->timer); |
106 | XFREE(MTYPE_TIMER_WHEEL_LIST, wheel->wheel_slot_lists); | |
107 | XFREE(MTYPE_TIMER_WHEEL, wheel); | |
cbea8737 DS |
108 | } |
109 | ||
d62a17ae | 110 | int wheel_stop(struct timer_wheel *wheel) |
cbea8737 | 111 | { |
d62a17ae | 112 | THREAD_OFF(wheel->timer); |
113 | return 0; | |
cbea8737 DS |
114 | } |
115 | ||
d62a17ae | 116 | int wheel_start(struct timer_wheel *wheel) |
cbea8737 | 117 | { |
d62a17ae | 118 | if (!wheel->timer) |
119 | thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel, | |
120 | wheel->nexttime, &wheel->timer); | |
cbea8737 | 121 | |
d62a17ae | 122 | return 0; |
cbea8737 DS |
123 | } |
124 | ||
d62a17ae | 125 | int wheel_add_item(struct timer_wheel *wheel, void *item) |
cbea8737 | 126 | { |
d62a17ae | 127 | long long slot; |
cbea8737 | 128 | |
d62a17ae | 129 | slot = (*wheel->slot_key)(item); |
cbea8737 | 130 | |
d62a17ae | 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); | |
cbea8737 | 135 | |
d62a17ae | 136 | return 0; |
cbea8737 DS |
137 | } |
138 | ||
d62a17ae | 139 | int wheel_remove_item(struct timer_wheel *wheel, void *item) |
cbea8737 | 140 | { |
d62a17ae | 141 | long long slot; |
cbea8737 | 142 | |
d62a17ae | 143 | slot = (*wheel->slot_key)(item); |
cbea8737 | 144 | |
d62a17ae | 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); | |
cbea8737 | 149 | |
d62a17ae | 150 | return 0; |
cbea8737 | 151 | } |