OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 } | 156 } |
157 | 157 |
158 MessageLoop::~MessageLoop() { | 158 MessageLoop::~MessageLoop() { |
159 DCHECK_EQ(this, current()); | 159 DCHECK_EQ(this, current()); |
160 | 160 |
161 DCHECK(!run_loop_); | 161 DCHECK(!run_loop_); |
162 #if defined(OS_WIN) | 162 #if defined(OS_WIN) |
163 if (in_high_res_mode_) | 163 if (in_high_res_mode_) |
164 Time::ActivateHighResolutionTimer(false); | 164 Time::ActivateHighResolutionTimer(false); |
165 #endif | 165 #endif |
| 166 |
| 167 FlushPendingTasks(); |
| 168 |
| 169 // Let interested parties have one last shot at accessing this. |
| 170 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, |
| 171 WillDestroyCurrentMessageLoop()); |
| 172 |
| 173 thread_task_runner_handle_.reset(); |
| 174 |
| 175 // Tell the incoming queue that we are dying. |
| 176 incoming_task_queue_->WillDestroyCurrentMessageLoop(); |
| 177 incoming_task_queue_ = NULL; |
| 178 message_loop_proxy_ = NULL; |
| 179 |
| 180 // OK, now make it so that no one can find us. |
| 181 lazy_tls_ptr.Pointer()->Set(NULL); |
| 182 } |
| 183 |
| 184 void MessageLoop::FlushPendingTasks() { |
166 // Clean up any unprocessed tasks, but take care: deleting a task could | 185 // Clean up any unprocessed tasks, but take care: deleting a task could |
167 // result in the addition of more tasks (e.g., via DeleteSoon). We set a | 186 // result in the addition of more tasks (e.g., via DeleteSoon). We set a |
168 // limit on the number of times we will allow a deleted task to generate more | 187 // limit on the number of times we will allow a deleted task to generate more |
169 // tasks. Normally, we should only pass through this loop once or twice. If | 188 // tasks. Normally, we should only pass through this loop once or twice. If |
170 // we end up hitting the loop limit, then it is probably due to one task that | 189 // we end up hitting the loop limit, then it is probably due to one task that |
171 // is being stubborn. Inspect the queues to see who is left. | 190 // is being stubborn. Inspect the queues to see who is left. |
172 bool did_work; | 191 bool did_work; |
173 for (int i = 0; i < 100; ++i) { | 192 for (int i = 0; i < 100; ++i) { |
174 DeletePendingTasks(); | 193 DeletePendingTasks(); |
175 ReloadWorkQueue(); | 194 ReloadWorkQueue(); |
176 // If we end up with empty queues, then break out of the loop. | 195 // If we end up with empty queues, then break out of the loop. |
177 did_work = DeletePendingTasks(); | 196 did_work = DeletePendingTasks(); |
178 if (!did_work) | 197 if (!did_work) |
179 break; | 198 break; |
180 } | 199 } |
181 DCHECK(!did_work); | 200 DCHECK(!did_work); |
182 | |
183 // Let interested parties have one last shot at accessing this. | |
184 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, | |
185 WillDestroyCurrentMessageLoop()); | |
186 | |
187 thread_task_runner_handle_.reset(); | |
188 | |
189 // Tell the incoming queue that we are dying. | |
190 incoming_task_queue_->WillDestroyCurrentMessageLoop(); | |
191 incoming_task_queue_ = NULL; | |
192 message_loop_proxy_ = NULL; | |
193 | |
194 // OK, now make it so that no one can find us. | |
195 lazy_tls_ptr.Pointer()->Set(NULL); | |
196 } | 201 } |
197 | 202 |
198 // static | 203 // static |
199 MessageLoop* MessageLoop::current() { | 204 MessageLoop* MessageLoop::current() { |
200 // TODO(darin): sadly, we cannot enable this yet since people call us even | 205 // TODO(darin): sadly, we cannot enable this yet since people call us even |
201 // when they have no intention of using us. | 206 // when they have no intention of using us. |
202 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; | 207 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; |
203 return lazy_tls_ptr.Pointer()->Get(); | 208 return lazy_tls_ptr.Pointer()->Get(); |
204 } | 209 } |
205 | 210 |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 persistent, | 715 persistent, |
711 mode, | 716 mode, |
712 controller, | 717 controller, |
713 delegate); | 718 delegate); |
714 } | 719 } |
715 #endif | 720 #endif |
716 | 721 |
717 #endif // !defined(OS_NACL) | 722 #endif // !defined(OS_NACL) |
718 | 723 |
719 } // namespace base | 724 } // namespace base |
OLD | NEW |