Replace InstanceCounter with CountedObject

This commit is contained in:
Vinnie Falco
2013-06-17 17:45:33 -07:00
parent abce930b8b
commit 2abec05b5b
53 changed files with 321 additions and 334 deletions

View File

@@ -0,0 +1,78 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
CountedObjects& CountedObjects::getInstance ()
{
static CountedObjects instance;
return instance;
}
CountedObjects::CountedObjects ()
{
}
CountedObjects::~CountedObjects ()
{
}
CountedObjects::List CountedObjects::getCounts (int minimumThreshold) const
{
List counts;
// When other operations are concurrent, the count
// might be temporarily less than the actual count.
int const count = m_count.get ();
counts.reserve (count);
CounterBase* counter = m_head.get ();
while (counter != nullptr)
{
if (counter->getCount () >= minimumThreshold)
{
Entry entry;
entry.first = counter->getName ();
entry.second = counter->getCount ();
counts.push_back (entry);
}
counter = counter->getNext ();
}
return counts;
}
//------------------------------------------------------------------------------
CountedObjects::CounterBase::CounterBase ()
{
// Insert ourselves at the front of the lock-free linked list
CountedObjects& instance = CountedObjects::getInstance ();
CounterBase* head;
do
{
head = instance.m_head.get ();
m_next = head;
}
while (! instance.m_head.compareAndSetBool (this, head));
++instance.m_count;
}
CountedObjects::CounterBase::~CounterBase ()
{
// VFALCO NOTE If the counters are destroyed before the singleton,
// undefined behavior will result if the singleton's member
// functions are called.
}
//------------------------------------------------------------------------------