Skip to content

Commit 71ef543

Browse files
committed
[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
1 parent 8f9f7d0 commit 71ef543

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ extern "C" {
7070
#include <mach/mach_time.h>
7171
#include <mach/vm_statistics.h>
7272
#include <malloc/malloc.h>
73+
#include <os/lock.h>
7374
#include <os/log.h>
7475
#include <pthread.h>
7576
#include <sched.h>
@@ -506,22 +507,42 @@ void MprotectMallocZones(void *addr, int prot) {
506507
}
507508

508509
BlockingMutex::BlockingMutex() {
510+
// Initialize all member variables to 0
509511
internal_memset(this, 0, sizeof(*this));
512+
static_assert(sizeof(os_unfair_lock_t) <= sizeof(opaque_storage_),
513+
"Not enough space in opaque storage to use os_unfair_lock");
514+
static_assert(sizeof(OSSpinLock) <= sizeof(opaque_storage_),
515+
"Not enough space in opaque storage to use OSSpinLock");
510516
}
511517

518+
static bool UnfairLockAvailable() { return bool(os_unfair_lock_lock); }
519+
static_assert(OS_UNFAIR_LOCK_INIT._os_unfair_lock_opaque == 0,
520+
"os_unfair_lock does not initialize to 0");
521+
static_assert(OS_SPINLOCK_INIT == 0, "OSSpinLock does not initialize to 0");
522+
512523
void BlockingMutex::Lock() {
513-
CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
514-
CHECK_EQ(OS_SPINLOCK_INIT, 0);
515524
CHECK_EQ(owner_, 0);
516-
OSSpinLockLock((OSSpinLock*)&opaque_storage_);
525+
if (UnfairLockAvailable()) {
526+
os_unfair_lock_lock((os_unfair_lock *)&opaque_storage_);
527+
} else {
528+
OSSpinLockLock((OSSpinLock *)&opaque_storage_);
529+
}
517530
}
518531

519532
void BlockingMutex::Unlock() {
520-
OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
533+
if (UnfairLockAvailable()) {
534+
os_unfair_lock_unlock((os_unfair_lock *)&opaque_storage_);
535+
} else {
536+
OSSpinLockUnlock((OSSpinLock *)&opaque_storage_);
537+
}
521538
}
522539

523540
void BlockingMutex::CheckLocked() {
524-
CHECK_NE(*(OSSpinLock*)&opaque_storage_, 0);
541+
if (UnfairLockAvailable()) {
542+
CHECK_NE((*(os_unfair_lock *)&opaque_storage_)._os_unfair_lock_opaque, 0);
543+
} else {
544+
CHECK_NE(*(OSSpinLock *)&opaque_storage_, 0);
545+
}
525546
}
526547

527548
u64 NanoTime() {

0 commit comments

Comments
 (0)