• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/threading/watchdog.h"
6 
7 #include "base/compiler_specific.h"
8 #include "base/logging.h"
9 #include "base/no_destructor.h"
10 #include "base/threading/platform_thread.h"
11 
12 namespace base {
13 
14 namespace {
15 
16 // When the debugger breaks (when we alarm), all the other alarms that are
17 // armed will expire (also alarm).  To diminish this effect, we track any
18 // delay due to debugger breaks, and we *try* to adjust the effective start
19 // time of other alarms to step past the debugging break.
20 // Without this safety net, any alarm will typically trigger a host of follow
21 // on alarms from callers that specify old times.
22 
23 struct StaticData {
24   // Lock for access of static data...
25   Lock lock;
26 
27   // When did we last alarm and get stuck (for a while) in a debugger?
28   TimeTicks last_debugged_alarm_time;
29 
30   // How long did we sit on a break in the debugger?
31   TimeDelta last_debugged_alarm_delay;
32 };
33 
GetStaticData()34 StaticData* GetStaticData() {
35   static base::NoDestructor<StaticData> static_data;
36   return static_data.get();
37 }
38 
39 }  // namespace
40 
41 // Start thread running in a Disarmed state.
Watchdog(const TimeDelta & duration,const std::string & thread_watched_name,bool enabled,Delegate * delegate)42 Watchdog::Watchdog(const TimeDelta& duration,
43                    const std::string& thread_watched_name,
44                    bool enabled,
45                    Delegate* delegate)
46     : enabled_(enabled),
47       condition_variable_(&lock_),
48       state_(DISARMED),
49       duration_(duration),
50       thread_watched_name_(thread_watched_name),
51       thread_delegate_(this),
52       delegate_(delegate) {
53   if (!enabled_) {
54     return;  // Don't start thread, or doing anything really.
55   }
56   enabled_ = PlatformThread::Create(0,  // Default stack size.
57                                     &thread_delegate_, &handle_);
58   DCHECK(enabled_);
59 }
60 
61 // Notify watchdog thread, and wait for it to finish up.
~Watchdog()62 Watchdog::~Watchdog() {
63   if (!enabled_)
64     return;
65   if (!IsJoinable())
66     Cleanup();
67   PlatformThread::Join(handle_);
68 }
69 
Cleanup()70 void Watchdog::Cleanup() {
71   if (!enabled_)
72     return;
73   AutoLock lock(lock_);
74   state_ = SHUTDOWN;
75   condition_variable_.Signal();
76 }
77 
IsJoinable()78 bool Watchdog::IsJoinable() {
79   if (!enabled_)
80     return true;
81   AutoLock lock(lock_);
82   return (state_ == JOINABLE);
83 }
84 
Arm()85 void Watchdog::Arm() {
86   ArmAtStartTime(TimeTicks::Now());
87 }
88 
ArmSomeTimeDeltaAgo(const TimeDelta & time_delta)89 void Watchdog::ArmSomeTimeDeltaAgo(const TimeDelta& time_delta) {
90   ArmAtStartTime(TimeTicks::Now() - time_delta);
91 }
92 
93 // Start clock for watchdog.
ArmAtStartTime(const TimeTicks start_time)94 void Watchdog::ArmAtStartTime(const TimeTicks start_time) {
95   AutoLock lock(lock_);
96   start_time_ = start_time;
97   state_ = ARMED;
98   // Force watchdog to wake up, and go to sleep with the timer ticking with the
99   // proper duration.
100   condition_variable_.Signal();
101 }
102 
103 // Disable watchdog so that it won't do anything when time expires.
Disarm()104 void Watchdog::Disarm() {
105   AutoLock lock(lock_);
106   state_ = DISARMED;
107   // We don't need to signal, as the watchdog will eventually wake up, and it
108   // will check its state and time, and act accordingly.
109 }
110 
Alarm()111 void Watchdog::Alarm() {
112   if (delegate_) {
113     delegate_->Alarm();
114   } else {
115     DefaultAlarm();
116   }
117 }
118 
DefaultAlarm()119 void Watchdog::DefaultAlarm() {
120   DVLOG(1) << "Watchdog alarmed for " << thread_watched_name_;
121 }
122 
123 //------------------------------------------------------------------------------
124 // Internal private methods that the watchdog thread uses.
125 
ThreadMain()126 void Watchdog::ThreadDelegate::ThreadMain() {
127   SetThreadName();
128   TimeDelta remaining_duration;
129   StaticData* static_data = GetStaticData();
130   while (true) {
131     AutoLock lock(watchdog_->lock_);
132     while (DISARMED == watchdog_->state_)
133       watchdog_->condition_variable_.Wait();
134     if (SHUTDOWN == watchdog_->state_) {
135       watchdog_->state_ = JOINABLE;
136       return;
137     }
138     DCHECK(ARMED == watchdog_->state_);
139     remaining_duration = watchdog_->duration_ -
140         (TimeTicks::Now() - watchdog_->start_time_);
141     if (remaining_duration.InMilliseconds() > 0) {
142       // Spurios wake?  Timer drifts?  Go back to sleep for remaining time.
143       watchdog_->condition_variable_.TimedWait(remaining_duration);
144       continue;
145     }
146     // We overslept, so this seems like a real alarm.
147     // Watch out for a user that stopped the debugger on a different alarm!
148     {
149       AutoLock static_lock(static_data->lock);
150       if (static_data->last_debugged_alarm_time > watchdog_->start_time_) {
151         // False alarm: we started our clock before the debugger break (last
152         // alarm time).
153         watchdog_->start_time_ += static_data->last_debugged_alarm_delay;
154         if (static_data->last_debugged_alarm_time > watchdog_->start_time_)
155           // Too many alarms must have taken place.
156           watchdog_->state_ = DISARMED;
157         continue;
158       }
159     }
160     watchdog_->state_ = DISARMED;  // Only alarm at most once.
161     TimeTicks last_alarm_time = TimeTicks::Now();
162     {
163       AutoUnlock unlock(watchdog_->lock_);
164       watchdog_->Alarm();  // Set a break point here to debug on alarms.
165     }
166     TimeDelta last_alarm_delay = TimeTicks::Now() - last_alarm_time;
167     if (last_alarm_delay <= Milliseconds(2))
168       continue;
169     // Ignore race of two alarms/breaks going off at roughly the same time.
170     AutoLock static_lock(static_data->lock);
171     // This was a real debugger break.
172     static_data->last_debugged_alarm_time = last_alarm_time;
173     static_data->last_debugged_alarm_delay = last_alarm_delay;
174   }
175 }
176 
SetThreadName() const177 void Watchdog::ThreadDelegate::SetThreadName() const {
178   std::string name = watchdog_->thread_watched_name_ + " Watchdog";
179   PlatformThread::SetName(name);
180   DVLOG(1) << "Watchdog active: " << name;
181 }
182 
183 // static
ResetStaticData()184 void Watchdog::ResetStaticData() {
185   StaticData* static_data = GetStaticData();
186   AutoLock lock(static_data->lock);
187   // See https://crbug.com/734232 for why this cannot be zero-initialized.
188   static_data->last_debugged_alarm_time = TimeTicks::Min();
189   static_data->last_debugged_alarm_delay = TimeDelta();
190 }
191 
192 }  // namespace base
193