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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/threading/thread_restrictions.h"
#if DCHECK_IS_ON()
#include "base/debug/stack_trace.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/threading/thread_local.h"
#include "build/build_config.h"
namespace base {
std::ostream& operator<<(std::ostream&out, const ThreadLocalBoolean& tl) {
out << "currently set to " << (tl.Get() ? "true" : "false");
return out;
}
namespace {
#if defined(OS_NACL) || defined(OS_ANDROID)
// NaCL doesn't support stack sampling and Android is slow at stack
// sampling and this causes timeouts (crbug.com/959139).
using ThreadLocalBooleanWithStacks = ThreadLocalBoolean;
#else
class ThreadLocalBooleanWithStacks {
public:
ThreadLocalBooleanWithStacks() = default;
bool Get() const { return bool_.Get(); }
void Set(bool val) {
stack_.Set(std::make_unique<debug::StackTrace>());
bool_.Set(val);
}
friend std::ostream& operator<<(std::ostream& out,
const ThreadLocalBooleanWithStacks& tl) {
out << tl.bool_ << " by ";
if (!tl.stack_.Get())
return out << "default value\n";
out << "\n";
tl.stack_.Get()->OutputToStream(&out);
return out;
}
private:
ThreadLocalBoolean bool_;
ThreadLocalOwnedPointer<debug::StackTrace> stack_;
DISALLOW_COPY_AND_ASSIGN(ThreadLocalBooleanWithStacks);
};
#endif // defined(OS_NACL)
LazyInstance<ThreadLocalBooleanWithStacks>::Leaky g_blocking_disallowed =
LAZY_INSTANCE_INITIALIZER;
LazyInstance<ThreadLocalBooleanWithStacks>::Leaky g_singleton_disallowed =
LAZY_INSTANCE_INITIALIZER;
LazyInstance<ThreadLocalBooleanWithStacks>::Leaky
g_base_sync_primitives_disallowed = LAZY_INSTANCE_INITIALIZER;
LazyInstance<ThreadLocalBooleanWithStacks>::Leaky
g_cpu_intensive_work_disallowed = LAZY_INSTANCE_INITIALIZER;
} // namespace
namespace internal {
void AssertBlockingAllowed() {
DCHECK(!g_blocking_disallowed.Get().Get())
<< "Function marked as blocking was called from a scope that disallows "
"blocking! If this task is running inside the ThreadPool, it needs "
"to have MayBlock() in its TaskTraits. Otherwise, consider making "
"this blocking work asynchronous or, as a last resort, you may use "
"ScopedAllowBlocking (see its documentation for best practices).\n"
<< "g_blocking_disallowed " << g_blocking_disallowed.Get();
}
} // namespace internal
void DisallowBlocking() {
g_blocking_disallowed.Get().Set(true);
}
ScopedDisallowBlocking::ScopedDisallowBlocking()
: was_disallowed_(g_blocking_disallowed.Get().Get()) {
g_blocking_disallowed.Get().Set(true);
}
ScopedDisallowBlocking::~ScopedDisallowBlocking() {
DCHECK(g_blocking_disallowed.Get().Get());
g_blocking_disallowed.Get().Set(was_disallowed_);
}
ScopedAllowBlocking::ScopedAllowBlocking()
: was_disallowed_(g_blocking_disallowed.Get().Get()) {
g_blocking_disallowed.Get().Set(false);
}
ScopedAllowBlocking::~ScopedAllowBlocking() {
DCHECK(!g_blocking_disallowed.Get().Get());
g_blocking_disallowed.Get().Set(was_disallowed_);
}
void DisallowBaseSyncPrimitives() {
g_base_sync_primitives_disallowed.Get().Set(true);
}
ScopedAllowBaseSyncPrimitives::ScopedAllowBaseSyncPrimitives()
: was_disallowed_(g_base_sync_primitives_disallowed.Get().Get()) {
DCHECK(!g_blocking_disallowed.Get().Get())
<< "To allow //base sync primitives in a scope where blocking is "
"disallowed use ScopedAllowBaseSyncPrimitivesOutsideBlockingScope.\n"
<< "g_blocking_disallowed " << g_blocking_disallowed.Get();
g_base_sync_primitives_disallowed.Get().Set(false);
}
ScopedAllowBaseSyncPrimitives::~ScopedAllowBaseSyncPrimitives() {
DCHECK(!g_base_sync_primitives_disallowed.Get().Get());
g_base_sync_primitives_disallowed.Get().Set(was_disallowed_);
}
ScopedAllowBaseSyncPrimitivesOutsideBlockingScope::
ScopedAllowBaseSyncPrimitivesOutsideBlockingScope()
: was_disallowed_(g_base_sync_primitives_disallowed.Get().Get()) {
g_base_sync_primitives_disallowed.Get().Set(false);
}
ScopedAllowBaseSyncPrimitivesOutsideBlockingScope::
~ScopedAllowBaseSyncPrimitivesOutsideBlockingScope() {
DCHECK(!g_base_sync_primitives_disallowed.Get().Get());
g_base_sync_primitives_disallowed.Get().Set(was_disallowed_);
}
ScopedAllowBaseSyncPrimitivesForTesting::
ScopedAllowBaseSyncPrimitivesForTesting()
: was_disallowed_(g_base_sync_primitives_disallowed.Get().Get()) {
g_base_sync_primitives_disallowed.Get().Set(false);
}
ScopedAllowBaseSyncPrimitivesForTesting::
~ScopedAllowBaseSyncPrimitivesForTesting() {
DCHECK(!g_base_sync_primitives_disallowed.Get().Get());
g_base_sync_primitives_disallowed.Get().Set(was_disallowed_);
}
ScopedAllowUnresponsiveTasksForTesting::ScopedAllowUnresponsiveTasksForTesting()
: was_disallowed_base_sync_(g_base_sync_primitives_disallowed.Get().Get()),
was_disallowed_blocking_(g_blocking_disallowed.Get().Get()),
was_disallowed_cpu_(g_cpu_intensive_work_disallowed.Get().Get()) {
g_base_sync_primitives_disallowed.Get().Set(false);
g_blocking_disallowed.Get().Set(false);
g_cpu_intensive_work_disallowed.Get().Set(false);
}
ScopedAllowUnresponsiveTasksForTesting::
~ScopedAllowUnresponsiveTasksForTesting() {
DCHECK(!g_base_sync_primitives_disallowed.Get().Get());
DCHECK(!g_blocking_disallowed.Get().Get());
DCHECK(!g_cpu_intensive_work_disallowed.Get().Get());
g_base_sync_primitives_disallowed.Get().Set(was_disallowed_base_sync_);
g_blocking_disallowed.Get().Set(was_disallowed_blocking_);
g_cpu_intensive_work_disallowed.Get().Set(was_disallowed_cpu_);
}
namespace internal {
void AssertBaseSyncPrimitivesAllowed() {
DCHECK(!g_base_sync_primitives_disallowed.Get().Get())
<< "Waiting on a //base sync primitive is not allowed on this thread to "
"prevent jank and deadlock. If waiting on a //base sync primitive is "
"unavoidable, do it within the scope of a "
"ScopedAllowBaseSyncPrimitives. If in a test, "
"use ScopedAllowBaseSyncPrimitivesForTesting.\n"
<< "g_base_sync_primitives_disallowed "
<< g_base_sync_primitives_disallowed.Get()
<< "It can be useful to know that g_blocking_disallowed is "
<< g_blocking_disallowed.Get();
}
void ResetThreadRestrictionsForTesting() {
g_blocking_disallowed.Get().Set(false);
g_singleton_disallowed.Get().Set(false);
g_base_sync_primitives_disallowed.Get().Set(false);
g_cpu_intensive_work_disallowed.Get().Set(false);
}
} // namespace internal
void AssertLongCPUWorkAllowed() {
DCHECK(!g_cpu_intensive_work_disallowed.Get().Get())
<< "Function marked as CPU intensive was called from a scope that "
"disallows this kind of work! Consider making this work "
"asynchronous.\n"
<< "g_cpu_intensive_work_disallowed "
<< g_cpu_intensive_work_disallowed.Get();
}
void DisallowUnresponsiveTasks() {
DisallowBlocking();
DisallowBaseSyncPrimitives();
g_cpu_intensive_work_disallowed.Get().Set(true);
}
ThreadRestrictions::ScopedAllowIO::ScopedAllowIO()
: was_allowed_(SetIOAllowed(true)) {}
ThreadRestrictions::ScopedAllowIO::~ScopedAllowIO() {
SetIOAllowed(was_allowed_);
}
// static
bool ThreadRestrictions::SetIOAllowed(bool allowed) {
bool previous_disallowed = g_blocking_disallowed.Get().Get();
g_blocking_disallowed.Get().Set(!allowed);
return !previous_disallowed;
}
// static
bool ThreadRestrictions::SetSingletonAllowed(bool allowed) {
bool previous_disallowed = g_singleton_disallowed.Get().Get();
g_singleton_disallowed.Get().Set(!allowed);
return !previous_disallowed;
}
// static
void ThreadRestrictions::AssertSingletonAllowed() {
DCHECK(!g_singleton_disallowed.Get().Get())
<< "LazyInstance/Singleton is not allowed to be used on this thread. "
"Most likely it's because this thread is not joinable (or the current "
"task is running with TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN "
"semantics), so AtExitManager may have deleted the object on "
"shutdown, leading to a potential shutdown crash. If you need to use "
"the object from this context, it'll have to be updated to use Leaky "
"traits.\n"
<< "g_singleton_disallowed " << g_singleton_disallowed.Get();
}
// static
void ThreadRestrictions::DisallowWaiting() {
DisallowBaseSyncPrimitives();
}
bool ThreadRestrictions::SetWaitAllowed(bool allowed) {
bool previous_disallowed = g_base_sync_primitives_disallowed.Get().Get();
g_base_sync_primitives_disallowed.Get().Set(!allowed);
return !previous_disallowed;
}
} // namespace base
#endif // DCHECK_IS_ON()
|