Sunshine latest
Self-hosted game stream host for Moonlight.
thread_pool.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <thread>
9
10// local includes
11#include "platform/common.h"
12#include "task_pool.h"
13
14namespace thread_pool_util {
18 class ThreadPool: public task_pool_util::TaskPool {
19 public:
23 typedef TaskPool::__task __task;
24
25 private:
26 std::vector<std::jthread> _thread;
27
28 std::condition_variable _cv;
29 std::mutex _lock;
30
31 bool _continue;
32
33 public:
34 ThreadPool():
35 _continue {false} {
36 }
37
43 explicit ThreadPool(int threads):
44 _thread(threads),
45 _continue {true} {
46 for (auto &t : _thread) {
47 t = std::jthread(&ThreadPool::_main, this);
48 }
49 }
50
51 ~ThreadPool() noexcept {
52 if (!_continue) {
53 return;
54 }
55
56 stop();
57 join();
58 }
59
67 template<class Function, class... Args>
68 auto push(Function &&newTask, Args &&...args) {
69 std::lock_guard lg(_lock);
70 auto future = TaskPool::push(std::forward<Function>(newTask), std::forward<Args>(args)...);
71
72 _cv.notify_one();
73 return future;
74 }
75
81 void pushDelayed(std::pair<__time_point, __task> &&task) {
82 std::lock_guard lg(_lock);
83
84 TaskPool::pushDelayed(std::move(task));
85 }
86
95 template<class Function, class X, class Y, class... Args>
96 auto pushDelayed(Function &&newTask, std::chrono::duration<X, Y> duration, Args &&...args) {
97 std::lock_guard lg(_lock);
98 auto future = TaskPool::pushDelayed(std::forward<Function>(newTask), duration, std::forward<Args>(args)...);
99
100 // Update all timers for wait_until
101 _cv.notify_all();
102 return future;
103 }
104
110 void start(int threads) {
111 _continue = true;
112
113 _thread.resize(threads);
114
115 for (auto &t : _thread) {
116 t = std::jthread(&ThreadPool::_main, this);
117 }
118 }
119
123 void stop() {
124 std::lock_guard lg(_lock);
125
126 _continue = false;
127 _cv.notify_all();
128 }
129
133 void join() {
134 for (auto &t : _thread) {
135 t.join();
136 }
137 }
138
144 bool running() {
145 std::lock_guard lg(_lock);
146 return _continue;
147 }
148
149 public:
153 void _main() {
154 platf::set_thread_name("TaskPool::worker");
155 while (_continue) {
156 if (auto task = this->pop()) {
157 (*task)->run();
158 } else {
159 std::unique_lock uniq_lock(_lock);
160
161 if (ready()) {
162 continue;
163 }
164
165 if (!_continue) {
166 break;
167 }
168
169 if (auto tp = next()) {
170 _cv.wait_until(uniq_lock, *tp);
171 } else {
172 _cv.wait(uniq_lock);
173 }
174 }
175 }
176
177 // Execute remaining tasks
178 while (auto task = this->pop()) {
179 (*task)->run();
180 }
181 }
182 };
183} // namespace thread_pool_util
Queue of immediate and delayed tasks executed by worker threads.
Definition task_pool.h:67
std::optional< std::pair< __time_point, __task > > pop(task_id_t task_id)
Remove and return the next queued item, waiting when requested.
Definition task_pool.h:282
std::optional< __time_point > next()
Return the next timer task ready for execution.
Definition task_pool.h:335
bool ready()
Check whether the task pool has active workers.
Definition task_pool.h:324
Definition thread_pool.h:18
auto pushDelayed(Function &&newTask, std::chrono::duration< X, Y > duration, Args &&...args)
Queue a task that becomes runnable after a delay.
Definition thread_pool.h:96
ThreadPool(int threads)
Start a pool with the requested number of worker threads.
Definition thread_pool.h:43
TaskPool::__task __task
Callable unit executed by a worker thread.
Definition thread_pool.h:23
void pushDelayed(std::pair< __time_point, __task > &&task)
Queue a task that becomes runnable after a delay.
Definition thread_pool.h:81
void start(int threads)
Start worker threads for queued thread-pool tasks.
Definition thread_pool.h:110
void join()
Wait for worker threads owned by the session to exit.
Definition thread_pool.h:133
bool running()
Check whether worker threads are available to execute queued tasks.
Definition thread_pool.h:144
void stop()
Stop worker threads and prevent additional task execution.
Definition thread_pool.h:123
void _main()
Run the main application or worker loop.
Definition thread_pool.h:153
auto push(Function &&newTask, Args &&...args)
Queue work for asynchronous execution.
Definition thread_pool.h:68
Declarations for common platform specific utilities.
void set_thread_name(std::string_view name)
Name the current thread for use with development tools.
Definition misc.cpp:552
Functions for handling command line arguments.
Definition entry_handler.cpp:41
Declarations for the task pool system.