OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_THREADING_WATCHDOG_H_ | 18 #ifndef BASE_THREADING_WATCHDOG_H_ |
19 #define BASE_THREADING_WATCHDOG_H_ | 19 #define BASE_THREADING_WATCHDOG_H_ |
20 #pragma once | 20 #pragma once |
21 | 21 |
22 #include <string> | 22 #include <string> |
23 | 23 |
24 #include "base/base_export.h" | 24 #include "base/base_export.h" |
| 25 #include "base/compiler_specific.h" |
25 #include "base/synchronization/condition_variable.h" | 26 #include "base/synchronization/condition_variable.h" |
26 #include "base/synchronization/lock.h" | 27 #include "base/synchronization/lock.h" |
27 #include "base/threading/platform_thread.h" | 28 #include "base/threading/platform_thread.h" |
28 #include "base/time.h" | 29 #include "base/time.h" |
29 | 30 |
30 namespace base { | 31 namespace base { |
31 | 32 |
32 class BASE_EXPORT Watchdog { | 33 class BASE_EXPORT Watchdog { |
33 public: | 34 public: |
34 // Constructor specifies how long the Watchdog will wait before alarming. | 35 // Constructor specifies how long the Watchdog will wait before alarming. |
(...skipping 16 matching lines...) Expand all Loading... |
51 | 52 |
52 // Reset static data to initial state. Useful for tests, to ensure | 53 // Reset static data to initial state. Useful for tests, to ensure |
53 // they are independent. | 54 // they are independent. |
54 static void ResetStaticData(); | 55 static void ResetStaticData(); |
55 | 56 |
56 private: | 57 private: |
57 class ThreadDelegate : public PlatformThread::Delegate { | 58 class ThreadDelegate : public PlatformThread::Delegate { |
58 public: | 59 public: |
59 explicit ThreadDelegate(Watchdog* watchdog) : watchdog_(watchdog) { | 60 explicit ThreadDelegate(Watchdog* watchdog) : watchdog_(watchdog) { |
60 } | 61 } |
61 virtual void ThreadMain(); | 62 virtual void ThreadMain() OVERRIDE; |
62 private: | 63 private: |
63 void SetThreadName() const; | 64 void SetThreadName() const; |
64 | 65 |
65 Watchdog* watchdog_; | 66 Watchdog* watchdog_; |
66 }; | 67 }; |
67 | 68 |
68 enum State {ARMED, DISARMED, SHUTDOWN }; | 69 enum State {ARMED, DISARMED, SHUTDOWN }; |
69 | 70 |
70 bool init_successful_; | 71 bool init_successful_; |
71 | 72 |
72 Lock lock_; // Mutex for state_. | 73 Lock lock_; // Mutex for state_. |
73 ConditionVariable condition_variable_; | 74 ConditionVariable condition_variable_; |
74 State state_; | 75 State state_; |
75 const TimeDelta duration_; // How long after start_time_ do we alarm? | 76 const TimeDelta duration_; // How long after start_time_ do we alarm? |
76 const std::string thread_watched_name_; | 77 const std::string thread_watched_name_; |
77 PlatformThreadHandle handle_; | 78 PlatformThreadHandle handle_; |
78 ThreadDelegate delegate_; // Store it, because it must outlive the thread. | 79 ThreadDelegate delegate_; // Store it, because it must outlive the thread. |
79 | 80 |
80 TimeTicks start_time_; // Start of epoch, and alarm after duration_. | 81 TimeTicks start_time_; // Start of epoch, and alarm after duration_. |
81 | 82 |
82 DISALLOW_COPY_AND_ASSIGN(Watchdog); | 83 DISALLOW_COPY_AND_ASSIGN(Watchdog); |
83 }; | 84 }; |
84 | 85 |
85 } // namespace base | 86 } // namespace base |
86 | 87 |
87 #endif // BASE_THREADING_WATCHDOG_H_ | 88 #endif // BASE_THREADING_WATCHDOG_H_ |
OLD | NEW |