2025-04-07 15:26:25 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2025 Blender Authors
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
|
|
|
|
|
|
#include "GPU_worker.hh"
|
|
|
|
|
|
|
|
|
|
namespace blender::gpu {
|
|
|
|
|
|
2025-05-08 18:16:47 +02:00
|
|
|
GPUWorker::GPUWorker(uint32_t threads_count,
|
|
|
|
|
ContextType context_type,
|
|
|
|
|
std::function<void()> run_cb)
|
2025-04-07 15:26:25 +02:00
|
|
|
{
|
|
|
|
|
for (int i : IndexRange(threads_count)) {
|
|
|
|
|
UNUSED_VARS(i);
|
|
|
|
|
std::shared_ptr<GPUSecondaryContext> thread_context =
|
2025-05-08 18:16:47 +02:00
|
|
|
context_type == ContextType::PerThread ? std::make_shared<GPUSecondaryContext>() : nullptr;
|
2025-04-07 15:26:25 +02:00
|
|
|
threads_.append(std::make_unique<std::thread>([=]() { this->run(thread_context, run_cb); }));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPUWorker::~GPUWorker()
|
|
|
|
|
{
|
|
|
|
|
terminate_ = true;
|
|
|
|
|
condition_var_.notify_all();
|
|
|
|
|
for (std::unique_ptr<std::thread> &thread : threads_) {
|
|
|
|
|
thread->join();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GPUWorker::run(std::shared_ptr<GPUSecondaryContext> context, std::function<void()> run_cb)
|
|
|
|
|
{
|
2025-05-08 18:16:47 +02:00
|
|
|
if (context) {
|
|
|
|
|
context->activate();
|
|
|
|
|
}
|
2025-04-07 15:26:25 +02:00
|
|
|
|
|
|
|
|
/* Loop until we get the terminate signal. */
|
|
|
|
|
while (!terminate_) {
|
|
|
|
|
{
|
|
|
|
|
/* Wait until wake_up() */
|
|
|
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
|
|
|
condition_var_.wait(lock);
|
|
|
|
|
}
|
|
|
|
|
if (terminate_) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run_cb();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace blender::gpu
|