Chromium Code Reviews| 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/threading/worker_pool.h" | 5 #include "base/power_monitor/power_monitor.h" |
|
vandebo (ex-Chrome)
2012/10/11 20:49:57
It detected this as a copy from the wrong file, yo
Hongbo Min
2012/10/12 14:43:53
Done.
| |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/debug/trace_event.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/pending_task.h" | |
| 12 #include "base/threading/thread_local.h" | |
| 13 #include "base/tracked_objects.h" | |
| 14 | 6 |
| 15 namespace base { | 7 namespace base { |
| 16 | 8 |
| 17 namespace { | 9 #if defined(ENABLE_BATTERY_MONITORING) |
| 10 // The amount of time (in ms) to wait before running the initial | |
| 11 // battery check. | |
| 12 static int kDelayedBatteryCheckMs = 10 * 1000; | |
| 13 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 18 | 14 |
| 19 base::LazyInstance<ThreadLocalBoolean>::Leaky | 15 PowerMonitor::PowerMonitor() |
| 20 g_worker_pool_running_on_this_thread = LAZY_INSTANCE_INITIALIZER; | 16 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
| 17 battery_in_use_(false), | |
| 18 suspended_(false) { | |
| 19 DCHECK(MessageLoop::current()); | |
| 20 #if defined(ENABLE_BATTERY_MONITORING) | |
| 21 delayed_battery_check_.Start(FROM_HERE, | |
| 22 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | |
| 23 &PowerMonitor::BatteryCheck); | |
| 24 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 21 | 25 |
| 22 DWORD CALLBACK WorkItemCallback(void* param) { | 26 #if defined(OS_MACOSX) |
| 23 PendingTask* pending_task = static_cast<PendingTask*>(param); | 27 PlatformInit(); |
| 24 TRACE_EVENT2("task", "WorkItemCallback::Run", | 28 #endif |
| 25 "src_file", pending_task->posted_from.file_name(), | |
| 26 "src_func", pending_task->posted_from.function_name()); | |
| 27 | |
| 28 tracked_objects::TrackedTime start_time = | |
| 29 tracked_objects::ThreadData::NowForStartOfRun(pending_task->birth_tally); | |
| 30 | |
| 31 g_worker_pool_running_on_this_thread.Get().Set(true); | |
| 32 pending_task->task.Run(); | |
| 33 g_worker_pool_running_on_this_thread.Get().Set(false); | |
| 34 | |
| 35 tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking( | |
| 36 pending_task->birth_tally, | |
| 37 tracked_objects::TrackedTime(pending_task->time_posted), start_time, | |
| 38 tracked_objects::ThreadData::NowForEndOfRun()); | |
| 39 | |
| 40 delete pending_task; | |
| 41 return 0; | |
| 42 } | 29 } |
| 43 | 30 |
| 44 // Takes ownership of |pending_task| | 31 PowerMonitor::~PowerMonitor() { |
| 45 bool PostTaskInternal(PendingTask* pending_task, bool task_is_slow) { | 32 #if defined(OS_MACOSX) |
| 46 ULONG flags = 0; | 33 PlaformDestory(); |
| 47 if (task_is_slow) | 34 #endif |
| 48 flags |= WT_EXECUTELONGFUNCTION; | |
| 49 | |
| 50 if (!QueueUserWorkItem(WorkItemCallback, pending_task, flags)) { | |
| 51 DLOG(ERROR) << "QueueUserWorkItem failed: " << GetLastError(); | |
| 52 delete pending_task; | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 // static | |
| 62 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, | |
| 63 const base::Closure& task, bool task_is_slow) { | |
| 64 PendingTask* pending_task = new PendingTask(from_here, task); | |
| 65 return PostTaskInternal(pending_task, task_is_slow); | |
| 66 } | 35 } |
| 67 | 36 |
| 68 // static | 37 // static |
| 69 bool WorkerPool::RunsTasksOnCurrentThread() { | 38 PowerMonitor* PowerMonitor::GetInstance() { |
| 70 return g_worker_pool_running_on_this_thread.Get().Get(); | 39 return Singleton<PowerMonitor>::get(); |
| 40 } | |
| 41 | |
| 42 void PowerMonitor::AddObserver(PowerObserver* obs) { | |
| 43 observers_->AddObserver(obs); | |
| 44 } | |
| 45 | |
| 46 void PowerMonitor::RemoveObserver(PowerObserver* obs) { | |
| 47 observers_->RemoveObserver(obs); | |
| 48 } | |
| 49 | |
| 50 void PowerMonitor::ProcessPowerMessage(PowerEvent event_id) { | |
| 51 // Suppress duplicate notifications. Some platforms may | |
| 52 // send multiple notifications of the same event. | |
| 53 switch (event_id) { | |
| 54 case POWER_STATE_EVENT: | |
| 55 { | |
| 56 bool on_battery = IsBatteryPower(); | |
| 57 if (on_battery != battery_in_use_) { | |
| 58 battery_in_use_ = on_battery; | |
| 59 NotifyPowerStateChange(); | |
| 60 } | |
| 61 } | |
| 62 break; | |
| 63 case RESUME_EVENT: | |
| 64 if (suspended_) { | |
| 65 suspended_ = false; | |
| 66 NotifyResume(); | |
| 67 } | |
| 68 break; | |
| 69 case SUSPEND_EVENT: | |
| 70 if (!suspended_) { | |
| 71 suspended_ = true; | |
| 72 NotifySuspend(); | |
| 73 } | |
| 74 break; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 void PowerMonitor::NotifyPowerStateChange() { | |
| 79 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | |
| 80 << " battery"; | |
| 81 observers_->Notify(&PowerObserver::OnPowerStateChange, | |
| 82 BatteryPower()); | |
| 83 } | |
| 84 | |
| 85 void PowerMonitor::NotifySuspend() { | |
| 86 DVLOG(1) << "Power Suspending"; | |
| 87 observers_->Notify(&PowerObserver::OnSuspend); | |
| 88 } | |
| 89 | |
| 90 void PowerMonitor::NotifyResume() { | |
| 91 DVLOG(1) << "Power Resuming"; | |
| 92 observers_->Notify(&PowerObserver::OnResume); | |
| 93 } | |
| 94 | |
| 95 void PowerMonitor::BatteryCheck() { | |
| 96 ProcessPowerMessage(PowerMonitor::POWER_STATE_EVENT); | |
| 71 } | 97 } |
| 72 | 98 |
| 73 } // namespace base | 99 } // namespace base |
| 100 | |
| OLD | NEW |