Futex.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2013-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <atomic>
  18. #include <cassert>
  19. #include <chrono>
  20. #include <cstdint>
  21. #include <limits>
  22. #include <type_traits>
  23. #include <folly/portability/Unistd.h>
  24. namespace folly {
  25. namespace detail {
  26. enum class FutexResult {
  27. VALUE_CHANGED, /* futex value didn't match expected */
  28. AWOKEN, /* wakeup by matching futex wake, or spurious wakeup */
  29. INTERRUPTED, /* wakeup by interrupting signal */
  30. TIMEDOUT, /* wakeup by expiring deadline */
  31. };
  32. /**
  33. * Futex is an atomic 32 bit unsigned integer that provides access to the
  34. * futex() syscall on that value. It is templated in such a way that it
  35. * can interact properly with DeterministicSchedule testing.
  36. *
  37. * If you don't know how to use futex(), you probably shouldn't be using
  38. * this class. Even if you do know how, you should have a good reason
  39. * (and benchmarks to back you up).
  40. *
  41. * Because of the semantics of the futex syscall, the futex family of
  42. * functions are available as free functions rather than member functions
  43. */
  44. template <template <typename> class Atom = std::atomic>
  45. using Futex = Atom<std::uint32_t>;
  46. /**
  47. * Puts the thread to sleep if this->load() == expected. Returns true when
  48. * it is returning because it has consumed a wake() event, false for any
  49. * other return (signal, this->load() != expected, or spurious wakeup).
  50. */
  51. template <typename Futex>
  52. FutexResult
  53. futexWait(const Futex* futex, uint32_t expected, uint32_t waitMask = -1);
  54. /**
  55. * Similar to futexWait but also accepts a deadline until when the wait call
  56. * may block.
  57. *
  58. * Optimal clock types: std::chrono::system_clock, std::chrono::steady_clock.
  59. * NOTE: On some systems steady_clock is just an alias for system_clock,
  60. * and is not actually steady.
  61. *
  62. * For any other clock type, now() will be invoked twice.
  63. */
  64. template <
  65. typename Futex,
  66. class Clock,
  67. class Duration = typename Clock::duration>
  68. FutexResult futexWaitUntil(
  69. const Futex* futex,
  70. uint32_t expected,
  71. std::chrono::time_point<Clock, Duration> const& deadline,
  72. uint32_t waitMask = -1);
  73. /**
  74. * Wakes up to count waiters where (waitMask & wakeMask) != 0, returning the
  75. * number of awoken threads, or -1 if an error occurred. Note that when
  76. * constructing a concurrency primitive that can guard its own destruction, it
  77. * is likely that you will want to ignore EINVAL here (as well as making sure
  78. * that you never touch the object after performing the memory store that is
  79. * the linearization point for unlock or control handoff). See
  80. * https://sourceware.org/bugzilla/show_bug.cgi?id=13690
  81. */
  82. template <typename Futex>
  83. int futexWake(
  84. const Futex* futex,
  85. int count = std::numeric_limits<int>::max(),
  86. uint32_t wakeMask = -1);
  87. /** A std::atomic subclass that can be used to force Futex to emulate
  88. * the underlying futex() syscall. This is primarily useful to test or
  89. * benchmark the emulated implementation on systems that don't need it. */
  90. template <typename T>
  91. struct EmulatedFutexAtomic : public std::atomic<T> {
  92. EmulatedFutexAtomic() noexcept = default;
  93. constexpr /* implicit */ EmulatedFutexAtomic(T init) noexcept
  94. : std::atomic<T>(init) {}
  95. // It doesn't copy or move
  96. EmulatedFutexAtomic(EmulatedFutexAtomic&& rhs) = delete;
  97. };
  98. } // namespace detail
  99. } // namespace folly
  100. #include <folly/detail/Futex-inl.h>