Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(413)

Side by Side Diff: base/watchdog.h

Issue 11326: Port base/watchdog to Linux. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 12 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 20
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/platform_thread.h"
25 #include "base/time.h" 26 #include "base/time.h"
26 27
27 class Watchdog { 28 class Watchdog {
28 public: 29 public:
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 base::TimeDelta& duration, 31 Watchdog(const base::TimeDelta& duration,
32 const std::wstring& thread_watched_name, 32 const std::string& thread_watched_name,
33 bool enabled = true); 33 bool enabled);
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 base::TimeDelta& time_delta); 38 void ArmSomeTimeDeltaAgo(const base::TimeDelta& time_delta);
39 void ArmAtStartTime(const base::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 class ThreadDelegate : public PlatformThread::Delegate {
52 public:
53 explicit ThreadDelegate(Watchdog* watchdog, const base::TimeDelta& duration)
wtc 2008/11/25 23:00:13 This constructor has two parameters, so it doesn't
54 : watchdog_(watchdog), duration_(duration) {
55 }
56 virtual void ThreadMain();
57 private:
58 Watchdog* watchdog_;
59 const base::TimeDelta duration_; // How long after start_time_ do we alarm?
wtc 2008/11/25 23:05:27 Another comment: it's not clear to me why you move
60
61 void SetThreadName() const;
62 };
63
51 enum State {ARMED, DISARMED, SHUTDOWN }; 64 enum State {ARMED, DISARMED, SHUTDOWN };
52 65
53 // Windows thread start callback 66 bool init_successful_;
54 static DWORD WINAPI ThreadStart(void* pThis);
55
56 // Loop and test function for our watchdog thread.
57 unsigned Run();
58 void Watchdog::SetThreadName() const;
59 67
60 Lock lock_; // Mutex for state_. 68 Lock lock_; // Mutex for state_.
61 ConditionVariable condition_variable_; 69 ConditionVariable condition_variable_;
62 State state_; 70 State state_;
63 const base::TimeDelta duration_; // How long after start_time_ do we alarm? 71 const std::string thread_watched_name_;
64 const std::wstring thread_watched_name_; 72 PlatformThreadHandle handle_;
65 HANDLE handle_; // Handle for watchdog thread. 73 ThreadDelegate delegate_; // Store it, because it must outlive the thread.
66 DWORD thread_id_; // Also for watchdog thread.
67 74
68 base::TimeTicks start_time_; // Start of epoch, and alarm after duration_. 75 base::TimeTicks start_time_; // Start of epoch, and alarm after duration_.
69 76
70 // When the debugger breaks (when we alarm), all the other alarms that are 77 // 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 78 // 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 79 // delay due to debugger breaks, and we *try* to adjust the effective start
73 // time of other alarms to step past the debugging break. 80 // time of other alarms to step past the debugging break.
74 // Without this safety net, any alarm will typically trigger a host of follow 81 // Without this safety net, any alarm will typically trigger a host of follow
75 // on alarms from callers that specify old times. 82 // on alarms from callers that specify old times.
76 static Lock static_lock_; // Lock for access of static data... 83 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? 84 // When did we last alarm and get stuck (for a while) in a debugger?
78 static base::TimeTicks last_debugged_alarm_time_; 85 static base::TimeTicks last_debugged_alarm_time_;
79 // How long did we sit on a break in the debugger? 86 // How long did we sit on a break in the debugger?
80 static base::TimeDelta last_debugged_alarm_delay_; 87 static base::TimeDelta last_debugged_alarm_delay_;
81 88
82 89 DISALLOW_COPY_AND_ASSIGN(Watchdog);
83 DISALLOW_EVIL_CONSTRUCTORS(Watchdog);
84 }; 90 };
85 91
86 #endif // BASE_WATCHDOG_H__ 92 #endif // BASE_WATCHDOG_H__
87 93
OLDNEW
« base/base_unittests.scons ('K') | « base/base_unittests.scons ('k') | base/watchdog.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698