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