Changeset - r22821:d604fbb6593e
[Not reviewed]
master
0 2 0
Charles Pigott - 6 years ago 2018-04-21 13:56:02
charlespigott@googlemail.com
Fix: Remove need to instantiate SmallStack's pool object by making it a singleton method
2 files changed with 16 insertions and 15 deletions:
0 comments (0 inline, 0 general)
src/core/smallstack_type.hpp
Show inline comments
 
@@ -192,16 +192,16 @@ public:
 
	 * underlying pool. Otherwise the topmost item's value gets overwritten.
 
	 * @param item Item to be pushed.
 
	 */
 
	inline void Push(const Titem &item)
 
	{
 
		if (this->value != Tinvalid) {
 
			ThreadMutexLocker lock(_pool.GetMutex());
 
			Tindex new_item = _pool.Create();
 
			ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
 
			Tindex new_item = SmallStack::GetPool().Create();
 
			if (new_item != Tmax_size) {
 
				PooledSmallStack &pushed = _pool.Get(new_item);
 
				PooledSmallStack &pushed = SmallStack::GetPool().Get(new_item);
 
				pushed.value = this->value;
 
				pushed.next = this->next;
 
				pushed.branch_count = 0;
 
				this->next = new_item;
 
			}
 
		}
 
@@ -215,22 +215,22 @@ public:
 
	inline Titem Pop()
 
	{
 
		Titem ret = this->value;
 
		if (this->next == Tmax_size) {
 
			this->value = Tinvalid;
 
		} else {
 
			ThreadMutexLocker lock(_pool.GetMutex());
 
			PooledSmallStack &popped = _pool.Get(this->next);
 
			ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
 
			PooledSmallStack &popped = SmallStack::GetPool().Get(this->next);
 
			this->value = popped.value;
 
			if (popped.branch_count == 0) {
 
				_pool.Destroy(this->next);
 
				SmallStack::GetPool().Destroy(this->next);
 
			} else {
 
				--popped.branch_count;
 
				/* We can't use Branch() here as we already have the mutex.*/
 
				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
 
			 * the validity flag, not actually delete the item, on Destroy().
 
			 * It's impossible for another thread to acquire the same item in
 
			 * the mean time because of the mutex. */
 
@@ -254,33 +254,37 @@ public:
 
	 * @return If the item is in the stack.
 
	 */
 
	inline bool Contains(const Titem &item) const
 
	{
 
		if (item == Tinvalid || item == this->value) return true;
 
		if (this->next != Tmax_size) {
 
			ThreadMutexLocker lock(_pool.GetMutex());
 
			ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
 
			const SmallStack *in_list = this;
 
			do {
 
				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;
 
			} while (in_list->next != Tmax_size);
 
		}
 
		return false;
 
	}
 

	
 
protected:
 
	static SmallStackPool _pool;
 
	static SmallStackPool &GetPool()
 
	{
 
		static SmallStackPool pool;
 
		return pool;
 
	}
 

	
 
	/**
 
	 * Create a branch in the pool if necessary.
 
	 */
 
	inline void Branch()
 
	{
 
		if (this->next != Tmax_size) {
 
			ThreadMutexLocker lock(_pool.GetMutex());
 
			++(_pool.Get(this->next).branch_count);
 
			ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
 
			++(SmallStack::GetPool().Get(this->next).branch_count);
 
		}
 
	}
 
};
 

	
 
#endif
src/station.cpp
Show inline comments
 
@@ -32,15 +32,12 @@
 
#include "safeguards.h"
 

	
 
/** The pool of stations. */
 
StationPool _station_pool("Station");
 
INSTANTIATE_POOL_METHODS(Station)
 

	
 
typedef StationIDStack::SmallStackPool StationIDStackPool;
 
template<> StationIDStackPool StationIDStack::_pool = StationIDStackPool();
 

	
 
BaseStation::~BaseStation()
 
{
 
	free(this->name);
 
	free(this->speclist);
 

	
 
	if (CleaningPool()) return;
0 comments (0 inline, 0 general)