Remove mutex from SmallStack/StationIDStack.
It is only ever used from the main thread.
This commit is contained in:
@@ -13,7 +13,6 @@
|
||||
#define SMALLSTACK_TYPE_HPP
|
||||
|
||||
#include "smallvec_type.hpp"
|
||||
#include "../thread/thread.h"
|
||||
|
||||
/**
|
||||
* A simplified pool which stores values instead of pointers and doesn't
|
||||
@@ -23,15 +22,7 @@
|
||||
template<typename Titem, typename Tindex, Tindex Tgrowth_step, Tindex Tmax_size>
|
||||
class SimplePool {
|
||||
public:
|
||||
inline SimplePool() : first_unused(0), first_free(0), mutex(ThreadMutex::New()) {}
|
||||
inline ~SimplePool() { delete this->mutex; }
|
||||
|
||||
/**
|
||||
* Get the mutex. We don't lock the mutex in the pool methods as the
|
||||
* SmallStack isn't necessarily in a consistent state after each method.
|
||||
* @return Mutex.
|
||||
*/
|
||||
inline ThreadMutex *GetMutex() { return this->mutex; }
|
||||
inline SimplePool() : first_unused(0), first_free(0) {}
|
||||
|
||||
/**
|
||||
* Get the item at position index.
|
||||
@@ -86,7 +77,6 @@ private:
|
||||
Tindex first_unused;
|
||||
Tindex first_free;
|
||||
|
||||
ThreadMutex *mutex;
|
||||
SmallVector<SimplePoolPoolItem, Tgrowth_step> data;
|
||||
};
|
||||
|
||||
@@ -124,10 +114,8 @@ struct SmallStackItem {
|
||||
* 5. You can choose your own index type, so that you can align it with your
|
||||
* value type. E.G. value types of 16 bits length like to be combined with
|
||||
* index types of the same length.
|
||||
* 6. All accesses to the underlying pool are guarded by a mutex and atomic in
|
||||
* the sense that the mutex stays locked until the pool has reacquired a
|
||||
* consistent state. This means that even though a common data structure is
|
||||
* used the SmallStack is still reentrant.
|
||||
* 6. This data structure is only ever used from the main thread, so
|
||||
* accesses to the underlying pool are not guarded by locks.
|
||||
* @tparam Titem Value type to be used.
|
||||
* @tparam Tindex Index type to use for the pool.
|
||||
* @tparam Tinvalid Invalid item to keep at the bottom of each stack.
|
||||
@@ -195,7 +183,6 @@ public:
|
||||
inline void Push(const Titem &item)
|
||||
{
|
||||
if (this->value != Tinvalid) {
|
||||
ThreadMutexLocker lock(_pool.GetMutex());
|
||||
Tindex new_item = _pool.Create();
|
||||
if (new_item != Tmax_size) {
|
||||
PooledSmallStack &pushed = _pool.Get(new_item);
|
||||
@@ -218,17 +205,13 @@ public:
|
||||
if (this->next == Tmax_size) {
|
||||
this->value = Tinvalid;
|
||||
} else {
|
||||
ThreadMutexLocker lock(_pool.GetMutex());
|
||||
PooledSmallStack &popped = _pool.Get(this->next);
|
||||
this->value = popped.value;
|
||||
if (popped.branch_count == 0) {
|
||||
_pool.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);
|
||||
}
|
||||
this->Branch();
|
||||
}
|
||||
/* Accessing popped here is no problem as the pool will only set
|
||||
* the validity flag, not actually delete the item, on Destroy().
|
||||
@@ -257,7 +240,6 @@ public:
|
||||
{
|
||||
if (item == Tinvalid || item == this->value) return true;
|
||||
if (this->next != Tmax_size) {
|
||||
ThreadMutexLocker lock(_pool.GetMutex());
|
||||
const SmallStack *in_list = this;
|
||||
do {
|
||||
in_list = static_cast<const SmallStack *>(
|
||||
@@ -277,7 +259,6 @@ protected:
|
||||
inline void Branch()
|
||||
{
|
||||
if (this->next != Tmax_size) {
|
||||
ThreadMutexLocker lock(_pool.GetMutex());
|
||||
++(_pool.Get(this->next).branch_count);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user