| 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. |
| 11 | 11 |
| 12 // Some watchdogs will be enabled or disabled via command line switches. To | 12 // Some watchdogs will be enabled or disabled via command line switches. To |
| 13 // facilitate such code, an "enabled" argument for the constuctor can be used | 13 // facilitate such code, an "enabled" argument for the constuctor can be used |
| 14 // to permanently disable the watchdog. Disabled watchdogs don't even spawn | 14 // to permanently disable the watchdog. Disabled watchdogs don't even spawn |
| 15 // a second thread, and their methods call (Arm() and Disarm()) return very | 15 // a second thread, and their methods call (Arm() and Disarm()) return very |
| 16 // quickly. | 16 // quickly. |
| 17 | 17 |
| 18 #ifndef BASE_WATCHDOG_H__ | 18 #ifndef BASE_WATCHDOG_H__ |
| 19 #define BASE_WATCHDOG_H__ | 19 #define BASE_WATCHDOG_H__ |
| 20 #pragma once | 20 #pragma once |
| 21 | 21 |
| 22 #include <string> | 22 #include <string> |
| 23 | 23 |
| 24 #include "base/condition_variable.h" | 24 #include "base/condition_variable.h" |
| 25 #include "base/lock.h" | 25 #include "base/lock.h" |
| 26 #include "base/logging.h" | |
| 27 #include "base/platform_thread.h" | 26 #include "base/platform_thread.h" |
| 28 #include "base/time.h" | 27 #include "base/time.h" |
| 29 | 28 |
| 30 class Watchdog { | 29 class Watchdog { |
| 31 public: | 30 public: |
| 32 // Constructor specifies how long the Watchdog will wait before alarming. | 31 // Constructor specifies how long the Watchdog will wait before alarming. |
| 33 Watchdog(const base::TimeDelta& duration, | 32 Watchdog(const base::TimeDelta& duration, |
| 34 const std::string& thread_watched_name, | 33 const std::string& thread_watched_name, |
| 35 bool enabled); | 34 bool enabled); |
| 36 virtual ~Watchdog(); | 35 virtual ~Watchdog(); |
| 37 | 36 |
| 38 // Start timing, and alarm when time expires (unless we're disarm()ed.) | 37 // Start timing, and alarm when time expires (unless we're disarm()ed.) |
| 39 void Arm(); // Arm starting now. | 38 void Arm(); // Arm starting now. |
| 40 void ArmSomeTimeDeltaAgo(const base::TimeDelta& time_delta); | 39 void ArmSomeTimeDeltaAgo(const base::TimeDelta& time_delta); |
| 41 void ArmAtStartTime(const base::TimeTicks start_time); | 40 void ArmAtStartTime(const base::TimeTicks start_time); |
| 42 | 41 |
| 43 // Reset time, and do not set off the alarm. | 42 // Reset time, and do not set off the alarm. |
| 44 void Disarm(); | 43 void Disarm(); |
| 45 | 44 |
| 46 // Alarm is called if the time expires after an Arm() without someone calling | 45 // Alarm is called if the time expires after an Arm() without someone calling |
| 47 // Disarm(). This method can be overridden to create testable classes. | 46 // Disarm(). This method can be overridden to create testable classes. |
| 48 virtual void Alarm() { | 47 virtual void Alarm(); |
| 49 DLOG(INFO) << "Watchdog alarmed for " << thread_watched_name_; | |
| 50 } | |
| 51 | 48 |
| 52 // Reset static data to initial state. Useful for tests, to ensure | 49 // Reset static data to initial state. Useful for tests, to ensure |
| 53 // they are independent. | 50 // they are independent. |
| 54 static void ResetStaticData(); | 51 static void ResetStaticData(); |
| 55 | 52 |
| 56 private: | 53 private: |
| 57 class ThreadDelegate : public PlatformThread::Delegate { | 54 class ThreadDelegate : public PlatformThread::Delegate { |
| 58 public: | 55 public: |
| 59 explicit ThreadDelegate(Watchdog* watchdog) : watchdog_(watchdog) { | 56 explicit ThreadDelegate(Watchdog* watchdog) : watchdog_(watchdog) { |
| 60 } | 57 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 88 static Lock static_lock_; // Lock for access of static data... | 85 static Lock static_lock_; // Lock for access of static data... |
| 89 // When did we last alarm and get stuck (for a while) in a debugger? | 86 // When did we last alarm and get stuck (for a while) in a debugger? |
| 90 static base::TimeTicks last_debugged_alarm_time_; | 87 static base::TimeTicks last_debugged_alarm_time_; |
| 91 // How long did we sit on a break in the debugger? | 88 // How long did we sit on a break in the debugger? |
| 92 static base::TimeDelta last_debugged_alarm_delay_; | 89 static base::TimeDelta last_debugged_alarm_delay_; |
| 93 | 90 |
| 94 DISALLOW_COPY_AND_ASSIGN(Watchdog); | 91 DISALLOW_COPY_AND_ASSIGN(Watchdog); |
| 95 }; | 92 }; |
| 96 | 93 |
| 97 #endif // BASE_WATCHDOG_H__ | 94 #endif // BASE_WATCHDOG_H__ |
| OLD | NEW |