Blender V5.0
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#pragma once
6
7#include <condition_variable>
8#include <functional>
9#include <mutex>
10
11#if defined(__APPLE__) || defined(__linux__) && !defined(__GLIBC__)
12# include <pthread.h>
13#else
14# include <thread>
15#endif
16
17#ifdef _WIN32
18# include "util/windows.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
26
27using thread_mutex = std::mutex;
28using thread_scoped_lock = std::unique_lock<std::mutex>;
29using thread_condition_variable = std::condition_variable;
30
35class thread {
36 public:
37 thread(std::function<void()> run_cb);
38 ~thread();
39
40 static void *run(void *arg);
41 bool join();
42
43 protected:
44 std::function<void()> run_cb_;
45#if defined(__APPLE__) || defined(__linux__) && !defined(__GLIBC__)
46 pthread_t pthread_id;
47#else
48 std::thread std_thread;
49#endif
50 bool joined_;
51};
52
53using thread_spin_lock = tbb::spin_mutex;
54
56 public:
58 {
59 lock_.lock();
60 }
61
63 {
64 lock_.unlock();
65 }
66
67 /* TODO(sergey): Implement manual control over lock/unlock. */
68
69 protected:
71};
72
volatile int lock
thread_spin_lock & lock_
Definition thread.h:70
thread_scoped_spin_lock(thread_spin_lock &lock)
Definition thread.h:57
~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
std::mutex thread_mutex
Definition thread.h:27
std::condition_variable thread_condition_variable
Definition thread.h:29
tbb::spin_mutex thread_spin_lock
Definition thread.h:53
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28