Skip to content

Commit 725f1a0

Browse files
committed
fixup mac os
1 parent 63f6551 commit 725f1a0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

compiler/threading/locks.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@
77
#include <cassert>
88
#include <sys/syscall.h>
99
#include <unistd.h>
10+
11+
#ifndef __APPLE__
1012
#include <linux/futex.h>
13+
#endif
1114

1215
#include "common/wrappers/copyable-atomic.h"
1316

1417
// This Mutex can is copyable, std::mutex is not
1518
class CustomMutex {
1619
public:
1720
void Lock() {
21+
#ifdef __APPLE__
22+
int old = kFree;
23+
24+
while (!state_.compare_exchange_strong(old, kLockedWithWaiters)) {
25+
usleep(250);
26+
old = kFree;
27+
}
28+
#else
1829
int old = kFree;
1930
if (state_.compare_exchange_strong(old, kLockedNoWaiters)) {
2031
return;
@@ -27,13 +38,18 @@ class CustomMutex {
2738
syscall(SYS_futex, &state_, FUTEX_WAIT, kLockedWithWaiters, 0, 0, 0);
2839
old = state_.exchange(kLockedWithWaiters);
2940
}
41+
#endif
3042
}
3143

3244
void Unlock() {
45+
#ifdef __APPLE__
46+
state_.store(kFree);
47+
#else
3348
if (state_.fetch_sub(1) == kLockedWithWaiters) {
3449
state_.store(kFree);
3550
syscall(SYS_futex, &state_, FUTEX_WAKE, 1, 0, 0, 0); // wake one
3651
}
52+
#endif
3753
}
3854

3955
// https://en.cppreference.com/w/cpp/named_req/BasicLockable

0 commit comments

Comments
 (0)