From 8165da581c45405655195bf536ed554276ad4dda Mon Sep 17 00:00:00 2001 From: Helena Pankov Date: Sun, 27 Apr 2025 10:59:36 -0700 Subject: [PATCH] Enable futex support in macOS 14.4+ through os_sync_wait_on_address --- include/boost/fiber/detail/config.hpp | 11 +++++++++-- include/boost/fiber/detail/futex.hpp | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/boost/fiber/detail/config.hpp b/include/boost/fiber/detail/config.hpp index b29102b4..69b2292c 100644 --- a/include/boost/fiber/detail/config.hpp +++ b/include/boost/fiber/detail/config.hpp @@ -10,9 +10,15 @@ #include #include -#include +#include #include +#if BOOST_OS_MACOS +#include +#define BOOST_OS_SYNC_WAIT_ON_ADDRESS_AVAILABLE \ + __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_VERSION_14_4 +#endif + #ifdef BOOST_FIBERS_DECL # undef BOOST_FIBERS_DECL #endif @@ -38,7 +44,8 @@ # include #endif -#if BOOST_OS_LINUX || BOOST_OS_BSD_OPEN || BOOST_OS_WINDOWS +#if BOOST_OS_LINUX || BOOST_OS_BSD_OPEN || BOOST_OS_WINDOWS || (BOOST_OS_MACOS && \ + BOOST_OS_SYNC_WAIT_ON_ADDRESS_AVAILABLE) # define BOOST_FIBERS_HAS_FUTEX #endif diff --git a/include/boost/fiber/detail/futex.hpp b/include/boost/fiber/detail/futex.hpp index daf75646..ad864d52 100644 --- a/include/boost/fiber/detail/futex.hpp +++ b/include/boost/fiber/detail/futex.hpp @@ -8,7 +8,7 @@ #define BOOST_FIBERS_DETAIL_FUTEX_H #include -#include +#include #include @@ -27,6 +27,8 @@ extern "C" { } #elif BOOST_OS_WINDOWS #include +#elif (BOOST_OS_MACOS && BOOST_OS_SYNC_WAIT_ON_ADDRESS_AVAILABLE) +#include #endif namespace boost { @@ -71,6 +73,17 @@ int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) { ::WaitOnAddress( static_cast< volatile void * >( addr), & x, sizeof( x), INFINITE); return 0; } +#elif (BOOST_OS_MACOS && BOOST_OS_SYNC_WAIT_ON_ADDRESS_AVAILABLE) +BOOST_FORCEINLINE +int futex_wake( std::atomic< std::int32_t > * addr) { + return 0 <= os_sync_wake_by_address_any(addr, 4, OS_SYNC_WAKE_BY_ADDRESS_NONE) ? 0 : -1; +} + +BOOST_FORCEINLINE +int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) { + return 0 <= os_sync_wait_on_address(static_cast< void * >( addr), x, sizeof(x), + OS_SYNC_WAIT_ON_ADDRESS_NONE) ? 0 : -1; +} #else # warn "no futex support on this platform" #endif