summaryrefslogtreecommitdiffstats
path: root/chromium/base/message_loop/message_loop.cc
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2018-08-24 12:15:48 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2018-08-28 13:30:04 +0000
commitb014812705fc80bff0a5c120dfcef88f349816dc (patch)
tree25a2e2d9fa285f1add86aa333389a839f81a39ae /chromium/base/message_loop/message_loop.cc
parent9f4560b1027ae06fdb497023cdcaf91b8511fa74 (diff)
BASELINE: Update Chromium to 68.0.3440.125
Change-Id: I23f19369e01f688e496f5bf179abb521ad73874f Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/base/message_loop/message_loop.cc')
-rw-r--r--chromium/base/message_loop/message_loop.cc196
1 files changed, 59 insertions, 137 deletions
diff --git a/chromium/base/message_loop/message_loop.cc b/chromium/base/message_loop/message_loop.cc
index b417f2fffe8..97ed778d8f1 100644
--- a/chromium/base/message_loop/message_loop.cc
+++ b/chromium/base/message_loop/message_loop.cc
@@ -12,11 +12,11 @@
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_pump_default.h"
+#include "base/message_loop/message_pump_for_io.h"
#include "base/message_loop/message_pump_for_ui.h"
#include "base/run_loop.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/thread_id_name_manager.h"
-#include "base/threading/thread_local.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/trace_event.h"
@@ -28,12 +28,6 @@ namespace base {
namespace {
-// A lazily created thread local storage for quick access to a thread's message
-// loop, if one exists.
-base::ThreadLocalPointer<MessageLoop>* GetTLSMessageLoop() {
- static auto* lazy_tls_ptr = new base::ThreadLocalPointer<MessageLoop>();
- return lazy_tls_ptr;
-}
MessageLoop::MessagePumpFactory* message_pump_for_ui_factory_ = nullptr;
std::unique_ptr<MessagePump> ReturnPump(std::unique_ptr<MessagePump> pump) {
@@ -44,14 +38,6 @@ std::unique_ptr<MessagePump> ReturnPump(std::unique_ptr<MessagePump> pump) {
//------------------------------------------------------------------------------
-MessageLoop::TaskObserver::TaskObserver() = default;
-
-MessageLoop::TaskObserver::~TaskObserver() = default;
-
-MessageLoop::DestructionObserver::~DestructionObserver() = default;
-
-//------------------------------------------------------------------------------
-
MessageLoop::MessageLoop(Type type)
: MessageLoop(type, MessagePumpFactoryCallback()) {
BindToCurrentThread();
@@ -67,7 +53,8 @@ MessageLoop::~MessageLoop() {
// current one on this thread. Otherwise, this loop is being destructed before
// it was bound to a thread, so a different message loop (or no loop at all)
// may be current.
- DCHECK((pump_ && current() == this) || (!pump_ && current() != this));
+ DCHECK((pump_ && MessageLoopCurrent::IsBoundToCurrentThreadInternal(this)) ||
+ (!pump_ && !MessageLoopCurrent::IsBoundToCurrentThreadInternal(this)));
// iOS just attaches to the loop, it doesn't Run it.
// TODO(stuartmorgan): Consider wiring up a Detach().
@@ -75,8 +62,10 @@ MessageLoop::~MessageLoop() {
// There should be no active RunLoops on this thread, unless this MessageLoop
// isn't bound to the current thread (see other condition at the top of this
// method).
- DCHECK((!pump_ && current() != this) || !RunLoop::IsRunningOnCurrentThread());
-#endif
+ DCHECK(
+ (!pump_ && !MessageLoopCurrent::IsBoundToCurrentThreadInternal(this)) ||
+ !RunLoop::IsRunningOnCurrentThread());
+#endif // !defined(OS_IOS)
#if defined(OS_WIN)
if (in_high_res_mode_)
@@ -111,16 +100,13 @@ MessageLoop::~MessageLoop() {
task_runner_ = nullptr;
// OK, now make it so that no one can find us.
- if (current() == this)
- GetTLSMessageLoop()->Set(nullptr);
+ if (MessageLoopCurrent::IsBoundToCurrentThreadInternal(this))
+ MessageLoopCurrent::UnbindFromCurrentThreadInternal(this);
}
// static
-MessageLoop* MessageLoop::current() {
- // TODO(darin): sadly, we cannot enable this yet since people call us even
- // when they have no intention of using us.
- // DCHECK(loop) << "Ouch, did you forget to initialize me?";
- return GetTLSMessageLoop()->Get();
+MessageLoopCurrent MessageLoop::current() {
+ return MessageLoopCurrent::Get();
}
// static
@@ -166,50 +152,20 @@ std::unique_ptr<MessagePump> MessageLoop::CreateMessagePumpForType(Type type) {
#endif
}
-void MessageLoop::AddDestructionObserver(
- DestructionObserver* destruction_observer) {
- DCHECK_EQ(this, current());
- destruction_observers_.AddObserver(destruction_observer);
-}
-
-void MessageLoop::RemoveDestructionObserver(
- DestructionObserver* destruction_observer) {
- DCHECK_EQ(this, current());
- destruction_observers_.RemoveObserver(destruction_observer);
-}
-
bool MessageLoop::IsType(Type type) const {
return type_ == type;
}
-// static
-Closure MessageLoop::QuitWhenIdleClosure() {
- return Bind(&RunLoop::QuitCurrentWhenIdleDeprecated);
-}
-
-void MessageLoop::SetNestableTasksAllowed(bool allowed) {
- if (allowed) {
- // Kick the native pump just in case we enter a OS-driven nested message
- // loop that does not go through RunLoop::Run().
- pump_->ScheduleWork();
- }
- task_execution_allowed_ = allowed;
-}
-
-bool MessageLoop::NestableTasksAllowed() const {
- return task_execution_allowed_;
-}
-
// TODO(gab): Migrate TaskObservers to RunLoop as part of separating concerns
// between MessageLoop and RunLoop and making MessageLoop a swappable
// implementation detail. http://crbug.com/703346
void MessageLoop::AddTaskObserver(TaskObserver* task_observer) {
- DCHECK_EQ(this, current());
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
task_observers_.AddObserver(task_observer);
}
void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) {
- DCHECK_EQ(this, current());
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
task_observers_.RemoveObserver(task_observer);
}
@@ -237,7 +193,8 @@ std::unique_ptr<MessageLoop> MessageLoop::CreateUnbound(
}
MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory)
- : type_(type),
+ : MessageLoopCurrent(this),
+ type_(type),
pump_factory_(std::move(pump_factory)),
incoming_task_queue_(new internal::IncomingTaskQueue(this)),
unbound_task_runner_(
@@ -245,17 +202,23 @@ MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory)
task_runner_(unbound_task_runner_) {
// If type is TYPE_CUSTOM non-null pump_factory must be given.
DCHECK(type_ != TYPE_CUSTOM || !pump_factory_.is_null());
+
+ // Bound in BindToCurrentThread();
+ DETACH_FROM_THREAD(bound_thread_checker_);
}
void MessageLoop::BindToCurrentThread() {
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
+
DCHECK(!pump_);
if (!pump_factory_.is_null())
pump_ = std::move(pump_factory_).Run();
else
pump_ = CreateMessagePumpForType(type_);
- DCHECK(!current()) << "should only have one message loop per thread";
- GetTLSMessageLoop()->Set(this);
+ DCHECK(!MessageLoopCurrent::IsSet())
+ << "should only have one message loop per thread";
+ MessageLoopCurrent::BindToCurrentThreadInternal(this);
incoming_task_queue_->StartScheduling();
unbound_task_runner_->BindToCurrentThread();
@@ -279,7 +242,8 @@ std::string MessageLoop::GetThreadName() const {
void MessageLoop::SetTaskRunner(
scoped_refptr<SingleThreadTaskRunner> task_runner) {
- DCHECK_EQ(this, current());
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
+
DCHECK(task_runner);
DCHECK(task_runner->BelongsToCurrentThread());
DCHECK(!unbound_task_runner_);
@@ -288,14 +252,15 @@ void MessageLoop::SetTaskRunner(
}
void MessageLoop::ClearTaskRunnerForTesting() {
- DCHECK_EQ(this, current());
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
+
DCHECK(!unbound_task_runner_);
task_runner_ = nullptr;
thread_task_runner_handle_.reset();
}
void MessageLoop::Run(bool application_tasks_allowed) {
- DCHECK_EQ(this, current());
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
if (application_tasks_allowed && !task_execution_allowed_) {
// Allow nested task execution as explicitly requested.
DCHECK(RunLoop::IsNestedOnCurrentThread());
@@ -308,18 +273,18 @@ void MessageLoop::Run(bool application_tasks_allowed) {
}
void MessageLoop::Quit() {
- DCHECK_EQ(this, current());
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
pump_->Quit();
}
void MessageLoop::EnsureWorkScheduled() {
- DCHECK_EQ(this, current());
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
if (incoming_task_queue_->triage_tasks().HasTasks())
pump_->ScheduleWork();
}
void MessageLoop::SetThreadTaskRunnerHandle() {
- DCHECK_EQ(this, current());
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
// Clear the previous thread task runner first, because only one can exist at
// a time.
thread_task_runner_handle_.reset();
@@ -471,12 +436,29 @@ bool MessageLoop::DoIdleWork() {
}
#if !defined(OS_NACL)
+
//------------------------------------------------------------------------------
// MessageLoopForUI
MessageLoopForUI::MessageLoopForUI(std::unique_ptr<MessagePump> pump)
: MessageLoop(TYPE_UI, BindOnce(&ReturnPump, std::move(pump))) {}
+// static
+MessageLoopCurrentForUI MessageLoopForUI::current() {
+ return MessageLoopCurrentForUI::Get();
+}
+
+// static
+bool MessageLoopForUI::IsCurrent() {
+ return MessageLoopCurrentForUI::IsSet();
+}
+
+#if defined(OS_IOS)
+void MessageLoopForUI::Attach() {
+ static_cast<MessagePumpUIApplication*>(pump_.get())->Attach(this);
+}
+#endif // defined(OS_IOS)
+
#if defined(OS_ANDROID)
void MessageLoopForUI::Start() {
// No Histogram support for UI message loop as it is managed by Java side
@@ -486,87 +468,27 @@ void MessageLoopForUI::Start() {
void MessageLoopForUI::Abort() {
static_cast<MessagePumpForUI*>(pump_.get())->Abort();
}
-#endif
+#endif // defined(OS_ANDROID)
-#if defined(OS_IOS)
-void MessageLoopForUI::Attach() {
- static_cast<MessagePumpUIApplication*>(pump_.get())->Attach(this);
+#if defined(OS_WIN)
+void MessageLoopForUI::EnableWmQuit() {
+ static_cast<MessagePumpForUI*>(pump_.get())->EnableWmQuit();
}
-#endif
-
-#if (defined(USE_OZONE) && !defined(OS_FUCHSIA)) || \
- (defined(USE_X11) && !defined(USE_GLIB))
-bool MessageLoopForUI::WatchFileDescriptor(
- int fd,
- bool persistent,
- MessagePumpLibevent::Mode mode,
- MessagePumpLibevent::FdWatchController* controller,
- MessagePumpLibevent::FdWatcher* delegate) {
- return static_cast<MessagePumpForUI*>(pump_.get())
- ->WatchFileDescriptor(fd, persistent, mode, controller, delegate);
-}
-#endif
+#endif // defined(OS_WIN)
#endif // !defined(OS_NACL)
//------------------------------------------------------------------------------
// MessageLoopForIO
-#if !defined(OS_NACL_SFI)
-
-namespace {
-
-MessagePumpForIO* ToPumpIO(MessagePump* pump) {
- return static_cast<MessagePumpForIO*>(pump);
-}
-
-} // namespace
-
-#if defined(OS_WIN)
-void MessageLoopForIO::RegisterIOHandler(HANDLE file,
- MessagePumpForIO::IOHandler* handler) {
- ToPumpIO(pump_.get())->RegisterIOHandler(file, handler);
-}
-
-bool MessageLoopForIO::RegisterJobObject(HANDLE job,
- MessagePumpForIO::IOHandler* handler) {
- return ToPumpIO(pump_.get())->RegisterJobObject(job, handler);
-}
-
-bool MessageLoopForIO::WaitForIOCompletion(
- DWORD timeout,
- MessagePumpForIO::IOHandler* filter) {
- return ToPumpIO(pump_.get())->WaitForIOCompletion(timeout, filter);
-}
-#elif defined(OS_POSIX)
-bool MessageLoopForIO::WatchFileDescriptor(
- int fd,
- bool persistent,
- MessagePumpForIO::Mode mode,
- MessagePumpForIO::FdWatchController* controller,
- MessagePumpForIO::FdWatcher* delegate) {
- return ToPumpIO(pump_.get())->WatchFileDescriptor(
- fd,
- persistent,
- mode,
- controller,
- delegate);
+// static
+MessageLoopCurrentForIO MessageLoopForIO::current() {
+ return MessageLoopCurrentForIO::Get();
}
-#endif
-#endif // !defined(OS_NACL_SFI)
-
-#if defined(OS_FUCHSIA)
-// Additional watch API for native platform resources.
-bool MessageLoopForIO::WatchZxHandle(
- zx_handle_t handle,
- bool persistent,
- zx_signals_t signals,
- MessagePumpForIO::ZxHandleWatchController* controller,
- MessagePumpForIO::ZxHandleWatcher* delegate) {
- return ToPumpIO(pump_.get())
- ->WatchZxHandle(handle, persistent, signals, controller, delegate);
+// static
+bool MessageLoopForIO::IsCurrent() {
+ return MessageLoopCurrentForIO::IsSet();
}
-#endif
} // namespace base