Changeset View
Changeset View
Standalone View
Standalone View
intern/cycles/util/util_thread.h
| Show All 23 Lines | |||||
| #include <thread> | #include <thread> | ||||
| #ifdef _WIN32 | #ifdef _WIN32 | ||||
| # include "util_windows.h" | # include "util_windows.h" | ||||
| #else | #else | ||||
| # include <pthread.h> | # include <pthread.h> | ||||
| #endif | #endif | ||||
| #ifdef __APPLE__ | /* NOTE: Use tbb/spin_mutex.h instead of util_tbb.h because some of the TBB | ||||
| # include <libkern/OSAtomic.h> | * functionality requires RTTI, which is disabled for OSL kernel. */ | ||||
| #endif | #include <tbb/spin_mutex.h> | ||||
| #include "util/util_function.h" | #include "util/util_function.h" | ||||
| CCL_NAMESPACE_BEGIN | CCL_NAMESPACE_BEGIN | ||||
| typedef std::mutex thread_mutex; | typedef std::mutex thread_mutex; | ||||
| typedef std::unique_lock<std::mutex> thread_scoped_lock; | typedef std::unique_lock<std::mutex> thread_scoped_lock; | ||||
| typedef std::condition_variable thread_condition_variable; | typedef std::condition_variable thread_condition_variable; | ||||
| Show All 17 Lines | #ifdef __APPLE__ | ||||
| pthread_t pthread_id; | pthread_t pthread_id; | ||||
| #else | #else | ||||
| std::thread std_thread; | std::thread std_thread; | ||||
| #endif | #endif | ||||
| bool joined_; | bool joined_; | ||||
| int node_; | int node_; | ||||
| }; | }; | ||||
| /* Own wrapper around pthread's spin lock to make it's use easier. */ | using thread_spin_lock = tbb::spin_mutex; | ||||
| class thread_spin_lock { | |||||
| public: | |||||
| #ifdef __APPLE__ | |||||
| inline thread_spin_lock() | |||||
| { | |||||
| spin_ = OS_SPINLOCK_INIT; | |||||
| } | |||||
| inline void lock() | |||||
| { | |||||
| OSSpinLockLock(&spin_); | |||||
| } | |||||
| inline void unlock() | |||||
| { | |||||
| OSSpinLockUnlock(&spin_); | |||||
| } | |||||
| #elif defined(_WIN32) | |||||
| inline thread_spin_lock() | |||||
| { | |||||
| const DWORD SPIN_COUNT = 50000; | |||||
| InitializeCriticalSectionAndSpinCount(&cs_, SPIN_COUNT); | |||||
| } | |||||
| inline ~thread_spin_lock() | |||||
| { | |||||
| DeleteCriticalSection(&cs_); | |||||
| } | |||||
| inline void lock() | |||||
| { | |||||
| EnterCriticalSection(&cs_); | |||||
| } | |||||
| inline void unlock() | |||||
| { | |||||
| LeaveCriticalSection(&cs_); | |||||
| } | |||||
| #else | |||||
| inline thread_spin_lock() | |||||
| { | |||||
| pthread_spin_init(&spin_, 0); | |||||
| } | |||||
| inline ~thread_spin_lock() | |||||
| { | |||||
| pthread_spin_destroy(&spin_); | |||||
| } | |||||
| inline void lock() | |||||
| { | |||||
| pthread_spin_lock(&spin_); | |||||
| } | |||||
| inline void unlock() | |||||
| { | |||||
| pthread_spin_unlock(&spin_); | |||||
| } | |||||
| #endif | |||||
| protected: | |||||
| #ifdef __APPLE__ | |||||
| OSSpinLock spin_; | |||||
| #elif defined(_WIN32) | |||||
| CRITICAL_SECTION cs_; | |||||
| #else | |||||
| pthread_spinlock_t spin_; | |||||
| #endif | |||||
| }; | |||||
| class thread_scoped_spin_lock { | class thread_scoped_spin_lock { | ||||
| public: | public: | ||||
| explicit thread_scoped_spin_lock(thread_spin_lock &lock) : lock_(lock) | explicit thread_scoped_spin_lock(thread_spin_lock &lock) : lock_(lock) | ||||
| { | { | ||||
| lock_.lock(); | lock_.lock(); | ||||
| } | } | ||||
| Show All 14 Lines | |||||