@@ -17,12 +17,14 @@ DEFINE_MTYPE_STATIC(LIB, TIMER_WHEEL_LIST, "Timer Wheel Slot List");
17
17
static int debug_timer_wheel = 0 ;
18
18
19
19
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 );
20
22
21
23
static void wheel_timer_thread (struct event * t )
22
24
{
23
25
struct listnode * node , * nextnode ;
24
26
unsigned long long curr_slot ;
25
- unsigned int slots_to_skip = 1 ;
27
+ int slots_to_skip = 1 ;
26
28
struct timer_wheel * wheel ;
27
29
void * data ;
28
30
@@ -47,6 +49,12 @@ static void wheel_timer_thread(struct event *t)
47
49
slots_to_skip ++ ;
48
50
49
51
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
+
50
58
event_add_timer_msec (wheel -> master , wheel_timer_thread , wheel ,
51
59
wheel -> nexttime * slots_to_skip , & wheel -> timer );
52
60
}
@@ -63,7 +71,6 @@ struct timer_wheel *wheel_init(struct event_loop *master, int period,
63
71
64
72
wheel -> slot_key = slot_key ;
65
73
wheel -> slot_run = slot_run ;
66
-
67
74
wheel -> period = period ;
68
75
wheel -> slots = slots ;
69
76
wheel -> curr_slot = 0 ;
@@ -75,9 +82,6 @@ struct timer_wheel *wheel_init(struct event_loop *master, int period,
75
82
for (i = 0 ; i < slots ; i ++ )
76
83
wheel -> wheel_slot_lists [i ] = list_new ();
77
84
78
- event_add_timer_msec (wheel -> master , wheel_timer_thread , wheel ,
79
- wheel -> nexttime , & wheel -> timer );
80
-
81
85
return wheel ;
82
86
}
83
87
@@ -104,7 +108,7 @@ int wheel_add_item(struct timer_wheel *wheel, void *item)
104
108
zlog_debug ("%s: Inserting %p: %lld %lld" , __func__ , item , slot ,
105
109
slot % wheel -> slots );
106
110
listnode_add (wheel -> wheel_slot_lists [slot % wheel -> slots ], item );
107
-
111
+ wheel_start ( wheel );
108
112
return 0 ;
109
113
}
110
114
@@ -121,3 +125,15 @@ int wheel_remove_item(struct timer_wheel *wheel, void *item)
121
125
122
126
return 0 ;
123
127
}
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