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