File indexing completed on 2025-09-17 08:25:49
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_FIBERS_DETAIL_FUTEX_H
0008 #define BOOST_FIBERS_DETAIL_FUTEX_H
0009
0010 #include <boost/config.hpp>
0011 #include <boost/predef.h>
0012
0013 #include <boost/fiber/detail/config.hpp>
0014
0015 #ifndef SYS_futex
0016 #define SYS_futex SYS_futex_time64
0017 #endif
0018
0019 #if BOOST_OS_LINUX
0020 extern "C" {
0021 #include <linux/futex.h>
0022 #include <sys/syscall.h>
0023 }
0024 #elif BOOST_OS_BSD_OPEN
0025 extern "C" {
0026 #include <sys/futex.h>
0027 }
0028 #elif BOOST_OS_WINDOWS
0029 #include <windows.h>
0030 #endif
0031
0032 namespace boost {
0033 namespace fibers {
0034 namespace detail {
0035
0036 #if BOOST_OS_LINUX || BOOST_OS_BSD_OPEN
0037 BOOST_FORCEINLINE
0038 int sys_futex( void * addr, std::int32_t op, std::int32_t x) {
0039 #if BOOST_OS_BSD_OPEN
0040 return ::futex
0041 (
0042 static_cast< volatile uint32_t* >(addr),
0043 static_cast< int >(op),
0044 x,
0045 nullptr,
0046 nullptr
0047 );
0048 #else
0049 return ::syscall( SYS_futex, addr, op, x, nullptr, nullptr, 0);
0050 #endif
0051 }
0052
0053 BOOST_FORCEINLINE
0054 int futex_wake( std::atomic< std::int32_t > * addr) {
0055 return 0 <= sys_futex( static_cast< void * >( addr), FUTEX_WAKE_PRIVATE, 1) ? 0 : -1;
0056 }
0057
0058 BOOST_FORCEINLINE
0059 int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) {
0060 return 0 <= sys_futex( static_cast< void * >( addr), FUTEX_WAIT_PRIVATE, x) ? 0 : -1;
0061 }
0062 #elif BOOST_OS_WINDOWS
0063 BOOST_FORCEINLINE
0064 int futex_wake( std::atomic< std::int32_t > * addr) {
0065 ::WakeByAddressSingle( static_cast< void * >( addr) );
0066 return 0;
0067 }
0068
0069 BOOST_FORCEINLINE
0070 int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) {
0071 ::WaitOnAddress( static_cast< volatile void * >( addr), & x, sizeof( x), INFINITE);
0072 return 0;
0073 }
0074 #else
0075 # warn "no futex support on this platform"
0076 #endif
0077
0078 }}}
0079
0080 #endif