1
0
Fork 0

Codechange: remove unneeded locking from SmallStack

pull/13834/head
Rubidium 2025-03-15 22:07:53 +01:00 committed by rubidium42
parent b28b35c239
commit b4e5b12047
1 changed files with 1 additions and 23 deletions

View File

@ -10,8 +10,6 @@
#ifndef SMALLSTACK_TYPE_HPP #ifndef SMALLSTACK_TYPE_HPP
#define SMALLSTACK_TYPE_HPP #define SMALLSTACK_TYPE_HPP
#include <mutex>
/** /**
* A simplified pool which stores values instead of pointers and doesn't * A simplified pool which stores values instead of pointers and doesn't
* redefine operator new/delete. It also never zeroes memory and always reuses * redefine operator new/delete. It also never zeroes memory and always reuses
@ -22,13 +20,6 @@ class SimplePool {
public: public:
inline SimplePool() : first_unused(0), first_free(0) {} inline SimplePool() : first_unused(0), first_free(0) {}
/**
* 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 std::mutex &GetMutex() { return this->mutex; }
/** /**
* Get the item at position index. * Get the item at position index.
* @return Item at index. * @return Item at index.
@ -82,7 +73,6 @@ private:
Tindex first_unused; Tindex first_unused;
Tindex first_free; Tindex first_free;
std::mutex mutex;
std::vector<SimplePoolPoolItem> data; std::vector<SimplePoolPoolItem> data;
}; };
@ -121,10 +111,6 @@ struct SmallStackItem {
* 5. You can choose your own index type, so that you can align it with your * 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 * value type. E.G. value types of 16 bits length like to be combined with
* index types of the same length. * 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.
* @tparam Titem Value type to be used. * @tparam Titem Value type to be used.
* @tparam Tindex Index type to use for the pool. * @tparam Tindex Index type to use for the pool.
* @tparam Tinvalid Invalid item to keep at the bottom of each stack. * @tparam Tinvalid Invalid item to keep at the bottom of each stack.
@ -157,7 +143,6 @@ public:
*/ */
inline ~SmallStack() inline ~SmallStack()
{ {
/* Pop() locks the mutex and after each pop the pool is consistent.*/
while (this->next != Tmax_size) this->Pop(); while (this->next != Tmax_size) this->Pop();
} }
@ -192,7 +177,6 @@ public:
inline void Push(const Titem &item) inline void Push(const Titem &item)
{ {
if (this->value != Tinvalid) { if (this->value != Tinvalid) {
std::lock_guard<std::mutex> lock(SmallStack::GetPool().GetMutex());
Tindex new_item = SmallStack::GetPool().Create(); Tindex new_item = SmallStack::GetPool().Create();
if (new_item != Tmax_size) { if (new_item != Tmax_size) {
PooledSmallStack &pushed = SmallStack::GetPool().Get(new_item); PooledSmallStack &pushed = SmallStack::GetPool().Get(new_item);
@ -215,22 +199,18 @@ public:
if (this->next == Tmax_size) { if (this->next == Tmax_size) {
this->value = Tinvalid; this->value = Tinvalid;
} else { } else {
std::lock_guard<std::mutex> lock(SmallStack::GetPool().GetMutex());
PooledSmallStack &popped = SmallStack::GetPool().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) {
SmallStack::GetPool().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.*/
if (popped.next != Tmax_size) { if (popped.next != Tmax_size) {
++(SmallStack::GetPool().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
* the validity flag, not actually delete the item, on Destroy(). * 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. */
this->next = popped.next; this->next = popped.next;
} }
return ret; return ret;
@ -254,7 +234,6 @@ 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) {
std::lock_guard<std::mutex> 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 *>(
@ -278,7 +257,6 @@ protected:
inline void Branch() inline void Branch()
{ {
if (this->next != Tmax_size) { if (this->next != Tmax_size) {
std::lock_guard<std::mutex> lock(SmallStack::GetPool().GetMutex());
++(SmallStack::GetPool().Get(this->next).branch_count); ++(SmallStack::GetPool().Get(this->next).branch_count);
} }
} }