-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[libc] Add Darwin mutex support via os_sync primitives #167722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d898aa9
4c527b7
44f2e9c
f181323
3ab7356
ca31528
042c8cc
452595a
c1d717a
c1dd7c9
e09d8d4
45a3f4e
eae74a2
f40f929
d0a441a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| if(NOT TARGET libc.src.__support.OSUtil.osutil) | ||
| return() | ||
| endif() | ||
|
|
||
| add_header_library( | ||
| futex_utils | ||
| HDRS | ||
| futex_utils.h | ||
| DEPENDS | ||
| libc.include.sys_syscall | ||
| libc.src.__support.OSUtil.osutil | ||
| libc.src.__support.CPP.atomic | ||
| libc.src.__support.CPP.limits | ||
| libc.src.__support.CPP.optional | ||
| libc.src.__support.threads.mutex_common | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| //===--- 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 "src/__support/time/units.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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the document says:
This extra cost is expected and it is the reason why POSIX API also distinguish shared memory locks and normal private locks. |
||
| // betweeen processes. Catch: it is recommended to only be used by shared | ||
| // processes, not threads of a same process. | ||
|
|
||
| for (;;) { | ||
| if (this->load(cpp::MemoryOrder::RELAXED) != val) | ||
| return 0; | ||
| long ret = 0; | ||
| if (timeout) { | ||
| // Assuming, OS_CLOCK_MACH_ABSOLUTE_TIME is equivalent to CLOCK_REALTIME | ||
SchrodingerZhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using namespace time_units; | ||
| uint64_t tnsec = timeout->get_timespec().tv_sec * 1_s_ns + | ||
| timeout->get_timespec().tv_nsec; | ||
| ret = os_sync_wait_on_address_with_timeout( | ||
| reinterpret_cast<void *>(this), static_cast<uint64_t>(val), | ||
| sizeof(FutexWordType), OS_SYNC_WAIT_ON_ADDRESS_NONE, | ||
| OS_CLOCK_MACH_ABSOLUTE_TIME, tnsec); | ||
| } else { | ||
| ret = os_sync_wait_on_address( | ||
| reinterpret_cast<void *>(this), static_cast<uint64_t>(val), | ||
| sizeof(FutexWordType), OS_SYNC_WAIT_ON_ADDRESS_NONE); | ||
| } | ||
| if ((ret < 0) && (errno == ETIMEDOUT)) | ||
| return -ETIMEDOUT; | ||
bojle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // case when os_sync returns early with an error. retry. | ||
| if ((ret < 0) && ((errno == EINTR) || (errno == EFAULT))) { | ||
| continue; | ||
| } | ||
| return ret; | ||
| } | ||
| } | ||
|
|
||
| LIBC_INLINE long notify_one(bool /* is_shared */) { | ||
| // TODO(bojle): deal with is_shared | ||
| return os_sync_wake_by_address_any(reinterpret_cast<void *>(this), | ||
| sizeof(FutexWordType), | ||
| OS_SYNC_WAKE_BY_ADDRESS_NONE); | ||
| } | ||
|
|
||
| LIBC_INLINE long notify_all(bool /* is_shared */) { | ||
| // TODO(bojle): deal with is_shared | ||
| return os_sync_wake_by_address_all(reinterpret_cast<void *>(this), | ||
| sizeof(FutexWordType), | ||
| OS_SYNC_WAKE_BY_ADDRESS_NONE); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC___SUPPORT_THREADS_DARWIN_FUTEX_UTILS_H | ||
Uh oh!
There was an error while loading. Please reload this page.