| 1 | |
| 2 | // Copyright Oliver Kowalke 2016. |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #ifndef BOOST_FIBERS_DETAIL_FUTEX_H |
| 8 | #define BOOST_FIBERS_DETAIL_FUTEX_H |
| 9 | |
| 10 | #include <boost/config.hpp> |
| 11 | #include <boost/predef.h> |
| 12 | |
| 13 | #include <boost/fiber/detail/config.hpp> |
| 14 | |
| 15 | #ifndef SYS_futex |
| 16 | #define SYS_futex SYS_futex_time64 |
| 17 | #endif |
| 18 | |
| 19 | #if BOOST_OS_LINUX |
| 20 | extern "C" { |
| 21 | #include <linux/futex.h> |
| 22 | #include <sys/syscall.h> |
| 23 | } |
| 24 | #elif BOOST_OS_WINDOWS |
| 25 | #include <windows.h> |
| 26 | #endif |
| 27 | |
| 28 | namespace boost { |
| 29 | namespace fibers { |
| 30 | namespace detail { |
| 31 | |
| 32 | #if BOOST_OS_LINUX |
| 33 | BOOST_FORCEINLINE |
| 34 | int sys_futex( void * addr, std::int32_t op, std::int32_t x) { |
| 35 | return ::syscall( SYS_futex, addr, op, x, nullptr, nullptr, 0); |
| 36 | } |
| 37 | |
| 38 | BOOST_FORCEINLINE |
| 39 | int futex_wake( std::atomic< std::int32_t > * addr) { |
| 40 | return 0 <= sys_futex( addr: static_cast< void * >( addr), FUTEX_WAKE_PRIVATE, x: 1) ? 0 : -1; |
| 41 | } |
| 42 | |
| 43 | BOOST_FORCEINLINE |
| 44 | int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) { |
| 45 | return 0 <= sys_futex( addr: static_cast< void * >( addr), FUTEX_WAIT_PRIVATE, x) ? 0 : -1; |
| 46 | } |
| 47 | #elif BOOST_OS_WINDOWS |
| 48 | BOOST_FORCEINLINE |
| 49 | int futex_wake( std::atomic< std::int32_t > * addr) { |
| 50 | ::WakeByAddressSingle( static_cast< void * >( addr) ); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | BOOST_FORCEINLINE |
| 55 | int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) { |
| 56 | ::WaitOnAddress( static_cast< volatile void * >( addr), & x, sizeof( x), INFINITE); |
| 57 | return 0; |
| 58 | } |
| 59 | #else |
| 60 | # warn "no futex support on this platform" |
| 61 | #endif |
| 62 | |
| 63 | }}} |
| 64 | |
| 65 | #endif // BOOST_FIBERS_DETAIL_FUTEX_H |
| 66 | |