From 27f3308ac39f13232113faebd598325019ca2d23 Mon Sep 17 00:00:00 2001 From: Kai Blaschke Date: Thu, 1 Feb 2024 19:03:21 +0100 Subject: [PATCH] Remove utterly useless multithreading switch Since we're using OpenGL, everything has to be done in the same thread anyways. Besides that, the actual "multithreading" implementation was basically starting an empty thread that did absolutely nothing, then wait synchronously for it to complete and join it on the next frame. --- BUILDING-cmake.md | 1 - CMakeLists.txt | 15 ----- src/libprojectM/BackgroundWorker.hpp | 76 ------------------------ src/libprojectM/CMakeLists.txt | 7 --- src/libprojectM/ProjectM.cpp | 43 +------------- src/libprojectM/ProjectM.hpp | 26 -------- src/libprojectM/projectM4Config.cmake.in | 3 - 7 files changed, 1 insertion(+), 170 deletions(-) delete mode 100644 src/libprojectM/BackgroundWorker.hpp diff --git a/BUILDING-cmake.md b/BUILDING-cmake.md index 75167b6aa..9f4f8fb17 100644 --- a/BUILDING-cmake.md +++ b/BUILDING-cmake.md @@ -118,7 +118,6 @@ developing libprojectM, trying experimental features or building the library for | CMake option | Default | Required dependencies | Description | |------------------------|---------|--------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------| | `ENABLE_SDL_UI` | `ON` | `SDL2` | Builds the SDL-based test application. Only used for development testing, will not be installed. | -| `ENABLE_THREADING` | `ON` | | Enable multithreading support for preset loading if available. | | `ENABLE_INSTALL` | `OFF` | Building as a CMake subproject | Enable projectM install targets when built as a subproject via `add_subdirectory()`. | | `ENABLE_DEBUG_POSTFIX` | `ON` | | Adds `d` (by default) to the name of any binary file in debug builds. | | `ENABLE_SYSTEM_GLM` | `OFF` | | Builds against a system-installed GLM library. | diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e49e2c85..fb993c7a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,7 +64,6 @@ cmake_dependent_option(BUILD_SHARED_LIBS "Build and install libprojectM as a sha option(ENABLE_PLAYLIST "Enable building the playlist management library" ON) cmake_dependent_option(ENABLE_SDL_UI "Build the SDL2-based developer test UI" OFF "NOT ENABLE_EMSCRIPTEN" OFF) cmake_dependent_option(ENABLE_GLES "Enable OpenGL ES support" OFF "NOT ENABLE_EMSCRIPTEN AND NOT CMAKE_SYSTEM_NAME STREQUAL Android" ON) -cmake_dependent_option(ENABLE_THREADING "Enable multithreading support." ON "NOT ENABLE_EMSCRIPTEN" OFF) option(ENABLE_BOOST_FILESYSTEM "Force the use of boost::filesystem, even if the compiler supports C++17." OFF) cmake_dependent_option(ENABLE_INSTALL "Enable installing projectM libraries and headers." OFF "NOT PROJECT_IS_TOP_LEVEL" ON) option(ENABLE_SYSTEM_GLM "Enable use of system-install GLM library" OFF) @@ -118,13 +117,6 @@ if(ENABLE_EMSCRIPTEN) "SHELL:-s NO_DISABLE_EXCEPTION_CATCHING" ) - if(ENABLE_THREADING) - message(AUTHOR_WARNING "Threading on emscripten is deemed stable, but may have issues. Use with care.\n" - "See https://emscripten.org/docs/porting/pthreads.html for more information.") - add_compile_options(-pthread) - add_link_options(-pthread) - endif() - set(USE_GLES ON) else() if(ENABLE_SDL_UI) @@ -168,12 +160,6 @@ else() endif() endif() endif() - - if(ENABLE_THREADING) - find_package(Threads REQUIRED) - set(PROJECTM_USE_THREADS YES) - endif() - endif() if(ENABLE_CXX_INTERFACE) @@ -216,7 +202,6 @@ message(STATUS "Features:") message(STATUS "==============================================") message(STATUS "") message(STATUS " Build shared libraries: ${BUILD_SHARED_LIBS}") -message(STATUS " Threading: ${ENABLE_THREADING}") if(ENABLE_BOOST_FILESYSTEM) message(STATUS " Filesystem support: Boost") message(STATUS " Boost version: ${Boost_VERSION}") diff --git a/src/libprojectM/BackgroundWorker.hpp b/src/libprojectM/BackgroundWorker.hpp deleted file mode 100644 index 37ef8b5c8..000000000 --- a/src/libprojectM/BackgroundWorker.hpp +++ /dev/null @@ -1,76 +0,0 @@ -#pragma once - -#include -#include - -namespace libprojectM { - -/** - * Small class to encapsulate synchronization of a simple background task runner - */ -class BackgroundWorkerSync -{ -public: - BackgroundWorkerSync() = default; - - void Reset() - { - m_isWorkToDo = false; - m_finished = false; - } - - // called by foreground - void WakeUpBackgroundTask() - { - std::lock_guard guard(m_mutex); - m_isWorkToDo = true; - m_conditionStartWork.notify_one(); - } - - // called by foreground - void WaitForBackgroundTaskToFinish() - { - std::unique_lock guard(m_mutex); - while (m_isWorkToDo) - { - m_conditionWorkDone.wait(guard); - } - } - - // called by foreground() when shutting down, background thread should exit - void FinishUp() - { - std::lock_guard guard(m_mutex); - m_finished = true; - m_conditionStartWork.notify_all(); - } - - // called by background - auto WaitForWork() -> bool - { - std::unique_lock guard(m_mutex); - while (!m_isWorkToDo && !m_finished) - { - m_conditionStartWork.wait(guard); - } - return !m_finished; - } - - // called by background - void FinishedWork() - { - std::lock_guard guard(m_mutex); - m_isWorkToDo = false; - m_conditionWorkDone.notify_one(); - } - -private: - mutable std::mutex m_mutex; //!< Mutex that controls access to the work flags. - std::condition_variable m_conditionStartWork; - std::condition_variable m_conditionWorkDone; - volatile bool m_isWorkToDo{ false }; - volatile bool m_finished{ false }; - -}; - -} // namespace libprojectM diff --git a/src/libprojectM/CMakeLists.txt b/src/libprojectM/CMakeLists.txt index 6720f0662..3bb883b2b 100644 --- a/src/libprojectM/CMakeLists.txt +++ b/src/libprojectM/CMakeLists.txt @@ -128,13 +128,6 @@ if(ENABLE_INSTALL) ) if(ENABLE_CXX_INTERFACE) - if(ENABLE_THREADING) - target_compile_definitions(projectM - INTERFACE - PROJECTM_USE_THREADS - ) - endif() - install(FILES Audio/PCM.hpp ProjectM.hpp diff --git a/src/libprojectM/ProjectM.cpp b/src/libprojectM/ProjectM.cpp index dcebb4a5c..8b0c6545b 100644 --- a/src/libprojectM/ProjectM.cpp +++ b/src/libprojectM/ProjectM.cpp @@ -32,27 +32,17 @@ #include "Renderer/TextureManager.hpp" #include "Renderer/TransitionShaderManager.hpp" -#if PROJECTM_USE_THREADS -#include "libprojectM/BackgroundWorker.hpp" -#endif - namespace libprojectM { ProjectM::ProjectM() : m_presetFactoryManager(std::make_unique()) -#if PROJECTM_USE_THREADS - , m_workerSync(std::make_unique()) -#endif { Initialize(); } ProjectM::~ProjectM() { -#if PROJECTM_USE_THREADS - m_workerSync->FinishUp(); - m_workerThread.join(); -#endif + // Can't use "=default" in the header due to unique_ptr requiring the actual type declarations. } void ProjectM::PresetSwitchRequestedEvent(bool) const @@ -113,22 +103,6 @@ void ProjectM::ResetTextures() m_textureManager = std::make_unique(m_textureSearchPaths); } -#if PROJECTM_USE_THREADS - -void ProjectM::ThreadWorker() -{ - while (true) - { - if (!m_workerSync->WaitForWork()) - { - return; - } - m_workerSync->FinishedWork(); - } -} - -#endif - void ProjectM::RenderFrame() { // Don't render if window area is zero. @@ -137,10 +111,6 @@ void ProjectM::RenderFrame() return; } -#if PROJECTM_USE_THREADS - std::lock_guard guard(m_presetSwitchMutex); -#endif - // Update FPS and other timer values. m_timeKeeper->UpdateTimers(); @@ -181,12 +151,6 @@ void ProjectM::RenderFrame() if (m_timeKeeper->IsSmoothing() && m_transitioningPreset != nullptr) { -#if PROJECTM_USE_THREADS - m_workerSync->WakeUpBackgroundTask(); - // FIXME: Instead of waiting after a single render pass, check every frame if it's done. - m_workerSync->WaitForBackgroundTaskToFinish(); -#endif - // ToDo: check if new preset is loaded. if (m_timeKeeper->SmoothRatio() >= 1.0) @@ -256,11 +220,6 @@ void ProjectM::Initialize() LoadIdlePreset(); -#if PROJECTM_USE_THREADS - m_workerSync->Reset(); - m_workerThread = std::thread(&ProjectM::ThreadWorker, this); -#endif - m_timeKeeper->StartPreset(); } diff --git a/src/libprojectM/ProjectM.hpp b/src/libprojectM/ProjectM.hpp index bfabb5a05..58291888b 100644 --- a/src/libprojectM/ProjectM.hpp +++ b/src/libprojectM/ProjectM.hpp @@ -40,21 +40,8 @@ #include #include -#if PROJECTM_USE_THREADS - -#include -#include - -#endif - namespace libprojectM { -class BackgroundWorkerSync; - -namespace Audio { -class BeatDetect; -} // namespace Audio - namespace Renderer { class CopyTexture; class PresetTransition; @@ -207,13 +194,6 @@ private: auto GetRenderContext() -> Renderer::RenderContext; -#if PROJECTM_USE_THREADS - - void ThreadWorker(); - -#endif - - uint32_t m_meshX{32}; //!< Per-point mesh horizontal resolution. uint32_t m_meshY{24}; //!< Per-point mesh vertical resolution. uint32_t m_targetFps{35}; //!< Target frames per second. @@ -247,12 +227,6 @@ private: std::unique_ptr m_transitioningPreset; //!< Destination preset when smooth preset switching. std::unique_ptr m_transition; //!< Transition effect used for blending. std::unique_ptr m_timeKeeper; //!< Keeps the different timers used to render and switch presets. - -#if PROJECTM_USE_THREADS - mutable std::recursive_mutex m_presetSwitchMutex; //!< Mutex for locking preset switching while rendering and vice versa. - std::thread m_workerThread; //!< Background worker for preloading presets. - std::unique_ptr m_workerSync; //!< Background work synchronizer. -#endif }; } // namespace libprojectM diff --git a/src/libprojectM/projectM4Config.cmake.in b/src/libprojectM/projectM4Config.cmake.in index 8c74e01b5..00244c4df 100644 --- a/src/libprojectM/projectM4Config.cmake.in +++ b/src/libprojectM/projectM4Config.cmake.in @@ -12,9 +12,6 @@ if(NOT "@ENABLE_EMSCRIPTEN@") # ENABLE_EMSCRIPTEN find_dependency(OpenGL) endif() endif() -if("@ENABLE_THREADING@") # ENABLE_THREADING - find_dependency(Threads) -endif() if("@ENABLE_BOOST_FILESYSTEM@") # ENABLE_BOOST_FILESYSTEM find_dependency(Boost COMPONENTS Filesystem) endif()