blob: 65234e93807b8d4e46cc160eb4faf50962010e81 (
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
|
// **********************************************************************
//
// Copyright (c) 2003-2006 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() :
_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())
{
assert(_sessions.empty());
wait();
}
else
{
assert(!_sessions.empty());
timedWait(_wakeInterval);
}
if(_terminated)
{
break;
}
list<ReapableItem>::iterator p = _sessions.begin();
while(p != _sessions.end())
{
try
{
if((IceUtil::Time::now() - 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;
}
//
// 10 seconds is the minimum permissable timeout.
//
if(timeout < 10)
{
timeout = 10;
}
ReapableItem item;
item.item = reapable;
item.timeout = IceUtil::Time::seconds(timeout);
_sessions.push_back(item);
//
// If there is a new minimum wake interval then wake the reaping
// thread.
//
if(calcWakeInterval())
{
notify();
}
//
// Since we just added a new session there must be a non-zero
// wakeInterval.
//
assert(_wakeInterval != IceUtil::Time());
}
// Returns true iff 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(first || p->timeout < minimum)
{
minimum = p->timeout;
first = false;
}
}
_wakeInterval = minimum;
return oldWakeInterval == IceUtil::Time() || minimum < oldWakeInterval;
}
|