abseil-cpp/absl/base/internal/spinlock_wait.cc
Go to the documentation of this file.
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 
15 // The OS-specific header included below must provide two calls:
16 // AbslInternalSpinLockDelay() and AbslInternalSpinLockWake().
17 // See spinlock_wait.h for the specs.
18 
19 #include <atomic>
20 #include <cstdint>
21 
22 #include "absl/base/internal/spinlock_wait.h"
23 
24 #if defined(_WIN32)
25 #include "absl/base/internal/spinlock_win32.inc"
26 #elif defined(__linux__)
27 #include "absl/base/internal/spinlock_linux.inc"
28 #elif defined(__akaros__)
29 #include "absl/base/internal/spinlock_akaros.inc"
30 #else
31 #include "absl/base/internal/spinlock_posix.inc"
32 #endif
33 
34 namespace absl {
36 namespace base_internal {
37 
38 // See spinlock_wait.h for spec.
39 uint32_t SpinLockWait(std::atomic<uint32_t> *w, int n,
40  const SpinLockWaitTransition trans[],
41  base_internal::SchedulingMode scheduling_mode) {
42  int loop = 0;
43  for (;;) {
44  uint32_t v = w->load(std::memory_order_acquire);
45  int i;
46  for (i = 0; i != n && v != trans[i].from; i++) {
47  }
48  if (i == n) {
49  SpinLockDelay(w, v, ++loop, scheduling_mode); // no matching transition
50  } else if (trans[i].to == v || // null transition
51  w->compare_exchange_strong(v, trans[i].to,
52  std::memory_order_acquire,
53  std::memory_order_relaxed)) {
54  if (trans[i].done) return v;
55  }
56  }
57 }
58 
59 static std::atomic<uint64_t> delay_rand;
60 
61 // Return a suggested delay in nanoseconds for iteration number "loop"
63  // Weak pseudo-random number generator to get some spread between threads
64  // when many are spinning.
65  uint64_t r = delay_rand.load(std::memory_order_relaxed);
66  r = 0x5deece66dLL * r + 0xb; // numbers from nrand48()
67  delay_rand.store(r, std::memory_order_relaxed);
68 
69  if (loop < 0 || loop > 32) { // limit loop to 0..32
70  loop = 32;
71  }
72  const int kMinDelay = 128 << 10; // 128us
73  // Double delay every 8 iterations, up to 16x (2ms).
74  int delay = kMinDelay << (loop / 8);
75  // Randomize in delay..2*delay range, for resulting 128us..4ms range.
76  return delay | ((delay - 1) & static_cast<int>(r));
77 }
78 
79 } // namespace base_internal
81 } // namespace absl
absl::base_internal::SpinLockDelay
void SpinLockDelay(std::atomic< uint32_t > *w, uint32_t value, int loop, base_internal::SchedulingMode scheduling_mode)
Definition: abseil-cpp/absl/base/internal/spinlock_wait.h:88
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
to
size_t to
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1385
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::base_internal::SpinLockWaitTransition
Definition: abseil-cpp/absl/base/internal/spinlock_wait.h:32
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
absl::base_internal::SchedulingMode
SchedulingMode
Definition: abseil-cpp/absl/base/internal/scheduling_mode.h:49
absl::base_internal::delay_rand
static std::atomic< uint64_t > delay_rand
Definition: abseil-cpp/absl/base/internal/spinlock_wait.cc:59
absl::base_internal::SpinLockWaitTransition::to
uint32_t to
Definition: abseil-cpp/absl/base/internal/spinlock_wait.h:34
fix_build_deps.r
r
Definition: fix_build_deps.py:491
absl::base_internal::SpinLockSuggestedDelayNS
int SpinLockSuggestedDelayNS(int loop)
Definition: abseil-cpp/absl/base/internal/spinlock_wait.cc:62
absl::base_internal::SpinLockWaitTransition::from
uint32_t from
Definition: abseil-cpp/absl/base/internal/spinlock_wait.h:33
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
LL
#define LL(x)
absl::base_internal::SpinLockWait
uint32_t SpinLockWait(std::atomic< uint32_t > *w, int n, const SpinLockWaitTransition trans[], base_internal::SchedulingMode scheduling_mode)
Definition: abseil-cpp/absl/base/internal/spinlock_wait.cc:39


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:15