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