futex.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2020 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_SYNCHRONIZATION_INTERNAL_FUTEX_H_
  15. #define ABSL_SYNCHRONIZATION_INTERNAL_FUTEX_H_
  16. #include "absl/base/config.h"
  17. #ifdef _WIN32
  18. #include <windows.h>
  19. #else
  20. #include <sys/time.h>
  21. #include <unistd.h>
  22. #endif
  23. #ifdef __linux__
  24. #include <linux/futex.h>
  25. #include <sys/syscall.h>
  26. #endif
  27. #include <errno.h>
  28. #include <stdio.h>
  29. #include <time.h>
  30. #include <atomic>
  31. #include <cstdint>
  32. #include "absl/base/optimization.h"
  33. #include "absl/synchronization/internal/kernel_timeout.h"
  34. #ifdef ABSL_INTERNAL_HAVE_FUTEX
  35. #error ABSL_INTERNAL_HAVE_FUTEX may not be set on the command line
  36. #elif defined(__BIONIC__)
  37. // Bionic supports all the futex operations we need even when some of the futex
  38. // definitions are missing.
  39. #define ABSL_INTERNAL_HAVE_FUTEX
  40. #elif defined(__linux__) && defined(FUTEX_CLOCK_REALTIME)
  41. // FUTEX_CLOCK_REALTIME requires Linux >= 2.6.28.
  42. #define ABSL_INTERNAL_HAVE_FUTEX
  43. #endif
  44. #ifdef ABSL_INTERNAL_HAVE_FUTEX
  45. namespace absl {
  46. ABSL_NAMESPACE_BEGIN
  47. namespace synchronization_internal {
  48. // Some Android headers are missing these definitions even though they
  49. // support these futex operations.
  50. #ifdef __BIONIC__
  51. #ifndef SYS_futex
  52. #define SYS_futex __NR_futex
  53. #endif
  54. #ifndef FUTEX_WAIT_BITSET
  55. #define FUTEX_WAIT_BITSET 9
  56. #endif
  57. #ifndef FUTEX_PRIVATE_FLAG
  58. #define FUTEX_PRIVATE_FLAG 128
  59. #endif
  60. #ifndef FUTEX_CLOCK_REALTIME
  61. #define FUTEX_CLOCK_REALTIME 256
  62. #endif
  63. #ifndef FUTEX_BITSET_MATCH_ANY
  64. #define FUTEX_BITSET_MATCH_ANY 0xFFFFFFFF
  65. #endif
  66. #endif
  67. #if defined(__NR_futex_time64) && !defined(SYS_futex_time64)
  68. #define SYS_futex_time64 __NR_futex_time64
  69. #endif
  70. #if defined(SYS_futex_time64) && !defined(SYS_futex)
  71. #define SYS_futex SYS_futex_time64
  72. #endif
  73. class FutexImpl {
  74. public:
  75. static int WaitUntil(std::atomic<int32_t> *v, int32_t val,
  76. KernelTimeout t) {
  77. int err = 0;
  78. if (t.has_timeout()) {
  79. // https://locklessinc.com/articles/futex_cheat_sheet/
  80. // Unlike FUTEX_WAIT, FUTEX_WAIT_BITSET uses absolute time.
  81. struct timespec abs_timeout = t.MakeAbsTimespec();
  82. // Atomically check that the futex value is still 0, and if it
  83. // is, sleep until abs_timeout or until woken by FUTEX_WAKE.
  84. err = syscall(
  85. SYS_futex, reinterpret_cast<int32_t *>(v),
  86. FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME, val,
  87. &abs_timeout, nullptr, FUTEX_BITSET_MATCH_ANY);
  88. } else {
  89. // Atomically check that the futex value is still 0, and if it
  90. // is, sleep until woken by FUTEX_WAKE.
  91. err = syscall(SYS_futex, reinterpret_cast<int32_t *>(v),
  92. FUTEX_WAIT | FUTEX_PRIVATE_FLAG, val, nullptr);
  93. }
  94. if (ABSL_PREDICT_FALSE(err != 0)) {
  95. err = -errno;
  96. }
  97. return err;
  98. }
  99. static int WaitBitsetAbsoluteTimeout(std::atomic<int32_t> *v, int32_t val,
  100. int32_t bits,
  101. const struct timespec *abstime) {
  102. int err = syscall(SYS_futex, reinterpret_cast<int32_t *>(v),
  103. FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG, val, abstime,
  104. nullptr, bits);
  105. if (ABSL_PREDICT_FALSE(err != 0)) {
  106. err = -errno;
  107. }
  108. return err;
  109. }
  110. static int Wake(std::atomic<int32_t> *v, int32_t count) {
  111. int err = syscall(SYS_futex, reinterpret_cast<int32_t *>(v),
  112. FUTEX_WAKE | FUTEX_PRIVATE_FLAG, count);
  113. if (ABSL_PREDICT_FALSE(err < 0)) {
  114. err = -errno;
  115. }
  116. return err;
  117. }
  118. // FUTEX_WAKE_BITSET
  119. static int WakeBitset(std::atomic<int32_t> *v, int32_t count, int32_t bits) {
  120. int err = syscall(SYS_futex, reinterpret_cast<int32_t *>(v),
  121. FUTEX_WAKE_BITSET | FUTEX_PRIVATE_FLAG, count, nullptr,
  122. nullptr, bits);
  123. if (ABSL_PREDICT_FALSE(err < 0)) {
  124. err = -errno;
  125. }
  126. return err;
  127. }
  128. };
  129. class Futex : public FutexImpl {};
  130. } // namespace synchronization_internal
  131. ABSL_NAMESPACE_END
  132. } // namespace absl
  133. #endif // ABSL_INTERNAL_HAVE_FUTEX
  134. #endif // ABSL_SYNCHRONIZATION_INTERNAL_FUTEX_H_