-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTimer.cpp
146 lines (125 loc) · 2.93 KB
/
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
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <assert.h>
#include "MozQuicInternal.h"
#include "Timer.h"
namespace mozquic {
std::list<Timer *> timerList;
Timer::Timer(TimerNotification *notification)
: mDeadline(0)
, mNotification(notification)
, mData(nullptr)
, mList(timerList.end())
{
}
Timer::~Timer()
{
Cancel();
}
void Timer::InsertIntoMasterList(Timer *newTimer)
{
assert(newTimer->mDeadline);
assert(newTimer->mList == timerList.end());
if (timerList.empty()) {
timerList.push_front(newTimer);
newTimer->mList = timerList.begin();
return;
}
// work from the back under the asumption that new timers are later
auto iter = timerList.end();
iter--;
do {
if ((*iter)->mDeadline < newTimer->mDeadline) {
iter++;
newTimer->mList = timerList.insert(iter, newTimer);
return;
}
if (iter == timerList.begin()) {
break;
}
iter--;
} while (1);
timerList.push_front(newTimer);
newTimer->mList = timerList.begin();
}
void Timer::Tick()
{
uint64_t now = MozQuic::Timestamp();
// iterate from the start calling notify as necessary
for (auto iter = timerList.begin();
(iter != timerList.end()) && ((*iter)->mDeadline <= now);
iter = timerList.erase(iter)) {
(*iter)->mDeadline = 0;
(*iter)->mList = timerList.end();
(*iter)->mNotification->Alarm((*iter));
}
}
uint64_t Timer::NextTimerInMsec()
{
auto timer = timerList.begin();
if (timerList.end() == timer) {
return 0;
}
uint64_t now = MozQuic::Timestamp();
if ((*timer)->mDeadline <= now) {
return 0;
}
return (*timer)->mDeadline - now;
}
void Timer::Cancel()
{
if (!mDeadline) {
return;
}
mDeadline = 0;
assert(mList != timerList.end());
timerList.erase(mList);
mList = timerList.end();
}
bool Timer::Armed()
{
return !!mDeadline;
}
bool Timer::Expired()
{
return mDeadline && (mDeadline <= MozQuic::Timestamp());
}
uint64_t Timer::Expires()
{
uint64_t now = MozQuic::Timestamp();
if (mDeadline <= now) {
return 0;
}
return mDeadline - now;
}
void Timer::Arm(uint64_t deadline)
{
if (!mDeadline) {
mDeadline = deadline + MozQuic::Timestamp();
InsertIntoMasterList(this);
return;
}
assert (mList != timerList.end());
auto next = mList;
if (++next == timerList.end()) {
next = mList;
}
auto prev = mList;
if (prev != timerList.begin()) {
prev--;
}
uint64_t newDeadline = deadline + MozQuic::Timestamp();
if (newDeadline >= (*prev)->mDeadline &&
newDeadline <= (*next)->mDeadline) {
// update in place
mDeadline = newDeadline;
return;
}
// find a new home
Cancel();
mDeadline = deadline + MozQuic::Timestamp();
InsertIntoMasterList(this);
}
}