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() { ...@@ -105,8 +105,7 @@ MessageLoop::~MessageLoop() {
// There should be no active RunLoops on this thread, unless this 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 // isn't bound to the current thread (see other condition at the top of this
// method). // method).
DCHECK((!pump_ && current() != this) || DCHECK((!pump_ && current() != this) || !RunLoop::IsRunningOnCurrentThread());
!run_loop_client_->GetTopMostRunLoop());
#endif #endif
#if defined(OS_WIN) #if defined(OS_WIN)
...@@ -566,7 +565,7 @@ bool MessageLoop::DoIdleWork() { ...@@ -566,7 +565,7 @@ bool MessageLoop::DoIdleWork() {
if (ProcessNextDelayedNonNestableTask()) if (ProcessNextDelayedNonNestableTask())
return true; return true;
if (run_loop_client_->GetTopMostRunLoop()->quit_when_idle_received_) if (run_loop_client_->ShouldQuitWhenIdle())
pump_->Quit(); pump_->Quit();
// When we return we will do a kernel wait for more tasks. // When we return we will do a kernel wait for more tasks.
......
...@@ -48,11 +48,10 @@ RunLoop::Delegate::~Delegate() { ...@@ -48,11 +48,10 @@ RunLoop::Delegate::~Delegate() {
tls_delegate.Get().Set(nullptr); 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_CALLED_ON_VALID_THREAD(outer_->bound_thread_checker_);
DCHECK(outer_->bound_); DCHECK(outer_->bound_);
return outer_->active_run_loops_.empty() ? nullptr return outer_->active_run_loops_.top()->quit_when_idle_received_;
: outer_->active_run_loops_.top();
} }
bool RunLoop::Delegate::Client::IsNested() const { bool RunLoop::Delegate::Client::IsNested() const {
...@@ -82,8 +81,8 @@ RunLoop::RunLoop() ...@@ -82,8 +81,8 @@ RunLoop::RunLoop()
: delegate_(tls_delegate.Get().Get()), : delegate_(tls_delegate.Get().Get()),
origin_task_runner_(ThreadTaskRunnerHandle::Get()), origin_task_runner_(ThreadTaskRunnerHandle::Get()),
weak_factory_(this) { weak_factory_(this) {
// A RunLoop::Delegate must be bound to this thread prior to using RunLoop. DCHECK(delegate_) << "A RunLoop::Delegate must be bound to this thread prior "
DCHECK(delegate_); "to using RunLoop.";
DCHECK(origin_task_runner_); DCHECK(origin_task_runner_);
} }
...@@ -272,7 +271,7 @@ void RunLoop::AfterRun() { ...@@ -272,7 +271,7 @@ void RunLoop::AfterRun() {
RunLoop* previous_run_loop = RunLoop* previous_run_loop =
active_run_loops_.empty() ? nullptr : active_run_loops_.top(); 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_) if (previous_run_loop && previous_run_loop->quit_called_)
delegate_->Quit(); delegate_->Quit();
} }
......
...@@ -134,12 +134,12 @@ class BASE_EXPORT RunLoop { ...@@ -134,12 +134,12 @@ class BASE_EXPORT RunLoop {
// The client interface provided back to the caller who registers this // The client interface provided back to the caller who registers this
// Delegate via RegisterDelegateForCurrentThread. // Delegate via RegisterDelegateForCurrentThread.
class Client { class BASE_EXPORT Client {
public: public:
// Returns the RunLoop with the topmost active Run() call on the stack. // Returns true if the Delegate should return from the topmost Run() when
// TODO(gab): Break the inter-dependency between MessageLoop and RunLoop // it becomes idle. The Delegate is responsible for probing this when it
// further. http://crbug.com/703346 // becomes idle.
RunLoop* GetTopMostRunLoop() const; bool ShouldQuitWhenIdle() const;
// Returns true if this |outer_| is currently in nested runs. This is a // Returns true if this |outer_| is currently in nested runs. This is a
// shortcut for RunLoop::IsNestedOnCurrentThread() for the owner of this // shortcut for RunLoop::IsNestedOnCurrentThread() for the owner of this
...@@ -166,7 +166,9 @@ class BASE_EXPORT RunLoop { ...@@ -166,7 +166,9 @@ class BASE_EXPORT RunLoop {
// eventual matching Quit() call. Upon receiving a Quit() call it should // eventual matching Quit() call. Upon receiving a Quit() call it should
// return from the Run() call as soon as possible without executing // return from the Run() call as soon as possible without executing
// remaining tasks/messages. Run() calls can nest in which case each Quit() // 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 Run() = 0;
virtual void Quit() = 0; virtual void Quit() = 0;
...@@ -207,17 +209,14 @@ class BASE_EXPORT RunLoop { ...@@ -207,17 +209,14 @@ class BASE_EXPORT RunLoop {
static void QuitCurrentWhenIdleDeprecated(); static void QuitCurrentWhenIdleDeprecated();
private: private:
// TODO(gab): Break the inter-dependency between MessageLoop and RunLoop
// further. http://crbug.com/703346
friend class MessageLoop;
#if defined(OS_ANDROID) #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. // BeforeRun and AfterRun directly.
friend class base::MessagePumpForUI; friend class base::MessagePumpForUI;
#endif #endif
#if defined(OS_IOS) #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. // BeforeRun directly.
friend class base::MessagePumpUIApplication; friend class base::MessagePumpUIApplication;
#endif #endif
...@@ -237,8 +236,11 @@ class BASE_EXPORT RunLoop { ...@@ -237,8 +236,11 @@ class BASE_EXPORT RunLoop {
bool quit_called_ = false; bool quit_called_ = false;
bool running_ = false; bool running_ = false;
// Used to record that QuitWhenIdle() was called on the MessageLoop, meaning // Used to record that QuitWhenIdle() was called on this RunLoop, meaning that
// that we should quit Run once it becomes idle. // 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; bool quit_when_idle_received_ = false;
// RunLoop is not thread-safe. Its state/methods, unless marked as such, may // 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