Skip to content

Commit

Permalink
fixup mac os
Browse files Browse the repository at this point in the history
  • Loading branch information
mkornaukhov03 committed Dec 26, 2024
1 parent 63f6551 commit 725f1a0
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions compiler/threading/locks.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@
#include <cassert>
#include <sys/syscall.h>
#include <unistd.h>

#ifndef __APPLE__
#include <linux/futex.h>
#endif

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

// This Mutex can is copyable, std::mutex is not
class CustomMutex {
public:
void Lock() {
#ifdef __APPLE__
int old = kFree;

while (!state_.compare_exchange_strong(old, kLockedWithWaiters)) {
usleep(250);
old = kFree;
}
#else
int old = kFree;
if (state_.compare_exchange_strong(old, kLockedNoWaiters)) {
return;
Expand All @@ -27,13 +38,18 @@ class CustomMutex {
syscall(SYS_futex, &state_, FUTEX_WAIT, kLockedWithWaiters, 0, 0, 0);
old = state_.exchange(kLockedWithWaiters);
}
#endif
}

void Unlock() {
#ifdef __APPLE__
state_.store(kFree);
#else
if (state_.fetch_sub(1) == kLockedWithWaiters) {
state_.store(kFree);
syscall(SYS_futex, &state_, FUTEX_WAKE, 1, 0, 0, 0); // wake one
}
#endif
}

// https://en.cppreference.com/w/cpp/named_req/BasicLockable
Expand Down

0 comments on commit 725f1a0

Please sign in to comment.