Blender V5.0
thread.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "util/thread.h"
6
7#if defined(__APPLE__) || defined(__linux__) && !defined(__GLIBC__)
8#else
9# include "util/system.h"
10# ifdef _WIN32
11# include "util/windows.h"
12# endif
13
14# include <system_error>
15#endif
16
18
19thread::thread(std::function<void()> run_cb) : run_cb_(run_cb), joined_(false)
20{
21#if defined(__APPLE__) || defined(__linux__) && !defined(__GLIBC__)
22 /* Set the stack size to 2MB to match GLIBC. The default 512KB on macOS is
23 * too small for Embree, and consistent stack size also makes things more
24 * predictable in general. */
25 pthread_attr_t attribute;
26 pthread_attr_init(&attribute);
27 pthread_attr_setstacksize(&attribute, 1024 * 1024 * 2);
28 pthread_create(&pthread_id, &attribute, run, (void *)this);
29#else
30 std_thread = std::thread(&thread::run, this);
31#endif
32}
33
35{
36 if (!joined_) {
37 join();
38 }
39}
40
41void *thread::run(void *arg)
42{
43 thread *self = (thread *)(arg);
44 self->run_cb_();
45 return nullptr;
46}
47
49{
50 joined_ = true;
51#if defined(__APPLE__) || defined(__linux__) && !defined(__GLIBC__)
52 return pthread_join(pthread_id, nullptr) == 0;
53#else
54 try {
55 std_thread.join();
56 return true;
57 }
58 catch (const std::system_error &) {
59 return false;
60 }
61#endif
62}
63
PyObject * self
~thread()
Definition thread.cpp:34
std::function< void()> run_cb_
Definition thread.h:44
static void * run(void *arg)
Definition thread.cpp:41
thread(std::function< void()> run_cb)
Definition thread.cpp:19
bool joined_
Definition thread.h:50
bool join()
Definition thread.cpp:48
std::thread std_thread
Definition thread.h:48
#define CCL_NAMESPACE_END