[libc-commits] [libc] [libc] Add Darwin mutex support via os_sync primitives (PR #167722)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Sun Nov 30 18:37:42 PST 2025


================
@@ -0,0 +1,78 @@
+//===--- Futex utils for Darwin ------------------------*- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_DARWIN_FUTEX_UTILS_H
+#define LLVM_LIBC_SRC___SUPPORT_THREADS_DARWIN_FUTEX_UTILS_H
+
+#include "src/__support/CPP/atomic.h"
+#include "src/__support/CPP/optional.h"
+#include "src/__support/time/abs_timeout.h"
+#include "src/__support/time/clock_conversion.h"
+
+#include <os/os_sync_wait_on_address.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+using FutexWordType = uint32_t;
+
+struct Futex : public cpp::Atomic<FutexWordType> {
+  using cpp::Atomic<FutexWordType>::Atomic;
+  using Timeout = internal::AbsTimeout;
+
+  LIBC_INLINE long wait(FutexWordType val, cpp::optional<Timeout> timeout,
+                        bool /* is_shared */) {
+    // TODO(bojle): consider using OS_SYNC_WAIT_ON_ADDRESS_SHARED to sync
----------------
SchrodingerZhu wrote:

So the document says:

> Use this flag when you pass an address allocated in shared memory to any futex wait function. Shared memory can be used for waits and wakes within a single process, but incur a performance hit.

This extra cost is expected and it is the reason why POSIX API also distinguish shared memory locks and normal private locks. 

https://github.com/llvm/llvm-project/pull/167722


More information about the libc-commits mailing list