You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

spinlock_wait.h 4.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2017 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_BASE_INTERNAL_SPINLOCK_WAIT_H_
  15. #define ABSL_BASE_INTERNAL_SPINLOCK_WAIT_H_
  16. // Operations to make atomic transitions on a word, and to allow
  17. // waiting for those transitions to become possible.
  18. #include <stdint.h>
  19. #include <atomic>
  20. #include "absl/base/internal/scheduling_mode.h"
  21. namespace absl
  22. {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace base_internal
  25. {
  26. // SpinLockWait() waits until it can perform one of several transitions from
  27. // "from" to "to". It returns when it performs a transition where done==true.
  28. struct SpinLockWaitTransition
  29. {
  30. uint32_t from;
  31. uint32_t to;
  32. bool done;
  33. };
  34. // Wait until *w can transition from trans[i].from to trans[i].to for some i
  35. // satisfying 0<=i<n && trans[i].done, atomically make the transition,
  36. // then return the old value of *w. Make any other atomic transitions
  37. // where !trans[i].done, but continue waiting.
  38. //
  39. // Wakeups for threads blocked on SpinLockWait do not respect priorities.
  40. uint32_t SpinLockWait(std::atomic<uint32_t>* w, int n, const SpinLockWaitTransition trans[], SchedulingMode scheduling_mode);
  41. // If possible, wake some thread that has called SpinLockDelay(w, ...). If `all`
  42. // is true, wake all such threads. On some systems, this may be a no-op; on
  43. // those systems, threads calling SpinLockDelay() will always wake eventually
  44. // even if SpinLockWake() is never called.
  45. void SpinLockWake(std::atomic<uint32_t>* w, bool all);
  46. // Wait for an appropriate spin delay on iteration "loop" of a
  47. // spin loop on location *w, whose previously observed value was "value".
  48. // SpinLockDelay() may do nothing, may yield the CPU, may sleep a clock tick,
  49. // or may wait for a call to SpinLockWake(w).
  50. void SpinLockDelay(std::atomic<uint32_t>* w, uint32_t value, int loop, base_internal::SchedulingMode scheduling_mode);
  51. // Helper used by AbslInternalSpinLockDelay.
  52. // Returns a suggested delay in nanoseconds for iteration number "loop".
  53. int SpinLockSuggestedDelayNS(int loop);
  54. } // namespace base_internal
  55. ABSL_NAMESPACE_END
  56. } // namespace absl
  57. // In some build configurations we pass --detect-odr-violations to the
  58. // gold linker. This causes it to flag weak symbol overrides as ODR
  59. // violations. Because ODR only applies to C++ and not C,
  60. // --detect-odr-violations ignores symbols not mangled with C++ names.
  61. // By changing our extension points to be extern "C", we dodge this
  62. // check.
  63. extern "C"
  64. {
  65. void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(std::atomic<uint32_t>* w, bool all);
  66. void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
  67. std::atomic<uint32_t>* w, uint32_t value, int loop, absl::base_internal::SchedulingMode scheduling_mode
  68. );
  69. }
  70. inline void absl::base_internal::SpinLockWake(std::atomic<uint32_t>* w, bool all)
  71. {
  72. ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)
  73. (w, all);
  74. }
  75. inline void absl::base_internal::SpinLockDelay(
  76. std::atomic<uint32_t>* w, uint32_t value, int loop, absl::base_internal::SchedulingMode scheduling_mode
  77. )
  78. {
  79. ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)
  80. (w, value, loop, scheduling_mode);
  81. }
  82. #endif // ABSL_BASE_INTERNAL_SPINLOCK_WAIT_H_