Blender V5.0
gpu_worker.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "GPU_worker.hh"
6
7namespace blender::gpu {
8
9GPUWorker::GPUWorker(uint32_t threads_count,
10 ContextType context_type,
11 std::mutex &mutex,
12 std::function<void *()> pop_work,
13 std::function<void(void *)> do_work)
14 : mutex_(mutex)
15{
16 for (int i : IndexRange(threads_count)) {
18 std::shared_ptr<GPUSecondaryContext> thread_context =
19 context_type == ContextType::PerThread ? std::make_shared<GPUSecondaryContext>() : nullptr;
20 threads_.append(
21 std::make_unique<std::thread>([=]() { this->run(thread_context, pop_work, do_work); }));
22 }
23}
24
26{
27 {
28 std::unique_lock<std::mutex> lock(mutex_);
29 terminate_ = true;
30 }
31 condition_var_.notify_all();
32 for (std::unique_ptr<std::thread> &thread : threads_) {
33 thread->join();
34 }
35}
36
37void GPUWorker::run(std::shared_ptr<GPUSecondaryContext> context,
38 std::function<void *()> pop_work,
39 std::function<void(void *)> do_work)
40{
41 if (context) {
42 context->activate();
43 }
44
45 std::unique_lock<std::mutex> lock(mutex_);
46
47 /* Loop until we get the terminate signal. */
48 while (!terminate_) {
49 void *work = pop_work();
50 if (!work) {
51 condition_var_.wait(lock);
52 if (terminate_) {
53 break;
54 }
55 continue;
56 }
57
58 lock.unlock();
59 do_work(work);
60 lock.lock();
61 }
62}
63
64} // namespace blender::gpu
#define UNUSED_VARS(...)
volatile int lock
GPUWorker(uint32_t threads_count, ContextType context_type, std::mutex &mutex, std::function< void *()> pop_work, std::function< void(void *)> do_work)
Definition gpu_worker.cc:9
bool join()
Definition thread.cpp:48
ThreadMutex mutex
i
Definition text_draw.cc:230