1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//===- LazyAtomicPointer.----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ADT_LAZYATOMICPOINTER_H
#define LLVM_ADT_LAZYATOMICPOINTER_H
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/Support/Compiler.h"
#include <assert.h>
#include <atomic>
namespace llvm {
/// Atomic pointer that's lock-free, but that can coordinate concurrent writes
/// from a lazy generator. Should be reserved for cases where concurrent uses of
/// a generator for the same storage is unlikely.
///
/// The laziness comes in with \a loadOrGenerate(), which lazily calls the
/// provided generator ONLY when the value is currently \c nullptr. With
/// concurrent calls, only one generator is called and the rest see that value.
///
/// Most other APIs treat an in-flight \a loadOrGenerate() as if \c nullptr
/// were stored. APIs that are required to write a value will spin.
///
/// The underlying storage is \a std::atomic<uintptr_t>.
///
/// TODO: In C++20, use std::atomic<T>::wait() instead of spinning and call
/// std::atomic<T>::notify_all() in \a loadOrGenerate().
template <class T> class LazyAtomicPointer {
static constexpr uintptr_t getNull() { return 0; }
static constexpr uintptr_t getBusy() { return -1ULL; }
static T *makePointer(uintptr_t Value) {
assert(Value != getBusy());
return Value ? reinterpret_cast<T *>(Value) : nullptr;
}
static uintptr_t makeRaw(T *Value) {
uintptr_t Raw = Value ? reinterpret_cast<uintptr_t>(Value) : getNull();
assert(Raw != getBusy());
return Raw;
}
public:
/// Store a value. Waits for concurrent \a loadOrGenerate() calls.
void store(T *Value) { return (void)exchange(Value); }
/// Set a value. Return the old value. Waits for concurrent \a
/// loadOrGenerate() calls.
T *exchange(T *Value) {
// Note: the call to compare_exchange_weak() fails "spuriously" if the
// current value is \a getBusy(), causing the loop to spin.
T *Old = nullptr;
while (!compare_exchange_weak(Old, Value)) {
}
return Old;
}
/// Compare-exchange. Returns \c false if there is a concurrent \a
/// loadOrGenerate() call, setting \p ExistingValue to \c nullptr.
bool compare_exchange_weak(T *&ExistingValue, T *NewValue) {
uintptr_t RawExistingValue = makeRaw(ExistingValue);
if (Storage.compare_exchange_weak(RawExistingValue, makeRaw(NewValue)))
return true;
/// Report the existing value as "None" if busy.
if (RawExistingValue == getBusy())
ExistingValue = nullptr;
else
ExistingValue = makePointer(RawExistingValue);
return false;
}
/// Compare-exchange. Keeps trying if there is a concurrent
/// \a loadOrGenerate() call.
bool compare_exchange_strong(T *&ExistingValue, T *NewValue) {
uintptr_t RawExistingValue = makeRaw(ExistingValue);
const uintptr_t OriginalRawExistingValue = RawExistingValue;
if (Storage.compare_exchange_strong(RawExistingValue, makeRaw(NewValue)))
return true;
/// Keep trying as long as it's busy.
if (LLVM_UNLIKELY(RawExistingValue == getBusy())) {
while (RawExistingValue == getBusy()) {
RawExistingValue = OriginalRawExistingValue;
if (Storage.compare_exchange_weak(RawExistingValue, makeRaw(NewValue)))
return true;
}
}
ExistingValue = makePointer(RawExistingValue);
return false;
}
/// Return the current stored value. Returns \a None if there is a concurrent
/// \a loadOrGenerate() in flight.
T *load() const {
uintptr_t RawValue = Storage.load();
return RawValue == getBusy() ? nullptr : makePointer(RawValue);
}
/// Get the current value, or call \p Generator to generate a value.
/// Guarantees that only one thread's \p Generator will run.
///
/// \pre \p Generator doesn't return \c nullptr.
T &loadOrGenerate(function_ref<T *()> Generator) {
// Return existing value, if already set.
uintptr_t Raw = Storage.load();
if (Raw != getNull() && Raw != getBusy())
return *makePointer(Raw);
// Try to mark as busy, then generate and store a new value.
if (LLVM_LIKELY(Raw == getNull() &&
Storage.compare_exchange_strong(Raw, getBusy()))) {
Raw = makeRaw(Generator());
assert(Raw != getNull() && "Expected non-null from generator");
Storage.store(Raw);
return *makePointer(Raw);
}
// Contended with another generator. Wait for it to complete.
while (Raw == getBusy())
Raw = Storage.load();
assert(Raw != getNull() && "Expected non-null from competing generator");
return *makePointer(Raw);
}
explicit operator bool() const { return load(); }
operator T *() const { return load(); }
T &operator*() const {
T *P = load();
assert(P && "Unexpected null dereference");
return *P;
}
T *operator->() const { return &operator*(); }
LazyAtomicPointer() : Storage(0) {}
LazyAtomicPointer(std::nullptr_t) : Storage(0) {}
LazyAtomicPointer(T *Value) : Storage(makeRaw(Value)) {}
LazyAtomicPointer(const LazyAtomicPointer &RHS)
: Storage(makeRaw(RHS.load())) {}
LazyAtomicPointer &operator=(std::nullptr_t) {
store(nullptr);
return *this;
}
LazyAtomicPointer &operator=(T *RHS) {
store(RHS);
return *this;
}
LazyAtomicPointer &operator=(const LazyAtomicPointer &RHS) {
store(RHS.load());
return *this;
}
private:
std::atomic<uintptr_t> Storage;
};
} // end namespace llvm
#endif // LLVM_ADT_LAZYATOMICPOINTER_H
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
|