Fix: Remove need to instantiate SmallStack's pool object by making it a singleton method

This commit is contained in:
Charles Pigott 2018-04-21 14:56:02 +01:00 committed by Patric Stout
parent 5c5267726f
commit 86012e10ea
2 changed files with 16 additions and 15 deletions

View File

@ -195,10 +195,10 @@ public:
inline void Push(const Titem &item) inline void Push(const Titem &item)
{ {
if (this->value != Tinvalid) { if (this->value != Tinvalid) {
ThreadMutexLocker lock(_pool.GetMutex()); ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
Tindex new_item = _pool.Create(); Tindex new_item = SmallStack::GetPool().Create();
if (new_item != Tmax_size) { if (new_item != Tmax_size) {
PooledSmallStack &pushed = _pool.Get(new_item); PooledSmallStack &pushed = SmallStack::GetPool().Get(new_item);
pushed.value = this->value; pushed.value = this->value;
pushed.next = this->next; pushed.next = this->next;
pushed.branch_count = 0; pushed.branch_count = 0;
@ -218,16 +218,16 @@ public:
if (this->next == Tmax_size) { if (this->next == Tmax_size) {
this->value = Tinvalid; this->value = Tinvalid;
} else { } else {
ThreadMutexLocker lock(_pool.GetMutex()); ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
PooledSmallStack &popped = _pool.Get(this->next); PooledSmallStack &popped = SmallStack::GetPool().Get(this->next);
this->value = popped.value; this->value = popped.value;
if (popped.branch_count == 0) { if (popped.branch_count == 0) {
_pool.Destroy(this->next); SmallStack::GetPool().Destroy(this->next);
} else { } else {
--popped.branch_count; --popped.branch_count;
/* We can't use Branch() here as we already have the mutex.*/ /* We can't use Branch() here as we already have the mutex.*/
if (popped.next != Tmax_size) { if (popped.next != Tmax_size) {
++(_pool.Get(popped.next).branch_count); ++(SmallStack::GetPool().Get(popped.next).branch_count);
} }
} }
/* Accessing popped here is no problem as the pool will only set /* Accessing popped here is no problem as the pool will only set
@ -257,11 +257,11 @@ public:
{ {
if (item == Tinvalid || item == this->value) return true; if (item == Tinvalid || item == this->value) return true;
if (this->next != Tmax_size) { if (this->next != Tmax_size) {
ThreadMutexLocker lock(_pool.GetMutex()); ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
const SmallStack *in_list = this; const SmallStack *in_list = this;
do { do {
in_list = static_cast<const SmallStack *>( in_list = static_cast<const SmallStack *>(
static_cast<const Item *>(&_pool.Get(in_list->next))); static_cast<const Item *>(&SmallStack::GetPool().Get(in_list->next)));
if (in_list->value == item) return true; if (in_list->value == item) return true;
} while (in_list->next != Tmax_size); } while (in_list->next != Tmax_size);
} }
@ -269,7 +269,11 @@ public:
} }
protected: protected:
static SmallStackPool _pool; static SmallStackPool &GetPool()
{
static SmallStackPool pool;
return pool;
}
/** /**
* Create a branch in the pool if necessary. * Create a branch in the pool if necessary.
@ -277,8 +281,8 @@ protected:
inline void Branch() inline void Branch()
{ {
if (this->next != Tmax_size) { if (this->next != Tmax_size) {
ThreadMutexLocker lock(_pool.GetMutex()); ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
++(_pool.Get(this->next).branch_count); ++(SmallStack::GetPool().Get(this->next).branch_count);
} }
} }
}; };

View File

@ -35,9 +35,6 @@
StationPool _station_pool("Station"); StationPool _station_pool("Station");
INSTANTIATE_POOL_METHODS(Station) INSTANTIATE_POOL_METHODS(Station)
typedef StationIDStack::SmallStackPool StationIDStackPool;
template<> StationIDStackPool StationIDStack::_pool = StationIDStackPool();
BaseStation::~BaseStation() BaseStation::~BaseStation()
{ {
free(this->name); free(this->name);