Skip to content

Commit f4eedc5

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 b5ff4fa commit f4eedc5

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

lib/wheel.c

+21-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ 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
{
@@ -47,6 +49,12 @@ static void wheel_timer_thread(struct event *t)
4749
slots_to_skip++;
4850

4951
wheel->slots_to_skip = slots_to_skip;
52+
if (wheel->slots_to_skip == wheel->slots) {
53+
wheel_pause(wheel);
54+
zlog_debug("Pasued an empty wheel %p", wheel);
55+
return;
56+
}
57+
5058
event_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
5159
wheel->nexttime * slots_to_skip, &wheel->timer);
5260
}
@@ -63,7 +71,6 @@ struct timer_wheel *wheel_init(struct event_loop *master, int period,
6371

6472
wheel->slot_key = slot_key;
6573
wheel->slot_run = slot_run;
66-
6774
wheel->period = period;
6875
wheel->slots = slots;
6976
wheel->curr_slot = 0;
@@ -75,9 +82,6 @@ struct timer_wheel *wheel_init(struct event_loop *master, int period,
7582
for (i = 0; i < slots; i++)
7683
wheel->wheel_slot_lists[i] = list_new();
7784

78-
event_add_timer_msec(wheel->master, wheel_timer_thread, wheel,
79-
wheel->nexttime, &wheel->timer);
80-
8185
return wheel;
8286
}
8387

@@ -104,7 +108,7 @@ int wheel_add_item(struct timer_wheel *wheel, void *item)
104108
zlog_debug("%s: Inserting %p: %lld %lld", __func__, item, slot,
105109
slot % wheel->slots);
106110
listnode_add(wheel->wheel_slot_lists[slot % wheel->slots], item);
107-
111+
wheel_start(wheel);
108112
return 0;
109113
}
110114

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

122126
return 0;
123127
}
128+
129+
static void wheel_pause(struct timer_wheel *wheel)
130+
{
131+
EVENT_OFF(wheel->timer);
132+
}
133+
134+
static void wheel_start(struct timer_wheel *wheel)
135+
{
136+
if (!event_is_scheduled(wheel->timer))
137+
event_add_timer_msec(wheel->master, wheel_timer_thread, wheel, wheel->nexttime,
138+
&wheel->timer);
139+
}

0 commit comments

Comments
 (0)