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