Blender  V2.93
util_thread.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 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 #ifndef __UTIL_THREAD_H__
18 #define __UTIL_THREAD_H__
19 
20 #include <condition_variable>
21 #include <functional>
22 #include <mutex>
23 #include <queue>
24 #include <thread>
25 
26 #ifdef _WIN32
27 # include "util_windows.h"
28 #else
29 # include <pthread.h>
30 #endif
31 
32 /* NOTE: Use tbb/spin_mutex.h instead of util_tbb.h because some of the TBB
33  * functionality requires RTTI, which is disabled for OSL kernel. */
34 #include <tbb/spin_mutex.h>
35 
36 #include "util/util_function.h"
37 
39 
41 typedef std::unique_lock<std::mutex> thread_scoped_lock;
42 typedef std::condition_variable thread_condition_variable;
43 
44 /* Own thread implementation similar to std::thread, so we can set a
45  * custom stack size on macOS. */
46 
47 class thread {
48  public:
49  /* NOTE: Node index of -1 means that affinity will be inherited from the
50  * parent thread and no override on top of that will happen. */
51  thread(function<void()> run_cb, int node = -1);
52  ~thread();
53 
54  static void *run(void *arg);
55  bool join();
56 
57  protected:
58  function<void()> run_cb_;
59 #ifdef __APPLE__
60  pthread_t pthread_id;
61 #else
62  std::thread std_thread;
63 #endif
64  bool joined_;
65  int node_;
66 };
67 
68 using thread_spin_lock = tbb::spin_mutex;
69 
71  public:
73  {
74  lock_.lock();
75  }
76 
78  {
79  lock_.unlock();
80  }
81 
82  /* TODO(sergey): Implement manual control over lock/unlock. */
83 
84  protected:
86 };
87 
89 
90 #endif /* __UTIL_THREAD_H__ */
ThreadMutex mutex
thread_spin_lock & lock_
Definition: util_thread.h:85
thread_scoped_spin_lock(thread_spin_lock &lock)
Definition: util_thread.h:72
int node_
Definition: util_thread.h:65
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
function< void()> run_cb_
Definition: util_thread.h:58
std::thread std_thread
Definition: util_thread.h:62
OperationNode * node
#define CCL_NAMESPACE_END
std::unique_lock< std::mutex > thread_scoped_lock
Definition: util_thread.h:41
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: util_thread.h:40
tbb::spin_mutex thread_spin_lock
Definition: util_thread.h:68
std::condition_variable thread_condition_variable
Definition: util_thread.h:42