[compiler-rt] 71ef543 - [darwin] switch blocking mutex from osspinlock to os_unfair_lock

Emily Shi via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 1 10:52:53 PST 2021


Author: Emily Shi
Date: 2021-03-01T10:52:47-08:00
New Revision: 71ef54337d5e83c6767e4816227ef340caa337fc

URL: https://github.com/llvm/llvm-project/commit/71ef54337d5e83c6767e4816227ef340caa337fc
DIFF: https://github.com/llvm/llvm-project/commit/71ef54337d5e83c6767e4816227ef340caa337fc.diff

LOG: [darwin] switch blocking mutex from osspinlock to os_unfair_lock

OSSpinLock is deprecated, so we are switching to `os_unfair_lock`. However, `os_unfair_lock` isn't available on older OSs, so we keep `OSSpinLock` as fallback.

Also change runtime assumption check to static since they only ever check constant values.

rdar://69588111

Reviewed By: delcypher, yln

Differential Revision: https://reviews.llvm.org/D97509

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
index b0d7bcc645fa..f7c216a90acd 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
@@ -70,6 +70,7 @@ extern "C" {
 #include <mach/mach_time.h>
 #include <mach/vm_statistics.h>
 #include <malloc/malloc.h>
+#include <os/lock.h>
 #include <os/log.h>
 #include <pthread.h>
 #include <sched.h>
@@ -506,22 +507,42 @@ void MprotectMallocZones(void *addr, int prot) {
 }
 
 BlockingMutex::BlockingMutex() {
+  // Initialize all member variables to 0
   internal_memset(this, 0, sizeof(*this));
+  static_assert(sizeof(os_unfair_lock_t) <= sizeof(opaque_storage_),
+                "Not enough space in opaque storage to use os_unfair_lock");
+  static_assert(sizeof(OSSpinLock) <= sizeof(opaque_storage_),
+                "Not enough space in opaque storage to use OSSpinLock");
 }
 
+static bool UnfairLockAvailable() { return bool(os_unfair_lock_lock); }
+static_assert(OS_UNFAIR_LOCK_INIT._os_unfair_lock_opaque == 0,
+              "os_unfair_lock does not initialize to 0");
+static_assert(OS_SPINLOCK_INIT == 0, "OSSpinLock does not initialize to 0");
+
 void BlockingMutex::Lock() {
-  CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
-  CHECK_EQ(OS_SPINLOCK_INIT, 0);
   CHECK_EQ(owner_, 0);
-  OSSpinLockLock((OSSpinLock*)&opaque_storage_);
+  if (UnfairLockAvailable()) {
+    os_unfair_lock_lock((os_unfair_lock *)&opaque_storage_);
+  } else {
+    OSSpinLockLock((OSSpinLock *)&opaque_storage_);
+  }
 }
 
 void BlockingMutex::Unlock() {
-  OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
+  if (UnfairLockAvailable()) {
+    os_unfair_lock_unlock((os_unfair_lock *)&opaque_storage_);
+  } else {
+    OSSpinLockUnlock((OSSpinLock *)&opaque_storage_);
+  }
 }
 
 void BlockingMutex::CheckLocked() {
-  CHECK_NE(*(OSSpinLock*)&opaque_storage_, 0);
+  if (UnfairLockAvailable()) {
+    CHECK_NE((*(os_unfair_lock *)&opaque_storage_)._os_unfair_lock_opaque, 0);
+  } else {
+    CHECK_NE(*(OSSpinLock *)&opaque_storage_, 0);
+  }
 }
 
 u64 NanoTime() {


        


More information about the llvm-commits mailing list