OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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. |
11 | 11 |
(...skipping 19 matching lines...) Expand all Loading... |
31 namespace base { | 31 namespace base { |
32 | 32 |
33 class BASE_EXPORT Watchdog { | 33 class BASE_EXPORT Watchdog { |
34 public: | 34 public: |
35 // Constructor specifies how long the Watchdog will wait before alarming. | 35 // Constructor specifies how long the Watchdog will wait before alarming. |
36 Watchdog(const TimeDelta& duration, | 36 Watchdog(const TimeDelta& duration, |
37 const std::string& thread_watched_name, | 37 const std::string& thread_watched_name, |
38 bool enabled); | 38 bool enabled); |
39 virtual ~Watchdog(); | 39 virtual ~Watchdog(); |
40 | 40 |
| 41 // Notify watchdog thread to finish up. Sets the state_ to SHUTDOWN. |
| 42 void Cleanup(); |
| 43 |
| 44 // Returns true if we state_ is JOINABLE (which indicates that Watchdog has |
| 45 // exited). |
| 46 bool IsJoinable(); |
| 47 |
41 // Start timing, and alarm when time expires (unless we're disarm()ed.) | 48 // Start timing, and alarm when time expires (unless we're disarm()ed.) |
42 void Arm(); // Arm starting now. | 49 void Arm(); // Arm starting now. |
43 void ArmSomeTimeDeltaAgo(const TimeDelta& time_delta); | 50 void ArmSomeTimeDeltaAgo(const TimeDelta& time_delta); |
44 void ArmAtStartTime(const TimeTicks start_time); | 51 void ArmAtStartTime(const TimeTicks start_time); |
45 | 52 |
46 // Reset time, and do not set off the alarm. | 53 // Reset time, and do not set off the alarm. |
47 void Disarm(); | 54 void Disarm(); |
48 | 55 |
49 // Alarm is called if the time expires after an Arm() without someone calling | 56 // Alarm is called if the time expires after an Arm() without someone calling |
50 // Disarm(). This method can be overridden to create testable classes. | 57 // Disarm(). This method can be overridden to create testable classes. |
51 virtual void Alarm(); | 58 virtual void Alarm(); |
52 | 59 |
53 // Reset static data to initial state. Useful for tests, to ensure | 60 // Reset static data to initial state. Useful for tests, to ensure |
54 // they are independent. | 61 // they are independent. |
55 static void ResetStaticData(); | 62 static void ResetStaticData(); |
56 | 63 |
57 private: | 64 private: |
58 class ThreadDelegate : public PlatformThread::Delegate { | 65 class ThreadDelegate : public PlatformThread::Delegate { |
59 public: | 66 public: |
60 explicit ThreadDelegate(Watchdog* watchdog) : watchdog_(watchdog) { | 67 explicit ThreadDelegate(Watchdog* watchdog) : watchdog_(watchdog) { |
61 } | 68 } |
62 virtual void ThreadMain() OVERRIDE; | 69 virtual void ThreadMain() OVERRIDE; |
63 private: | 70 private: |
64 void SetThreadName() const; | 71 void SetThreadName() const; |
65 | 72 |
66 Watchdog* watchdog_; | 73 Watchdog* watchdog_; |
67 }; | 74 }; |
68 | 75 |
69 enum State {ARMED, DISARMED, SHUTDOWN }; | 76 enum State {ARMED, DISARMED, SHUTDOWN, JOINABLE }; |
70 | 77 |
71 bool init_successful_; | 78 bool enabled_; |
72 | 79 |
73 Lock lock_; // Mutex for state_. | 80 Lock lock_; // Mutex for state_. |
74 ConditionVariable condition_variable_; | 81 ConditionVariable condition_variable_; |
75 State state_; | 82 State state_; |
76 const TimeDelta duration_; // How long after start_time_ do we alarm? | 83 const TimeDelta duration_; // How long after start_time_ do we alarm? |
77 const std::string thread_watched_name_; | 84 const std::string thread_watched_name_; |
78 PlatformThreadHandle handle_; | 85 PlatformThreadHandle handle_; |
79 ThreadDelegate delegate_; // Store it, because it must outlive the thread. | 86 ThreadDelegate delegate_; // Store it, because it must outlive the thread. |
80 | 87 |
81 TimeTicks start_time_; // Start of epoch, and alarm after duration_. | 88 TimeTicks start_time_; // Start of epoch, and alarm after duration_. |
82 | 89 |
83 DISALLOW_COPY_AND_ASSIGN(Watchdog); | 90 DISALLOW_COPY_AND_ASSIGN(Watchdog); |
84 }; | 91 }; |
85 | 92 |
86 } // namespace base | 93 } // namespace base |
87 | 94 |
88 #endif // BASE_THREADING_WATCHDOG_H_ | 95 #endif // BASE_THREADING_WATCHDOG_H_ |
OLD | NEW |