diff options
Diffstat (limited to 'core/templates')
-rw-r--r-- | core/templates/cowdata.h | 2 | ||||
-rw-r--r-- | core/templates/safe_list.h | 159 | ||||
-rw-r--r-- | core/templates/safe_refcount.h | 139 |
3 files changed, 1 insertions, 299 deletions
diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h index f98b2308c9..1482e3eef1 100644 --- a/core/templates/cowdata.h +++ b/core/templates/cowdata.h @@ -46,9 +46,7 @@ class CharString; template <class T, class V> class VMap; -#if !defined(NO_THREADS) SAFE_NUMERIC_TYPE_PUN_GUARANTEES(uint32_t) -#endif // Silence a false positive warning (see GH-52119). #if defined(__GNUC__) && !defined(__clang__) diff --git a/core/templates/safe_list.h b/core/templates/safe_list.h index e850f3bd5e..95e8ca1a14 100644 --- a/core/templates/safe_list.h +++ b/core/templates/safe_list.h @@ -33,11 +33,9 @@ #include "core/os/memory.h" #include "core/typedefs.h" -#include <functional> - -#if !defined(NO_THREADS) #include <atomic> +#include <functional> #include <type_traits> // Design goals for these classes: @@ -239,159 +237,4 @@ public: } }; -#else // NO_THREADS - -// Effectively the same structure without the atomics. It's probably possible to simplify it but the semantics shouldn't differ greatly. -template <class T, class A = DefaultAllocator> -class SafeList { - struct SafeListNode { - SafeListNode *next = nullptr; - - // If the node is logically deleted, this pointer will typically point to the previous list item in time that was also logically deleted. - SafeListNode *graveyard_next = nullptr; - - std::function<void(T)> deletion_fn = [](T t) { return; }; - - T val; - }; - - SafeListNode *head = nullptr; - SafeListNode *graveyard_head = nullptr; - - unsigned int active_iterator_count = 0; - -public: - class Iterator { - friend class SafeList; - - SafeListNode *cursor = nullptr; - SafeList *list = nullptr; - - public: - Iterator(SafeListNode *p_cursor, SafeList *p_list) : - cursor(p_cursor), list(p_list) { - list->active_iterator_count++; - } - - ~Iterator() { - list->active_iterator_count--; - } - - T &operator*() { - return cursor->val; - } - - Iterator &operator++() { - cursor = cursor->next; - return *this; - } - - // These two operators are mostly useful for comparisons to nullptr. - bool operator==(const void *p_other) const { - return cursor == p_other; - } - - bool operator!=(const void *p_other) const { - return cursor != p_other; - } - - // These two allow easy range-based for loops. - bool operator==(const Iterator &p_other) const { - return cursor == p_other.cursor; - } - - bool operator!=(const Iterator &p_other) const { - return cursor != p_other.cursor; - } - }; - -public: - // Calling this will cause an allocation. - void insert(T p_value) { - SafeListNode *new_node = memnew_allocator(SafeListNode, A); - new_node->val = p_value; - new_node->next = head; - head = new_node; - } - - Iterator find(T p_value) { - for (Iterator it = begin(); it != end(); ++it) { - if (*it == p_value) { - return it; - } - } - return end(); - } - - void erase(T p_value, std::function<void(T)> p_deletion_fn) { - erase(find(p_value), p_deletion_fn); - } - - void erase(T p_value) { - erase(find(p_value), [](T t) { return; }); - } - - void erase(Iterator p_iterator, std::function<void(T)> p_deletion_fn) { - p_iterator.cursor->deletion_fn = p_deletion_fn; - erase(p_iterator); - } - - void erase(Iterator p_iterator) { - Iterator prev = begin(); - for (; prev != end(); ++prev) { - if (prev.cursor && prev.cursor->next == p_iterator.cursor) { - break; - } - } - if (prev == end()) { - // Not in the list, nothing to do. - return; - } - // First, remove the node from the list. - prev.cursor->next = p_iterator.cursor->next; - - // Then queue it for deletion by putting it in the node graveyard. Don't touch `next` because an iterator might still be pointing at this node. - p_iterator.cursor->graveyard_next = graveyard_head; - graveyard_head = p_iterator.cursor; - } - - Iterator begin() { - return Iterator(head, this); - } - - Iterator end() { - return Iterator(nullptr, this); - } - - // Calling this will cause zero to many deallocations. - bool maybe_cleanup() { - SafeListNode *cursor = graveyard_head; - if (active_iterator_count != 0) { - // It's not safe to clean up with an active iterator, because that iterator could be pointing to an element that we want to delete. - return false; - } - graveyard_head = nullptr; - // Our graveyard list is now unreachable by any active iterators, detached from the main graveyard head and ready for deletion. - while (cursor) { - SafeListNode *tmp = cursor; - cursor = cursor->next; - tmp->deletion_fn(tmp->val); - memdelete_allocator<SafeListNode, A>(tmp); - } - return true; - } - - ~SafeList() { -#ifdef DEBUG_ENABLED - if (!maybe_cleanup()) { - ERR_PRINT("There are still iterators around when destructing a SafeList. Memory will be leaked. This is a bug."); - } -#else - maybe_cleanup(); -#endif - } -}; - -#endif - #endif // SAFE_LIST_H diff --git a/core/templates/safe_refcount.h b/core/templates/safe_refcount.h index 1f6551762e..c4ffe5ca02 100644 --- a/core/templates/safe_refcount.h +++ b/core/templates/safe_refcount.h @@ -33,8 +33,6 @@ #include "core/typedefs.h" -#if !defined(NO_THREADS) - #include <atomic> #include <type_traits> @@ -191,141 +189,4 @@ public: } }; -#else - -template <class T> -class SafeNumeric { -protected: - T value; - -public: - _ALWAYS_INLINE_ void set(T p_value) { - value = p_value; - } - - _ALWAYS_INLINE_ T get() const { - return value; - } - - _ALWAYS_INLINE_ T increment() { - return ++value; - } - - _ALWAYS_INLINE_ T postincrement() { - return value++; - } - - _ALWAYS_INLINE_ T decrement() { - return --value; - } - - _ALWAYS_INLINE_ T postdecrement() { - return value--; - } - - _ALWAYS_INLINE_ T add(T p_value) { - return value += p_value; - } - - _ALWAYS_INLINE_ T postadd(T p_value) { - T old = value; - value += p_value; - return old; - } - - _ALWAYS_INLINE_ T sub(T p_value) { - return value -= p_value; - } - - _ALWAYS_INLINE_ T postsub(T p_value) { - T old = value; - value -= p_value; - return old; - } - - _ALWAYS_INLINE_ T exchange_if_greater(T p_value) { - if (value < p_value) { - value = p_value; - } - return value; - } - - _ALWAYS_INLINE_ T conditional_increment() { - if (value == 0) { - return 0; - } else { - return ++value; - } - } - - _ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) : - value(p_value) { - } -}; - -class SafeFlag { -protected: - bool flag; - -public: - _ALWAYS_INLINE_ bool is_set() const { - return flag; - } - - _ALWAYS_INLINE_ void set() { - flag = true; - } - - _ALWAYS_INLINE_ void clear() { - flag = false; - } - - _ALWAYS_INLINE_ void set_to(bool p_value) { - flag = p_value; - } - - _ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) : - flag(p_value) {} -}; - -class SafeRefCount { - uint32_t count = 0; - -public: - _ALWAYS_INLINE_ bool ref() { // true on success - if (count != 0) { - ++count; - return true; - } else { - return false; - } - } - - _ALWAYS_INLINE_ uint32_t refval() { // none-zero on success - if (count != 0) { - return ++count; - } else { - return 0; - } - } - - _ALWAYS_INLINE_ bool unref() { // true if must be disposed of - return --count == 0; - } - - _ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of - return --count; - } - - _ALWAYS_INLINE_ uint32_t get() const { - return count; - } - - _ALWAYS_INLINE_ void init(uint32_t p_value = 1) { - count = p_value; - } -}; - -#endif - #endif // SAFE_REFCOUNT_H |