futex.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #if BOOST_OS_LINUX
  11. extern "C" {
  12. #include <linux/futex.h>
  13. #include <sys/syscall.h>
  14. }
  15. #elif BOOST_OS_WINDOWS
  16. #include <windows.h>
  17. #endif
  18. namespace boost {
  19. namespace fibers {
  20. namespace detail {
  21. #if BOOST_OS_LINUX
  22. BOOST_FORCEINLINE
  23. int sys_futex( void * addr, std::int32_t op, std::int32_t x) {
  24. return ::syscall( SYS_futex, addr, op, x, nullptr, nullptr, 0);
  25. }
  26. BOOST_FORCEINLINE
  27. int futex_wake( std::atomic< std::int32_t > * addr) {
  28. return 0 <= sys_futex( static_cast< void * >( addr), FUTEX_WAKE_PRIVATE, 1) ? 0 : -1;
  29. }
  30. BOOST_FORCEINLINE
  31. int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) {
  32. return 0 <= sys_futex( static_cast< void * >( addr), FUTEX_WAIT_PRIVATE, x) ? 0 : -1;
  33. }
  34. #elif BOOST_OS_WINDOWS
  35. BOOST_FORCEINLINE
  36. int futex_wake( std::atomic< std::int32_t > * addr) {
  37. ::WakeByAddressSingle( static_cast< void * >( addr) );
  38. return 0;
  39. }
  40. BOOST_FORCEINLINE
  41. int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) {
  42. ::WaitOnAddress( static_cast< volatile void * >( addr), & x, sizeof( x), INFINITE);
  43. return 0;
  44. }
  45. #else
  46. # warn "no futex support on this platform"
  47. #endif
  48. }}}
  49. #endif // BOOST_FIBERS_DETAIL_FUTEX_H