Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/base/internal/spinlock_wait.cc
Line
Count
Source
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 "absl/base/internal/spinlock_wait.h"
20
21
#include <atomic>
22
#include <cstdint>
23
24
#include "absl/base/config.h"
25
26
#if defined(_WIN32)
27
#include "absl/base/internal/spinlock_win32.inc"
28
#elif defined(__linux__)
29
#include "absl/base/internal/spinlock_linux.inc"
30
#elif defined(__akaros__)
31
#include "absl/base/internal/spinlock_akaros.inc"
32
#else
33
#include "absl/base/internal/spinlock_posix.inc"
34
#endif
35
36
namespace absl {
37
ABSL_NAMESPACE_BEGIN
38
namespace base_internal {
39
40
// See spinlock_wait.h for spec.
41
uint32_t SpinLockWait(std::atomic<uint32_t> *w, int n,
42
                      const SpinLockWaitTransition trans[],
43
0
                      base_internal::SchedulingMode scheduling_mode) {
44
0
  int loop = 0;
45
0
  for (;;) {
46
0
    uint32_t v = w->load(std::memory_order_acquire);
47
0
    int i;
48
0
    for (i = 0; i != n && v != trans[i].from; i++) {
49
0
    }
50
0
    if (i == n) {
51
0
      SpinLockDelay(w, v, ++loop, scheduling_mode);  // no matching transition
52
0
    } else if (trans[i].to == v ||                   // null transition
53
0
               w->compare_exchange_strong(v, trans[i].to,
54
0
                                          std::memory_order_acquire,
55
0
                                          std::memory_order_relaxed)) {
56
0
      if (trans[i].done) return v;
57
0
    }
58
0
  }
59
0
}
60
61
static std::atomic<uint64_t> delay_rand;
62
63
// Return a suggested delay in nanoseconds for iteration number "loop"
64
0
int SpinLockSuggestedDelayNS(int loop) {
65
  // Weak pseudo-random number generator to get some spread between threads
66
  // when many are spinning.
67
0
  uint64_t r = delay_rand.load(std::memory_order_relaxed);
68
0
  r = 0x5deece66dLL * r + 0xb;   // numbers from nrand48()
69
0
  delay_rand.store(r, std::memory_order_relaxed);
70
71
0
  if (loop < 0 || loop > 32) {   // limit loop to 0..32
72
0
    loop = 32;
73
0
  }
74
0
  const int kMinDelay = 128 << 10;  // 128us
75
  // Double delay every 8 iterations, up to 16x (2ms).
76
0
  int delay = kMinDelay << (loop / 8);
77
  // Randomize in delay..2*delay range, for resulting 128us..4ms range.
78
0
  return delay | ((delay - 1) & static_cast<int>(r));
79
0
}
80
81
}  // namespace base_internal
82
ABSL_NAMESPACE_END
83
}  // namespace absl