| 6 |
| 7 | #include "base/trace_event/trace_event.h" |
| 8 |
| 9 | #if DCHECK_IS_ON() |
| 10 |
| 11 | #include "base/debug/stack_trace.h" |
| 12 | #include "base/lazy_instance.h" |
| 13 | #include "base/logging.h" |
| 14 | #include "base/threading/thread_local.h" |
| 15 | #include "build/build_config.h" |
| 16 |
| 17 | namespace base { |
| 18 |
| 19 | std::ostream& operator<<(std::ostream&out, const ThreadLocalBoolean& tl) { |
| 20 | out << "currently set to " << (tl.Get() ? "true" : "false"); |
| 21 | return out; |
| 22 | } |
| 23 |
| 24 | namespace { |
| 25 |
| 26 | #if defined(OS_NACL) || defined(OS_ANDROID) |
| 27 | // NaCL doesn't support stack sampling and Android is slow at stack |
| 28 | // sampling and this causes timeouts (crbug.com/959139). |
| 29 | using ThreadLocalBooleanWithStacks = ThreadLocalBoolean; |
| 30 | #else |
| 31 | class ThreadLocalBooleanWithStacks { |
| 32 | public: |
| 33 | ThreadLocalBooleanWithStacks() = default; |
| 34 |
| 35 | bool Get() const { return bool_.Get(); } |
| 36 |
| 37 | void Set(bool val) { |
| 38 | stack_.Set(std::make_unique<debug::StackTrace>()); |
| 39 | bool_.Set(val); |
| 40 | } |
| 41 |
| 42 | friend std::ostream& operator<<(std::ostream& out, |
| 43 | const ThreadLocalBooleanWithStacks& tl) { |
| 44 | out << tl.bool_ << " by "; |
| 45 |
| 46 | if (!tl.stack_.Get()) |
| 47 | return out << "default value\n"; |
| 48 | out << "\n"; |
| 49 | tl.stack_.Get()->OutputToStream(&out); |
| 50 | return out; |
| 51 | } |
| 52 |
| 53 | private: |
| 54 | ThreadLocalBoolean bool_; |
| 55 | ThreadLocalOwnedPointer<debug::StackTrace> stack_; |
| 56 |
| 57 | DISALLOW_COPY_AND_ASSIGN(ThreadLocalBooleanWithStacks); |
| 58 | }; |
| 59 | #endif // defined(OS_NACL) |
| 60 |
| 61 | LazyInstance<ThreadLocalBooleanWithStacks>::Leaky g_blocking_disallowed = |
| 62 | LAZY_INSTANCE_INITIALIZER; |
| 63 |
| 64 | LazyInstance<ThreadLocalBooleanWithStacks>::Leaky g_singleton_disallowed = |
| 65 | LAZY_INSTANCE_INITIALIZER; |
| 66 |
| 67 | LazyInstance<ThreadLocalBooleanWithStacks>::Leaky |
| 68 | g_base_sync_primitives_disallowed = LAZY_INSTANCE_INITIALIZER; |
| 69 |
| 70 | LazyInstance<ThreadLocalBooleanWithStacks>::Leaky |
| 71 | g_cpu_intensive_work_disallowed = LAZY_INSTANCE_INITIALIZER; |
| 72 |
| 73 | } // namespace |
| 74 |
| 75 | namespace internal { |
| 76 |
| 77 | void AssertBlockingAllowed() { |
| 78 | DCHECK(!g_blocking_disallowed.Get().Get()) |
| 79 | << "Function marked as blocking was called from a scope that disallows " |
| 80 | "blocking! If this task is running inside the ThreadPool, it needs " |
| 81 | "to have MayBlock() in its TaskTraits. Otherwise, consider making " |
| 82 | "this blocking work asynchronous or, as a last resort, you may use " |
| 83 | "ScopedAllowBlocking (see its documentation for best practices).\n" |
| 84 | << "g_blocking_disallowed " << g_blocking_disallowed.Get(); |
| 85 | } |
| 86 |
| 87 | } // namespace internal |
| 88 |
| 89 | void DisallowBlocking() { |
| 90 | g_blocking_disallowed.Get().Set(true); |
| 91 | } |
| 92 |
| 93 | ScopedDisallowBlocking::ScopedDisallowBlocking() |
| 94 | : was_disallowed_(g_blocking_disallowed.Get().Get()) { |
| 95 | g_blocking_disallowed.Get().Set(true); |
| 96 | } |
| 97 |
| 98 | ScopedDisallowBlocking::~ScopedDisallowBlocking() { |
| 99 | DCHECK(g_blocking_disallowed.Get().Get()); |
| 100 | g_blocking_disallowed.Get().Set(was_disallowed_); |
| 101 | } |
| 102 |
| 103 | void DisallowBaseSyncPrimitives() { |
| 104 | g_base_sync_primitives_disallowed.Get().Set(true); |
| 105 | } |
| 106 |
| 107 | ScopedAllowBaseSyncPrimitives::ScopedAllowBaseSyncPrimitives() |
| 108 | : was_disallowed_(g_base_sync_primitives_disallowed.Get().Get()) { |
| 109 | DCHECK(!g_blocking_disallowed.Get().Get()) |
| 110 | << "To allow //base sync primitives in a scope where blocking is " |
| 111 | "disallowed use ScopedAllowBaseSyncPrimitivesOutsideBlockingScope.\n" |
| 112 | << "g_blocking_disallowed " << g_blocking_disallowed.Get(); |
| 113 | g_base_sync_primitives_disallowed.Get().Set(false); |
| 114 | } |
| 115 |
| 116 | ScopedAllowBaseSyncPrimitives::~ScopedAllowBaseSyncPrimitives() { |
| 117 | DCHECK(!g_base_sync_primitives_disallowed.Get().Get()); |
| 118 | g_base_sync_primitives_disallowed.Get().Set(was_disallowed_); |
| 119 | } |
| 120 |
| 121 | ScopedAllowBaseSyncPrimitivesForTesting:: |
| 122 | ScopedAllowBaseSyncPrimitivesForTesting() |
| 123 | : was_disallowed_(g_base_sync_primitives_disallowed.Get().Get()) { |
| 124 | g_base_sync_primitives_disallowed.Get().Set(false); |
| 125 | } |
| 126 |
| 127 | ScopedAllowBaseSyncPrimitivesForTesting:: |
| 128 | ~ScopedAllowBaseSyncPrimitivesForTesting() { |
| 129 | DCHECK(!g_base_sync_primitives_disallowed.Get().Get()); |
| 130 | g_base_sync_primitives_disallowed.Get().Set(was_disallowed_); |
| 131 | } |
| 132 |
| 133 | ScopedAllowUnresponsiveTasksForTesting::ScopedAllowUnresponsiveTasksForTesting() |
| 134 | : was_disallowed_base_sync_(g_base_sync_primitives_disallowed.Get().Get()), |
| 135 | was_disallowed_blocking_(g_blocking_disallowed.Get().Get()), |
| 136 | was_disallowed_cpu_(g_cpu_intensive_work_disallowed.Get().Get()) { |
| 137 | g_base_sync_primitives_disallowed.Get().Set(false); |
| 138 | g_blocking_disallowed.Get().Set(false); |
| 139 | g_cpu_intensive_work_disallowed.Get().Set(false); |
| 140 | } |
| 141 |
| 142 | ScopedAllowUnresponsiveTasksForTesting:: |
| 143 | ~ScopedAllowUnresponsiveTasksForTesting() { |
| 144 | DCHECK(!g_base_sync_primitives_disallowed.Get().Get()); |
| 145 | DCHECK(!g_blocking_disallowed.Get().Get()); |
| 146 | DCHECK(!g_cpu_intensive_work_disallowed.Get().Get()); |
| 147 | g_base_sync_primitives_disallowed.Get().Set(was_disallowed_base_sync_); |
| 148 | g_blocking_disallowed.Get().Set(was_disallowed_blocking_); |
| 149 | g_cpu_intensive_work_disallowed.Get().Set(was_disallowed_cpu_); |
| 150 | } |
| 151 |
| 152 | namespace internal { |
| 153 |
| 154 | void AssertBaseSyncPrimitivesAllowed() { |
| 155 | DCHECK(!g_base_sync_primitives_disallowed.Get().Get()) |
| 156 | << "Waiting on a //base sync primitive is not allowed on this thread to " |
| 157 | "prevent jank and deadlock. If waiting on a //base sync primitive is " |
| 158 | "unavoidable, do it within the scope of a " |
| 159 | "ScopedAllowBaseSyncPrimitives. If in a test, " |
| 160 | "use ScopedAllowBaseSyncPrimitivesForTesting.\n" |
| 161 | << "g_base_sync_primitives_disallowed " |
| 162 | << g_base_sync_primitives_disallowed.Get() |
| 163 | << "It can be useful to know that g_blocking_disallowed is " |
| 164 | << g_blocking_disallowed.Get(); |
| 165 | } |
| 166 |
| 167 | void ResetThreadRestrictionsForTesting() { |
| 168 | g_blocking_disallowed.Get().Set(false); |
| 169 | g_singleton_disallowed.Get().Set(false); |
| 170 | g_base_sync_primitives_disallowed.Get().Set(false); |
| 171 | g_cpu_intensive_work_disallowed.Get().Set(false); |
| 172 | } |
| 173 |
| 174 | } // namespace internal |
| 175 |
| 176 | void AssertLongCPUWorkAllowed() { |
| 177 | DCHECK(!g_cpu_intensive_work_disallowed.Get().Get()) |
| 178 | << "Function marked as CPU intensive was called from a scope that " |
| 179 | "disallows this kind of work! Consider making this work " |
| 180 | "asynchronous.\n" |
| 181 | << "g_cpu_intensive_work_disallowed " |
| 182 | << g_cpu_intensive_work_disallowed.Get(); |
| 183 | } |
| 184 |
| 185 | void DisallowUnresponsiveTasks() { |
| 186 | DisallowBlocking(); |
| 187 | DisallowBaseSyncPrimitives(); |
| 188 | g_cpu_intensive_work_disallowed.Get().Set(true); |
| 189 | } |
| 190 |
| 191 | // static |
| 192 | bool ThreadRestrictions::SetIOAllowed(bool allowed) { |
| 193 | bool previous_disallowed = g_blocking_disallowed.Get().Get(); |
| 194 | g_blocking_disallowed.Get().Set(!allowed); |
| 195 | return !previous_disallowed; |
| 196 | } |
| 197 |
| 198 | // static |
| 199 | bool ThreadRestrictions::SetSingletonAllowed(bool allowed) { |
| 200 | bool previous_disallowed = g_singleton_disallowed.Get().Get(); |
| 201 | g_singleton_disallowed.Get().Set(!allowed); |
| 202 | return !previous_disallowed; |
| 203 | } |
| 204 |
| 205 | // static |
| 206 | void ThreadRestrictions::AssertSingletonAllowed() { |
| 207 | DCHECK(!g_singleton_disallowed.Get().Get()) |
| 208 | << "LazyInstance/Singleton is not allowed to be used on this thread. " |
| 209 | "Most likely it's because this thread is not joinable (or the current " |
| 210 | "task is running with TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN " |
| 211 | "semantics), so AtExitManager may have deleted the object on " |
| 212 | "shutdown, leading to a potential shutdown crash. If you need to use " |
| 213 | "the object from this context, it'll have to be updated to use Leaky " |
| 214 | "traits.\n" |
| 215 | << "g_singleton_disallowed " << g_singleton_disallowed.Get(); |
| 216 | } |
| 217 |
| 218 | // static |
| 219 | void ThreadRestrictions::DisallowWaiting() { |
| 220 | DisallowBaseSyncPrimitives(); |
| 221 | } |
| 222 |
| 223 | bool ThreadRestrictions::SetWaitAllowed(bool allowed) { |
| 224 | bool previous_disallowed = g_base_sync_primitives_disallowed.Get().Get(); |
| 225 | g_base_sync_primitives_disallowed.Get().Set(!allowed); |
| 226 | return !previous_disallowed; |
| 227 | } |
| 228 |
| 229 | } // namespace base |
| 230 |
| 231 | #endif // DCHECK_IS_ON() |
| 232 |
| 233 | namespace base { |
| 234 |
| 235 | ScopedAllowBlocking::ScopedAllowBlocking(const Location& from_here) |
| 236 | #if DCHECK_IS_ON() |
| 237 | : was_disallowed_(g_blocking_disallowed.Get().Get()) |
| 238 | #endif |
| 239 | { |
| 240 | TRACE_EVENT_BEGIN2("base", "ScopedAllowBlocking", "file_name", |
| 241 | from_here.file_name(), "function_name", |
| 242 | from_here.function_name()); |
| 243 |
| 244 | #if DCHECK_IS_ON() |
| 245 | g_blocking_disallowed.Get().Set(false); |
| 246 | #endif |
| 247 | } |
| 248 |
| 249 | ScopedAllowBlocking::~ScopedAllowBlocking() { |
| 250 | TRACE_EVENT_END0("base", "ScopedAllowBlocking"); |
| 251 |
| 252 | #if DCHECK_IS_ON() |
| 253 | DCHECK(!g_blocking_disallowed.Get().Get()); |
| 254 | g_blocking_disallowed.Get().Set(was_disallowed_); |
| 255 | #endif |
| 256 | } |
| 257 |
| 258 | ScopedAllowBaseSyncPrimitivesOutsideBlockingScope:: |
| 259 | ScopedAllowBaseSyncPrimitivesOutsideBlockingScope(const Location& from_here) |
| 260 | #if DCHECK_IS_ON() |
| 261 | : was_disallowed_(g_base_sync_primitives_disallowed.Get().Get()) |
| 262 | #endif |
| 263 | { |
| 264 | TRACE_EVENT_BEGIN2( |
| 265 | "base", "ScopedAllowBaseSyncPrimitivesOutsideBlockingScope", "file_name", |
| 266 | from_here.file_name(), "function_name", from_here.function_name()); |
| 267 |
| 268 | #if DCHECK_IS_ON() |
| 269 | g_base_sync_primitives_disallowed.Get().Set(false); |
| 270 | #endif |
| 271 | } |
| 272 |
| 273 | ScopedAllowBaseSyncPrimitivesOutsideBlockingScope:: |
| 274 | ~ScopedAllowBaseSyncPrimitivesOutsideBlockingScope() { |
| 275 | TRACE_EVENT_END0("base", "ScopedAllowBaseSyncPrimitivesOutsideBlockingScope"); |
| 276 |
| 277 | #if DCHECK_IS_ON() |
| 278 | DCHECK(!g_base_sync_primitives_disallowed.Get().Get()); |
| 279 | g_base_sync_primitives_disallowed.Get().Set(was_disallowed_); |
| 280 | #endif |
| 281 | } |
| 282 |
| 283 | ThreadRestrictions::ScopedAllowIO::ScopedAllowIO(const Location& from_here) |
| 284 | #if DCHECK_IS_ON() |
| 285 | : was_allowed_(SetIOAllowed(true)) |
| 286 | #endif |
| 287 | { |
| 288 | TRACE_EVENT_BEGIN2("base", "ScopedAllowIO", "file_name", |
| 289 | from_here.file_name(), "function_name", |
| 290 | from_here.function_name()); |
| 291 | } |
| 292 |
| 293 | ThreadRestrictions::ScopedAllowIO::~ScopedAllowIO() { |
| 294 | TRACE_EVENT_END0("base", "ScopedAllowIO"); |
| 295 |
| 296 | #if DCHECK_IS_ON() |
| 297 | SetIOAllowed(was_allowed_); |
| 298 | #endif |
| 299 | } |
| 300 |
| 301 | } // namespace base |
| 302 |