From c51fdc1a02e05b3ab9996bde9edf4c38b48d83cb Mon Sep 17 00:00:00 2001 From: pigeatgarlic Date: Sun, 5 Apr 2026 21:32:08 +0700 Subject: [PATCH] remove task and thread pool --- cmake/compile_definitions/common.cmake | 2 - src/globals.h | 1 - src/main.cpp | 7 - src/task_pool.h | 262 ------------------------- src/thread_pool.h | 131 ------------- 5 files changed, 403 deletions(-) delete mode 100644 src/task_pool.h delete mode 100644 src/thread_pool.h diff --git a/cmake/compile_definitions/common.cmake b/cmake/compile_definitions/common.cmake index cdee7f89..297f3e9e 100644 --- a/cmake/compile_definitions/common.cmake +++ b/cmake/compile_definitions/common.cmake @@ -67,8 +67,6 @@ set(SUNSHINE_TARGET_FILES "${CMAKE_SOURCE_DIR}/src/audio.h" "${CMAKE_SOURCE_DIR}/src/platform/common.h" "${CMAKE_SOURCE_DIR}/src/move_by_copy.h" - "${CMAKE_SOURCE_DIR}/src/task_pool.h" - "${CMAKE_SOURCE_DIR}/src/thread_pool.h" "${CMAKE_SOURCE_DIR}/src/thread_safe.h" "${CMAKE_SOURCE_DIR}/src/sync.h" "${CMAKE_SOURCE_DIR}/src/round_robin.h" diff --git a/src/globals.h b/src/globals.h index 30b4e955..c1942a86 100644 --- a/src/globals.h +++ b/src/globals.h @@ -4,7 +4,6 @@ */ #pragma once -#include "thread_pool.h" #include "thread_safe.h" extern bool display_cursor; diff --git a/src/main.cpp b/src/main.cpp index d89c2f4e..47654c73 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -159,13 +159,6 @@ main(int argc, char *argv[]) { // Wait as long as possible to terminate Sunshine.exe during logoff/shutdown SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY); - // We must create a hidden window to receive shutdown notifications since we load gdi32.dll - std::promise session_monitor_hwnd_promise; - auto session_monitor_hwnd_future = session_monitor_hwnd_promise.get_future(); - std::promise session_monitor_join_thread_promise; - auto session_monitor_join_thread_future = session_monitor_join_thread_promise.get_future(); - - auto platf_deinit_guard = platf::init(); diff --git a/src/task_pool.h b/src/task_pool.h deleted file mode 100644 index 8da85ed0..00000000 --- a/src/task_pool.h +++ /dev/null @@ -1,262 +0,0 @@ -/** - * @file src/task_pool.h - * @brief todo - */ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "move_by_copy.h" -#include "utility.h" -namespace task_pool_util { - - class _ImplBase { - public: - // _unique_base_type _this_ptr; - - inline virtual ~_ImplBase() = default; - - virtual void - run() = 0; - }; - - template - class _Impl: public _ImplBase { - Function _func; - - public: - _Impl(Function &&f): - _func(std::forward(f)) {} - - void - run() override { - _func(); - } - }; - - class TaskPool { - public: - typedef std::unique_ptr<_ImplBase> __task; - typedef _ImplBase *task_id_t; - - typedef std::chrono::steady_clock::time_point __time_point; - - template - class timer_task_t { - public: - task_id_t task_id; - std::future future; - - timer_task_t(task_id_t task_id, std::future &future): - task_id { task_id }, future { std::move(future) } {} - }; - - protected: - std::deque<__task> _tasks; - std::vector> _timer_tasks; - std::mutex _task_mutex; - - public: - TaskPool() = default; - TaskPool(TaskPool &&other) noexcept: - _tasks { std::move(other._tasks) }, _timer_tasks { std::move(other._timer_tasks) } {} - - TaskPool & - operator=(TaskPool &&other) noexcept { - std::swap(_tasks, other._tasks); - std::swap(_timer_tasks, other._timer_tasks); - - return *this; - } - - template - auto - push(Function &&newTask, Args &&...args) { - static_assert(std::is_invocable_v, "arguments don't match the function"); - - using __return = std::invoke_result_t; - using task_t = std::packaged_task<__return()>; - - auto bind = [task = std::forward(newTask), tuple_args = std::make_tuple(std::forward(args)...)]() mutable { - return std::apply(task, std::move(tuple_args)); - }; - - task_t task(std::move(bind)); - - auto future = task.get_future(); - - std::lock_guard lg(_task_mutex); - _tasks.emplace_back(toRunnable(std::move(task))); - - return future; - } - - void - pushDelayed(std::pair<__time_point, __task> &&task) { - std::lock_guard lg(_task_mutex); - - auto it = _timer_tasks.cbegin(); - for (; it < _timer_tasks.cend(); ++it) { - if (std::get<0>(*it) < task.first) { - break; - } - } - - _timer_tasks.emplace(it, task.first, std::move(task.second)); - } - - /** - * @return an id to potentially delay the task. - */ - template - auto - pushDelayed(Function &&newTask, std::chrono::duration duration, Args &&...args) { - static_assert(std::is_invocable_v, "arguments don't match the function"); - - using __return = std::invoke_result_t; - using task_t = std::packaged_task<__return()>; - - __time_point time_point; - if constexpr (std::is_floating_point_v) { - time_point = std::chrono::steady_clock::now() + std::chrono::duration_cast(duration); - } - else { - time_point = std::chrono::steady_clock::now() + duration; - } - - auto bind = [task = std::forward(newTask), tuple_args = std::make_tuple(std::forward(args)...)]() mutable { - return std::apply(task, std::move(tuple_args)); - }; - - task_t task(std::move(bind)); - - auto future = task.get_future(); - auto runnable = toRunnable(std::move(task)); - - task_id_t task_id = &*runnable; - - pushDelayed(std::pair { time_point, std::move(runnable) }); - - return timer_task_t<__return> { task_id, future }; - } - - /** - * @param task_id The id of the task to delay. - * @param duration The delay before executing the task. - */ - template - void - delay(task_id_t task_id, std::chrono::duration duration) { - std::lock_guard lg(_task_mutex); - - auto it = _timer_tasks.begin(); - for (; it < _timer_tasks.cend(); ++it) { - const __task &task = std::get<1>(*it); - - if (&*task == task_id) { - std::get<0>(*it) = std::chrono::steady_clock::now() + duration; - - break; - } - } - - if (it == _timer_tasks.cend()) { - return; - } - - // smaller time goes to the back - auto prev = it - 1; - while (it > _timer_tasks.cbegin()) { - if (std::get<0>(*it) > std::get<0>(*prev)) { - std::swap(*it, *prev); - } - - --prev; - --it; - } - } - - bool - cancel(task_id_t task_id) { - std::lock_guard lg(_task_mutex); - - auto it = _timer_tasks.begin(); - for (; it < _timer_tasks.cend(); ++it) { - const __task &task = std::get<1>(*it); - - if (&*task == task_id) { - _timer_tasks.erase(it); - - return true; - } - } - - return false; - } - - std::optional> - pop(task_id_t task_id) { - std::lock_guard lg(_task_mutex); - - auto pos = std::find_if(std::begin(_timer_tasks), std::end(_timer_tasks), [&task_id](const auto &t) { return t.second.get() == task_id; }); - - if (pos == std::end(_timer_tasks)) { - return std::nullopt; - } - - return std::move(*pos); - } - - std::optional<__task> - pop() { - std::lock_guard lg(_task_mutex); - - if (!_tasks.empty()) { - __task task = std::move(_tasks.front()); - _tasks.pop_front(); - return task; - } - - if (!_timer_tasks.empty() && std::get<0>(_timer_tasks.back()) <= std::chrono::steady_clock::now()) { - __task task = std::move(std::get<1>(_timer_tasks.back())); - _timer_tasks.pop_back(); - return task; - } - - return std::nullopt; - } - - bool - ready() { - std::lock_guard lg(_task_mutex); - - return !_tasks.empty() || (!_timer_tasks.empty() && std::get<0>(_timer_tasks.back()) <= std::chrono::steady_clock::now()); - } - - std::optional<__time_point> - next() { - std::lock_guard lg(_task_mutex); - - if (_timer_tasks.empty()) { - return std::nullopt; - } - - return std::get<0>(_timer_tasks.back()); - } - - private: - template - std::unique_ptr<_ImplBase> - toRunnable(Function &&f) { - return std::make_unique<_Impl>(std::forward(f)); - } - }; -} // namespace task_pool_util diff --git a/src/thread_pool.h b/src/thread_pool.h deleted file mode 100644 index c97f29a1..00000000 --- a/src/thread_pool.h +++ /dev/null @@ -1,131 +0,0 @@ -/** - * @file src/thread_pool.h - * @brief todo - */ -#pragma once - -#include "task_pool.h" -#include - -namespace thread_pool_util { - /** - * Allow threads to execute unhindered while keeping full control over the threads. - */ - class ThreadPool: public task_pool_util::TaskPool { - public: - typedef TaskPool::__task __task; - - private: - std::vector _thread; - - std::condition_variable _cv; - std::mutex _lock; - - bool _continue; - - public: - ThreadPool(): - _continue { false } {} - - explicit ThreadPool(int threads): - _thread(threads), _continue { true } { - for (auto &t : _thread) { - t = std::thread(&ThreadPool::_main, this); - } - } - - ~ThreadPool() noexcept { - if (!_continue) return; - - stop(); - join(); - } - - template - auto - push(Function &&newTask, Args &&...args) { - std::lock_guard lg(_lock); - auto future = TaskPool::push(std::forward(newTask), std::forward(args)...); - - _cv.notify_one(); - return future; - } - - void - pushDelayed(std::pair<__time_point, __task> &&task) { - std::lock_guard lg(_lock); - - TaskPool::pushDelayed(std::move(task)); - } - - template - auto - pushDelayed(Function &&newTask, std::chrono::duration duration, Args &&...args) { - std::lock_guard lg(_lock); - auto future = TaskPool::pushDelayed(std::forward(newTask), duration, std::forward(args)...); - - // Update all timers for wait_until - _cv.notify_all(); - return future; - } - - void - start(int threads) { - _continue = true; - - _thread.resize(threads); - - for (auto &t : _thread) { - t = std::thread(&ThreadPool::_main, this); - } - } - - void - stop() { - std::lock_guard lg(_lock); - - _continue = false; - _cv.notify_all(); - } - - void - join() { - for (auto &t : _thread) { - t.join(); - } - } - - public: - void - _main() { - while (_continue) { - if (auto task = this->pop()) { - (*task)->run(); - } - else { - std::unique_lock uniq_lock(_lock); - - if (ready()) { - continue; - } - - if (!_continue) { - break; - } - - if (auto tp = next()) { - _cv.wait_until(uniq_lock, *tp); - } - else { - _cv.wait(uniq_lock); - } - } - } - - // Execute remaining tasks - while (auto task = this->pop()) { - (*task)->run(); - } - } - }; -} // namespace thread_pool_util