Blender  V2.93
util_thread.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "util/util_thread.h"
18 
19 #include "util/util_system.h"
20 #include "util/util_windows.h"
21 
23 
24 thread::thread(function<void()> run_cb, int node) : run_cb_(run_cb), joined_(false), node_(node)
25 {
26 #ifdef __APPLE__
27  /* Set the stack size to 2MB to match Linux. The default 512KB on macOS is
28  * too small for Embree, and consistent stack size also makes things more
29  * predictable in general. */
30  pthread_attr_t attribute;
31  pthread_attr_init(&attribute);
32  pthread_attr_setstacksize(&attribute, 1024 * 1024 * 2);
33  pthread_create(&pthread_id, &attribute, run, (void *)this);
34 #else
35  std_thread = std::thread(&thread::run, this);
36 #endif
37 }
38 
40 {
41  if (!joined_) {
42  join();
43  }
44 }
45 
46 void *thread::run(void *arg)
47 {
48  thread *self = (thread *)(arg);
49  if (self->node_ != -1) {
51  }
52  self->run_cb_();
53  return NULL;
54 }
55 
57 {
58  joined_ = true;
59 #ifdef __APPLE__
60  return pthread_join(pthread_id, NULL) == 0;
61 #else
62  try {
63  std_thread.join();
64  return true;
65  }
66  catch (const std::system_error &) {
67  return false;
68  }
69 #endif
70 }
71 
PyObject * self
Definition: bpy_driver.c:185
static void * run(void *arg)
Definition: util_thread.cpp:46
bool joined_
Definition: util_thread.h:64
thread(function< void()> run_cb, int node=-1)
Definition: util_thread.cpp:24
bool join()
Definition: util_thread.cpp:56
std::thread std_thread
Definition: util_thread.h:62
OperationNode * node
#define CCL_NAMESPACE_END
bool system_cpu_run_thread_on_node(int node)