OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/watchdog.h" | |
6 | |
7 #include "base/compiler_specific.h" | |
8 #include "base/logging.h" | |
9 #include "base/platform_thread.h" | |
10 | |
11 using base::TimeDelta; | |
12 using base::TimeTicks; | |
13 | |
14 //------------------------------------------------------------------------------ | |
15 // Public API methods. | |
16 | |
17 // Start thread running in a Disarmed state. | |
18 Watchdog::Watchdog(const TimeDelta& duration, | |
19 const std::string& thread_watched_name, | |
20 bool enabled) | |
21 : init_successful_(false), | |
22 lock_(), | |
23 condition_variable_(&lock_), | |
24 state_(DISARMED), | |
25 duration_(duration), | |
26 thread_watched_name_(thread_watched_name), | |
27 ALLOW_THIS_IN_INITIALIZER_LIST(delegate_(this)) { | |
28 if (!enabled) | |
29 return; // Don't start thread, or doing anything really. | |
30 init_successful_ = PlatformThread::Create(0, // Default stack size. | |
31 &delegate_, | |
32 &handle_); | |
33 DCHECK(init_successful_); | |
34 } | |
35 | |
36 // Notify watchdog thread, and wait for it to finish up. | |
37 Watchdog::~Watchdog() { | |
38 if (!init_successful_) | |
39 return; | |
40 { | |
41 AutoLock lock(lock_); | |
42 state_ = SHUTDOWN; | |
43 } | |
44 condition_variable_.Signal(); | |
45 PlatformThread::Join(handle_); | |
46 } | |
47 | |
48 void Watchdog::Arm() { | |
49 ArmAtStartTime(TimeTicks::Now()); | |
50 } | |
51 | |
52 void Watchdog::ArmSomeTimeDeltaAgo(const TimeDelta& time_delta) { | |
53 ArmAtStartTime(TimeTicks::Now() - time_delta); | |
54 } | |
55 | |
56 // Start clock for watchdog. | |
57 void Watchdog::ArmAtStartTime(const TimeTicks start_time) { | |
58 { | |
59 AutoLock lock(lock_); | |
60 start_time_ = start_time; | |
61 state_ = ARMED; | |
62 } | |
63 // Force watchdog to wake up, and go to sleep with the timer ticking with the | |
64 // proper duration. | |
65 condition_variable_.Signal(); | |
66 } | |
67 | |
68 // Disable watchdog so that it won't do anything when time expires. | |
69 void Watchdog::Disarm() { | |
70 AutoLock lock(lock_); | |
71 state_ = DISARMED; | |
72 // We don't need to signal, as the watchdog will eventually wake up, and it | |
73 // will check its state and time, and act accordingly. | |
74 } | |
75 | |
76 void Watchdog::Alarm() { | |
77 DVLOG(1) << "Watchdog alarmed for " << thread_watched_name_; | |
78 } | |
79 | |
80 //------------------------------------------------------------------------------ | |
81 // Internal private methods that the watchdog thread uses. | |
82 | |
83 void Watchdog::ThreadDelegate::ThreadMain() { | |
84 SetThreadName(); | |
85 TimeDelta remaining_duration; | |
86 while (1) { | |
87 AutoLock lock(watchdog_->lock_); | |
88 while (DISARMED == watchdog_->state_) | |
89 watchdog_->condition_variable_.Wait(); | |
90 if (SHUTDOWN == watchdog_->state_) | |
91 return; | |
92 DCHECK(ARMED == watchdog_->state_); | |
93 remaining_duration = watchdog_->duration_ - | |
94 (TimeTicks::Now() - watchdog_->start_time_); | |
95 if (remaining_duration.InMilliseconds() > 0) { | |
96 // Spurios wake? Timer drifts? Go back to sleep for remaining time. | |
97 watchdog_->condition_variable_.TimedWait(remaining_duration); | |
98 continue; | |
99 } | |
100 // We overslept, so this seems like a real alarm. | |
101 // Watch out for a user that stopped the debugger on a different alarm! | |
102 { | |
103 AutoLock static_lock(static_lock_); | |
104 if (last_debugged_alarm_time_ > watchdog_->start_time_) { | |
105 // False alarm: we started our clock before the debugger break (last | |
106 // alarm time). | |
107 watchdog_->start_time_ += last_debugged_alarm_delay_; | |
108 if (last_debugged_alarm_time_ > watchdog_->start_time_) | |
109 // Too many alarms must have taken place. | |
110 watchdog_->state_ = DISARMED; | |
111 continue; | |
112 } | |
113 } | |
114 watchdog_->state_ = DISARMED; // Only alarm at most once. | |
115 TimeTicks last_alarm_time = TimeTicks::Now(); | |
116 watchdog_->Alarm(); // Set a break point here to debug on alarms. | |
117 TimeDelta last_alarm_delay = TimeTicks::Now() - last_alarm_time; | |
118 if (last_alarm_delay <= TimeDelta::FromMilliseconds(2)) | |
119 continue; | |
120 // Ignore race of two alarms/breaks going off at roughly the same time. | |
121 AutoLock static_lock(static_lock_); | |
122 // This was a real debugger break. | |
123 last_debugged_alarm_time_ = last_alarm_time; | |
124 last_debugged_alarm_delay_ = last_alarm_delay; | |
125 } | |
126 } | |
127 | |
128 void Watchdog::ThreadDelegate::SetThreadName() const { | |
129 std::string name = watchdog_->thread_watched_name_ + " Watchdog"; | |
130 PlatformThread::SetName(name.c_str()); | |
131 DVLOG(1) << "Watchdog active: " << name; | |
132 } | |
133 | |
134 // static | |
135 void Watchdog::ResetStaticData() { | |
136 AutoLock lock(static_lock_); | |
137 last_debugged_alarm_time_ = TimeTicks(); | |
138 last_debugged_alarm_delay_ = TimeDelta(); | |
139 } | |
140 | |
141 // static | |
142 Lock Watchdog::static_lock_; // Lock for access of static data... | |
143 // static | |
144 TimeTicks Watchdog::last_debugged_alarm_time_ = TimeTicks(); | |
145 // static | |
146 TimeDelta Watchdog::last_debugged_alarm_delay_; | |
OLD | NEW |