spinlock_wait.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // The OS-specific header included below must provide two calls:
  15. // AbslInternalSpinLockDelay() and AbslInternalSpinLockWake().
  16. // See spinlock_wait.h for the specs.
  17. #include <atomic>
  18. #include <cstdint>
  19. #include "absl/base/internal/spinlock_wait.h"
  20. #if defined(_WIN32)
  21. #include "absl/base/internal/spinlock_win32.inc"
  22. #elif defined(__linux__)
  23. #include "absl/base/internal/spinlock_linux.inc"
  24. #elif defined(__akaros__)
  25. #include "absl/base/internal/spinlock_akaros.inc"
  26. #else
  27. #include "absl/base/internal/spinlock_posix.inc"
  28. #endif
  29. namespace absl {
  30. ABSL_NAMESPACE_BEGIN
  31. namespace base_internal {
  32. // See spinlock_wait.h for spec.
  33. uint32_t SpinLockWait(std::atomic<uint32_t> *w, int n,
  34. const SpinLockWaitTransition trans[],
  35. base_internal::SchedulingMode scheduling_mode) {
  36. int loop = 0;
  37. for (;;) {
  38. uint32_t v = w->load(std::memory_order_acquire);
  39. int i;
  40. for (i = 0; i != n && v != trans[i].from; i++) {
  41. }
  42. if (i == n) {
  43. SpinLockDelay(w, v, ++loop, scheduling_mode); // no matching transition
  44. } else if (trans[i].to == v || // null transition
  45. w->compare_exchange_strong(v, trans[i].to,
  46. std::memory_order_acquire,
  47. std::memory_order_relaxed)) {
  48. if (trans[i].done) return v;
  49. }
  50. }
  51. }
  52. static std::atomic<uint64_t> delay_rand;
  53. // Return a suggested delay in nanoseconds for iteration number "loop"
  54. int SpinLockSuggestedDelayNS(int loop) {
  55. // Weak pseudo-random number generator to get some spread between threads
  56. // when many are spinning.
  57. uint64_t r = delay_rand.load(std::memory_order_relaxed);
  58. r = 0x5deece66dLL * r + 0xb; // numbers from nrand48()
  59. delay_rand.store(r, std::memory_order_relaxed);
  60. if (loop < 0 || loop > 32) { // limit loop to 0..32
  61. loop = 32;
  62. }
  63. const int kMinDelay = 128 << 10; // 128us
  64. // Double delay every 8 iterations, up to 16x (2ms).
  65. int delay = kMinDelay << (loop / 8);
  66. // Randomize in delay..2*delay range, for resulting 128us..4ms range.
  67. return delay | ((delay - 1) & static_cast<int>(r));
  68. }
  69. } // namespace base_internal
  70. ABSL_NAMESPACE_END
  71. } // namespace absl