Coverage Report

Created: 2024-09-23 06:29

/src/abseil-cpp/absl/base/internal/spinlock_wait.cc
Line
Count
Source (jump to first uncovered line)
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 {
35
ABSL_NAMESPACE_BEGIN
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
0
                      base_internal::SchedulingMode scheduling_mode) {
42
0
  int loop = 0;
43
0
  for (;;) {
44
0
    uint32_t v = w->load(std::memory_order_acquire);
45
0
    int i;
46
0
    for (i = 0; i != n && v != trans[i].from; i++) {
47
0
    }
48
0
    if (i == n) {
49
0
      SpinLockDelay(w, v, ++loop, scheduling_mode);  // no matching transition
50
0
    } else if (trans[i].to == v ||                   // null transition
51
0
               w->compare_exchange_strong(v, trans[i].to,
52
0
                                          std::memory_order_acquire,
53
0
                                          std::memory_order_relaxed)) {
54
0
      if (trans[i].done) return v;
55
0
    }
56
0
  }
57
0
}
58
59
static std::atomic<uint64_t> delay_rand;
60
61
// Return a suggested delay in nanoseconds for iteration number "loop"
62
0
int SpinLockSuggestedDelayNS(int loop) {
63
  // Weak pseudo-random number generator to get some spread between threads
64
  // when many are spinning.
65
0
  uint64_t r = delay_rand.load(std::memory_order_relaxed);
66
0
  r = 0x5deece66dLL * r + 0xb;   // numbers from nrand48()
67
0
  delay_rand.store(r, std::memory_order_relaxed);
68
69
0
  if (loop < 0 || loop > 32) {   // limit loop to 0..32
70
0
    loop = 32;
71
0
  }
72
0
  const int kMinDelay = 128 << 10;  // 128us
73
  // Double delay every 8 iterations, up to 16x (2ms).
74
0
  int delay = kMinDelay << (loop / 8);
75
  // Randomize in delay..2*delay range, for resulting 128us..4ms range.
76
0
  return delay | ((delay - 1) & static_cast<int>(r));
77
0
}
78
79
}  // namespace base_internal
80
ABSL_NAMESPACE_END
81
}  // namespace absl