Blender  V3.3
thread.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __UTIL_THREAD_H__
5 #define __UTIL_THREAD_H__
6 
7 #include <condition_variable>
8 #include <functional>
9 #include <mutex>
10 #include <queue>
11 #include <thread>
12 
13 #ifdef _WIN32
14 # include "util/windows.h"
15 #else
16 # include <pthread.h>
17 #endif
18 
19 /* NOTE: Use tbb/spin_mutex.h instead of util_tbb.h because some of the TBB
20  * functionality requires RTTI, which is disabled for OSL kernel. */
21 #include <tbb/spin_mutex.h>
22 
23 #include "util/function.h"
24 
26 
28 typedef std::unique_lock<std::mutex> thread_scoped_lock;
29 typedef std::condition_variable thread_condition_variable;
30 
31 /* Own thread implementation similar to std::thread, so we can set a
32  * custom stack size on macOS. */
33 
34 class thread {
35  public:
36  thread(function<void()> run_cb);
37  ~thread();
38 
39  static void *run(void *arg);
40  bool join();
41 
42  protected:
43  function<void()> run_cb_;
44 #ifdef __APPLE__
45  pthread_t pthread_id;
46 #else
47  std::thread std_thread;
48 #endif
49  bool joined_;
50 };
51 
52 using thread_spin_lock = tbb::spin_mutex;
53 
55  public:
57  {
58  lock_.lock();
59  }
60 
62  {
63  lock_.unlock();
64  }
65 
66  /* TODO(sergey): Implement manual control over lock/unlock. */
67 
68  protected:
70 };
71 
73 
74 #endif /* __UTIL_THREAD_H__ */
ThreadMutex mutex
volatile int lock
thread_spin_lock & lock_
Definition: thread.h:69
thread_scoped_spin_lock(thread_spin_lock &lock)
Definition: thread.h:56
Definition: thread.h:34
~thread()
Definition: thread.cpp:28
static void * run(void *arg)
Definition: thread.cpp:35
bool joined_
Definition: thread.h:49
bool join()
Definition: thread.cpp:42
function< void()> run_cb_
Definition: thread.h:43
std::thread std_thread
Definition: thread.h:47
thread(function< void()> run_cb)
Definition: thread.cpp:13
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
SyclQueue void void size_t num_bytes void
std::unique_lock< std::mutex > thread_scoped_lock
Definition: thread.h:28
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: thread.h:27
tbb::spin_mutex thread_spin_lock
Definition: thread.h:52
std::condition_variable thread_condition_variable
Definition: thread.h:29