AtomicNotification.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2004-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 <condition_variable>
  19. namespace folly {
  20. /**
  21. * The behavior of the atomic_wait() family of functions is semantically
  22. * identical to futex(). Correspondingly, calling atomic_notify_one(),
  23. * atomic_notify_all() is identical to futexWake() with 1 and
  24. * std::numeric_limits<int>::max() respectively
  25. *
  26. * The difference here compared to the futex API above is that it works with
  27. * all types of atomic widths. When a 32 bit atomic integer is used, the
  28. * implementation falls back to using futex() if possible, and the
  29. * compatibility implementation for non-linux systems otherwise. For all
  30. * other integer widths, the compatibility implementation is used
  31. *
  32. * The templating of this API is changed from the standard in the following
  33. * ways
  34. *
  35. * - At the time of writing, libstdc++'s implementation of std::atomic<> does
  36. * not include the value_type alias. So we rely on the atomic type being a
  37. * template class such that the first type is the underlying value type
  38. * - The Atom parameter allows this API to be compatible with
  39. * DeterministicSchedule testing.
  40. * - atomic_wait_until() does not exist in the linked paper, the version here
  41. * is identical to futexWaitUntil() and returns std::cv_status
  42. */
  43. // mimic: std::atomic_wait, p1135r0
  44. template <typename Integer>
  45. void atomic_wait(const std::atomic<Integer>* atomic, Integer expected);
  46. template <typename Integer, typename Clock, typename Duration>
  47. std::cv_status atomic_wait_until(
  48. const std::atomic<Integer>* atomic,
  49. Integer expected,
  50. const std::chrono::time_point<Clock, Duration>& deadline);
  51. // mimic: std::atomic_notify_one, p1135r0
  52. template <typename Integer>
  53. void atomic_notify_one(const std::atomic<Integer>* atomic);
  54. // mimic: std::atomic_notify_all, p1135r0
  55. template <typename Integer>
  56. void atomic_notify_all(const std::atomic<Integer>* atomic);
  57. // mimic: std::atomic_uint_fast_wait_t, p1135r0
  58. using atomic_uint_fast_wait_t = std::atomic<std::uint32_t>;
  59. } // namespace folly
  60. #include <folly/synchronization/AtomicNotification-inl.h>