Blender
V5.0
source
blender
gpu
intern
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
7
namespace
blender::gpu
{
8
9
GPUWorker::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)) {
17
UNUSED_VARS
(
i
);
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
25
GPUWorker::~GPUWorker
()
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
37
void
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
UNUSED_VARS
#define UNUSED_VARS(...)
Definition
BLI_utildefines.h:505
GPU_worker.hh
lock
volatile int lock
Definition
atomic_ops_unix.h:0
blender::IndexRange
Definition
BLI_index_range.hh:50
blender::gpu::GPUWorker::~GPUWorker
~GPUWorker()
Definition
gpu_worker.cc:25
blender::gpu::GPUWorker::ContextType
ContextType
Definition
GPU_worker.hh:32
blender::gpu::GPUWorker::ContextType::PerThread
@ PerThread
Definition
GPU_worker.hh:36
blender::gpu::GPUWorker::GPUWorker
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
thread
Definition
thread.h:35
thread::join
bool join()
Definition
thread.cpp:48
mutex
ThreadMutex mutex
Definition
gpu_batch_presets.cc:39
blender::gpu
Definition
blf_internal_types.hh:32
i
i
Definition
text_draw.cc:230
Generated on
for Blender by
doxygen
1.16.1