Skip to content

Commit fe352cb

Browse files
committed
lib: add option to start/stop wheel timer
This fix add option to start and stop a wheel timer Signed-off-by: Soumya Roy <[email protected]>
1 parent f6d4070 commit fe352cb

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

lib/wheel.c

+26-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ DEFINE_MTYPE_STATIC(LIB, TIMER_WHEEL_LIST, "Timer Wheel Slot List");
1717
static int debug_timer_wheel = 0;
1818

1919
static void wheel_timer_thread(struct event *t);
20+
static void wheel_pause(struct timer_wheel *wheel);
21+
static void wheel_start(struct timer_wheel *wheel);
2022

2123
static void wheel_timer_thread(struct event *t)
2224
{
2325
struct listnode *node, *nextnode;
2426
unsigned long long curr_slot;
25-
unsigned int slots_to_skip = 1;
27+
int slots_to_skip = 1;
2628
struct timer_wheel *wheel;
2729
void *data;
2830

@@ -47,6 +49,16 @@ static void wheel_timer_thread(struct event *t)
4749
slots_to_skip++;
4850

4951
wheel->slots_to_skip = slots_to_skip;
52+
if ((((curr_slot + slots_to_skip) % wheel->slots) == curr_slot) &&
53+
list_isempty(wheel->wheel_slot_lists[curr_slot])) {
54+
/* Cam to back to same slot and that is empty
55+
* so the wheel is empty, puase it
56+
*/
57+
wheel_pause(wheel);
58+
zlog_debug("Pasued an empty wheel %p", wheel);
59+
return;
60+
}
61+
5062
event_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
5163
wheel->nexttime * slots_to_skip, &wheel->timer);
5264
}
@@ -63,7 +75,6 @@ struct timer_wheel *wheel_init(struct event_loop *master, int period,
6375

6476
wheel->slot_key = slot_key;
6577
wheel->slot_run = slot_run;
66-
6778
wheel->period = period;
6879
wheel->slots = slots;
6980
wheel->curr_slot = 0;
@@ -75,9 +86,6 @@ struct timer_wheel *wheel_init(struct event_loop *master, int period,
7586
for (i = 0; i < slots; i++)
7687
wheel->wheel_slot_lists[i] = list_new();
7788

78-
event_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
79-
wheel->nexttime, &wheel->timer);
80-
8189
return wheel;
8290
}
8391

@@ -104,7 +112,7 @@ int wheel_add_item(struct timer_wheel *wheel, void *item)
104112
zlog_debug("%s: Inserting %p: %lld %lld", __func__, item, slot,
105113
slot % wheel->slots);
106114
listnode_add(wheel->wheel_slot_lists[slot % wheel->slots], item);
107-
115+
wheel_start(wheel);
108116
return 0;
109117
}
110118

@@ -121,3 +129,15 @@ int wheel_remove_item(struct timer_wheel *wheel, void *item)
121129

122130
return 0;
123131
}
132+
133+
static void wheel_pause(struct timer_wheel *wheel)
134+
{
135+
EVENT_OFF(wheel->timer);
136+
}
137+
138+
static void wheel_start(struct timer_wheel *wheel)
139+
{
140+
if (!event_is_scheduled(wheel->timer))
141+
event_add_timer_msec(wheel->master, wheel_timer_thread, wheel, wheel->nexttime,
142+
&wheel->timer);
143+
}

lib/wheel.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct timer_wheel {
1717
long long curr_slot;
1818
unsigned int period;
1919
unsigned int nexttime;
20-
unsigned int slots_to_skip;
20+
int slots_to_skip;
2121

2222
struct list **wheel_slot_lists;
2323
struct event *timer;

0 commit comments

Comments
 (0)