futex.hpp 1.6 KB

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