Switch back to native mutexes on macOS
It seems native mutex performance has improved considerably on Mac
lately, primarily by switching to a different default scheduling
policy. For safety, set this policy explicitly.
The special implementation previously used on Mac is still faster but
suffers a problem when used on realtime audio threads, where they will
not get rescheduled as quickly as when using native mutexes.
Bug: webrtc:10373
Change-Id: Iabf97afc5c2609096331bba0199f433fd26b68b2
Reviewed-on: https://webrtc-review.googlesource.com/c/125186
Commit-Queue: Oskar Sundbom <ossu@webrtc.org>
Reviewed-by: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26948}
diff --git a/rtc_base/critical_section.cc b/rtc_base/critical_section.cc
index 72a238f..2100fe9 100644
--- a/rtc_base/critical_section.cc
+++ b/rtc_base/critical_section.cc
@@ -34,6 +34,10 @@
pthread_mutexattr_t mutex_attribute;
pthread_mutexattr_init(&mutex_attribute);
pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE);
+#if defined(WEBRTC_MAC)
+ pthread_mutexattr_setpolicy_np(&mutex_attribute,
+ _PTHREAD_MUTEX_POLICY_FAIRSHARE);
+#endif
pthread_mutex_init(&mutex_, &mutex_attribute);
pthread_mutexattr_destroy(&mutex_attribute);
#endif
diff --git a/rtc_base/critical_section.h b/rtc_base/critical_section.h
index a81fa0b..d575b97 100644
--- a/rtc_base/critical_section.h
+++ b/rtc_base/critical_section.h
@@ -34,7 +34,7 @@
#endif
// See notes in the 'Performance' unit test for the effects of this flag.
-#define USE_NATIVE_MUTEX_ON_MAC 0
+#define USE_NATIVE_MUTEX_ON_MAC 1
#if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
#include <dispatch/dispatch.h>
diff --git a/rtc_base/critical_section_unittest.cc b/rtc_base/critical_section_unittest.cc
index e988046..5405c61 100644
--- a/rtc_base/critical_section_unittest.cc
+++ b/rtc_base/critical_section_unittest.cc
@@ -372,25 +372,33 @@
int my_id_ = 0;
};
-// Comparison of output of this test as tested on a MacBook Pro Retina, 15-inch,
-// Mid 2014, 2,8 GHz Intel Core i7, 16 GB 1600 MHz DDR3,
-// running OS X El Capitan, 10.11.2.
+// Comparison of output of this test as tested on a MacBook Pro, 13-inch,
+// 2017, 3,5 GHz Intel Core i7, 16 GB 2133 MHz LPDDR3,
+// running macOS Mojave, 10.14.3.
//
-// Native mutex implementation:
+// Native mutex implementation using fair policy (previously macOS default):
// Approximate CPU usage:
-// System: ~16%
-// User mode: ~1.3%
-// Idle: ~82%
+// real 4m54.612s
+// user 1m20.575s
+// sys 3m48.872s
// Unit test output:
-// [ OK ] CriticalSectionTest.Performance (234545 ms)
+// [ OK ] CriticalSectionTest.Performance (294375 ms)
+//
+// Native mutex implementation using first fit policy (current macOS default):
+// Approximate CPU usage:
+// real 0m11.535s
+// user 0m12.738s
+// sys 0m31.207s
+// Unit test output:
+// [ OK ] CriticalSectionTest.Performance (11444 ms)
//
// Special partially spin lock based implementation:
// Approximate CPU usage:
-// System: ~75%
-// User mode: ~16%
-// Idle: ~8%
+// real 0m2.113s
+// user 0m3.014s
+// sys 0m4.495s
// Unit test output:
-// [ OK ] CriticalSectionTest.Performance (2107 ms)
+// [ OK ] CriticalSectionTest.Performance (1885 ms)
//
// The test is disabled by default to avoid unecessarily loading the bots.
TEST(CriticalSectionTest, DISABLED_Performance) {
diff --git a/rtc_base/platform_thread_types.h b/rtc_base/platform_thread_types.h
index 0bc42eb..6b9101e 100644
--- a/rtc_base/platform_thread_types.h
+++ b/rtc_base/platform_thread_types.h
@@ -25,6 +25,9 @@
#elif defined(WEBRTC_POSIX)
#include <pthread.h>
#include <unistd.h>
+#if defined(WEBRTC_MAC)
+#include <pthread_spis.h>
+#endif
#endif
// clang-format on