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 "base/threading/watchdog.h" | 5 #include "base/threading/watchdog.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
10 | 11 |
11 namespace base { | 12 namespace base { |
12 | 13 |
| 14 namespace { |
| 15 |
| 16 // When the debugger breaks (when we alarm), all the other alarms that are |
| 17 // armed will expire (also alarm). To diminish this effect, we track any |
| 18 // delay due to debugger breaks, and we *try* to adjust the effective start |
| 19 // time of other alarms to step past the debugging break. |
| 20 // Without this safety net, any alarm will typically trigger a host of follow |
| 21 // on alarms from callers that specify old times. |
| 22 |
| 23 // Lock for access of static data... |
| 24 LazyInstance<Lock, LeakyLazyInstanceTraits<Lock> > g_static_lock( |
| 25 LINKER_INITIALIZED); |
| 26 |
| 27 // When did we last alarm and get stuck (for a while) in a debugger? |
| 28 TimeTicks g_last_debugged_alarm_time; |
| 29 |
| 30 // How long did we sit on a break in the debugger? |
| 31 TimeDelta g_last_debugged_alarm_delay; |
| 32 |
| 33 } // namespace |
| 34 |
13 // Start thread running in a Disarmed state. | 35 // Start thread running in a Disarmed state. |
14 Watchdog::Watchdog(const TimeDelta& duration, | 36 Watchdog::Watchdog(const TimeDelta& duration, |
15 const std::string& thread_watched_name, | 37 const std::string& thread_watched_name, |
16 bool enabled) | 38 bool enabled) |
17 : init_successful_(false), | 39 : init_successful_(false), |
18 lock_(), | 40 lock_(), |
19 condition_variable_(&lock_), | 41 condition_variable_(&lock_), |
20 state_(DISARMED), | 42 state_(DISARMED), |
21 duration_(duration), | 43 duration_(duration), |
22 thread_watched_name_(thread_watched_name), | 44 thread_watched_name_(thread_watched_name), |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 remaining_duration = watchdog_->duration_ - | 111 remaining_duration = watchdog_->duration_ - |
90 (TimeTicks::Now() - watchdog_->start_time_); | 112 (TimeTicks::Now() - watchdog_->start_time_); |
91 if (remaining_duration.InMilliseconds() > 0) { | 113 if (remaining_duration.InMilliseconds() > 0) { |
92 // Spurios wake? Timer drifts? Go back to sleep for remaining time. | 114 // Spurios wake? Timer drifts? Go back to sleep for remaining time. |
93 watchdog_->condition_variable_.TimedWait(remaining_duration); | 115 watchdog_->condition_variable_.TimedWait(remaining_duration); |
94 continue; | 116 continue; |
95 } | 117 } |
96 // We overslept, so this seems like a real alarm. | 118 // We overslept, so this seems like a real alarm. |
97 // Watch out for a user that stopped the debugger on a different alarm! | 119 // Watch out for a user that stopped the debugger on a different alarm! |
98 { | 120 { |
99 AutoLock static_lock(static_lock_); | 121 AutoLock static_lock(*g_static_lock.Pointer()); |
100 if (last_debugged_alarm_time_ > watchdog_->start_time_) { | 122 if (g_last_debugged_alarm_time > watchdog_->start_time_) { |
101 // False alarm: we started our clock before the debugger break (last | 123 // False alarm: we started our clock before the debugger break (last |
102 // alarm time). | 124 // alarm time). |
103 watchdog_->start_time_ += last_debugged_alarm_delay_; | 125 watchdog_->start_time_ += g_last_debugged_alarm_delay; |
104 if (last_debugged_alarm_time_ > watchdog_->start_time_) | 126 if (g_last_debugged_alarm_time > watchdog_->start_time_) |
105 // Too many alarms must have taken place. | 127 // Too many alarms must have taken place. |
106 watchdog_->state_ = DISARMED; | 128 watchdog_->state_ = DISARMED; |
107 continue; | 129 continue; |
108 } | 130 } |
109 } | 131 } |
110 watchdog_->state_ = DISARMED; // Only alarm at most once. | 132 watchdog_->state_ = DISARMED; // Only alarm at most once. |
111 TimeTicks last_alarm_time = TimeTicks::Now(); | 133 TimeTicks last_alarm_time = TimeTicks::Now(); |
112 watchdog_->Alarm(); // Set a break point here to debug on alarms. | 134 watchdog_->Alarm(); // Set a break point here to debug on alarms. |
113 TimeDelta last_alarm_delay = TimeTicks::Now() - last_alarm_time; | 135 TimeDelta last_alarm_delay = TimeTicks::Now() - last_alarm_time; |
114 if (last_alarm_delay <= TimeDelta::FromMilliseconds(2)) | 136 if (last_alarm_delay <= TimeDelta::FromMilliseconds(2)) |
115 continue; | 137 continue; |
116 // Ignore race of two alarms/breaks going off at roughly the same time. | 138 // Ignore race of two alarms/breaks going off at roughly the same time. |
117 AutoLock static_lock(static_lock_); | 139 AutoLock static_lock(*g_static_lock.Pointer()); |
118 // This was a real debugger break. | 140 // This was a real debugger break. |
119 last_debugged_alarm_time_ = last_alarm_time; | 141 g_last_debugged_alarm_time = last_alarm_time; |
120 last_debugged_alarm_delay_ = last_alarm_delay; | 142 g_last_debugged_alarm_delay = last_alarm_delay; |
121 } | 143 } |
122 } | 144 } |
123 | 145 |
124 void Watchdog::ThreadDelegate::SetThreadName() const { | 146 void Watchdog::ThreadDelegate::SetThreadName() const { |
125 std::string name = watchdog_->thread_watched_name_ + " Watchdog"; | 147 std::string name = watchdog_->thread_watched_name_ + " Watchdog"; |
126 PlatformThread::SetName(name.c_str()); | 148 PlatformThread::SetName(name.c_str()); |
127 DVLOG(1) << "Watchdog active: " << name; | 149 DVLOG(1) << "Watchdog active: " << name; |
128 } | 150 } |
129 | 151 |
130 // static | 152 // static |
131 void Watchdog::ResetStaticData() { | 153 void Watchdog::ResetStaticData() { |
132 AutoLock lock(static_lock_); | 154 AutoLock lock(*g_static_lock.Pointer()); |
133 last_debugged_alarm_time_ = TimeTicks(); | 155 g_last_debugged_alarm_time = TimeTicks(); |
134 last_debugged_alarm_delay_ = TimeDelta(); | 156 g_last_debugged_alarm_delay = TimeDelta(); |
135 } | 157 } |
136 | 158 |
137 // static | |
138 Lock Watchdog::static_lock_; // Lock for access of static data... | |
139 // static | |
140 TimeTicks Watchdog::last_debugged_alarm_time_ = TimeTicks(); | |
141 // static | |
142 TimeDelta Watchdog::last_debugged_alarm_delay_; | |
143 | |
144 } // namespace base | 159 } // namespace base |
OLD | NEW |