Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Sharybin <sergey.vfx@gmail.com>2020-07-02 17:40:30 +0300
committerSergey Sharybin <sergey.vfx@gmail.com>2020-07-03 12:14:17 +0300
commit0e1ee29f777dbcd7f74a4d99690f8d29022b8091 (patch)
tree4cc2728c21f972c9dec1875baf8ada3aef32022c
parent0f4049db5f3c6be582d878e2ae1e6fd12dcad6bf (diff)
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
-rw-r--r--source/blender/blenlib/BLI_threads.h16
-rw-r--r--source/blender/blenlib/intern/threads.cc51
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 */