OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/run_loop.h" | 5 #include "base/run_loop.h" |
6 | 6 |
| 7 #include <stack> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/lazy_instance.h" |
| 11 #include "base/observer_list.h" |
| 12 #include "base/threading/thread_local_storage.h" |
8 #include "base/tracked_objects.h" | 13 #include "base/tracked_objects.h" |
9 #include "build/build_config.h" | 14 #include "build/build_config.h" |
10 | 15 |
11 namespace base { | 16 namespace base { |
12 | 17 |
| 18 namespace { |
| 19 |
| 20 class ThreadLocalRunLoopState { |
| 21 public: |
| 22 // A vector-based stack is more memory efficient than the default deque-based |
| 23 // stack as the active RunLoop stack isn't expected to ever have more than a |
| 24 // few entries. |
| 25 using RunLoopStack = std::stack<RunLoop*, std::vector<RunLoop*>>; |
| 26 |
| 27 ThreadLocalRunLoopState() |
| 28 : slot_(&ThreadLocalRunLoopState::OnTLSDestruction) {} |
| 29 |
| 30 ~ThreadLocalRunLoopState() = delete; |
| 31 |
| 32 RunLoopStack& GetActiveRunLoops() { |
| 33 return GetOrCreateInternalState()->active_run_loops; |
| 34 } |
| 35 |
| 36 ObserverList<RunLoop::NestingObserver>& GetNestingObservers() { |
| 37 InternalState* state = GetOrCreateInternalState(); |
| 38 CHECK(state->allow_nesting); |
| 39 return state->nesting_observers; |
| 40 } |
| 41 |
| 42 bool IsNestingAllowed() { return GetOrCreateInternalState()->allow_nesting; } |
| 43 |
| 44 void DisallowNesting() { GetOrCreateInternalState()->allow_nesting = false; } |
| 45 |
| 46 private: |
| 47 struct InternalState { |
| 48 bool allow_nesting = true; |
| 49 RunLoopStack active_run_loops; |
| 50 ObserverList<RunLoop::NestingObserver> nesting_observers; |
| 51 }; |
| 52 |
| 53 static void OnTLSDestruction(void* internal_state) { |
| 54 delete static_cast<InternalState*>(internal_state); |
| 55 } |
| 56 |
| 57 InternalState* GetOrCreateInternalState() { |
| 58 InternalState* state = static_cast<InternalState*>(slot_.Get()); |
| 59 if (!state) { |
| 60 state = new InternalState; |
| 61 slot_.Set(static_cast<void*>(state)); |
| 62 } |
| 63 return state; |
| 64 } |
| 65 |
| 66 ThreadLocalStorage::Slot slot_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(ThreadLocalRunLoopState); |
| 69 }; |
| 70 |
| 71 LazyInstance<ThreadLocalRunLoopState>::Leaky tls_run_loop_state = |
| 72 LAZY_INSTANCE_INITIALIZER; |
| 73 |
| 74 } // namespace |
| 75 |
13 RunLoop::RunLoop() | 76 RunLoop::RunLoop() |
14 : loop_(MessageLoop::current()), | 77 : loop_(MessageLoop::current()), |
15 previous_run_loop_(NULL), | |
16 run_depth_(0), | |
17 run_called_(false), | |
18 quit_called_(false), | |
19 running_(false), | |
20 quit_when_idle_received_(false), | |
21 weak_factory_(this) { | 78 weak_factory_(this) { |
22 DCHECK(loop_); | 79 DCHECK(loop_); |
23 } | 80 } |
24 | 81 |
25 RunLoop::~RunLoop() { | 82 RunLoop::~RunLoop() { |
| 83 // TODO(gab): Fix bad usage of this API... |
| 84 // DCHECK(thread_checker_.CalledOnValidThread()); |
26 } | 85 } |
27 | 86 |
28 void RunLoop::Run() { | 87 void RunLoop::Run() { |
29 DCHECK(thread_checker_.CalledOnValidThread()); | 88 DCHECK(thread_checker_.CalledOnValidThread()); |
| 89 |
30 if (!BeforeRun()) | 90 if (!BeforeRun()) |
31 return; | 91 return; |
32 | 92 |
33 // Use task stopwatch to exclude the loop run time from the current task, if | 93 // Use task stopwatch to exclude the loop run time from the current task, if |
34 // any. | 94 // any. |
35 tracked_objects::TaskStopwatch stopwatch; | 95 tracked_objects::TaskStopwatch stopwatch; |
36 stopwatch.Start(); | 96 stopwatch.Start(); |
37 loop_->RunHandler(); | 97 loop_->RunHandler(); |
38 stopwatch.Stop(); | 98 stopwatch.Stop(); |
39 | 99 |
40 AfterRun(); | 100 AfterRun(); |
41 } | 101 } |
42 | 102 |
43 void RunLoop::RunUntilIdle() { | 103 void RunLoop::RunUntilIdle() { |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); |
| 105 |
44 quit_when_idle_received_ = true; | 106 quit_when_idle_received_ = true; |
45 Run(); | 107 Run(); |
46 } | 108 } |
47 | 109 |
48 void RunLoop::Quit() { | 110 void RunLoop::Quit() { |
49 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(thread_checker_.CalledOnValidThread()); |
| 112 |
50 quit_called_ = true; | 113 quit_called_ = true; |
51 if (running_ && loop_->run_loop_ == this) { | 114 if (running_ && loop_->run_loop_ == this) { |
52 // This is the inner-most RunLoop, so quit now. | 115 // This is the inner-most RunLoop, so quit now. |
53 loop_->QuitNow(); | 116 loop_->QuitNow(); |
54 } | 117 } |
55 } | 118 } |
56 | 119 |
57 void RunLoop::QuitWhenIdle() { | 120 void RunLoop::QuitWhenIdle() { |
58 DCHECK(thread_checker_.CalledOnValidThread()); | 121 DCHECK(thread_checker_.CalledOnValidThread()); |
59 quit_when_idle_received_ = true; | 122 quit_when_idle_received_ = true; |
60 } | 123 } |
61 | 124 |
62 base::Closure RunLoop::QuitClosure() { | 125 base::Closure RunLoop::QuitClosure() { |
| 126 // TODO(gab): Fix bad usage of this API... |
| 127 // DCHECK(thread_checker_.CalledOnValidThread()); |
63 return base::Bind(&RunLoop::Quit, weak_factory_.GetWeakPtr()); | 128 return base::Bind(&RunLoop::Quit, weak_factory_.GetWeakPtr()); |
64 } | 129 } |
65 | 130 |
66 base::Closure RunLoop::QuitWhenIdleClosure() { | 131 base::Closure RunLoop::QuitWhenIdleClosure() { |
| 132 // TODO(gab): Fix bad usage of this API... |
| 133 // DCHECK(thread_checker_.CalledOnValidThread()); |
67 return base::Bind(&RunLoop::QuitWhenIdle, weak_factory_.GetWeakPtr()); | 134 return base::Bind(&RunLoop::QuitWhenIdle, weak_factory_.GetWeakPtr()); |
68 } | 135 } |
69 | 136 |
| 137 // static |
| 138 bool RunLoop::IsRunningOnCurrentThread() { |
| 139 return !tls_run_loop_state.Get().GetActiveRunLoops().empty(); |
| 140 } |
| 141 |
| 142 // static |
| 143 bool RunLoop::IsNestedOnCurrentThread() { |
| 144 return tls_run_loop_state.Get().GetActiveRunLoops().size() > 1; |
| 145 } |
| 146 |
| 147 // static |
| 148 void RunLoop::AddNestingObserverOnCurrentThread(NestingObserver* observer) { |
| 149 tls_run_loop_state.Get().GetNestingObservers().AddObserver(observer); |
| 150 } |
| 151 |
| 152 // static |
| 153 void RunLoop::RemoveNestingObserverOnCurrentThread(NestingObserver* observer) { |
| 154 tls_run_loop_state.Get().GetNestingObservers().RemoveObserver(observer); |
| 155 } |
| 156 |
| 157 // static |
| 158 bool RunLoop::IsNestingAllowedOnCurrentThread() { |
| 159 return tls_run_loop_state.Get().IsNestingAllowed(); |
| 160 } |
| 161 |
| 162 // static |
| 163 void RunLoop::DisallowNestingOnCurrentThread() { |
| 164 tls_run_loop_state.Get().DisallowNesting(); |
| 165 } |
| 166 |
70 bool RunLoop::BeforeRun() { | 167 bool RunLoop::BeforeRun() { |
| 168 DCHECK(thread_checker_.CalledOnValidThread()); |
| 169 |
71 DCHECK(!run_called_); | 170 DCHECK(!run_called_); |
72 run_called_ = true; | 171 run_called_ = true; |
73 | 172 |
74 // Allow Quit to be called before Run. | 173 // Allow Quit to be called before Run. |
75 if (quit_called_) | 174 if (quit_called_) |
76 return false; | 175 return false; |
77 | 176 |
78 // Push RunLoop stack: | 177 auto& active_run_loops = tls_run_loop_state.Get().GetActiveRunLoops(); |
79 previous_run_loop_ = loop_->run_loop_; | 178 active_run_loops.push(this); |
80 run_depth_ = previous_run_loop_? previous_run_loop_->run_depth_ + 1 : 1; | 179 |
| 180 const bool is_nested = active_run_loops.size() > 1; |
| 181 |
| 182 // TODO(gab): Break the inter-dependency between MessageLoop and RunLoop |
| 183 // further. http://crbug.com/703346 |
81 loop_->run_loop_ = this; | 184 loop_->run_loop_ = this; |
| 185 loop_->is_nested_ = is_nested; |
82 | 186 |
83 if (run_depth_ > 1) | 187 if (is_nested) { |
84 loop_->NotifyBeginNestedLoop(); | 188 CHECK(tls_run_loop_state.Get().IsNestingAllowed()); |
| 189 for (auto& observer : tls_run_loop_state.Get().GetNestingObservers()) |
| 190 observer.OnBeginNestedRunLoop(); |
| 191 } |
85 | 192 |
86 running_ = true; | 193 running_ = true; |
87 return true; | 194 return true; |
88 } | 195 } |
89 | 196 |
90 void RunLoop::AfterRun() { | 197 void RunLoop::AfterRun() { |
| 198 DCHECK(thread_checker_.CalledOnValidThread()); |
| 199 |
91 running_ = false; | 200 running_ = false; |
92 | 201 |
93 // Pop RunLoop stack: | 202 auto& active_run_loops = tls_run_loop_state.Get().GetActiveRunLoops(); |
94 loop_->run_loop_ = previous_run_loop_; | 203 DCHECK_EQ(active_run_loops.top(), this); |
| 204 active_run_loops.pop(); |
| 205 |
| 206 RunLoop* previous_run_loop = |
| 207 active_run_loops.empty() ? nullptr : active_run_loops.top(); |
| 208 loop_->run_loop_ = previous_run_loop; |
| 209 loop_->is_nested_ = active_run_loops.size() > 1; |
95 | 210 |
96 // Execute deferred QuitNow, if any: | 211 // Execute deferred QuitNow, if any: |
97 if (previous_run_loop_ && previous_run_loop_->quit_called_) | 212 if (previous_run_loop && previous_run_loop->quit_called_) |
98 loop_->QuitNow(); | 213 loop_->QuitNow(); |
99 } | 214 } |
100 | 215 |
101 } // namespace base | 216 } // namespace base |
OLD | NEW |