summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar ameerj2021-09-15 20:10:25 -0400
committerGravatar ameerj2021-09-15 20:49:07 -0400
commit84f7e7e91c441636b93accae6f7bd52f70a8ab99 (patch)
treef1ea78c339972670042f3b6930dff6cdde8b1ff5 /src
parentgpu: Use std::jthread for async gpu thread (diff)
downloadyuzu-84f7e7e91c441636b93accae6f7bd52f70a8ab99.tar.gz
yuzu-84f7e7e91c441636b93accae6f7bd52f70a8ab99.tar.xz
yuzu-84f7e7e91c441636b93accae6f7bd52f70a8ab99.zip
vk_scheduler: Use std::jthread
Diffstat (limited to 'src')
-rw-r--r--src/video_core/renderer_vulkan/vk_scheduler.cpp19
-rw-r--r--src/video_core/renderer_vulkan/vk_scheduler.h7
2 files changed, 9 insertions, 17 deletions
diff --git a/src/video_core/renderer_vulkan/vk_scheduler.cpp b/src/video_core/renderer_vulkan/vk_scheduler.cpp
index 1d438787a..0c11c814f 100644
--- a/src/video_core/renderer_vulkan/vk_scheduler.cpp
+++ b/src/video_core/renderer_vulkan/vk_scheduler.cpp
@@ -43,17 +43,10 @@ VKScheduler::VKScheduler(const Device& device_, StateTracker& state_tracker_)
43 command_pool{std::make_unique<CommandPool>(*master_semaphore, device)} { 43 command_pool{std::make_unique<CommandPool>(*master_semaphore, device)} {
44 AcquireNewChunk(); 44 AcquireNewChunk();
45 AllocateWorkerCommandBuffer(); 45 AllocateWorkerCommandBuffer();
46 worker_thread = std::thread(&VKScheduler::WorkerThread, this); 46 worker_thread = std::jthread([this](std::stop_token token) { WorkerThread(token); });
47} 47}
48 48
49VKScheduler::~VKScheduler() { 49VKScheduler::~VKScheduler() = default;
50 {
51 std::lock_guard lock{work_mutex};
52 quit = true;
53 }
54 work_cv.notify_all();
55 worker_thread.join();
56}
57 50
58void VKScheduler::Flush(VkSemaphore signal_semaphore, VkSemaphore wait_semaphore) { 51void VKScheduler::Flush(VkSemaphore signal_semaphore, VkSemaphore wait_semaphore) {
59 SubmitExecution(signal_semaphore, wait_semaphore); 52 SubmitExecution(signal_semaphore, wait_semaphore);
@@ -135,7 +128,7 @@ bool VKScheduler::UpdateGraphicsPipeline(GraphicsPipeline* pipeline) {
135 return true; 128 return true;
136} 129}
137 130
138void VKScheduler::WorkerThread() { 131void VKScheduler::WorkerThread(std::stop_token stop_token) {
139 Common::SetCurrentThreadName("yuzu:VulkanWorker"); 132 Common::SetCurrentThreadName("yuzu:VulkanWorker");
140 do { 133 do {
141 if (work_queue.empty()) { 134 if (work_queue.empty()) {
@@ -144,8 +137,8 @@ void VKScheduler::WorkerThread() {
144 std::unique_ptr<CommandChunk> work; 137 std::unique_ptr<CommandChunk> work;
145 { 138 {
146 std::unique_lock lock{work_mutex}; 139 std::unique_lock lock{work_mutex};
147 work_cv.wait(lock, [this] { return !work_queue.empty() || quit; }); 140 work_cv.wait(lock, stop_token, [this] { return !work_queue.empty(); });
148 if (quit) { 141 if (stop_token.stop_requested()) {
149 continue; 142 continue;
150 } 143 }
151 work = std::move(work_queue.front()); 144 work = std::move(work_queue.front());
@@ -158,7 +151,7 @@ void VKScheduler::WorkerThread() {
158 } 151 }
159 std::lock_guard reserve_lock{reserve_mutex}; 152 std::lock_guard reserve_lock{reserve_mutex};
160 chunk_reserve.push_back(std::move(work)); 153 chunk_reserve.push_back(std::move(work));
161 } while (!quit); 154 } while (!stop_token.stop_requested());
162} 155}
163 156
164void VKScheduler::AllocateWorkerCommandBuffer() { 157void VKScheduler::AllocateWorkerCommandBuffer() {
diff --git a/src/video_core/renderer_vulkan/vk_scheduler.h b/src/video_core/renderer_vulkan/vk_scheduler.h
index 759ed5a48..bd22e4e83 100644
--- a/src/video_core/renderer_vulkan/vk_scheduler.h
+++ b/src/video_core/renderer_vulkan/vk_scheduler.h
@@ -187,7 +187,7 @@ private:
187 GraphicsPipeline* graphics_pipeline = nullptr; 187 GraphicsPipeline* graphics_pipeline = nullptr;
188 }; 188 };
189 189
190 void WorkerThread(); 190 void WorkerThread(std::stop_token stop_token);
191 191
192 void AllocateWorkerCommandBuffer(); 192 void AllocateWorkerCommandBuffer();
193 193
@@ -212,7 +212,7 @@ private:
212 vk::CommandBuffer current_cmdbuf; 212 vk::CommandBuffer current_cmdbuf;
213 213
214 std::unique_ptr<CommandChunk> chunk; 214 std::unique_ptr<CommandChunk> chunk;
215 std::thread worker_thread; 215 std::jthread worker_thread;
216 216
217 State state; 217 State state;
218 218
@@ -224,9 +224,8 @@ private:
224 std::vector<std::unique_ptr<CommandChunk>> chunk_reserve; 224 std::vector<std::unique_ptr<CommandChunk>> chunk_reserve;
225 std::mutex reserve_mutex; 225 std::mutex reserve_mutex;
226 std::mutex work_mutex; 226 std::mutex work_mutex;
227 std::condition_variable work_cv; 227 std::condition_variable_any work_cv;
228 std::condition_variable wait_cv; 228 std::condition_variable wait_cv;
229 std::atomic_bool quit{};
230}; 229};
231 230
232} // namespace Vulkan 231} // namespace Vulkan