blob: fbea95802122b0d3708155299e0b3247d3e725cf (
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
|
#include <EvictorBase.h>
#include <Ice/LocalException.h>
EvictorBase::EvictorBase(Ice::Int size)
: _size(size)
{
if (_size < 0)
{
_size = 1000;
}
}
Ice::ObjectPtr
EvictorBase::locate(const Ice::Current& c, Ice::LocalObjectPtr& cookie)
{
IceUtil::Mutex::Lock lock(_mutex);
//
// Create a cookie.
//
EvictorCookiePtr ec = new EvictorCookie;
cookie = ec;
//
// Check if we have a servant in the map already.
//
EvictorMap::iterator i = _map.find(c.id);
bool newEntry = i == _map.end();
if(!newEntry)
{
//
// Got an entry already, dequeue the entry from its current position.
//
ec->entry = i->second;
_queue.erase(ec->entry->pos);
}
else
{
//
// We do not have an entry. Ask the derived class to
// instantiate a servant and add a new entry to the map.
//
ec->entry = new EvictorEntry;
ec->entry->servant = add(c, ec->entry->userCookie); // Down-call
if(!ec->entry->servant)
{
return 0;
}
ec->entry->useCount = 0;
i = _map.insert(std::make_pair(c.id, ec->entry)).first;
}
//
// Increment the use count of the servant and enqueue
// the entry at the front, so we get LRU order.
//
++(ec->entry->useCount);
ec->entry->pos = _queue.insert(_queue.begin(), i);
return ec->entry->servant;
}
void
EvictorBase::finished(const Ice::Current&, const Ice::ObjectPtr&, const Ice::LocalObjectPtr& cookie)
{
IceUtil::Mutex::Lock lock(_mutex);
EvictorCookiePtr ec = EvictorCookiePtr::dynamicCast(cookie);
//
// Decrement use count and check if there is something to evict.
//
--(ec->entry->useCount);
evictServants();
}
void
EvictorBase::deactivate(const std::string& category)
{
IceUtil::Mutex::Lock lock(_mutex);
_size = 0;
evictServants();
}
void
EvictorBase::evictServants()
{
//
// If the evictor queue has grown larger than the limit,
// look at the excess elements to see whether any of them
// can be evicted.
//
for(int i = static_cast<int>(_map.size() - _size); i > 0; --i)
{
EvictorQueue::reverse_iterator p = _queue.rbegin();
if((*p)->second->useCount == 0)
{
evict((*p)->second->servant, (*p)->second->userCookie); // Down-call
EvictorMap::iterator pos = *p;
_queue.erase((*p)->second->pos);
_map.erase(pos);
}
}
}
|