From d0f53ddab64fc257d4b6868ed38831aeb978d9e9 Mon Sep 17 00:00:00 2001 From: Zaggy1024 Date: Fri, 23 Jan 2026 15:11:21 -0600 Subject: [PATCH] LibThreading: Remove WorkerThread This is unused. --- AK/Debug.h.in | 4 - Libraries/LibThreading/Forward.h | 3 - Libraries/LibThreading/WorkerThread.h | 172 -------------------------- Meta/CMake/all_the_debug_macros.cmake | 1 - Meta/gn/secondary/AK/BUILD.gn | 1 - 5 files changed, 181 deletions(-) delete mode 100644 Libraries/LibThreading/WorkerThread.h diff --git a/AK/Debug.h.in b/AK/Debug.h.in index 4b8bf06ef0..48dd2cba61 100644 --- a/AK/Debug.h.in +++ b/AK/Debug.h.in @@ -346,10 +346,6 @@ # cmakedefine01 WEBP_DEBUG #endif -#ifndef WORKER_THREAD_DEBUG -# cmakedefine01 WORKER_THREAD_DEBUG -#endif - #ifndef XML_PARSER_DEBUG # cmakedefine01 XML_PARSER_DEBUG #endif diff --git a/Libraries/LibThreading/Forward.h b/Libraries/LibThreading/Forward.h index 3ca58e6eb4..94a346d880 100644 --- a/Libraries/LibThreading/Forward.h +++ b/Libraries/LibThreading/Forward.h @@ -10,7 +10,4 @@ namespace Threading { class Thread; -template -class WorkerThread; - } diff --git a/Libraries/LibThreading/WorkerThread.h b/Libraries/LibThreading/WorkerThread.h deleted file mode 100644 index 690d1ffc1e..0000000000 --- a/Libraries/LibThreading/WorkerThread.h +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (c) 2022, Gregory Bertilson - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include -#include -#include -#include - -namespace Threading { - -// Macro to allow single-line logging prints with fields that only exist in debug mode. -#if WORKER_THREAD_DEBUG -# define WORKER_LOG(args...) ({ dbgln(args); }) -#else -# define WORKER_LOG(args...) -#endif - -template -class WorkerThread { - enum class State { - Idle, - Working, - Stopped, - }; - using WorkerTask = Function()>; - using WorkerState = Variant; - -public: - static ErrorOr> create(StringView name) - { - auto worker_thread = TRY(adopt_nonnull_own_or_enomem(new (nothrow) WorkerThread())); - worker_thread->m_thread = TRY(Threading::Thread::try_create([&self = *worker_thread]() { - WORKER_LOG("Starting worker loop {}", self.m_id); - - while (true) { - self.m_mutex.lock(); - if (self.m_stop) { - WORKER_LOG("Exiting {}", self.m_id); - self.m_state = State::Stopped; - self.m_condition.broadcast(); - self.m_mutex.unlock(); - return 0; - } - if (self.m_state.template has()) { - auto task = move(self.m_state.template get()); - self.m_state = State::Working; - self.m_mutex.unlock(); - - WORKER_LOG("Starting task on {}", self.m_id); - auto result = task(); - if (result.is_error()) { - WORKER_LOG("Task finished on {} with error", self.m_id); - self.m_mutex.lock(); - self.m_state = result.release_error(); - self.m_condition.broadcast(); - } else { - WORKER_LOG("Task finished successfully on {}", self.m_id); - self.m_mutex.lock(); - self.m_state = State::Idle; - self.m_condition.broadcast(); - } - } - WORKER_LOG("Awaiting new task in {}...", self.m_id); - self.m_condition.wait(); - WORKER_LOG("Worker thread awoken in {}", self.m_id); - self.m_mutex.unlock(); - } - - return 0; - }, - name)); - worker_thread->m_thread->start(); - return worker_thread; - } - - ~WorkerThread() - { - m_mutex.lock(); - m_stop = true; - m_condition.broadcast(); - while (!is_in_state(State::Stopped)) - m_condition.wait(); - m_mutex.unlock(); - (void)m_thread->join(); - WORKER_LOG("Worker thread {} joined successfully", m_id); - } - - // Returns whether the task is starting. - bool start_task(WorkerTask&& task) - { - m_mutex.lock(); - VERIFY(!is_in_state(State::Stopped)); - - bool start_work = false; - if (is_in_state(State::Idle)) { - start_work = true; - } else if (m_state.template has()) { - WORKER_LOG("Starting task and ignoring previous error: {}", m_state.template get().string_literal()); - start_work = true; - } - if (start_work) { - WORKER_LOG("Queuing task on {}", m_id); - m_state = move(task); - m_condition.broadcast(); - } - - m_mutex.unlock(); - return start_work; - } - - ErrorOr wait_until_task_is_finished() - { - WORKER_LOG("Waiting for task to finish on {}...", m_id); - m_mutex.lock(); - while (true) { - if (m_state.template has() || is_in_state(State::Working)) { - m_condition.wait(); - } else if (m_state.template has()) { - auto error = move(m_state.template get()); - m_state = State::Idle; - m_mutex.unlock(); - WORKER_LOG("Finished waiting with error on {}: {}", m_id, error.string_literal()); - return error; - } else { - m_mutex.unlock(); - WORKER_LOG("Finished waiting on {}", m_id); - return {}; - } - } - m_mutex.unlock(); - } - -private: -#if WORKER_THREAD_DEBUG - static inline size_t current_id = 0; -#endif - - WorkerThread() - : m_condition(m_mutex) -#if WORKER_THREAD_DEBUG - , m_id(current_id++) -#endif - { - } - WorkerThread(WorkerThread const&) = delete; - WorkerThread(WorkerThread&&) = delete; - - // Must be called with the mutex locked. - bool is_in_state(State state) - { - return m_state.template has() && m_state.template get() == state; - } - - RefPtr m_thread; - Threading::Mutex m_mutex; - Threading::ConditionVariable m_condition; - WorkerState m_state { State::Idle }; - bool m_stop { false }; -#if WORKER_THREAD_DEBUG - size_t m_id; -#endif -}; - -#undef WORKER_LOG - -} diff --git a/Meta/CMake/all_the_debug_macros.cmake b/Meta/CMake/all_the_debug_macros.cmake index 5f1ac8aa89..d8cbf45f99 100644 --- a/Meta/CMake/all_the_debug_macros.cmake +++ b/Meta/CMake/all_the_debug_macros.cmake @@ -82,7 +82,6 @@ set(WEBVIEW_PROCESS_DEBUG ON) set(WEB_FETCH_DEBUG ON) set(WEB_WORKER_DEBUG ON) set(WEBP_DEBUG ON) -set(WORKER_THREAD_DEBUG ON) set(XML_PARSER_DEBUG ON) # False positive: ANDROID_LOG_DEBUG is a log level, not a debug flag diff --git a/Meta/gn/secondary/AK/BUILD.gn b/Meta/gn/secondary/AK/BUILD.gn index ee31849cad..1f940c1547 100644 --- a/Meta/gn/secondary/AK/BUILD.gn +++ b/Meta/gn/secondary/AK/BUILD.gn @@ -286,7 +286,6 @@ write_cmake_config("ak_debug_gen") { "WEB_FETCH_DEBUG=", "WEB_WORKER_DEBUG=", "WEBP_DEBUG=", - "WORKER_THREAD_DEBUG=", "XML_PARSER_DEBUG=", ] }