diff options
-rw-r--r-- | source/blender/blenlib/BLI_threads.h | 16 | ||||
-rw-r--r-- | source/blender/blenlib/intern/threads.cc | 51 |
2 files changed, 49 insertions, 18 deletions
diff --git a/source/blender/blenlib/BLI_threads.h b/source/blender/blenlib/BLI_threads.h index 03fe27c10ed..6f810144a48 100644 --- a/source/blender/blenlib/BLI_threads.h +++ b/source/blender/blenlib/BLI_threads.h @@ -28,10 +28,6 @@ #include "BLI_sys_types.h" -#ifdef __APPLE__ -# include <libkern/OSAtomic.h> -#endif - #ifdef __cplusplus extern "C" { #endif @@ -100,8 +96,16 @@ void BLI_mutex_unlock(ThreadMutex *mutex); /* Spin Lock */ -#if defined(__APPLE__) -typedef OSSpinLock SpinLock; +/* By default we use TBB for spin lock on all platforms. When building without + * TBB fall-back to spin lock implementation which is native to the platform. + * + * On macOS we use mutex lock instead of spin since the spin lock has been + * deprecated in SDK 10.12 and is discouraged from use. */ + +#ifdef WITH_TBB +typedef uint32_t SpinLock; +#elif defined(__APPLE__) +typedef ThreadMutex SpinLock; #elif defined(_MSC_VER) typedef volatile int SpinLock; #else diff --git a/source/blender/blenlib/intern/threads.cc b/source/blender/blenlib/intern/threads.cc index c2adc432644..7acd9b071e1 100644 --- a/source/blender/blenlib/intern/threads.cc +++ b/source/blender/blenlib/intern/threads.cc @@ -47,6 +47,10 @@ # include <unistd.h> #endif +#ifdef WITH_TBB +# include <tbb/spin_mutex.h> +#endif + #include "atomic_ops.h" #include "numaapi.h" @@ -429,10 +433,24 @@ void BLI_mutex_free(ThreadMutex *mutex) /* Spin Locks */ +#if WITH_TBB +static tbb::spin_mutex *tbb_spin_mutex_cast(SpinLock *spin) +{ + static_assert(sizeof(SpinLock) >= sizeof(tbb::spin_mutex), + "SpinLock must match tbb::spin_mutex"); + static_assert(alignof(SpinLock) % alignof(tbb::spin_mutex) == 0, + "SpinLock must be aligned same as tbb::spin_mutex"); + return reinterpret_cast<tbb::spin_mutex *>(spin); +} +#endif + void BLI_spin_init(SpinLock *spin) { -#if defined(__APPLE__) - *spin = OS_SPINLOCK_INIT; +#ifdef WITH_TBB + tbb::spin_mutex *spin_mutex = tbb_spin_mutex_cast(spin); + new (spin_mutex) tbb::spin_mutex(); +#elif defined(__APPLE__) + BLI_mutex_init(spin); #elif defined(_MSC_VER) *spin = 0; #else @@ -442,8 +460,11 @@ void BLI_spin_init(SpinLock *spin) void BLI_spin_lock(SpinLock *spin) { -#if defined(__APPLE__) - OSSpinLockLock(spin); +#ifdef WITH_TBB + tbb::spin_mutex *spin_mutex = tbb_spin_mutex_cast(spin); + spin_mutex->lock(); +#elif defined(__APPLE__) + BLI_mutex_lock(spin); #elif defined(_MSC_VER) while (InterlockedExchangeAcquire(spin, 1)) { while (*spin) { @@ -458,8 +479,11 @@ void BLI_spin_lock(SpinLock *spin) void BLI_spin_unlock(SpinLock *spin) { -#if defined(__APPLE__) - OSSpinLockUnlock(spin); +#ifdef WITH_TBB + tbb::spin_mutex *spin_mutex = tbb_spin_mutex_cast(spin); + spin_mutex->unlock(); +#elif defined(__APPLE__) + BLI_mutex_unlock(spin); #elif defined(_MSC_VER) _ReadWriteBarrier(); *spin = 0; @@ -468,16 +492,19 @@ void BLI_spin_unlock(SpinLock *spin) #endif } -#if defined(__APPLE__) || defined(_MSC_VER) -void BLI_spin_end(SpinLock *UNUSED(spin)) -{ -} -#else void BLI_spin_end(SpinLock *spin) { +#ifdef WITH_TBB + tbb::spin_mutex *spin_mutex = tbb_spin_mutex_cast(spin); + spin_mutex->~spin_mutex(); +#elif defined(__APPLE__) + BLI_mutex_end(spin); +#elif defined(_MSC_VER) + BLI_mutex_unlock(spin); +#else pthread_spin_destroy(spin); -} #endif +} /* Read/Write Mutex Lock */ |