OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/common/worker_thread_ticker.h" | 5 #include "chrome/common/worker_thread_ticker.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 is_running_(false), | 37 is_running_(false), |
38 tick_interval_(tick_interval) { | 38 tick_interval_(tick_interval) { |
39 } | 39 } |
40 | 40 |
41 WorkerThreadTicker::~WorkerThreadTicker() { | 41 WorkerThreadTicker::~WorkerThreadTicker() { |
42 Stop(); | 42 Stop(); |
43 } | 43 } |
44 | 44 |
45 bool WorkerThreadTicker::RegisterTickHandler(Callback *tick_handler) { | 45 bool WorkerThreadTicker::RegisterTickHandler(Callback *tick_handler) { |
46 DCHECK(tick_handler); | 46 DCHECK(tick_handler); |
47 AutoLock lock(lock_); | 47 base::AutoLock lock(lock_); |
48 // You cannot change the list of handlers when the timer is running. | 48 // You cannot change the list of handlers when the timer is running. |
49 // You need to call Stop first. | 49 // You need to call Stop first. |
50 if (IsRunning()) | 50 if (IsRunning()) |
51 return false; | 51 return false; |
52 tick_handler_list_.push_back(tick_handler); | 52 tick_handler_list_.push_back(tick_handler); |
53 return true; | 53 return true; |
54 } | 54 } |
55 | 55 |
56 bool WorkerThreadTicker::UnregisterTickHandler(Callback *tick_handler) { | 56 bool WorkerThreadTicker::UnregisterTickHandler(Callback *tick_handler) { |
57 DCHECK(tick_handler); | 57 DCHECK(tick_handler); |
58 AutoLock lock(lock_); | 58 base::AutoLock lock(lock_); |
59 // You cannot change the list of handlers when the timer is running. | 59 // You cannot change the list of handlers when the timer is running. |
60 // You need to call Stop first. | 60 // You need to call Stop first. |
61 if (IsRunning()) { | 61 if (IsRunning()) { |
62 return false; | 62 return false; |
63 } | 63 } |
64 TickHandlerListType::iterator index = std::remove(tick_handler_list_.begin(), | 64 TickHandlerListType::iterator index = std::remove(tick_handler_list_.begin(), |
65 tick_handler_list_.end(), | 65 tick_handler_list_.end(), |
66 tick_handler); | 66 tick_handler); |
67 if (index == tick_handler_list_.end()) { | 67 if (index == tick_handler_list_.end()) { |
68 return false; | 68 return false; |
69 } | 69 } |
70 tick_handler_list_.erase(index, tick_handler_list_.end()); | 70 tick_handler_list_.erase(index, tick_handler_list_.end()); |
71 return true; | 71 return true; |
72 } | 72 } |
73 | 73 |
74 bool WorkerThreadTicker::Start() { | 74 bool WorkerThreadTicker::Start() { |
75 // Do this in a lock because we don't want 2 threads to | 75 // Do this in a lock because we don't want 2 threads to |
76 // call Start at the same time | 76 // call Start at the same time |
77 AutoLock lock(lock_); | 77 base::AutoLock lock(lock_); |
78 if (IsRunning()) | 78 if (IsRunning()) |
79 return false; | 79 return false; |
80 if (!timer_thread_.Start()) | 80 if (!timer_thread_.Start()) |
81 return false; | 81 return false; |
82 is_running_ = true; | 82 is_running_ = true; |
83 ScheduleTimerTask(); | 83 ScheduleTimerTask(); |
84 return true; | 84 return true; |
85 } | 85 } |
86 | 86 |
87 bool WorkerThreadTicker::Stop() { | 87 bool WorkerThreadTicker::Stop() { |
88 // Do this in a lock because we don't want 2 threads to | 88 // Do this in a lock because we don't want 2 threads to |
89 // call Stop at the same time | 89 // call Stop at the same time |
90 AutoLock lock(lock_); | 90 base::AutoLock lock(lock_); |
91 if (!IsRunning()) | 91 if (!IsRunning()) |
92 return false; | 92 return false; |
93 is_running_ = false; | 93 is_running_ = false; |
94 timer_thread_.Stop(); | 94 timer_thread_.Stop(); |
95 return true; | 95 return true; |
96 } | 96 } |
97 | 97 |
98 void WorkerThreadTicker::ScheduleTimerTask() { | 98 void WorkerThreadTicker::ScheduleTimerTask() { |
99 timer_thread_.message_loop()->PostDelayedTask(FROM_HERE, new TimerTask(this), | 99 timer_thread_.message_loop()->PostDelayedTask(FROM_HERE, new TimerTask(this), |
100 tick_interval_); | 100 tick_interval_); |
101 } | 101 } |
OLD | NEW |