-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.cpp
53 lines (45 loc) · 1014 Bytes
/
timer.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
/**
***All Rights Reserved
*
*author frankiezhu
*date 2013-03-22
*/
#include "log.h"
#include "timer.h"
struct timeval g_cur_tv;
void TimerObj::add_timer(int time_out, TimerList *tl)
{
m_expected_time = get_cur_time_ms() + time_out;
tl->add_time_obj(this, m_expected_time);
}
void TimerList::add_time_obj(TimerObj *obj, int64_t expected_time)
{
list<TimerObj *>::iterator it;
for (it = m_timer_list.begin(); it != m_timer_list.end(); ++it)
{
if ((*it)->get_expected_time() >= expected_time)
{
break;
}
}
m_timer_list.insert(it, obj);
}
int TimerList::check_expired()
{
list<TimerObj *>::iterator it;
int n = 0;
for (it = m_timer_list.begin(); it != m_timer_list.end();)
{
if ((*it)->get_expected_time() <= get_cur_time_ms())
{
(*it)->handle_time_out();
it = m_timer_list.erase(it);
++n;
}
else
{
++it;
}
}
return n;
}