blob: 569e6f7a4b206531d2e72d08cb57c4f6361534a2 (
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
173
174
175
176
177
178
179
|
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Ice/Ice.h>
#include <IceGrid/ReapThread.h>
using namespace std;
using namespace IceGrid;
ReapThread::ReapThread() :
IceUtil::Thread("Icegrid reaper thread"),
_terminated(false)
{
}
void
ReapThread::run()
{
vector<ReapableItem> reap;
while(true)
{
{
Lock sync(*this);
if(_terminated)
{
break;
}
calcWakeInterval();
//
// If the wake interval is zero then we wait forever.
//
if(_wakeInterval == IceUtil::Time())
{
wait();
}
else
{
timedWait(_wakeInterval);
}
if(_terminated)
{
break;
}
list<ReapableItem>::iterator p = _sessions.begin();
while(p != _sessions.end())
{
try
{
if(p->timeout == IceUtil::Time())
{
p->item->timestamp(); // This should throw if the reapable is destroyed.
++p;
}
else if((IceUtil::Time::now(IceUtil::Time::Monotonic) - p->item->timestamp()) > p->timeout)
{
reap.push_back(*p);
p = _sessions.erase(p);
}
else
{
++p;
}
}
catch(const Ice::ObjectNotExistException&)
{
p = _sessions.erase(p);
}
}
}
for(vector<ReapableItem>::const_iterator p = reap.begin(); p != reap.end(); ++p)
{
p->item->destroy(false);
}
reap.clear();
}
}
void
ReapThread::terminate()
{
list<ReapableItem> reap;
{
Lock sync(*this);
if(_terminated)
{
assert(_sessions.empty());
return;
}
_terminated = true;
notify();
reap.swap(_sessions);
}
for(list<ReapableItem>::iterator p = reap.begin(); p != reap.end(); ++p)
{
p->item->destroy(true);
}
}
void
ReapThread::add(const ReapablePtr& reapable, int timeout)
{
Lock sync(*this);
if(_terminated)
{
return;
}
//
// NOTE: registering a reapable with a null timeout is allowed. The reapable is reaped
// only when the reaper thread is shutdown.
//
//
// 10 seconds is the minimum permissable timeout.
//
if(timeout > 0 && timeout < 10)
{
timeout = 10;
}
ReapableItem item;
item.item = reapable;
item.timeout = timeout == 0 ? IceUtil::Time() : IceUtil::Time::seconds(timeout);
_sessions.push_back(item);
if(timeout > 0)
{
//
// If there is a new minimum wake interval then wake the reaping
// thread.
//
if(calcWakeInterval())
{
notify();
}
//
// Since we just added a new session with a non null timeout there
// must be a non-zero wakeInterval.
//
assert(_wakeInterval != IceUtil::Time());
}
}
//
// Returns true if the calculated wake interval is less than the current wake
// interval (or if the original wake interval was "forever").
//
bool
ReapThread::calcWakeInterval()
{
// Re-calculate minimum timeout
IceUtil::Time oldWakeInterval = _wakeInterval;
IceUtil::Time minimum;
bool first = true;
for(list<ReapableItem>::const_iterator p = _sessions.begin(); p != _sessions.end(); ++p)
{
if(p->timeout != IceUtil::Time() && (first || p->timeout < minimum))
{
minimum = p->timeout;
first = false;
}
}
_wakeInterval = minimum;
return oldWakeInterval == IceUtil::Time() || minimum < oldWakeInterval;
}
|