]> git.proxmox.com Git - mirror_frr.git/blame - lib/wheel.c
Merge pull request #12708 from donaldsharp/no_notification
[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 *
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
bf8d3d6a
DL
27DEFINE_MTYPE_STATIC(LIB, TIMER_WHEEL, "Timer Wheel");
28DEFINE_MTYPE_STATIC(LIB, TIMER_WHEEL_LIST, "Timer Wheel Slot List");
cbea8737
DS
29
30static int debug_timer_wheel = 0;
31
cc9f21da 32static void wheel_timer_thread(struct thread *t);
c2cfa843 33
cc9f21da 34static void 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);
cbea8737 43
d62a17ae 44 wheel->curr_slot += wheel->slots_to_skip;
cbea8737 45
d62a17ae 46 curr_slot = wheel->curr_slot % wheel->slots;
cbea8737 47
d62a17ae 48 if (debug_timer_wheel)
5e81f5dd
DS
49 zlog_debug("%s: Wheel Slot: %lld(%lld) count: %d", __func__,
50 wheel->curr_slot, curr_slot,
d62a17ae 51 listcount(wheel->wheel_slot_lists[curr_slot]));
cbea8737 52
d62a17ae 53 for (ALL_LIST_ELEMENTS(wheel->wheel_slot_lists[curr_slot], node,
54 nextnode, data))
55 (*wheel->slot_run)(data);
cbea8737 56
d62a17ae 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
d62a17ae 62 wheel->slots_to_skip = slots_to_skip;
63 thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
64 wheel->nexttime * slots_to_skip, &wheel->timer);
cbea8737
DS
65}
66
cc9f21da 67static void wheel_timer_thread(struct thread *t)
c2cfa843
DS
68{
69 struct timer_wheel *wheel;
70
71 wheel = THREAD_ARG(t);
72
60a3efec 73 thread_execute(wheel->master, wheel_timer_thread_helper, wheel, 0);
c2cfa843
DS
74}
75
d62a17ae 76struct timer_wheel *wheel_init(struct thread_master *master, int period,
d8b87afe 77 size_t slots, unsigned int (*slot_key)(const void *),
c2cfa843
DS
78 void (*slot_run)(void *),
79 const char *run_name)
cbea8737 80{
d62a17ae 81 struct timer_wheel *wheel;
82 size_t i;
cbea8737 83
d62a17ae 84 wheel = XCALLOC(MTYPE_TIMER_WHEEL, sizeof(struct timer_wheel));
cbea8737 85
c2cfa843 86 wheel->name = XSTRDUP(MTYPE_TIMER_WHEEL, run_name);
d62a17ae 87 wheel->slot_key = slot_key;
88 wheel->slot_run = slot_run;
cbea8737 89
d62a17ae 90 wheel->period = period;
91 wheel->slots = slots;
92 wheel->curr_slot = 0;
93 wheel->master = master;
94 wheel->nexttime = period / slots;
cbea8737 95
d62a17ae 96 wheel->wheel_slot_lists = XCALLOC(MTYPE_TIMER_WHEEL_LIST,
5e7d0337 97 slots * sizeof(struct list *));
d62a17ae 98 for (i = 0; i < slots; i++)
99 wheel->wheel_slot_lists[i] = list_new();
cbea8737 100
d62a17ae 101 thread_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
102 wheel->nexttime, &wheel->timer);
cbea8737 103
d62a17ae 104 return wheel;
cbea8737
DS
105}
106
d62a17ae 107void wheel_delete(struct timer_wheel *wheel)
cbea8737 108{
d62a17ae 109 int i;
cbea8737 110
d62a17ae 111 for (i = 0; i < wheel->slots; i++) {
6a154c88 112 list_delete(&wheel->wheel_slot_lists[i]);
d62a17ae 113 }
cbea8737 114
d62a17ae 115 THREAD_OFF(wheel->timer);
116 XFREE(MTYPE_TIMER_WHEEL_LIST, wheel->wheel_slot_lists);
c2cfa843 117 XFREE(MTYPE_TIMER_WHEEL, wheel->name);
d62a17ae 118 XFREE(MTYPE_TIMER_WHEEL, wheel);
cbea8737
DS
119}
120
d62a17ae 121int wheel_add_item(struct timer_wheel *wheel, void *item)
cbea8737 122{
d62a17ae 123 long long slot;
cbea8737 124
d62a17ae 125 slot = (*wheel->slot_key)(item);
cbea8737 126
d62a17ae 127 if (debug_timer_wheel)
15569c58
DA
128 zlog_debug("%s: Inserting %p: %lld %lld", __func__, item, slot,
129 slot % wheel->slots);
d62a17ae 130 listnode_add(wheel->wheel_slot_lists[slot % wheel->slots], item);
cbea8737 131
d62a17ae 132 return 0;
cbea8737
DS
133}
134
d62a17ae 135int wheel_remove_item(struct timer_wheel *wheel, void *item)
cbea8737 136{
d62a17ae 137 long long slot;
cbea8737 138
d62a17ae 139 slot = (*wheel->slot_key)(item);
cbea8737 140
d62a17ae 141 if (debug_timer_wheel)
15569c58
DA
142 zlog_debug("%s: Removing %p: %lld %lld", __func__, item, slot,
143 slot % wheel->slots);
d62a17ae 144 listnode_delete(wheel->wheel_slot_lists[slot % wheel->slots], item);
cbea8737 145
d62a17ae 146 return 0;
cbea8737 147}