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

Side by Side Diff: base/watchdog.cc

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

Powered by Google App Engine
This is Rietveld 408576698