From 3d8d9ef61c9eb30c61cada8ebcdb56f5dd981c34 Mon Sep 17 00:00:00 2001 From: Eladash Date: Wed, 26 Apr 2023 22:39:26 +0300 Subject: [PATCH] Threads.cpp: Fix infinite looping --- Utilities/Thread.cpp | 29 +++++++++++------------------ Utilities/Thread.h | 2 +- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index a4cb579789..0ad97940c6 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -2647,26 +2647,24 @@ void thread_base::exec() while (shared_ptr head = m_taskq.exchange(null_ptr)) { // TODO: check if adapting reverse algorithm is feasible here - shared_ptr* prev{}; + thread_future* prev_head{head.get()}; - for (auto ptr = head.get(); ptr; ptr = ptr->next.get()) + for (thread_future* prev{};;) { - utils::prefetch_exec(ptr->exec.load()); + utils::prefetch_exec(prev_head->exec.load()); - ptr->prev = prev; - - if (ptr->next) + if (auto next = prev_head->next.get()) { - prev = &ptr->next; + prev = std::exchange(prev_head, next); + prev_head->prev = prev; + } + else + { + break; } } - if (!prev) - { - prev = &head; - } - - for (auto ptr = prev->get(); ptr; ptr = ptr->prev->get()) + for (auto ptr = prev_head; ptr; ptr = ptr->prev) { if (auto task = ptr->exec.load()) [[likely]] { @@ -2690,11 +2688,6 @@ void thread_base::exec() // Partial cleanup ptr->next.reset(); } - - if (!ptr->prev) - { - break; - } } if (!m_taskq) [[likely]] diff --git a/Utilities/Thread.h b/Utilities/Thread.h index ae4d625f1e..a64d64cb99 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -95,7 +95,7 @@ class thread_future shared_ptr next{}; - shared_ptr* prev{}; + thread_future* prev{}; protected: atomic_t exec{};