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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/browser_thread_impl.h"
#include <array>
#include <atomic>
#include <string>
#include <utility>
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/sequence_checker.h"
#include "base/task/current_thread.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "content/browser/scheduler/browser_task_executor.h"
#include "content/public/browser/content_browser_client.h"
namespace content {
namespace {
// State of a given BrowserThread::ID in chronological order throughout the
// browser process' lifetime.
enum BrowserThreadState {
// BrowserThread::ID isn't associated with anything yet.
UNINITIALIZED = 0,
// BrowserThread::ID is associated to a TaskRunner and is accepting tasks.
RUNNING,
// BrowserThread::ID no longer accepts tasks (it's still associated to a
// TaskRunner but that TaskRunner doesn't have to accept tasks).
SHUTDOWN
};
struct BrowserThreadGlobals {
BrowserThreadGlobals() {
// A few unit tests which do not use a BrowserTaskEnvironment still invoke
// code that reaches into CurrentlyOn()/IsThreadInitialized(). This can
// result in instantiating BrowserThreadGlobals off the main thread.
// |main_thread_checker_| being bound incorrectly would then result in a
// flake in the next test that instantiates a BrowserTaskEnvironment in the
// same process. Detaching here postpones binding |main_thread_checker_| to
// the first invocation of BrowserThreadImpl::BrowserThreadImpl() and works
// around this issue.
DETACH_FROM_THREAD(main_thread_checker_);
}
// BrowserThreadGlobals must be initialized on main thread before it's used by
// any other threads.
THREAD_CHECKER(main_thread_checker_);
// |task_runners[id]| is safe to access on |main_thread_checker_| as
// well as on any thread once it's read-only after initialization
// (i.e. while |states[id] >= RUNNING|).
std::array<scoped_refptr<base::SingleThreadTaskRunner>,
BrowserThread::ID_COUNT>
task_runners;
// Tracks the runtime state of BrowserThreadImpls. Atomic because a few
// methods below read this value outside |main_thread_checker_| to
// confirm it's >= RUNNING and doing so requires an atomic read as it could be
// in the middle of transitioning to SHUTDOWN (which the check is fine with
// but reading a non-atomic value as it's written to by another thread can
// result in undefined behaviour on some platforms).
// Only NoBarrier atomic operations should be used on |states| as it shouldn't
// be used to establish happens-after relationships but rather checking the
// runtime state of various threads (once again: it's only atomic to support
// reading while transitioning from RUNNING=>SHUTDOWN).
std::array<std::atomic<BrowserThreadState>, BrowserThread::ID_COUNT> states =
{};
};
BrowserThreadGlobals& GetBrowserThreadGlobals() {
static base::NoDestructor<BrowserThreadGlobals> globals;
return *globals;
}
} // namespace
scoped_refptr<base::SingleThreadTaskRunner> GetUIThreadTaskRunner(
const BrowserTaskTraits& traits) {
return BrowserTaskExecutor::GetUIThreadTaskRunner(traits);
}
scoped_refptr<base::SingleThreadTaskRunner> GetIOThreadTaskRunner(
const BrowserTaskTraits& traits) {
return BrowserTaskExecutor::GetIOThreadTaskRunner(traits);
}
BrowserThreadImpl::BrowserThreadImpl(
ID identifier,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: identifier_(identifier) {
DCHECK_GE(identifier_, 0);
DCHECK_LT(identifier_, ID_COUNT);
DCHECK(task_runner);
BrowserThreadGlobals& globals = GetBrowserThreadGlobals();
DCHECK_CALLED_ON_VALID_THREAD(globals.main_thread_checker_);
DCHECK_EQ(
UNSAFE_TODO(globals.states[identifier_]).load(std::memory_order_relaxed),
BrowserThreadState::UNINITIALIZED);
UNSAFE_TODO(globals.states[identifier_])
.store(BrowserThreadState::RUNNING, std::memory_order_relaxed);
DCHECK(UNSAFE_TODO(!globals.task_runners[identifier_]));
UNSAFE_TODO(globals.task_runners[identifier_]) = std::move(task_runner);
if (identifier_ == BrowserThread::ID::UI) {
#if BUILDFLAG(IS_POSIX)
// Allow usage of the FileDescriptorWatcher API on the UI thread, using the
// IO thread to watch the file descriptors.
//
// In unit tests, usage of the FileDescriptorWatcher API is already allowed
// if the UI thread is running a MessageLoopForIO.
if (!base::CurrentIOThread::IsSet()) {
file_descriptor_watcher_.emplace(GetIOThreadTaskRunner({}));
}
base::FileDescriptorWatcher::AssertAllowed();
#endif
}
}
BrowserThreadImpl::~BrowserThreadImpl() {
BrowserThreadGlobals& globals = GetBrowserThreadGlobals();
DCHECK_CALLED_ON_VALID_THREAD(globals.main_thread_checker_);
DCHECK_EQ(
UNSAFE_TODO(globals.states[identifier_]).load(std::memory_order_relaxed),
BrowserThreadState::RUNNING);
UNSAFE_TODO(globals.states[identifier_])
.store(BrowserThreadState::SHUTDOWN, std::memory_order_relaxed);
// The mapping is kept alive after shutdown to avoid requiring a lock only for
// shutdown (the SingleThreadTaskRunner itself may stop accepting tasks at any
// point -- usually soon before/after destroying the BrowserThreadImpl).
DCHECK(UNSAFE_TODO(globals.task_runners[identifier_]));
}
// static
void BrowserThreadImpl::ResetGlobalsForTesting(BrowserThread::ID identifier) {
BrowserThreadGlobals& globals = GetBrowserThreadGlobals();
DCHECK_CALLED_ON_VALID_THREAD(globals.main_thread_checker_);
DCHECK_EQ(
UNSAFE_TODO(globals.states[identifier]).load(std::memory_order_relaxed),
BrowserThreadState::SHUTDOWN);
UNSAFE_TODO(globals.states[identifier])
.store(BrowserThreadState::UNINITIALIZED, std::memory_order_relaxed);
UNSAFE_TODO(globals.task_runners[identifier]) = nullptr;
}
// static
const char* BrowserThreadImpl::GetThreadName(BrowserThread::ID thread) {
static const std::array<const char* const, BrowserThread::ID_COUNT>
kBrowserThreadNames = {
"", // UI (name assembled in browser_main_loop.cc).
"Chrome_IOThread", // IO
};
if (BrowserThread::UI < thread && thread < BrowserThread::ID_COUNT)
return kBrowserThreadNames[thread];
if (thread == BrowserThread::UI)
return "Chrome_UIThread";
return "Unknown Thread";
}
// static
bool BrowserThread::IsThreadInitialized(ID identifier) {
DCHECK_GE(identifier, 0);
DCHECK_LT(identifier, ID_COUNT);
BrowserThreadGlobals& globals = GetBrowserThreadGlobals();
return UNSAFE_TODO(globals.states[identifier])
.load(std::memory_order_relaxed) == BrowserThreadState::RUNNING;
}
// static
bool BrowserThread::CurrentlyOn(ID identifier) {
DCHECK_GE(identifier, 0);
DCHECK_LT(identifier, ID_COUNT);
BrowserThreadGlobals& globals = GetBrowserThreadGlobals();
// Thread-safe since |globals.task_runners| is read-only after being
// initialized from main thread (which happens before //content and embedders
// are kicked off and enabled to call the BrowserThread API from other
// threads).
return UNSAFE_TODO(globals.task_runners[identifier]) &&
UNSAFE_TODO(
globals.task_runners[identifier]->RunsTasksInCurrentSequence());
}
// static
std::string BrowserThread::GetCurrentlyOnErrorMessage(ID expected) {
std::string actual_name = base::PlatformThread::GetName();
if (actual_name.empty())
actual_name = "Unknown Thread";
std::string result = "Must be called on ";
result += BrowserThreadImpl::GetThreadName(expected);
result += "; actually called on ";
result += actual_name;
result += ".";
return result;
}
// static
bool BrowserThread::GetCurrentThreadIdentifier(ID* identifier) {
BrowserThreadGlobals& globals = GetBrowserThreadGlobals();
// Thread-safe since |globals.task_runners| is read-only after being
// initialized from main thread (which happens before //content and embedders
// are kicked off and enabled to call the BrowserThread API from other
// threads).
for (int i = 0; i < ID_COUNT; ++i) {
if (UNSAFE_TODO(globals.task_runners[i]) &&
UNSAFE_TODO(globals.task_runners[i])->RunsTasksInCurrentSequence()) {
*identifier = static_cast<ID>(i);
return true;
}
}
return false;
}
// static
scoped_refptr<base::SingleThreadTaskRunner>
BrowserThread::GetTaskRunnerForThread(ID identifier) {
DCHECK_GE(identifier, 0);
DCHECK_LT(identifier, ID_COUNT);
switch (identifier) {
case UI:
return GetUIThreadTaskRunner({});
case IO:
return GetIOThreadTaskRunner({});
case ID_COUNT:
NOTREACHED();
}
}
// static
void BrowserThread::RunAllPendingTasksOnThreadForTesting(ID identifier) {
BrowserTaskExecutor::RunAllPendingTasksOnThreadForTesting(identifier);
}
// static
void BrowserThread::PostBestEffortTask(
const base::Location& from_here,
scoped_refptr<base::TaskRunner> task_runner,
base::OnceClosure task) {
content::GetIOThreadTaskRunner({base::TaskPriority::BEST_EFFORT})
->PostTask(
FROM_HERE,
base::BindOnce(base::IgnoreResult(&base::TaskRunner::PostTask),
std::move(task_runner), from_here, std::move(task)));
}
namespace internal {
bool BrowserThreadChecker::CalledOnValidBrowserThread(
BrowserThread::ID thread_identifier) const {
return BrowserThread::CurrentlyOn(thread_identifier);
}
const BrowserThreadChecker& GetBrowserThreadChecker(
BrowserThread::ID thread_identifier) {
static std::array<BrowserThreadChecker, BrowserThread::ID_COUNT>
browser_thread_checkers;
return browser_thread_checkers[thread_identifier];
}
ScopedValidateBrowserThreadChecker::ScopedValidateBrowserThreadChecker(
BrowserThread::ID thread_identifier,
base::NotFatalUntil fatal_milestone) {
const auto& checker = GetBrowserThreadChecker(thread_identifier);
CHECK(checker.CalledOnValidBrowserThread(thread_identifier), fatal_milestone)
<< BrowserThread::GetCurrentlyOnErrorMessage(thread_identifier);
}
ScopedValidateBrowserThreadChecker::~ScopedValidateBrowserThreadChecker() =
default;
#if DCHECK_IS_ON()
ScopedValidateBrowserThreadDebugChecker::
ScopedValidateBrowserThreadDebugChecker(
BrowserThread::ID thread_identifier) {
const auto& checker = GetBrowserThreadChecker(thread_identifier);
DCHECK(checker.CalledOnValidBrowserThread(thread_identifier))
<< BrowserThread::GetCurrentlyOnErrorMessage(thread_identifier);
}
#endif // DCHECK_IS_ON()
} // namespace internal
} // namespace content
|