1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker
15*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_BASE_INTERNAL_SPINLOCK_WAIT_H_
16*9356374aSAndroid Build Coastguard Worker #define ABSL_BASE_INTERNAL_SPINLOCK_WAIT_H_
17*9356374aSAndroid Build Coastguard Worker
18*9356374aSAndroid Build Coastguard Worker // Operations to make atomic transitions on a word, and to allow
19*9356374aSAndroid Build Coastguard Worker // waiting for those transitions to become possible.
20*9356374aSAndroid Build Coastguard Worker
21*9356374aSAndroid Build Coastguard Worker #include <stdint.h>
22*9356374aSAndroid Build Coastguard Worker #include <atomic>
23*9356374aSAndroid Build Coastguard Worker
24*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/scheduling_mode.h"
25*9356374aSAndroid Build Coastguard Worker
26*9356374aSAndroid Build Coastguard Worker namespace absl {
27*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
28*9356374aSAndroid Build Coastguard Worker namespace base_internal {
29*9356374aSAndroid Build Coastguard Worker
30*9356374aSAndroid Build Coastguard Worker // SpinLockWait() waits until it can perform one of several transitions from
31*9356374aSAndroid Build Coastguard Worker // "from" to "to". It returns when it performs a transition where done==true.
32*9356374aSAndroid Build Coastguard Worker struct SpinLockWaitTransition {
33*9356374aSAndroid Build Coastguard Worker uint32_t from;
34*9356374aSAndroid Build Coastguard Worker uint32_t to;
35*9356374aSAndroid Build Coastguard Worker bool done;
36*9356374aSAndroid Build Coastguard Worker };
37*9356374aSAndroid Build Coastguard Worker
38*9356374aSAndroid Build Coastguard Worker // Wait until *w can transition from trans[i].from to trans[i].to for some i
39*9356374aSAndroid Build Coastguard Worker // satisfying 0<=i<n && trans[i].done, atomically make the transition,
40*9356374aSAndroid Build Coastguard Worker // then return the old value of *w. Make any other atomic transitions
41*9356374aSAndroid Build Coastguard Worker // where !trans[i].done, but continue waiting.
42*9356374aSAndroid Build Coastguard Worker //
43*9356374aSAndroid Build Coastguard Worker // Wakeups for threads blocked on SpinLockWait do not respect priorities.
44*9356374aSAndroid Build Coastguard Worker uint32_t SpinLockWait(std::atomic<uint32_t> *w, int n,
45*9356374aSAndroid Build Coastguard Worker const SpinLockWaitTransition trans[],
46*9356374aSAndroid Build Coastguard Worker SchedulingMode scheduling_mode);
47*9356374aSAndroid Build Coastguard Worker
48*9356374aSAndroid Build Coastguard Worker // If possible, wake some thread that has called SpinLockDelay(w, ...). If `all`
49*9356374aSAndroid Build Coastguard Worker // is true, wake all such threads. On some systems, this may be a no-op; on
50*9356374aSAndroid Build Coastguard Worker // those systems, threads calling SpinLockDelay() will always wake eventually
51*9356374aSAndroid Build Coastguard Worker // even if SpinLockWake() is never called.
52*9356374aSAndroid Build Coastguard Worker void SpinLockWake(std::atomic<uint32_t> *w, bool all);
53*9356374aSAndroid Build Coastguard Worker
54*9356374aSAndroid Build Coastguard Worker // Wait for an appropriate spin delay on iteration "loop" of a
55*9356374aSAndroid Build Coastguard Worker // spin loop on location *w, whose previously observed value was "value".
56*9356374aSAndroid Build Coastguard Worker // SpinLockDelay() may do nothing, may yield the CPU, may sleep a clock tick,
57*9356374aSAndroid Build Coastguard Worker // or may wait for a call to SpinLockWake(w).
58*9356374aSAndroid Build Coastguard Worker void SpinLockDelay(std::atomic<uint32_t> *w, uint32_t value, int loop,
59*9356374aSAndroid Build Coastguard Worker base_internal::SchedulingMode scheduling_mode);
60*9356374aSAndroid Build Coastguard Worker
61*9356374aSAndroid Build Coastguard Worker // Helper used by AbslInternalSpinLockDelay.
62*9356374aSAndroid Build Coastguard Worker // Returns a suggested delay in nanoseconds for iteration number "loop".
63*9356374aSAndroid Build Coastguard Worker int SpinLockSuggestedDelayNS(int loop);
64*9356374aSAndroid Build Coastguard Worker
65*9356374aSAndroid Build Coastguard Worker } // namespace base_internal
66*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
67*9356374aSAndroid Build Coastguard Worker } // namespace absl
68*9356374aSAndroid Build Coastguard Worker
69*9356374aSAndroid Build Coastguard Worker // In some build configurations we pass --detect-odr-violations to the
70*9356374aSAndroid Build Coastguard Worker // gold linker. This causes it to flag weak symbol overrides as ODR
71*9356374aSAndroid Build Coastguard Worker // violations. Because ODR only applies to C++ and not C,
72*9356374aSAndroid Build Coastguard Worker // --detect-odr-violations ignores symbols not mangled with C++ names.
73*9356374aSAndroid Build Coastguard Worker // By changing our extension points to be extern "C", we dodge this
74*9356374aSAndroid Build Coastguard Worker // check.
75*9356374aSAndroid Build Coastguard Worker extern "C" {
76*9356374aSAndroid Build Coastguard Worker void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(std::atomic<uint32_t> *w,
77*9356374aSAndroid Build Coastguard Worker bool all);
78*9356374aSAndroid Build Coastguard Worker void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
79*9356374aSAndroid Build Coastguard Worker std::atomic<uint32_t> *w, uint32_t value, int loop,
80*9356374aSAndroid Build Coastguard Worker absl::base_internal::SchedulingMode scheduling_mode);
81*9356374aSAndroid Build Coastguard Worker }
82*9356374aSAndroid Build Coastguard Worker
SpinLockWake(std::atomic<uint32_t> * w,bool all)83*9356374aSAndroid Build Coastguard Worker inline void absl::base_internal::SpinLockWake(std::atomic<uint32_t> *w,
84*9356374aSAndroid Build Coastguard Worker bool all) {
85*9356374aSAndroid Build Coastguard Worker ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(w, all);
86*9356374aSAndroid Build Coastguard Worker }
87*9356374aSAndroid Build Coastguard Worker
SpinLockDelay(std::atomic<uint32_t> * w,uint32_t value,int loop,absl::base_internal::SchedulingMode scheduling_mode)88*9356374aSAndroid Build Coastguard Worker inline void absl::base_internal::SpinLockDelay(
89*9356374aSAndroid Build Coastguard Worker std::atomic<uint32_t> *w, uint32_t value, int loop,
90*9356374aSAndroid Build Coastguard Worker absl::base_internal::SchedulingMode scheduling_mode) {
91*9356374aSAndroid Build Coastguard Worker ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)
92*9356374aSAndroid Build Coastguard Worker (w, value, loop, scheduling_mode);
93*9356374aSAndroid Build Coastguard Worker }
94*9356374aSAndroid Build Coastguard Worker
95*9356374aSAndroid Build Coastguard Worker #endif // ABSL_BASE_INTERNAL_SPINLOCK_WAIT_H_
96