Changeset View
Changeset View
Standalone View
Standalone View
intern/cycles/util/util_thread.h
| Show All 18 Lines | |||||
| #if (__cplusplus > 199711L) || (defined(_MSC_VER) && _MSC_VER >= 1800) | #if (__cplusplus > 199711L) || (defined(_MSC_VER) && _MSC_VER >= 1800) | ||||
| # include <thread> | # include <thread> | ||||
| # include <mutex> | # include <mutex> | ||||
| # include <condition_variable> | # include <condition_variable> | ||||
| # include <functional> | # include <functional> | ||||
| #else | #else | ||||
| # include <boost/thread.hpp> | # include <boost/thread.hpp> | ||||
| #endif | |||||
| #include <pthread.h> | # include <pthread.h> | ||||
| #endif | |||||
| #include <queue> | #include <queue> | ||||
| #ifdef _WIN32 | |||||
| # include "util_windows.h" | |||||
| #else | |||||
| # include <pthread.h> | |||||
| #endif | |||||
| #ifdef __APPLE__ | #ifdef __APPLE__ | ||||
| # include <libkern/OSAtomic.h> | # include <libkern/OSAtomic.h> | ||||
| #endif | #endif | ||||
| #include "util/util_function.h" | #include "util/util_function.h" | ||||
| CCL_NAMESPACE_BEGIN | CCL_NAMESPACE_BEGIN | ||||
| Show All 16 Lines | public: | ||||
| thread(function<void(void)> run_cb, int group = -1); | thread(function<void(void)> run_cb, int group = -1); | ||||
| ~thread(); | ~thread(); | ||||
| static void *run(void *arg); | static void *run(void *arg); | ||||
| bool join(); | bool join(); | ||||
| protected: | protected: | ||||
| function<void(void)> run_cb_; | function<void(void)> run_cb_; | ||||
| #if (__cplusplus > 199711L) || (defined(_MSC_VER) && _MSC_VER >= 1800) | |||||
| std::thread thread_; | |||||
| #else | |||||
| pthread_t pthread_id_; | pthread_t pthread_id_; | ||||
| #endif | |||||
| bool joined_; | bool joined_; | ||||
| int group_; | int group_; | ||||
| }; | }; | ||||
| /* Own wrapper around pthread's spin lock to make it's use easier. */ | /* Own wrapper around pthread's spin lock to make it's use easier. */ | ||||
| class thread_spin_lock { | class thread_spin_lock { | ||||
| public: | public: | ||||
| #ifdef __APPLE__ | #ifdef __APPLE__ | ||||
| inline thread_spin_lock() { | inline thread_spin_lock() { | ||||
| spin_ = OS_SPINLOCK_INIT; | spin_ = OS_SPINLOCK_INIT; | ||||
| } | } | ||||
| inline void lock() { | inline void lock() { | ||||
| OSSpinLockLock(&spin_); | OSSpinLockLock(&spin_); | ||||
| } | } | ||||
| inline void unlock() { | inline void unlock() { | ||||
| OSSpinLockUnlock(&spin_); | OSSpinLockUnlock(&spin_); | ||||
| } | } | ||||
| #else /* __APPLE__ */ | #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() { | inline thread_spin_lock() { | ||||
| pthread_spin_init(&spin_, 0); | pthread_spin_init(&spin_, 0); | ||||
| } | } | ||||
| inline ~thread_spin_lock() { | inline ~thread_spin_lock() { | ||||
| pthread_spin_destroy(&spin_); | pthread_spin_destroy(&spin_); | ||||
| } | } | ||||
| inline void lock() { | inline void lock() { | ||||
| pthread_spin_lock(&spin_); | pthread_spin_lock(&spin_); | ||||
| } | } | ||||
| inline void unlock() { | inline void unlock() { | ||||
| pthread_spin_unlock(&spin_); | pthread_spin_unlock(&spin_); | ||||
| } | } | ||||
| #endif /* __APPLE__ */ | #endif | ||||
| protected: | protected: | ||||
| #ifdef __APPLE__ | #ifdef __APPLE__ | ||||
| OSSpinLock spin_; | OSSpinLock spin_; | ||||
| #elif defined(_WIN32) | |||||
| CRITICAL_SECTION cs_; | |||||
| #else | #else | ||||
| pthread_spinlock_t spin_; | pthread_spinlock_t spin_; | ||||
| #endif | #endif | ||||
| }; | }; | ||||
| class thread_scoped_spin_lock { | class thread_scoped_spin_lock { | ||||
| public: | public: | ||||
| explicit thread_scoped_spin_lock(thread_spin_lock& lock) | explicit thread_scoped_spin_lock(thread_spin_lock& lock) | ||||
| Show All 18 Lines | |||||