OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // The Watchdog class creates a second thread that can Alarm if a specific | 5 // The Watchdog class creates a second thread that can Alarm if a specific |
6 // duration of time passes without proper attention. The duration of time is | 6 // duration of time passes without proper attention. The duration of time is |
7 // specified at construction time. The Watchdog may be used many times by | 7 // specified at construction time. The Watchdog may be used many times by |
8 // simply calling Arm() (to start timing) and Disarm() (to reset the timer). | 8 // simply calling Arm() (to start timing) and Disarm() (to reset the timer). |
9 // The Watchdog is typically used under a debugger, where the stack traces on | 9 // The Watchdog is typically used under a debugger, where the stack traces on |
10 // other threads can be examined if/when the Watchdog alarms. | 10 // other threads can be examined if/when the Watchdog alarms. |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include <string> | 21 #include <string> |
22 | 22 |
23 #include "base/condition_variable.h" | 23 #include "base/condition_variable.h" |
24 #include "base/lock.h" | 24 #include "base/lock.h" |
25 #include "base/time.h" | 25 #include "base/time.h" |
26 | 26 |
27 class Watchdog { | 27 class Watchdog { |
28 public: | 28 public: |
29 // TODO(JAR)change default arg to required arg after all users have migrated. | 29 // TODO(JAR)change default arg to required arg after all users have migrated. |
30 // Constructor specifies how long the Watchdog will wait before alarming. | 30 // Constructor specifies how long the Watchdog will wait before alarming. |
31 Watchdog(const TimeDelta& duration, | 31 Watchdog(const base::TimeDelta& duration, |
32 const std::wstring& thread_watched_name, | 32 const std::wstring& thread_watched_name, |
33 bool enabled = true); | 33 bool enabled = true); |
34 virtual ~Watchdog(); | 34 virtual ~Watchdog(); |
35 | 35 |
36 // Start timing, and alarm when time expires (unless we're disarm()ed.) | 36 // Start timing, and alarm when time expires (unless we're disarm()ed.) |
37 void Arm(); // Arm starting now. | 37 void Arm(); // Arm starting now. |
38 void ArmSomeTimeDeltaAgo(const TimeDelta& time_delta); | 38 void ArmSomeTimeDeltaAgo(const base::TimeDelta& time_delta); |
39 void ArmAtStartTime(const TimeTicks start_time); | 39 void ArmAtStartTime(const base::TimeTicks start_time); |
40 | 40 |
41 // Reset time, and do not set off the alarm. | 41 // Reset time, and do not set off the alarm. |
42 void Disarm(); | 42 void Disarm(); |
43 | 43 |
44 // Alarm is called if the time expires after an Arm() without someone calling | 44 // Alarm is called if the time expires after an Arm() without someone calling |
45 // Disarm(). This method can be overridden to create testable classes. | 45 // Disarm(). This method can be overridden to create testable classes. |
46 virtual void Alarm() { | 46 virtual void Alarm() { |
47 DLOG(INFO) << "Watchdog alarmed for " << thread_watched_name_; | 47 DLOG(INFO) << "Watchdog alarmed for " << thread_watched_name_; |
48 } | 48 } |
49 | 49 |
50 private: | 50 private: |
51 enum State {ARMED, DISARMED, SHUTDOWN }; | 51 enum State {ARMED, DISARMED, SHUTDOWN }; |
52 | 52 |
53 // Windows thread start callback | 53 // Windows thread start callback |
54 static DWORD WINAPI ThreadStart(void* pThis); | 54 static DWORD WINAPI ThreadStart(void* pThis); |
55 | 55 |
56 // Loop and test function for our watchdog thread. | 56 // Loop and test function for our watchdog thread. |
57 unsigned Run(); | 57 unsigned Run(); |
58 void Watchdog::SetThreadName() const; | 58 void Watchdog::SetThreadName() const; |
59 | 59 |
60 Lock lock_; // Mutex for state_. | 60 Lock lock_; // Mutex for state_. |
61 ConditionVariable condition_variable_; | 61 ConditionVariable condition_variable_; |
62 State state_; | 62 State state_; |
63 const TimeDelta duration_; // How long after start_time_ do we alarm? | 63 const base::TimeDelta duration_; // How long after start_time_ do we alarm? |
64 const std::wstring thread_watched_name_; | 64 const std::wstring thread_watched_name_; |
65 HANDLE handle_; // Handle for watchdog thread. | 65 HANDLE handle_; // Handle for watchdog thread. |
66 DWORD thread_id_; // Also for watchdog thread. | 66 DWORD thread_id_; // Also for watchdog thread. |
67 | 67 |
68 TimeTicks start_time_; // Start of epoch, and alarm after duration_. | 68 base::TimeTicks start_time_; // Start of epoch, and alarm after duration_. |
69 | 69 |
70 // When the debugger breaks (when we alarm), all the other alarms that are | 70 // When the debugger breaks (when we alarm), all the other alarms that are |
71 // armed will expire (also alarm). To diminish this effect, we track any | 71 // armed will expire (also alarm). To diminish this effect, we track any |
72 // delay due to debugger breaks, and we *try* to adjust the effective start | 72 // delay due to debugger breaks, and we *try* to adjust the effective start |
73 // time of other alarms to step past the debugging break. | 73 // time of other alarms to step past the debugging break. |
74 // Without this safety net, any alarm will typically trigger a host of follow | 74 // Without this safety net, any alarm will typically trigger a host of follow |
75 // on alarms from callers that specify old times. | 75 // on alarms from callers that specify old times. |
76 static Lock static_lock_; // Lock for access of static data... | 76 static Lock static_lock_; // Lock for access of static data... |
77 // When did we last alarm and get stuck (for a while) in a debugger? | 77 // When did we last alarm and get stuck (for a while) in a debugger? |
78 static TimeTicks last_debugged_alarm_time_; | 78 static base::TimeTicks last_debugged_alarm_time_; |
79 // How long did we sit on a break in the debugger? | 79 // How long did we sit on a break in the debugger? |
80 static TimeDelta last_debugged_alarm_delay_; | 80 static base::TimeDelta last_debugged_alarm_delay_; |
81 | 81 |
82 | 82 |
83 DISALLOW_EVIL_CONSTRUCTORS(Watchdog); | 83 DISALLOW_EVIL_CONSTRUCTORS(Watchdog); |
84 }; | 84 }; |
85 | 85 |
86 #endif // BASE_WATCHDOG_H__ | 86 #endif // BASE_WATCHDOG_H__ |
87 | 87 |
OLD | NEW |