|
| 1 | +//===--- Implementation of a Darwin mutex class ------------------*- C++ |
| 2 | +//-*-===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_DARWIN_MUTEX_H |
| 11 | +#define LLVM_LIBC_SRC___SUPPORT_THREADS_DARWIN_MUTEX_H |
| 12 | + |
| 13 | +#include "src/__support/libc_assert.h" |
| 14 | +#include "src/__support/macros/config.h" |
| 15 | +#include "src/__support/threads/mutex_common.h" |
| 16 | +#include "src/__support/threads/sleep.h" // For sleep_briefly |
| 17 | +#include "src/__support/time/linux/abs_timeout.h" |
| 18 | + |
| 19 | +#include <mach/mach_init.h> // For mach_thread_self |
| 20 | +#include <mach/mach_port.h> // For mach_port_t and MACH_PORT_NULL |
| 21 | +#include <os/lock.h> // For os_unfair_lock |
| 22 | +#include <time.h> // For clock_gettime |
| 23 | + |
| 24 | +namespace LIBC_NAMESPACE_DECL { |
| 25 | + |
| 26 | +// This file is an implementation of `LIBC_NAMESPACE::mutex` for Darwin-based |
| 27 | +// platforms. It is a wrapper around `os_unfair_lock`, which is a low-level, |
| 28 | +// high-performance locking primitive provided by the kernel. |
| 29 | +// |
| 30 | +// `os_unfair_lock` is a non-recursive, thread-owned lock that blocks waiters |
| 31 | +// efficiently in the kernel. As the name implies, it is "unfair," meaning |
| 32 | +// it does not guarantee the order in which waiting threads acquire the lock. |
| 33 | +// This trade-off allows for higher performance in contended scenarios. |
| 34 | +// |
| 35 | +// The lock must be unlocked from the same thread that locked it. Attempting |
| 36 | +// to unlock from a different thread will result in a runtime error. |
| 37 | +// |
| 38 | +// This implementation is suitable for simple critical sections where fairness |
| 39 | +// and reentrancy are not concerns. |
| 40 | + |
| 41 | +class Mutex final { |
| 42 | + os_unfair_lock_s lock_val = OS_UNFAIR_LOCK_INIT; |
| 43 | + mach_port_t owner = MACH_PORT_NULL; |
| 44 | + |
| 45 | + // API compatibility fields. |
| 46 | + unsigned char timed; |
| 47 | + unsigned char recursive; |
| 48 | + unsigned char robust; |
| 49 | + unsigned char pshared; |
| 50 | + |
| 51 | +public: |
| 52 | + LIBC_INLINE constexpr Mutex(bool is_timed, bool is_recursive, bool is_robust, |
| 53 | + bool is_pshared) |
| 54 | + : owner(MACH_PORT_NULL), timed(is_timed), recursive(is_recursive), |
| 55 | + robust(is_robust), pshared(is_pshared) {} |
| 56 | + |
| 57 | + LIBC_INLINE constexpr Mutex() |
| 58 | + : owner(MACH_PORT_NULL), timed(0), recursive(0), robust(0), pshared(0) {} |
| 59 | + |
| 60 | + LIBC_INLINE static MutexError init(Mutex *mutex, bool is_timed, bool is_recur, |
| 61 | + bool is_robust, bool is_pshared) { |
| 62 | + mutex->lock_val = OS_UNFAIR_LOCK_INIT; |
| 63 | + mutex->owner = MACH_PORT_NULL; |
| 64 | + mutex->timed = is_timed; |
| 65 | + mutex->recursive = is_recur; |
| 66 | + mutex->robust = is_robust; |
| 67 | + mutex->pshared = is_pshared; |
| 68 | + return MutexError::NONE; |
| 69 | + } |
| 70 | + |
| 71 | + LIBC_INLINE static MutexError destroy(Mutex *lock) { |
| 72 | + LIBC_ASSERT(lock->owner == MACH_PORT_NULL && |
| 73 | + "Mutex destroyed while locked."); |
| 74 | + return MutexError::NONE; |
| 75 | + } |
| 76 | + |
| 77 | + LIBC_INLINE MutexError lock() { |
| 78 | + os_unfair_lock_lock(&lock_val); |
| 79 | + owner = mach_thread_self(); |
| 80 | + return MutexError::NONE; |
| 81 | + } |
| 82 | + |
| 83 | + LIBC_INLINE MutexError timed_lock(internal::AbsTimeout abs_time) { |
| 84 | + while (true) { |
| 85 | + if (try_lock() == MutexError::NONE) { |
| 86 | + return MutexError::NONE; |
| 87 | + } |
| 88 | + |
| 89 | + // Manually check if the timeout has expired. |
| 90 | + struct timespec now; |
| 91 | + // The clock used here must match the clock used to create the |
| 92 | + // absolute timeout. |
| 93 | + clock_gettime(abs_time.is_realtime() ? CLOCK_REALTIME : CLOCK_MONOTONIC, |
| 94 | + &now); |
| 95 | + const timespec &target_ts = abs_time.get_timespec(); |
| 96 | + |
| 97 | + if (now.tv_sec > target_ts.tv_sec || (now.tv_sec == target_ts.tv_sec && |
| 98 | + now.tv_nsec >= target_ts.tv_nsec)) { |
| 99 | + // We might have acquired the lock between the last try_lock() and now. |
| 100 | + // To avoid returning TIMEOUT incorrectly, we do one last try_lock(). |
| 101 | + if (try_lock() == MutexError::NONE) |
| 102 | + return MutexError::NONE; |
| 103 | + return MutexError::TIMEOUT; |
| 104 | + } |
| 105 | + |
| 106 | + sleep_briefly(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + LIBC_INLINE MutexError unlock() { |
| 111 | + // This check is crucial. It prevents both double-unlocks and unlocks |
| 112 | + // by threads that do not own the mutex. |
| 113 | + if (owner != mach_thread_self()) { |
| 114 | + return MutexError::UNLOCK_WITHOUT_LOCK; |
| 115 | + } |
| 116 | + owner = MACH_PORT_NULL; |
| 117 | + os_unfair_lock_unlock(&lock_val); |
| 118 | + return MutexError::NONE; |
| 119 | + } |
| 120 | + |
| 121 | + LIBC_INLINE MutexError try_lock() { |
| 122 | + if (os_unfair_lock_trylock(&lock_val)) { |
| 123 | + owner = mach_thread_self(); |
| 124 | + return MutexError::NONE; |
| 125 | + } |
| 126 | + return MutexError::BUSY; |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +} // namespace LIBC_NAMESPACE_DECL |
| 131 | + |
| 132 | +#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_DARWIN_MUTEX_H |
0 commit comments