[Bf-blender-cvs] [0e1ee29f777] master: BLI: Use TBB spin_mutex as SpinLock implementation
Sergey Sharybin
noreply at git.blender.org
Fri Jul 3 11:15:04 CEST 2020
Commit: 0e1ee29f777dbcd7f74a4d99690f8d29022b8091
Author: Sergey Sharybin
Date: Thu Jul 2 16:40:30 2020 +0200
Branches: master
https://developer.blender.org/rB0e1ee29f777dbcd7f74a4d99690f8d29022b8091
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
===================================================================
M source/blender/blenlib/BLI_threads.h
M source/blender/blenlib/intern/threads.cc
===================================================================
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 */
More information about the Bf-blender-cvs
mailing list