-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer_manager.cpp
179 lines (138 loc) · 4.62 KB
/
timer_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* timer_manager.cpp
*
* Created on: 2011-10-18
* Author: gxl2007@hotmail.com
*/
#include "timer_manager.h"
namespace xlnet
{
void base_timer::base_timer_insert(base_timer* prev,base_timer* curr,base_timer* next)
{
prev->m_next = curr ;
curr->m_prev = prev ;
curr->m_next = next ;
next->m_prev = curr ;
}
void base_timer::base_timer_remove(base_timer* timer)
{
timer->m_prev->m_next = timer->m_next ;
timer->m_next->m_prev = timer->m_prev ;
timer->m_next = timer->m_prev = NULL ;
}
timer_manager::timer_manager():m_low_array(NULL),m_high_array(NULL)
{
}
timer_manager::~timer_manager()
{
fini() ;
}
int timer_manager::init(int64_t start_time , int slot_bits)
{
if(slot_bits < step_slot_min_bits || slot_bits > step_slot_max_bits) return -1 ;
if(m_low_array != NULL) return -2 ;
m_step_slot_bits = slot_bits ;
m_step_slot_size = (0x1 << m_step_slot_bits) ;
m_step_slot_mask = ~((~0x0) << m_step_slot_bits ) ;
m_max_expired = m_step_slot_size << m_step_slot_bits ;
m_curr_expired = start_time ;
m_next_expired = m_curr_expired + m_step_slot_size ;
m_low_pos = m_high_pos = 0 ;
base_timer *timer_array = new base_timer[m_step_slot_size*2] ;
if(timer_array == NULL) return -3 ;
m_low_array = timer_array ;
m_high_array = timer_array + m_step_slot_size ;
base_timer* head = NULL ;
for(int i=0 ; i < m_step_slot_size ; ++i)
{
head = m_low_array + i ;
head->m_next = head->m_prev = head ;
head = m_high_array + i ;
head->m_next = head->m_prev = head ;
}
return 0 ;
}
void timer_manager::fini()
{
if(m_low_array)
{
delete[] m_low_array ;
m_low_array = m_high_array = NULL ;
}
}
int timer_manager::add_timer(base_timer* timer)
{
if(m_low_array == NULL || timer == NULL || (timer->m_next!= NULL) ) return -1 ;
int interval = timer->m_expired - m_curr_expired ;
if ( interval < 1 || interval > m_max_expired ) return -2 ;
base_timer* head = NULL ;
if ( interval >= m_step_slot_size )
{
head = m_high_array +( ( ((interval + m_low_pos) >> m_step_slot_bits) -1 + m_high_pos ) & m_step_slot_mask) ;
}
else
{
head = m_low_array + ((interval + m_low_pos) & m_step_slot_mask) ;
}
base_timer::base_timer_insert(head->m_prev,timer,head) ;
if(m_next_expired > timer->m_expired ) m_next_expired = timer->m_expired ;
return 0 ;
}
void timer_manager::del_timer(base_timer* timer)
{
if(timer->m_next && timer->m_prev)
{
base_timer::base_timer_remove(timer) ;
}
} ;
void timer_manager::run_until(int64_t now)
{
//printf("run_until,now=%ld curr_expired=%ld next_expired=%ld\n",now,m_curr_expired,m_next_expired);
int skip = (now > m_next_expired ? m_next_expired : now) - m_curr_expired -1 ;
if(skip > 0)
{
m_curr_expired += skip ;
m_high_pos = ( m_high_pos +((m_low_pos + skip) >> m_step_slot_bits) ) & m_step_slot_mask ;
m_low_pos = ( m_low_pos +skip ) & m_step_slot_mask ;
}
//run all timers expired
while( m_curr_expired < now )
{
base_timer* head = m_low_array + m_low_pos ;
base_timer* curr = NULL ;
while(head != head->m_next)
{
curr = head->m_next ;
base_timer::base_timer_remove(curr) ;
curr->on_timeout(this) ;
}
++m_curr_expired ;
m_low_pos = ( m_low_pos +1 ) & m_step_slot_mask ;
//move timers from high slot to low slots when run a cycle
if(m_low_pos == 0)
{
head = m_high_array + m_high_pos ;
while(head!=head->m_next)
{
curr = head->m_next ;
base_timer::base_timer_remove(curr) ;
int low_pos = (curr->m_expired - m_curr_expired + m_low_pos) & m_step_slot_mask ;
base_timer* low_head = m_low_array + low_pos ;
base_timer::base_timer_insert(low_head->m_prev,curr,low_head) ;
}
m_high_pos = ( m_high_pos +1 ) & m_step_slot_mask ;
}
}
//update next expired counter by nearist timer , try max step_slot_size times
if(m_next_expired < m_curr_expired )
{
int i = m_low_pos ;
for( ; i < m_step_slot_size ; ++i )
{
base_timer* next_head = m_low_array + i ;
if(next_head != next_head->m_next) break ;
}
m_next_expired = m_curr_expired + i - m_low_pos ;
}
}
}