From 3c5745cfcfbe6da1429154834795d92a1f85897a Mon Sep 17 00:00:00 2001 From: Maximilian Seidler Date: Sat, 21 Jun 2025 22:06:23 +0200 Subject: [PATCH] fix allow incrWeak as long as impl is not destroyed. --- include/hyprutils/memory/ImplAtomic.hpp | 2 +- include/hyprutils/memory/ImplBase.hpp | 2 +- prmsg.txt | 17 ----------------- 3 files changed, 2 insertions(+), 19 deletions(-) delete mode 100644 prmsg.txt diff --git a/include/hyprutils/memory/ImplAtomic.hpp b/include/hyprutils/memory/ImplAtomic.hpp index 3105536..0423547 100644 --- a/include/hyprutils/memory/ImplAtomic.hpp +++ b/include/hyprutils/memory/ImplAtomic.hpp @@ -91,7 +91,7 @@ namespace Hyprutils::Memory::Impl_ { virtual bool incWeak() { std::lock_guard lg(_mtx); - if (_ref == 0) + if (_ref == 0 && _weak == 0) return false; _weak++; diff --git a/include/hyprutils/memory/ImplBase.hpp b/include/hyprutils/memory/ImplBase.hpp index 180575f..6c53d1e 100644 --- a/include/hyprutils/memory/ImplBase.hpp +++ b/include/hyprutils/memory/ImplBase.hpp @@ -99,7 +99,7 @@ namespace Hyprutils { } virtual bool incWeak() { - if (_ref == 0) + if (_ref == 0 && _weak == 0) return false; _weak++; diff --git a/prmsg.txt b/prmsg.txt deleted file mode 100644 index a408fd4..0000000 --- a/prmsg.txt +++ /dev/null @@ -1,17 +0,0 @@ - -This PR was motivated by https://github.com/hyprwm/hyprlock/pull/799. - -It's goal is to get a thread-safe smart pointer implementation into Hyprutils. - -Vaxry suggested the following: -> No. Instead, make hyprutils pointers thread safe. Wrap a shared ptr into an ARC (just dont name it that cuz rust cring) and make it thread safe with a simple std::mutex - -I first tried to wrap a shared pointer. I have an implementation with a global mutex where that works ok, but it really wasn't ideal and a bit messy. I also never figured out how to not make it a recursive_mutex in that case. - -I experimented with different ways of wrapping the shared pointer, but i wasn't able to come up with a decent implementation. -Then I decided to try to rework the interface to the control block slightly, such that thread safety could be achieved by using a different implementation for the controlblock. -I liked that idea, because it would allow to share the interface for shared and weak pointers for default ones and thread-safe ones. - -First I experimented with atomic counters, but I ditched that and just added a std::mutex to the thread-safe control block implementation. - -