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 LazyInstance<Lock> g_static_lock_( // Lock for access of static data... | |
Mark Mentovai
2011/11/03 22:11:03
Can you write this line like a human being?
| |
23 LINKER_INITIALIZED); | |
24 // When did we last alarm and get stuck (for a while) in a debugger? | |
Mark Mentovai
2011/11/03 22:11:03
Put a blank line before me.
| |
25 TimeTicks g_last_debugged_alarm_time_; | |
26 // How long did we sit on a break in the debugger? | |
Mark Mentovai
2011/11/03 22:11:03
Me too.
| |
27 TimeDelta g_last_debugged_alarm_delay_; | |
Mark Mentovai
2011/11/03 22:11:03
None of these should have trailing underscores any
| |
28 | |
29 } // namespace | |
30 | |
13 // Start thread running in a Disarmed state. | 31 // Start thread running in a Disarmed state. |
14 Watchdog::Watchdog(const TimeDelta& duration, | 32 Watchdog::Watchdog(const TimeDelta& duration, |
15 const std::string& thread_watched_name, | 33 const std::string& thread_watched_name, |
16 bool enabled) | 34 bool enabled) |
17 : init_successful_(false), | 35 : init_successful_(false), |
18 lock_(), | 36 lock_(), |
19 condition_variable_(&lock_), | 37 condition_variable_(&lock_), |
20 state_(DISARMED), | 38 state_(DISARMED), |
21 duration_(duration), | 39 duration_(duration), |
22 thread_watched_name_(thread_watched_name), | 40 thread_watched_name_(thread_watched_name), |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 remaining_duration = watchdog_->duration_ - | 107 remaining_duration = watchdog_->duration_ - |
90 (TimeTicks::Now() - watchdog_->start_time_); | 108 (TimeTicks::Now() - watchdog_->start_time_); |
91 if (remaining_duration.InMilliseconds() > 0) { | 109 if (remaining_duration.InMilliseconds() > 0) { |
92 // Spurios wake? Timer drifts? Go back to sleep for remaining time. | 110 // Spurios wake? Timer drifts? Go back to sleep for remaining time. |
93 watchdog_->condition_variable_.TimedWait(remaining_duration); | 111 watchdog_->condition_variable_.TimedWait(remaining_duration); |
94 continue; | 112 continue; |
95 } | 113 } |
96 // We overslept, so this seems like a real alarm. | 114 // We overslept, so this seems like a real alarm. |
97 // Watch out for a user that stopped the debugger on a different alarm! | 115 // Watch out for a user that stopped the debugger on a different alarm! |
98 { | 116 { |
99 AutoLock static_lock(static_lock_); | 117 AutoLock static_lock(*g_static_lock_.Pointer()); |
100 if (last_debugged_alarm_time_ > watchdog_->start_time_) { | 118 if (g_last_debugged_alarm_time_ > watchdog_->start_time_) { |
101 // False alarm: we started our clock before the debugger break (last | 119 // False alarm: we started our clock before the debugger break (last |
102 // alarm time). | 120 // alarm time). |
103 watchdog_->start_time_ += last_debugged_alarm_delay_; | 121 watchdog_->start_time_ += g_last_debugged_alarm_delay_; |
104 if (last_debugged_alarm_time_ > watchdog_->start_time_) | 122 if (g_last_debugged_alarm_time_ > watchdog_->start_time_) |
105 // Too many alarms must have taken place. | 123 // Too many alarms must have taken place. |
106 watchdog_->state_ = DISARMED; | 124 watchdog_->state_ = DISARMED; |
107 continue; | 125 continue; |
108 } | 126 } |
109 } | 127 } |
110 watchdog_->state_ = DISARMED; // Only alarm at most once. | 128 watchdog_->state_ = DISARMED; // Only alarm at most once. |
111 TimeTicks last_alarm_time = TimeTicks::Now(); | 129 TimeTicks last_alarm_time = TimeTicks::Now(); |
112 watchdog_->Alarm(); // Set a break point here to debug on alarms. | 130 watchdog_->Alarm(); // Set a break point here to debug on alarms. |
113 TimeDelta last_alarm_delay = TimeTicks::Now() - last_alarm_time; | 131 TimeDelta last_alarm_delay = TimeTicks::Now() - last_alarm_time; |
114 if (last_alarm_delay <= TimeDelta::FromMilliseconds(2)) | 132 if (last_alarm_delay <= TimeDelta::FromMilliseconds(2)) |
115 continue; | 133 continue; |
116 // Ignore race of two alarms/breaks going off at roughly the same time. | 134 // Ignore race of two alarms/breaks going off at roughly the same time. |
117 AutoLock static_lock(static_lock_); | 135 AutoLock static_lock(*g_static_lock_.Pointer()); |
118 // This was a real debugger break. | 136 // This was a real debugger break. |
119 last_debugged_alarm_time_ = last_alarm_time; | 137 g_last_debugged_alarm_time_ = last_alarm_time; |
120 last_debugged_alarm_delay_ = last_alarm_delay; | 138 g_last_debugged_alarm_delay_ = last_alarm_delay; |
121 } | 139 } |
122 } | 140 } |
123 | 141 |
124 void Watchdog::ThreadDelegate::SetThreadName() const { | 142 void Watchdog::ThreadDelegate::SetThreadName() const { |
125 std::string name = watchdog_->thread_watched_name_ + " Watchdog"; | 143 std::string name = watchdog_->thread_watched_name_ + " Watchdog"; |
126 PlatformThread::SetName(name.c_str()); | 144 PlatformThread::SetName(name.c_str()); |
127 DVLOG(1) << "Watchdog active: " << name; | 145 DVLOG(1) << "Watchdog active: " << name; |
128 } | 146 } |
129 | 147 |
130 // static | 148 // static |
131 void Watchdog::ResetStaticData() { | 149 void Watchdog::ResetStaticData() { |
132 AutoLock lock(static_lock_); | 150 AutoLock lock(*g_static_lock_.Pointer()); |
133 last_debugged_alarm_time_ = TimeTicks(); | 151 g_last_debugged_alarm_time_ = TimeTicks(); |
134 last_debugged_alarm_delay_ = TimeDelta(); | 152 g_last_debugged_alarm_delay_ = TimeDelta(); |
135 } | 153 } |
136 | 154 |
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 | 155 } // namespace base |
OLD | NEW |