Cycles: don't require pthreads as dependency on Windows.

Use C++11 threads when available, and native critical section on Windows.
Later on we can remove pthread code when C+11 becomes required.

Differential Revision: https://developer.blender.org/D3116
This commit is contained in:
Jeff Witthuhn 2018-03-28 11:31:51 +02:00 committed by Brecht Van Lommel
parent e49d66f22c
commit bc3a55c343
2 changed files with 46 additions and 3 deletions

@ -26,7 +26,11 @@ thread::thread(function<void(void)> run_cb, int group)
joined_(false),
group_(group)
{
#if (__cplusplus > 199711L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
thread_ = std::thread(&thread::run, this);
#else
pthread_create(&pthread_id_, NULL, run, (void*)this);
#endif
}
thread::~thread()
@ -60,7 +64,17 @@ void *thread::run(void *arg)
bool thread::join()
{
joined_ = true;
#if (__cplusplus > 199711L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
try {
thread_.join();
return true;
}
catch (const std::system_error&) {
return false;
}
#else
return pthread_join(pthread_id_, NULL) == 0;
#endif
}
CCL_NAMESPACE_END

@ -24,10 +24,16 @@
# include <functional>
#else
# include <boost/thread.hpp>
#endif
# include <pthread.h>
#endif
#include <queue>
#ifdef _WIN32
# include "util_windows.h"
#else
# include <pthread.h>
#endif
#ifdef __APPLE__
# include <libkern/OSAtomic.h>
#endif
@ -60,7 +66,11 @@ public:
protected:
function<void(void)> run_cb_;
#if (__cplusplus > 199711L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
std::thread thread_;
#else
pthread_t pthread_id_;
#endif
bool joined_;
int group_;
};
@ -81,7 +91,24 @@ public:
inline void unlock() {
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() {
pthread_spin_init(&spin_, 0);
}
@ -97,10 +124,12 @@ public:
inline void unlock() {
pthread_spin_unlock(&spin_);
}
#endif /* __APPLE__ */
#endif
protected:
#ifdef __APPLE__
OSSpinLock spin_;
#elif defined(_WIN32)
CRITICAL_SECTION cs_;
#else
pthread_spinlock_t spin_;
#endif