futex.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #if defined(SYS_futex)
  25. #define BOOST_ATOMIC_DETAIL_SYS_FUTEX SYS_futex
  26. #elif defined(SYS_futex_time64)
  27. // On some 32-bit targets (e.g. riscv32) SYS_futex is not defined and instead SYS_futex_time64 is implemented,
  28. // which is equivalent to SYS_futex but uses 64-bit time_t.
  29. #define BOOST_ATOMIC_DETAIL_SYS_FUTEX SYS_futex_time64
  30. #elif defined(__NR_futex)
  31. // Some Android NDKs (Google NDK and older Crystax.NET NDK versions) don't define SYS_futex.
  32. #define BOOST_ATOMIC_DETAIL_SYS_FUTEX __NR_futex
  33. #elif defined(SYS___futex)
  34. // NetBSD defines SYS___futex, which has slightly different parameters. Basically, it has decoupled timeout and val2 parameters:
  35. // int __futex(int *addr1, int op, int val1, const struct timespec *timeout, int *addr2, int val2, int val3);
  36. // https://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/sys/sys/syscall.h
  37. // http://bxr.su/NetBSD/sys/kern/sys_futex.c
  38. #define BOOST_ATOMIC_DETAIL_SYS_FUTEX SYS___futex
  39. #define BOOST_ATOMIC_DETAIL_NETBSD_FUTEX
  40. #endif
  41. #if defined(BOOST_ATOMIC_DETAIL_SYS_FUTEX)
  42. #include <cstddef>
  43. #if defined(__linux__)
  44. #include <linux/futex.h>
  45. #else
  46. #include <sys/futex.h>
  47. #endif
  48. #include <boost/atomic/detail/intptr.hpp>
  49. #include <boost/atomic/detail/header.hpp>
  50. #define BOOST_ATOMIC_DETAIL_HAS_FUTEX
  51. #if defined(FUTEX_PRIVATE_FLAG)
  52. #define BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG FUTEX_PRIVATE_FLAG
  53. #elif defined(__ANDROID__)
  54. // On Android, futex.h is lacking many definitions, but the actual Linux kernel supports the API in full.
  55. #define BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG 128
  56. #else
  57. #define BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG 0
  58. #endif
  59. namespace boost {
  60. namespace atomics {
  61. namespace detail {
  62. //! Invokes an operation on the futex
  63. 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
  64. {
  65. #if !defined(BOOST_ATOMIC_DETAIL_NETBSD_FUTEX)
  66. return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, timeout, addr2, val3);
  67. #else
  68. // Pass 0 in val2.
  69. return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, timeout, addr2, 0u, val3);
  70. #endif
  71. }
  72. //! Invokes an operation on the futex
  73. BOOST_FORCEINLINE int futex_invoke(void* addr1, int op, unsigned int val1, unsigned int val2, void* addr2 = NULL, unsigned int val3 = 0) BOOST_NOEXCEPT
  74. {
  75. #if !defined(BOOST_ATOMIC_DETAIL_NETBSD_FUTEX)
  76. return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, static_cast< atomics::detail::uintptr_t >(val2), addr2, val3);
  77. #else
  78. // Pass NULL in timeout.
  79. return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, static_cast< void* >(NULL), addr2, val2, val3);
  80. #endif
  81. }
  82. //! Checks that the value \c pval is \c expected and blocks
  83. BOOST_FORCEINLINE int futex_wait(void* pval, unsigned int expected) BOOST_NOEXCEPT
  84. {
  85. return futex_invoke(pval, FUTEX_WAIT, expected);
  86. }
  87. //! Checks that the value \c pval is \c expected and blocks
  88. BOOST_FORCEINLINE int futex_wait_private(void* pval, unsigned int expected) BOOST_NOEXCEPT
  89. {
  90. return futex_invoke(pval, FUTEX_WAIT | BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG, expected);
  91. }
  92. //! Wakes the specified number of threads waiting on the futex
  93. BOOST_FORCEINLINE int futex_signal(void* pval, unsigned int count = 1u) BOOST_NOEXCEPT
  94. {
  95. return futex_invoke(pval, FUTEX_WAKE, count);
  96. }
  97. //! Wakes the specified number of threads waiting on the futex
  98. BOOST_FORCEINLINE int futex_signal_private(void* pval, unsigned int count = 1u) BOOST_NOEXCEPT
  99. {
  100. return futex_invoke(pval, FUTEX_WAKE | BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG, count);
  101. }
  102. //! Wakes all threads waiting on the futex
  103. BOOST_FORCEINLINE int futex_broadcast(void* pval) BOOST_NOEXCEPT
  104. {
  105. return futex_signal(pval, (~static_cast< unsigned int >(0u)) >> 1);
  106. }
  107. //! Wakes all threads waiting on the futex
  108. BOOST_FORCEINLINE int futex_broadcast_private(void* pval) BOOST_NOEXCEPT
  109. {
  110. return futex_signal_private(pval, (~static_cast< unsigned int >(0u)) >> 1);
  111. }
  112. //! Wakes the wake_count threads waiting on the futex pval1 and requeues up to requeue_count of the blocked threads onto another futex pval2
  113. 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
  114. {
  115. return futex_invoke(pval1, FUTEX_REQUEUE, wake_count, requeue_count, pval2);
  116. }
  117. //! Wakes the wake_count threads waiting on the futex pval1 and requeues up to requeue_count of the blocked threads onto another futex pval2
  118. 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
  119. {
  120. return futex_invoke(pval1, FUTEX_REQUEUE | BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG, wake_count, requeue_count, pval2);
  121. }
  122. } // namespace detail
  123. } // namespace atomics
  124. } // namespace boost
  125. #include <boost/atomic/detail/footer.hpp>
  126. #endif // defined(BOOST_ATOMIC_DETAIL_SYS_FUTEX)
  127. #endif // defined(__linux__) || defined(__OpenBSD__) || defined(__NETBSD__) || defined(__NetBSD__)
  128. #endif // BOOST_ATOMIC_DETAIL_FUTEX_HPP_INCLUDED_