Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1046)

Unified Diff: base/run_loop.cc

Issue 2880453003: Introduce RunLoop::Delegate splitting RunLoop/MessageLoop some more. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« base/run_loop.h ('K') | « base/run_loop.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/run_loop.cc
diff --git a/base/run_loop.cc b/base/run_loop.cc
index b7df5b23d8d72f737c5c6f468c508ec12ade8fd9..adf3b5a003d6f8f71d9794e680712bc11fe1ffbf 100644
--- a/base/run_loop.cc
+++ b/base/run_loop.cc
@@ -4,12 +4,9 @@
#include "base/run_loop.h"
-#include <stack>
-
#include "base/bind.h"
#include "base/lazy_instance.h"
-#include "base/observer_list.h"
-#include "base/threading/thread_local_storage.h"
+#include "base/threading/thread_local.h"
#include "base/tracked_objects.h"
#include "build/build_config.h"
@@ -17,74 +14,52 @@ namespace base {
namespace {
-class ThreadLocalRunLoopState {
- public:
- // A vector-based stack is more memory efficient than the default deque-based
- // stack as the active RunLoop stack isn't expected to ever have more than a
- // few entries.
- using RunLoopStack = std::stack<RunLoop*, std::vector<RunLoop*>>;
-
- ThreadLocalRunLoopState()
- : slot_(&ThreadLocalRunLoopState::OnTLSDestruction) {}
-
- ~ThreadLocalRunLoopState() = delete;
-
- RunLoopStack& GetActiveRunLoops() {
- return GetOrCreateInternalState()->active_run_loops;
- }
-
- ObserverList<RunLoop::NestingObserver>& GetNestingObservers() {
- InternalState* state = GetOrCreateInternalState();
- CHECK(state->allow_nesting);
- return state->nesting_observers;
- }
-
- bool IsNestingAllowed() { return GetOrCreateInternalState()->allow_nesting; }
-
- void DisallowNesting() { GetOrCreateInternalState()->allow_nesting = false; }
-
- void Reset() {
- InternalState* state = static_cast<InternalState*>(slot_.Get());
- if (state) {
- slot_.Set(nullptr);
- delete state;
- }
- }
+LazyInstance<ThreadLocalPointer<RunLoop::Delegate>>::Leaky tls_delegate =
+ LAZY_INSTANCE_INITIALIZER;
- private:
- struct InternalState {
- bool allow_nesting = true;
- RunLoopStack active_run_loops;
- ObserverList<RunLoop::NestingObserver> nesting_observers;
- };
+} // namespace
- static void OnTLSDestruction(void* internal_state) {
- delete static_cast<InternalState*>(internal_state);
- }
+RunLoop::Delegate::Delegate() {
+ // The Delegate can be created on another thread. It is only bound in
+ // BindToCurrentThread().
+ DETACH_FROM_THREAD(bound_thread_checker_);
+}
- InternalState* GetOrCreateInternalState() {
- InternalState* state = static_cast<InternalState*>(slot_.Get());
- if (!state) {
- state = new InternalState;
- slot_.Set(static_cast<void*>(state));
- }
- return state;
- }
+RunLoop::Delegate::~Delegate() {
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
+ // A RunLoop::Delegate may be destroyed before it is bound, if so it may still
+ // be on its creation thread (e.g. a Thread that fails to start) and
+ // shouldn't disrupt that thread's state.
+ if (bound_)
+ tls_delegate.Get().Set(nullptr);
+}
- ThreadLocalStorage::Slot slot_;
+bool RunLoop::Delegate::IsNested() const {
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
+ DCHECK(bound_);
+ return active_run_loops_.size() > 1;
+}
- DISALLOW_COPY_AND_ASSIGN(ThreadLocalRunLoopState);
-};
+RunLoop* RunLoop::Delegate::GetTopMostRunLoop() const {
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
+ DCHECK(bound_);
+ return active_run_loops_.empty() ? nullptr : active_run_loops_.top();
+}
-LazyInstance<ThreadLocalRunLoopState>::Leaky tls_run_loop_state =
- LAZY_INSTANCE_INITIALIZER;
+void RunLoop::Delegate::BindToCurrentThread() {
+ // Bind the checker to this thread.
+ DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
-} // namespace
+ // There can only be one RunLoop::Delegate per thread.
+ DCHECK(!tls_delegate.Get().Get());
+ DCHECK(!bound_);
+ tls_delegate.Get().Set(this);
+ bound_ = true;
+}
-RunLoop::RunLoop()
- : loop_(MessageLoop::current()),
- weak_factory_(this) {
- DCHECK(loop_);
+RunLoop::RunLoop() : delegate_(tls_delegate.Get().Get()), weak_factory_(this) {
+ // A RunLoop::Delegate must be bound to this thread prior to using RunLoop.
+ DCHECK(delegate_);
}
RunLoop::~RunLoop() {
@@ -102,7 +77,7 @@ void RunLoop::Run() {
// any.
tracked_objects::TaskStopwatch stopwatch;
stopwatch.Start();
- loop_->RunHandler();
+ delegate_->Run();
stopwatch.Stop();
AfterRun();
@@ -119,9 +94,9 @@ void RunLoop::Quit() {
DCHECK(thread_checker_.CalledOnValidThread());
quit_called_ = true;
- if (running_ && loop_->run_loop_ == this) {
+ if (running_ && delegate_->active_run_loops_.top() == this) {
// This is the inner-most RunLoop, so quit now.
- loop_->QuitNow();
+ delegate_->Quit();
}
}
@@ -143,38 +118,37 @@ base::Closure RunLoop::QuitWhenIdleClosure() {
}
// static
-void RunLoop::ResetTLSState() {
- tls_run_loop_state.Get().Reset();
-}
-
-// static
bool RunLoop::IsRunningOnCurrentThread() {
- return !tls_run_loop_state.Get().GetActiveRunLoops().empty();
+ return !tls_delegate.Get().Get()->active_run_loops_.empty();
}
// static
bool RunLoop::IsNestedOnCurrentThread() {
- return tls_run_loop_state.Get().GetActiveRunLoops().size() > 1;
+ return tls_delegate.Get().Get()->active_run_loops_.size() > 1;
}
// static
void RunLoop::AddNestingObserverOnCurrentThread(NestingObserver* observer) {
- tls_run_loop_state.Get().GetNestingObservers().AddObserver(observer);
+ Delegate* delegate = tls_delegate.Get().Get();
+ CHECK(delegate->allow_nesting_);
+ delegate->nesting_observers_.AddObserver(observer);
}
// static
void RunLoop::RemoveNestingObserverOnCurrentThread(NestingObserver* observer) {
- tls_run_loop_state.Get().GetNestingObservers().RemoveObserver(observer);
+ Delegate* delegate = tls_delegate.Get().Get();
+ CHECK(delegate->allow_nesting_);
+ delegate->nesting_observers_.RemoveObserver(observer);
}
// static
bool RunLoop::IsNestingAllowedOnCurrentThread() {
- return tls_run_loop_state.Get().IsNestingAllowed();
+ return tls_delegate.Get().Get()->allow_nesting_;
}
// static
void RunLoop::DisallowNestingOnCurrentThread() {
- tls_run_loop_state.Get().DisallowNesting();
+ tls_delegate.Get().Get()->allow_nesting_ = false;
}
bool RunLoop::BeforeRun() {
@@ -187,19 +161,14 @@ bool RunLoop::BeforeRun() {
if (quit_called_)
return false;
- auto& active_run_loops = tls_run_loop_state.Get().GetActiveRunLoops();
- active_run_loops.push(this);
-
- const bool is_nested = active_run_loops.size() > 1;
+ auto& active_run_loops_ = delegate_->active_run_loops_;
+ active_run_loops_.push(this);
- // TODO(gab): Break the inter-dependency between MessageLoop and RunLoop
- // further. http://crbug.com/703346
- loop_->run_loop_ = this;
- loop_->is_nested_ = is_nested;
+ const bool is_nested = active_run_loops_.size() > 1;
if (is_nested) {
- CHECK(tls_run_loop_state.Get().IsNestingAllowed());
- for (auto& observer : tls_run_loop_state.Get().GetNestingObservers())
+ CHECK(delegate_->allow_nesting_);
+ for (auto& observer : delegate_->nesting_observers_)
observer.OnBeginNestedRunLoop();
}
@@ -212,18 +181,16 @@ void RunLoop::AfterRun() {
running_ = false;
- auto& active_run_loops = tls_run_loop_state.Get().GetActiveRunLoops();
- DCHECK_EQ(active_run_loops.top(), this);
- active_run_loops.pop();
+ auto& active_run_loops_ = delegate_->active_run_loops_;
+ DCHECK_EQ(active_run_loops_.top(), this);
+ active_run_loops_.pop();
RunLoop* previous_run_loop =
- active_run_loops.empty() ? nullptr : active_run_loops.top();
- loop_->run_loop_ = previous_run_loop;
- loop_->is_nested_ = active_run_loops.size() > 1;
+ active_run_loops_.empty() ? nullptr : active_run_loops_.top();
// Execute deferred QuitNow, if any:
if (previous_run_loop && previous_run_loop->quit_called_)
- loop_->QuitNow();
+ delegate_->Quit();
}
} // namespace base
« base/run_loop.h ('K') | « base/run_loop.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698