Commit e2b632b5 authored by Gabriel Charette's avatar Gabriel Charette Committed by Commit Bot

Make RunLoop 100% standalone.

MessageLoop is no longer a friend of RunLoop :)!

The only minor dependency remaining shows up in tests as allowing
nestable tasks in a scope (different from allowing nested loops on a
thread in general) is still a MessageLoop concept. I plan to move that
to RunLoop as well in a follow-up.

R=danakj@chromium.org

Bug: 703346
Change-Id: I0a6269c94de179d97c14855499edd70be9e1d1af
Reviewed-on: https://chromium-review.googlesource.com/594352Reviewed-by: default avatardanakj <danakj@chromium.org>
Commit-Queue: Gabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491258}
parent a2cd2257
......@@ -105,8 +105,7 @@ 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) ||
!run_loop_client_->GetTopMostRunLoop());
DCHECK((!pump_ && current() != this) || !RunLoop::IsRunningOnCurrentThread());
#endif
#if defined(OS_WIN)
......@@ -566,7 +565,7 @@ bool MessageLoop::DoIdleWork() {
if (ProcessNextDelayedNonNestableTask())
return true;
if (run_loop_client_->GetTopMostRunLoop()->quit_when_idle_received_)
if (run_loop_client_->ShouldQuitWhenIdle())
pump_->Quit();
// When we return we will do a kernel wait for more tasks.
......
......@@ -48,11 +48,10 @@ RunLoop::Delegate::~Delegate() {
tls_delegate.Get().Set(nullptr);
}
RunLoop* RunLoop::Delegate::Client::GetTopMostRunLoop() const {
bool RunLoop::Delegate::Client::ShouldQuitWhenIdle() const {
DCHECK_CALLED_ON_VALID_THREAD(outer_->bound_thread_checker_);
DCHECK(outer_->bound_);
return outer_->active_run_loops_.empty() ? nullptr
: outer_->active_run_loops_.top();
return outer_->active_run_loops_.top()->quit_when_idle_received_;
}
bool RunLoop::Delegate::Client::IsNested() const {
......@@ -82,8 +81,8 @@ RunLoop::RunLoop()
: delegate_(tls_delegate.Get().Get()),
origin_task_runner_(ThreadTaskRunnerHandle::Get()),
weak_factory_(this) {
// A RunLoop::Delegate must be bound to this thread prior to using RunLoop.
DCHECK(delegate_);
DCHECK(delegate_) << "A RunLoop::Delegate must be bound to this thread prior "
"to using RunLoop.";
DCHECK(origin_task_runner_);
}
......@@ -272,7 +271,7 @@ void RunLoop::AfterRun() {
RunLoop* previous_run_loop =
active_run_loops_.empty() ? nullptr : active_run_loops_.top();
// Execute deferred QuitNow, if any:
// Execute deferred Quit, if any:
if (previous_run_loop && previous_run_loop->quit_called_)
delegate_->Quit();
}
......
......@@ -134,12 +134,12 @@ class BASE_EXPORT RunLoop {
// The client interface provided back to the caller who registers this
// Delegate via RegisterDelegateForCurrentThread.
class Client {
class BASE_EXPORT Client {
public:
// Returns the RunLoop with the topmost active Run() call on the stack.
// TODO(gab): Break the inter-dependency between MessageLoop and RunLoop
// further. http://crbug.com/703346
RunLoop* GetTopMostRunLoop() const;
// Returns true if the Delegate should return from the topmost Run() when
// it becomes idle. The Delegate is responsible for probing this when it
// becomes idle.
bool ShouldQuitWhenIdle() const;
// Returns true if this |outer_| is currently in nested runs. This is a
// shortcut for RunLoop::IsNestedOnCurrentThread() for the owner of this
......@@ -166,7 +166,9 @@ class BASE_EXPORT RunLoop {
// eventual matching Quit() call. Upon receiving a Quit() call it should
// return from the Run() call as soon as possible without executing
// remaining tasks/messages. Run() calls can nest in which case each Quit()
// call should result in the topmost active Run() call returning.
// call should result in the topmost active Run() call returning. The
// only other trigger for Run() to return is Client::ShouldQuitWhenIdle()
// which the Delegate should probe before sleeping when it becomes idle.
virtual void Run() = 0;
virtual void Quit() = 0;
......@@ -207,17 +209,14 @@ class BASE_EXPORT RunLoop {
static void QuitCurrentWhenIdleDeprecated();
private:
// TODO(gab): Break the inter-dependency between MessageLoop and RunLoop
// further. http://crbug.com/703346
friend class MessageLoop;
#if defined(OS_ANDROID)
// Android doesn't support the blocking MessageLoop::Run, so it calls
// Android doesn't support the blocking RunLoop::Run, so it calls
// BeforeRun and AfterRun directly.
friend class base::MessagePumpForUI;
#endif
#if defined(OS_IOS)
// iOS doesn't support the blocking MessageLoop::Run, so it calls
// iOS doesn't support the blocking RunLoop::Run, so it calls
// BeforeRun directly.
friend class base::MessagePumpUIApplication;
#endif
......@@ -237,8 +236,11 @@ class BASE_EXPORT RunLoop {
bool quit_called_ = false;
bool running_ = false;
// Used to record that QuitWhenIdle() was called on the MessageLoop, meaning
// that we should quit Run once it becomes idle.
// Used to record that QuitWhenIdle() was called on this RunLoop, meaning that
// the Delegate should quit Run() once it becomes idle (it's responsible for
// probing this state via Client::ShouldQuitWhenIdle()). This state is stored
// here rather than pushed to Delegate via, e.g., Delegate::QuitWhenIdle() to
// support nested RunLoops.
bool quit_when_idle_received_ = false;
// RunLoop is not thread-safe. Its state/methods, unless marked as such, may
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment