3 * Copyright (C) 2016 Cumulus Networks, Inc.
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.
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.
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
27 DEFINE_MTYPE_STATIC(LIB
, TIMER_WHEEL
, "Timer Wheel");
28 DEFINE_MTYPE_STATIC(LIB
, TIMER_WHEEL_LIST
, "Timer Wheel Slot List");
30 static int debug_timer_wheel
= 0;
32 static void wheel_timer_thread(struct thread
*t
);
34 static void wheel_timer_thread_helper(struct thread
*t
)
36 struct listnode
*node
, *nextnode
;
37 unsigned long long curr_slot
;
38 unsigned int slots_to_skip
= 1;
39 struct timer_wheel
*wheel
;
42 wheel
= THREAD_ARG(t
);
43 THREAD_OFF(wheel
->timer
);
45 wheel
->curr_slot
+= wheel
->slots_to_skip
;
47 curr_slot
= wheel
->curr_slot
% wheel
->slots
;
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
]));
54 for (ALL_LIST_ELEMENTS(wheel
->wheel_slot_lists
[curr_slot
], node
,
56 (*wheel
->slot_run
)(data
);
58 while (list_isempty(wheel
->wheel_slot_lists
[(curr_slot
+ slots_to_skip
)
60 && (curr_slot
+ slots_to_skip
) % wheel
->slots
!= curr_slot
)
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
);
68 static void wheel_timer_thread(struct thread
*t
)
70 struct timer_wheel
*wheel
;
72 wheel
= THREAD_ARG(t
);
74 thread_execute(wheel
->master
, wheel_timer_thread_helper
, wheel
, 0);
77 struct timer_wheel
*wheel_init(struct thread_master
*master
, int period
,
78 size_t slots
, unsigned int (*slot_key
)(const void *),
79 void (*slot_run
)(void *),
82 struct timer_wheel
*wheel
;
85 wheel
= XCALLOC(MTYPE_TIMER_WHEEL
, sizeof(struct timer_wheel
));
87 wheel
->name
= XSTRDUP(MTYPE_TIMER_WHEEL
, run_name
);
88 wheel
->slot_key
= slot_key
;
89 wheel
->slot_run
= slot_run
;
91 wheel
->period
= period
;
94 wheel
->master
= master
;
95 wheel
->nexttime
= period
/ slots
;
97 wheel
->wheel_slot_lists
= XCALLOC(MTYPE_TIMER_WHEEL_LIST
,
98 slots
* sizeof(struct listnode
*));
99 for (i
= 0; i
< slots
; i
++)
100 wheel
->wheel_slot_lists
[i
] = list_new();
102 thread_add_timer_msec(wheel
->master
, wheel_timer_thread
, wheel
,
103 wheel
->nexttime
, &wheel
->timer
);
108 void wheel_delete(struct timer_wheel
*wheel
)
112 for (i
= 0; i
< wheel
->slots
; i
++) {
113 list_delete(&wheel
->wheel_slot_lists
[i
]);
116 THREAD_OFF(wheel
->timer
);
117 XFREE(MTYPE_TIMER_WHEEL_LIST
, wheel
->wheel_slot_lists
);
118 XFREE(MTYPE_TIMER_WHEEL
, wheel
->name
);
119 XFREE(MTYPE_TIMER_WHEEL
, wheel
);
122 int wheel_stop(struct timer_wheel
*wheel
)
124 THREAD_OFF(wheel
->timer
);
128 int wheel_start(struct timer_wheel
*wheel
)
131 thread_add_timer_msec(wheel
->master
, wheel_timer_thread
, wheel
,
132 wheel
->nexttime
, &wheel
->timer
);
137 int wheel_add_item(struct timer_wheel
*wheel
, void *item
)
141 slot
= (*wheel
->slot_key
)(item
);
143 if (debug_timer_wheel
)
144 zlog_debug("%s: Inserting %p: %lld %lld", __func__
, item
, slot
,
145 slot
% wheel
->slots
);
146 listnode_add(wheel
->wheel_slot_lists
[slot
% wheel
->slots
], item
);
151 int wheel_remove_item(struct timer_wheel
*wheel
, void *item
)
155 slot
= (*wheel
->slot_key
)(item
);
157 if (debug_timer_wheel
)
158 zlog_debug("%s: Removing %p: %lld %lld", __func__
, item
, slot
,
159 slot
% wheel
->slots
);
160 listnode_delete(wheel
->wheel_slot_lists
[slot
% wheel
->slots
], item
);