Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "base/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" |
| 11 #include "base/debug/alias.h" | |
| 11 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/message_pump_default.h" | 14 #include "base/message_pump_default.h" |
| 14 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 15 #include "base/scoped_ptr.h" | 16 #include "base/scoped_ptr.h" |
| 16 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 17 #include "base/threading/thread_local.h" | 18 #include "base/threading/thread_local.h" |
| 18 #include "base/time.h" | 19 #include "base/time.h" |
| 19 #include "base/tracked_objects.h" | 20 #include "base/tracked_objects.h" |
| 20 | 21 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 // if this class should leak the Task on destruction. This is important | 92 // if this class should leak the Task on destruction. This is important |
| 92 // at MessageLoop shutdown since not all tasks can be safely deleted without | 93 // at MessageLoop shutdown since not all tasks can be safely deleted without |
| 93 // running. See MessageLoop::DeletePendingTasks() for the exact behavior | 94 // running. See MessageLoop::DeletePendingTasks() for the exact behavior |
| 94 // of when a Task should be deleted. It is subtle. | 95 // of when a Task should be deleted. It is subtle. |
| 95 TaskClosureAdapter(Task* task, bool* should_leak_task) | 96 TaskClosureAdapter(Task* task, bool* should_leak_task) |
| 96 : task_(task), | 97 : task_(task), |
| 97 should_leak_task_(should_leak_task) { | 98 should_leak_task_(should_leak_task) { |
| 98 } | 99 } |
| 99 | 100 |
| 100 void Run() { | 101 void Run() { |
| 102 // Before running the task, store the program counter where it was posted | |
| 103 // and deliberately alias it to ensure it is on the stack if the task | |
| 104 // crashes. Be careful not to assume that the variable itself will have the | |
| 105 // expected value when displayed by the optimizer in an optimized build. | |
| 106 // Look at a memory dump of the stack. | |
| 107 void* program_counter = task_->get_birth_program_counter(); | |
| 108 base::debug::Alias(&program_counter); | |
| 109 | |
| 101 task_->Run(); | 110 task_->Run(); |
| 102 delete task_; | 111 delete task_; |
| 103 task_ = NULL; | 112 task_ = NULL; |
| 104 } | 113 } |
| 105 | 114 |
| 106 private: | 115 private: |
| 107 friend class base::RefCounted<TaskClosureAdapter>; | 116 friend class base::RefCounted<TaskClosureAdapter>; |
| 108 | 117 |
| 109 ~TaskClosureAdapter() { | 118 ~TaskClosureAdapter() { |
| 110 if (!*should_leak_task_) { | 119 if (!*should_leak_task_) { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 } | 268 } |
| 260 | 269 |
| 261 void MessageLoop::RemoveDestructionObserver( | 270 void MessageLoop::RemoveDestructionObserver( |
| 262 DestructionObserver* destruction_observer) { | 271 DestructionObserver* destruction_observer) { |
| 263 DCHECK_EQ(this, current()); | 272 DCHECK_EQ(this, current()); |
| 264 destruction_observers_.RemoveObserver(destruction_observer); | 273 destruction_observers_.RemoveObserver(destruction_observer); |
| 265 } | 274 } |
| 266 | 275 |
| 267 void MessageLoop::PostTask( | 276 void MessageLoop::PostTask( |
| 268 const tracked_objects::Location& from_here, Task* task) { | 277 const tracked_objects::Location& from_here, Task* task) { |
| 269 CHECK(task); | 278 CHECK(task); |
|
jar (doing other things)
2011/05/18 01:32:34
These checks are probably not needed, now that you
apatrick_chromium
2011/05/18 21:09:47
SetBirthPlace calls are gone so the CHECK is not r
| |
| 279 task->SetBirthPlace(from_here); | |
| 270 PendingTask pending_task( | 280 PendingTask pending_task( |
| 271 base::Bind(&TaskClosureAdapter::Run, | 281 base::Bind(&TaskClosureAdapter::Run, |
| 272 new TaskClosureAdapter(task, &should_leak_tasks_)), | 282 new TaskClosureAdapter(task, &should_leak_tasks_)), |
| 273 from_here, | 283 from_here, |
| 274 CalculateDelayedRuntime(0), true); | 284 CalculateDelayedRuntime(0), true); |
| 275 AddToIncomingQueue(&pending_task); | 285 AddToIncomingQueue(&pending_task); |
| 276 } | 286 } |
| 277 | 287 |
| 278 void MessageLoop::PostDelayedTask( | 288 void MessageLoop::PostDelayedTask( |
| 279 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { | 289 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { |
| 280 CHECK(task); | 290 CHECK(task); |
| 291 task->SetBirthPlace(from_here); | |
| 281 PendingTask pending_task( | 292 PendingTask pending_task( |
| 282 base::Bind(&TaskClosureAdapter::Run, | 293 base::Bind(&TaskClosureAdapter::Run, |
| 283 new TaskClosureAdapter(task, &should_leak_tasks_)), | 294 new TaskClosureAdapter(task, &should_leak_tasks_)), |
| 284 from_here, | 295 from_here, |
| 285 CalculateDelayedRuntime(delay_ms), true); | 296 CalculateDelayedRuntime(delay_ms), true); |
| 286 AddToIncomingQueue(&pending_task); | 297 AddToIncomingQueue(&pending_task); |
| 287 } | 298 } |
| 288 | 299 |
| 289 void MessageLoop::PostNonNestableTask( | 300 void MessageLoop::PostNonNestableTask( |
| 290 const tracked_objects::Location& from_here, Task* task) { | 301 const tracked_objects::Location& from_here, Task* task) { |
| 291 CHECK(task); | 302 CHECK(task); |
| 303 task->SetBirthPlace(from_here); | |
| 292 PendingTask pending_task( | 304 PendingTask pending_task( |
| 293 base::Bind(&TaskClosureAdapter::Run, | 305 base::Bind(&TaskClosureAdapter::Run, |
| 294 new TaskClosureAdapter(task, &should_leak_tasks_)), | 306 new TaskClosureAdapter(task, &should_leak_tasks_)), |
| 295 from_here, | 307 from_here, |
| 296 CalculateDelayedRuntime(0), false); | 308 CalculateDelayedRuntime(0), false); |
| 297 AddToIncomingQueue(&pending_task); | 309 AddToIncomingQueue(&pending_task); |
| 298 } | 310 } |
| 299 | 311 |
| 300 void MessageLoop::PostNonNestableDelayedTask( | 312 void MessageLoop::PostNonNestableDelayedTask( |
| 301 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { | 313 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { |
| 302 CHECK(task); | 314 CHECK(task); |
| 315 task->SetBirthPlace(from_here); | |
| 303 PendingTask pending_task( | 316 PendingTask pending_task( |
| 304 base::Bind(&TaskClosureAdapter::Run, | 317 base::Bind(&TaskClosureAdapter::Run, |
| 305 new TaskClosureAdapter(task, &should_leak_tasks_)), | 318 new TaskClosureAdapter(task, &should_leak_tasks_)), |
| 306 from_here, | 319 from_here, |
| 307 CalculateDelayedRuntime(delay_ms), false); | 320 CalculateDelayedRuntime(delay_ms), false); |
| 308 AddToIncomingQueue(&pending_task); | 321 AddToIncomingQueue(&pending_task); |
| 309 } | 322 } |
| 310 | 323 |
| 311 void MessageLoop::PostTask( | 324 void MessageLoop::PostTask( |
| 312 const tracked_objects::Location& from_here, const base::Closure& task) { | 325 const tracked_objects::Location& from_here, const base::Closure& task) { |
| (...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 760 MessageLoop::PendingTask::PendingTask( | 773 MessageLoop::PendingTask::PendingTask( |
| 761 const base::Closure& task, | 774 const base::Closure& task, |
| 762 const tracked_objects::Location& posted_from, | 775 const tracked_objects::Location& posted_from, |
| 763 TimeTicks delayed_run_time, | 776 TimeTicks delayed_run_time, |
| 764 bool nestable) | 777 bool nestable) |
| 765 : task(task), | 778 : task(task), |
| 766 time_posted(TimeTicks::Now()), | 779 time_posted(TimeTicks::Now()), |
| 767 delayed_run_time(delayed_run_time), | 780 delayed_run_time(delayed_run_time), |
| 768 sequence_num(0), | 781 sequence_num(0), |
| 769 nestable(nestable) { | 782 nestable(nestable) { |
| 770 #if defined(TRACK_ALL_TASK_OBJECTS) | |
|
apatrick_chromium
2011/05/17 22:48:04
Removing this is wrong but what it does overlaps w
awong
2011/05/17 23:54:01
Corrently...we were trying to make it so Task does
awong
2011/05/17 23:54:55
Corrently -> Correct.
One of these days I will le
jar (doing other things)
2011/05/18 01:32:34
It is similar to, but is not replaced by, what you
apatrick_chromium
2011/05/18 21:09:47
I think I see how this works now. This code block
| |
| 771 if (tracked_objects::ThreadData::IsActive()) { | |
| 772 tracked_objects::ThreadData* current_thread_data = | |
| 773 tracked_objects::ThreadData::current(); | |
| 774 if (current_thread_data) { | |
| 775 post_births = current_thread_data->TallyABirth(posted_from); | |
| 776 } else { | |
| 777 // Shutdown started, and this thread wasn't registered. | |
| 778 post_births = NULL; | |
| 779 } | |
| 780 } | |
| 781 #endif // defined(TRACK_ALL_TASK_OBJECTS) | |
| 782 } | 783 } |
| 783 | 784 |
| 784 MessageLoop::PendingTask::~PendingTask() { | 785 MessageLoop::PendingTask::~PendingTask() { |
| 785 } | 786 } |
| 786 | 787 |
| 787 bool MessageLoop::PendingTask::operator<(const PendingTask& other) const { | 788 bool MessageLoop::PendingTask::operator<(const PendingTask& other) const { |
| 788 // Since the top of a priority queue is defined as the "greatest" element, we | 789 // Since the top of a priority queue is defined as the "greatest" element, we |
| 789 // need to invert the comparison here. We want the smaller time to be at the | 790 // need to invert the comparison here. We want the smaller time to be at the |
| 790 // top of the heap. | 791 // top of the heap. |
| 791 | 792 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 853 Watcher *delegate) { | 854 Watcher *delegate) { |
| 854 return pump_libevent()->WatchFileDescriptor( | 855 return pump_libevent()->WatchFileDescriptor( |
| 855 fd, | 856 fd, |
| 856 persistent, | 857 persistent, |
| 857 static_cast<base::MessagePumpLibevent::Mode>(mode), | 858 static_cast<base::MessagePumpLibevent::Mode>(mode), |
| 858 controller, | 859 controller, |
| 859 delegate); | 860 delegate); |
| 860 } | 861 } |
| 861 | 862 |
| 862 #endif | 863 #endif |
| OLD | NEW |