futex.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  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. *
  6. * Copyright (c) 2020 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/futex.hpp
  10. *
  11. * This header defines wrappers around futex syscall.
  12. *
  13. * http://man7.org/linux/man-pages/man2/futex.2.html
  14. * https://man.openbsd.org/futex
  15. */
  16. #ifndef BOOST_ATOMIC_DETAIL_FUTEX_HPP_INCLUDED_
  17. #define BOOST_ATOMIC_DETAIL_FUTEX_HPP_INCLUDED_
  18. #include <boost/atomic/detail/config.hpp>
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. #pragma once
  21. #endif
  22. #if defined(__linux__) || defined(__OpenBSD__) || defined(__NETBSD__) || defined(__NetBSD__)
  23. #include <sys/syscall.h>
  24. // Some Android NDKs (Google NDK and older Crystax.NET NDK versions) don't define SYS_futex.
  25. #if defined(SYS_futex)
  26. #define BOOST_ATOMIC_DETAIL_SYS_FUTEX SYS_futex
  27. #elif defined(__NR_futex)
  28. #define BOOST_ATOMIC_DETAIL_SYS_FUTEX __NR_futex
  29. #elif defined(SYS___futex)
  30. // NetBSD defines SYS___futex, which has slightly different parameters. Basically, it has decoupled timeout and val2 parameters:
  31. // int __futex(int *addr1, int op, int val1, const struct timespec *timeout, int *addr2, int val2, int val3);
  32. // https://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/sys/sys/syscall.h
  33. // http://bxr.su/NetBSD/sys/kern/sys_futex.c
  34. #define BOOST_ATOMIC_DETAIL_SYS_FUTEX SYS___futex
  35. #define BOOST_ATOMIC_DETAIL_NETBSD_FUTEX
  36. #endif
  37. #if defined(BOOST_ATOMIC_DETAIL_SYS_FUTEX)
  38. #include <cstddef>
  39. #if defined(__linux__)
  40. #include <linux/futex.h>
  41. #else
  42. #include <sys/futex.h>
  43. #endif
  44. #include <boost/atomic/detail/intptr.hpp>
  45. #include <boost/atomic/detail/header.hpp>
  46. #define BOOST_ATOMIC_DETAIL_HAS_FUTEX
  47. #if defined(FUTEX_PRIVATE_FLAG)
  48. #define BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG FUTEX_PRIVATE_FLAG
  49. #else
  50. #define BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG 0
  51. #endif
  52. namespace boost {
  53. namespace atomics {
  54. namespace detail {
  55. //! Invokes an operation on the futex
  56. BOOST_FORCEINLINE int futex_invoke(void* addr1, int op, unsigned int val1, const void* timeout = NULL, void* addr2 = NULL, unsigned int val3 = 0) BOOST_NOEXCEPT
  57. {
  58. #if !defined(BOOST_ATOMIC_DETAIL_NETBSD_FUTEX)
  59. return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, timeout, addr2, val3);
  60. #else
  61. // Pass 0 in val2.
  62. return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, timeout, addr2, 0u, val3);
  63. #endif
  64. }
  65. //! Invokes an operation on the futex
  66. BOOST_FORCEINLINE int futex_invoke(void* addr1, int op, unsigned int val1, unsigned int val2, void* addr2 = NULL, unsigned int val3 = 0) BOOST_NOEXCEPT
  67. {
  68. #if !defined(BOOST_ATOMIC_DETAIL_NETBSD_FUTEX)
  69. return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, static_cast< atomics::detail::uintptr_t >(val2), addr2, val3);
  70. #else
  71. // Pass NULL in timeout.
  72. return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, static_cast< void* >(NULL), addr2, val2, val3);
  73. #endif
  74. }
  75. //! Checks that the value \c pval is \c expected and blocks
  76. BOOST_FORCEINLINE int futex_wait(void* pval, unsigned int expected) BOOST_NOEXCEPT
  77. {
  78. return futex_invoke(pval, FUTEX_WAIT, expected);
  79. }
  80. //! Checks that the value \c pval is \c expected and blocks
  81. BOOST_FORCEINLINE int futex_wait_private(void* pval, unsigned int expected) BOOST_NOEXCEPT
  82. {
  83. return futex_invoke(pval, FUTEX_WAIT | BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG, expected);
  84. }
  85. //! Wakes the specified number of threads waiting on the futex
  86. BOOST_FORCEINLINE int futex_signal(void* pval, unsigned int count = 1u) BOOST_NOEXCEPT
  87. {
  88. return futex_invoke(pval, FUTEX_WAKE, count);
  89. }
  90. //! Wakes the specified number of threads waiting on the futex
  91. BOOST_FORCEINLINE int futex_signal_private(void* pval, unsigned int count = 1u) BOOST_NOEXCEPT
  92. {
  93. return futex_invoke(pval, FUTEX_WAKE | BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG, count);
  94. }
  95. //! Wakes all threads waiting on the futex
  96. BOOST_FORCEINLINE int futex_broadcast(void* pval) BOOST_NOEXCEPT
  97. {
  98. return futex_signal(pval, (~static_cast< unsigned int >(0u)) >> 1);
  99. }
  100. //! Wakes all threads waiting on the futex
  101. BOOST_FORCEINLINE int futex_broadcast_private(void* pval) BOOST_NOEXCEPT
  102. {
  103. return futex_signal_private(pval, (~static_cast< unsigned int >(0u)) >> 1);
  104. }
  105. //! Wakes the wake_count threads waiting on the futex pval1 and requeues up to requeue_count of the blocked threads onto another futex pval2
  106. BOOST_FORCEINLINE int futex_requeue(void* pval1, void* pval2, unsigned int wake_count = 1u, unsigned int requeue_count = (~static_cast< unsigned int >(0u)) >> 1) BOOST_NOEXCEPT
  107. {
  108. return futex_invoke(pval1, FUTEX_REQUEUE, wake_count, requeue_count, pval2);
  109. }
  110. //! Wakes the wake_count threads waiting on the futex pval1 and requeues up to requeue_count of the blocked threads onto another futex pval2
  111. BOOST_FORCEINLINE int futex_requeue_private(void* pval1, void* pval2, unsigned int wake_count = 1u, unsigned int requeue_count = (~static_cast< unsigned int >(0u)) >> 1) BOOST_NOEXCEPT
  112. {
  113. return futex_invoke(pval1, FUTEX_REQUEUE | BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG, wake_count, requeue_count, pval2);
  114. }
  115. } // namespace detail
  116. } // namespace atomics
  117. } // namespace boost
  118. #include <boost/atomic/detail/footer.hpp>
  119. #endif // defined(BOOST_ATOMIC_DETAIL_SYS_FUTEX)
  120. #endif // defined(__linux__) || defined(__OpenBSD__) || defined(__NETBSD__) || defined(__NetBSD__)
  121. #endif // BOOST_ATOMIC_DETAIL_FUTEX_HPP_INCLUDED_