summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar ReinUsesLisp2020-07-21 04:40:28 -0300
committerGravatar ReinUsesLisp2020-07-28 01:47:03 -0300
commit2c67bbf6091477a09d280c322539cbaf1b6e4a8e (patch)
tree1128e37d5d397e3cb1d89aa01381463a5014842d /src
parentservice/sockets: Add worker abstraction to execute blocking calls asynchronously (diff)
downloadyuzu-2c67bbf6091477a09d280c322539cbaf1b6e4a8e.tar.gz
yuzu-2c67bbf6091477a09d280c322539cbaf1b6e4a8e.tar.xz
yuzu-2c67bbf6091477a09d280c322539cbaf1b6e4a8e.zip
service/sockets: Add worker pool abstraction
Manage worker threads with an easy to use abstraction. We can expand this to support thread deletion in the future.
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/service/sockets/blocking_worker.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/core/hle/service/sockets/blocking_worker.h b/src/core/hle/service/sockets/blocking_worker.h
index 7bd486530..31ef6b821 100644
--- a/src/core/hle/service/sockets/blocking_worker.h
+++ b/src/core/hle/service/sockets/blocking_worker.h
@@ -10,6 +10,7 @@
10#include <string_view> 10#include <string_view>
11#include <thread> 11#include <thread>
12#include <variant> 12#include <variant>
13#include <vector>
13 14
14#include <fmt/format.h> 15#include <fmt/format.h>
15 16
@@ -129,4 +130,33 @@ private:
129 std::atomic_bool is_available{true}; 130 std::atomic_bool is_available{true};
130}; 131};
131 132
133template <class Service, class... Types>
134class BlockingWorkerPool {
135 using Worker = BlockingWorker<Service, Types...>;
136
137public:
138 explicit BlockingWorkerPool(Core::System& system_, Service* service_)
139 : system{system_}, service{service_} {}
140
141 /// Returns a captured worker thread, creating new ones if necessary
142 Worker* CaptureWorker() {
143 for (auto& worker : workers) {
144 if (worker->TryCapture()) {
145 return worker.get();
146 }
147 }
148 auto new_worker = Worker::Create(system, service, fmt::format("BSD:{}", workers.size()));
149 [[maybe_unused]] const bool success = new_worker->TryCapture();
150 ASSERT(success);
151
152 return workers.emplace_back(std::move(new_worker)).get();
153 }
154
155private:
156 Core::System& system;
157 Service* const service;
158
159 std::vector<std::unique_ptr<Worker>> workers;
160};
161
132} // namespace Service::Sockets 162} // namespace Service::Sockets