Skip to content

Conversation

@bojle
Copy link
Contributor

@bojle bojle commented Nov 8, 2025

This patch adds a basic implementation/wrapper for mutex using the underlying
os_unfair_lock API that macos provides.

bojle added 14 commits November 2, 2025 16:38
ExecuteFunctionUnix.cpp which is guarded by this check should reliably work
on darwin as it only uses POSIX API - nothing specific to linux.
Signed-off-by: Shreeyash Pandey <[email protected]>
Signed-off-by: Shreeyash Pandey <[email protected]>
_Exit(3) is a fairly simple syscall wrapper whereas exit(3) calls
atexit-registered functions + whole lot of stuff that require support for sync
primitives.

Splitting the tests allows testing the former easily (especially for new port
projects)

Signed-off-by: Shreeyash Pandey <[email protected]>
Signed-off-by: Shreeyash Pandey <[email protected]>
Signed-off-by: Shreeyash Pandey <[email protected]>
This patch adds support for clock_gettime for Darwin. Darwin syscall
'gettimeofday' is used to query the time from the system.

Many headers in llvm-libc-types, namely clockid_t, struct_timespec,
struct_timeval, suseconds_t, time_t_32, time_t_64, are modified to include
header guards as Darwin has its own implementation of primitive types.
@bojle
Copy link
Contributor Author

bojle commented Nov 11, 2025

@SchrodingerZhu

This patch adds a basic implementation/wrapper for mutex using the underlying
os_unfair_lock API that macos provides.
@bojle bojle force-pushed the users/bojle/darwin_mutex branch from 06ec1fd to 6d31260 Compare November 11, 2025 14:27
@SchrodingerZhu
Copy link
Contributor

SchrodingerZhu commented Nov 11, 2025

Thank you for the patch, however, I have a major concern regarding it.

I think we should prefer

os_sync_wait_on_address
__ulock_wait

in this case:

  • wait on address API is typically more modern. rust's std library, for example, is using it.
  • it allows us to align futex-based code across platforms.
  • have futex directly allow us to use existing sync primitives, including both raw mutex and call_once.

This also avoid unaligned behavior across platforms:
For example, in your code, you still handle the realtime as it is. However, as per bionic libc's discussion, realtime is almost never the expected behavior of the mutex. Hence, even if the user use realtime, the underlying mutex will still convert it to monotonic time. If you use raw mutex, this practice is directly applied.

So, instead of specialize mutex, let's make raw mutex a common library for both linux and darwin, by providing all futex
definitions.

Also, using user space busy loop to implement timed lock is definitely not what we want.

There are existing API like the following if you use the futex approach:

os_sync_wait_on_address_with_timeout

@bojle
Copy link
Contributor Author

bojle commented Nov 11, 2025

Thank you for the detailed review. I agree with you here. I went with os_unfair_lock primarily for simplicity. I'll take your suggestions, and reimplement this with os_sync_wait.

@bojle bojle closed this Nov 12, 2025
@bojle bojle deleted the users/bojle/darwin_mutex branch November 12, 2025 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants