blob: 8097f37f35240558dd0908dbbe05b24d3a1b3808 (
plain)
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
|
/*
** Copyright 2002, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "curses_config.h"
#include "timer.H"
std::set<Timer *> Timer::timer_list;
Timer::Timer()
{
timeout.tv_sec=0;
timeout.tv_usec=0;
timer_list.insert(this);
}
Timer::~Timer()
{
timer_list.erase(this);
}
void Timer::setTimer(int nSeconds)
{
gettimeofday(&timeout, NULL);
timeout.tv_sec += nSeconds;
}
void Timer::setTimer(struct timeval tv)
{
gettimeofday(&timeout, NULL);
timeout.tv_sec += tv.tv_sec;
timeout.tv_usec += tv.tv_usec;
if (timeout.tv_usec >= 1000000)
{
++timeout.tv_sec;
timeout.tv_usec %= 1000000;
}
}
struct timeval Timer::getTimer() const
{
struct timeval tv;
gettimeofday(&tv, NULL);
return getTimer(tv);
}
struct timeval Timer::getTimer(const struct timeval &tv) const
{
struct timeval t;
t.tv_sec=0;
t.tv_usec=0;
if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
return t;
if (tv.tv_sec > timeout.tv_sec)
return t;
if (tv.tv_sec == timeout.tv_sec &&
tv.tv_usec >= timeout.tv_usec)
return t;
t=timeout;
t.tv_sec -= tv.tv_sec;
t.tv_usec -= tv.tv_usec;
if (t.tv_usec < 0)
{
t.tv_usec += 1000000;
t.tv_sec--;
}
return t;
}
#define DEBUG 1
#undef DEBUG
struct timeval Timer::getNextTimeout(bool &alarmCalledFlag)
{
struct timeval now;
gettimeofday(&now, NULL);
bool alarmed;
bool wasAlarmed=false;
struct timeval s;
alarmCalledFlag=false;
do
{
#if DEBUG
cerr << "In getNextTimeout:" << endl;
#endif
alarmed=false;
std::set<Timer *>::iterator b=timer_list.begin(),
e=timer_list.end();
s.tv_sec=0;
s.tv_usec=0;
while (b != e)
{
Timer *t= *b++;
if (t->timeout.tv_sec == 0 &&
t->timeout.tv_usec == 0)
continue;
struct timeval v=t->getTimer(now);
#if DEBUG
cerr << "Timer " << t << ": " << v.tv_sec
<< "." << v.tv_usec << endl;
#endif
if (v.tv_sec == 0 && v.tv_usec == 0)
{
#if DEBUG
cerr << "ALARM: " <<
(wasAlarmed ? "ignored":"handled")
<< endl;
#endif
if (wasAlarmed)
{
// We can get here if an alarm
// went off, and the alarm handler
// reset the alarm to 0 seconds again.
// We'll get it on the next go-round
s=v;
break; // Kick the alarm next time
}
t->timeout=v;
t->alarm();
alarmCalledFlag=true;
alarmed=true;
}
else if ((s.tv_sec == 0 && s.tv_usec == 0) ||
v.tv_sec < s.tv_sec ||
(v.tv_sec == s.tv_sec
&& v.tv_usec < s.tv_usec))
s=v;
}
wasAlarmed=alarmed;
} while (alarmed);
#if DEBUG
cerr << "getNextTimeout: " << s.tv_sec << "." << s.tv_usec << endl;
#endif
return s;
}
void Timer::alarm()
{
}
|