BLI: Use TBB spin_mutex as SpinLock implementation

When building without TBB use native to the platform spin lock
implementation. For Windows it is an atomic-based busy-wait,
for Linux it is pthreads' spin lock.

For macOS it is a mutex lock. The reason behind this is to stop
using atomics library which has been declared deprecated in SDK
version 10.12. So this changes fixes a lot of noisy warnings on
the newer SDK.

Differential Revision: https://developer.blender.org/D8180
This commit is contained in:
Sergey Sharybin
2020-07-02 16:40:30 +02:00
parent 0f4049db5f
commit 0e1ee29f77
2 changed files with 49 additions and 18 deletions

View File

@@ -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

View File

@@ -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 */