futex.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_BSD_OPEN
  19. extern "C" {
  20. #include <sys/futex.h>
  21. }
  22. #elif BOOST_OS_WINDOWS
  23. #include <windows.h>
  24. #endif
  25. namespace boost {
  26. namespace fibers {
  27. namespace detail {
  28. #if BOOST_OS_LINUX || BOOST_OS_BSD_OPEN
  29. BOOST_FORCEINLINE
  30. int sys_futex( void * addr, std::int32_t op, std::int32_t x) {
  31. #if BOOST_OS_BSD_OPEN
  32. return ::futex
  33. (
  34. static_cast< volatile uint32_t* >(addr),
  35. static_cast< int >(op),
  36. x,
  37. nullptr,
  38. nullptr
  39. );
  40. #else
  41. return ::syscall( SYS_futex, addr, op, x, nullptr, nullptr, 0);
  42. #endif
  43. }
  44. BOOST_FORCEINLINE
  45. int futex_wake( std::atomic< std::int32_t > * addr) {
  46. return 0 <= sys_futex( static_cast< void * >( addr), FUTEX_WAKE_PRIVATE, 1) ? 0 : -1;
  47. }
  48. BOOST_FORCEINLINE
  49. int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) {
  50. return 0 <= sys_futex( static_cast< void * >( addr), FUTEX_WAIT_PRIVATE, x) ? 0 : -1;
  51. }
  52. #elif BOOST_OS_WINDOWS
  53. BOOST_FORCEINLINE
  54. int futex_wake( std::atomic< std::int32_t > * addr) {
  55. ::WakeByAddressSingle( static_cast< void * >( addr) );
  56. return 0;
  57. }
  58. BOOST_FORCEINLINE
  59. int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) {
  60. ::WaitOnAddress( static_cast< volatile void * >( addr), & x, sizeof( x), INFINITE);
  61. return 0;
  62. }
  63. #else
  64. # warn "no futex support on this platform"
  65. #endif
  66. }}}
  67. #endif // BOOST_FIBERS_DETAIL_FUTEX_H