[email protected] | 5f99f45c3 | 2012-01-30 22:21:44 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 623c0bd | 2011-03-12 01:00:41 | [diff] [blame] | 5 | #ifndef CONTENT_GPU_GPU_WATCHDOG_THREAD_H_ |
6 | #define CONTENT_GPU_GPU_WATCHDOG_THREAD_H_ | ||||
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 7 | |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 8 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
[email protected] | 35a5b75 | 2011-11-17 23:58:58 | [diff] [blame] | 10 | #include "base/memory/weak_ptr.h" |
[email protected] | e55f564 | 2013-07-18 00:22:54 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
[email protected] | 21e2490 | 2013-08-06 20:23:48 | [diff] [blame] | 12 | #include "base/power_monitor/power_observer.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 13 | #include "base/threading/thread.h" |
[email protected] | abb52216 | 2013-06-28 01:54:16 | [diff] [blame] | 14 | #include "base/time/time.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | 2dcf702 | 2011-04-15 19:20:41 | [diff] [blame] | 16 | #include "content/common/gpu/gpu_watchdog.h" |
sohan.jyoti | cf28abf | 2015-02-18 03:13:42 | [diff] [blame] | 17 | #include "ui/gfx/native_widget_types.h" |
18 | |||||
19 | #if defined(USE_X11) | ||||
20 | extern "C" { | ||||
21 | #include <X11/Xlib.h> | ||||
22 | #include <X11/Xatom.h> | ||||
23 | } | ||||
24 | #include <sys/poll.h> | ||||
25 | #include "ui/base/x/x11_util.h" | ||||
26 | #include "ui/gfx/x/x11_types.h" | ||||
27 | |||||
28 | #endif | ||||
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 29 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 30 | namespace content { |
31 | |||||
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 32 | // A thread that intermitently sends tasks to a group of watched message loops |
33 | // and deliberately crashes if one of them does not respond after a timeout. | ||||
34 | class GpuWatchdogThread : public base::Thread, | ||||
[email protected] | 2dcf702 | 2011-04-15 19:20:41 | [diff] [blame] | 35 | public GpuWatchdog, |
[email protected] | 21e2490 | 2013-08-06 20:23:48 | [diff] [blame] | 36 | public base::PowerObserver, |
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 37 | public base::RefCountedThreadSafe<GpuWatchdogThread> { |
38 | public: | ||||
[email protected] | 981c1c5 | 2010-12-01 20:09:24 | [diff] [blame] | 39 | explicit GpuWatchdogThread(int timeout); |
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 40 | |
[email protected] | 49eab48 | 2010-11-24 00:07:43 | [diff] [blame] | 41 | // Accessible on watched thread but only modified by watchdog thread. |
42 | bool armed() const { return armed_; } | ||||
43 | void PostAcknowledge(); | ||||
[email protected] | 2dcf702 | 2011-04-15 19:20:41 | [diff] [blame] | 44 | |
45 | // Implement GpuWatchdog. | ||||
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 46 | void CheckArmed() override; |
[email protected] | 49eab48 | 2010-11-24 00:07:43 | [diff] [blame] | 47 | |
[email protected] | 21e2490 | 2013-08-06 20:23:48 | [diff] [blame] | 48 | // Must be called after a PowerMonitor has been created. Can be called from |
49 | // any thread. | ||||
50 | void AddPowerObserver(); | ||||
51 | |||||
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 52 | protected: |
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 53 | void Init() override; |
54 | void CleanUp() override; | ||||
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 55 | |
56 | private: | ||||
[email protected] | 69d5c51 | 2012-04-28 06:36:15 | [diff] [blame] | 57 | friend class base::RefCountedThreadSafe<GpuWatchdogThread>; |
[email protected] | 49eab48 | 2010-11-24 00:07:43 | [diff] [blame] | 58 | |
59 | // An object of this type intercepts the reception and completion of all tasks | ||||
60 | // on the watched thread and checks whether the watchdog is armed. | ||||
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 61 | class GpuWatchdogTaskObserver : public base::MessageLoop::TaskObserver { |
[email protected] | 49eab48 | 2010-11-24 00:07:43 | [diff] [blame] | 62 | public: |
63 | explicit GpuWatchdogTaskObserver(GpuWatchdogThread* watchdog); | ||||
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 64 | ~GpuWatchdogTaskObserver() override; |
[email protected] | 49eab48 | 2010-11-24 00:07:43 | [diff] [blame] | 65 | |
66 | // Implements MessageLoop::TaskObserver. | ||||
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 67 | void WillProcessTask(const base::PendingTask& pending_task) override; |
68 | void DidProcessTask(const base::PendingTask& pending_task) override; | ||||
[email protected] | 49eab48 | 2010-11-24 00:07:43 | [diff] [blame] | 69 | |
70 | private: | ||||
[email protected] | 49eab48 | 2010-11-24 00:07:43 | [diff] [blame] | 71 | GpuWatchdogThread* watchdog_; |
72 | }; | ||||
73 | |||||
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 74 | ~GpuWatchdogThread() override; |
[email protected] | 69d5c51 | 2012-04-28 06:36:15 | [diff] [blame] | 75 | |
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 76 | void OnAcknowledge(); |
[email protected] | 25cb8c45 | 2013-01-03 02:22:18 | [diff] [blame] | 77 | void OnCheck(bool after_suspend); |
[email protected] | f9a7e08f | 2011-08-18 21:20:16 | [diff] [blame] | 78 | void DeliberatelyTerminateToRecoverFromHang(); |
sohan.jyoti | cf28abf | 2015-02-18 03:13:42 | [diff] [blame] | 79 | #if defined(USE_X11) |
80 | void SetupXServer(); | ||||
81 | void SetupXChangeProp(); | ||||
82 | bool MatchXEventAtom(XEvent* event); | ||||
83 | #endif | ||||
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 84 | |
[email protected] | 21e2490 | 2013-08-06 20:23:48 | [diff] [blame] | 85 | void OnAddPowerObserver(); |
86 | |||||
87 | // Implement PowerObserver. | ||||
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 88 | void OnSuspend() override; |
89 | void OnResume() override; | ||||
[email protected] | 21e2490 | 2013-08-06 20:23:48 | [diff] [blame] | 90 | |
[email protected] | 006d3aa | 2013-04-11 18:09:00 | [diff] [blame] | 91 | #if defined(OS_WIN) |
92 | base::TimeDelta GetWatchedThreadTime(); | ||||
93 | #endif | ||||
94 | |||||
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 95 | base::MessageLoop* watched_message_loop_; |
[email protected] | 5f99f45c3 | 2012-01-30 22:21:44 | [diff] [blame] | 96 | base::TimeDelta timeout_; |
[email protected] | 49eab48 | 2010-11-24 00:07:43 | [diff] [blame] | 97 | volatile bool armed_; |
98 | GpuWatchdogTaskObserver task_observer_; | ||||
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 99 | |
[email protected] | 006d3aa | 2013-04-11 18:09:00 | [diff] [blame] | 100 | #if defined(OS_WIN) |
101 | void* watched_thread_handle_; | ||||
102 | base::TimeDelta arm_cpu_time_; | ||||
103 | #endif | ||||
104 | |||||
[email protected] | 25cb8c45 | 2013-01-03 02:22:18 | [diff] [blame] | 105 | // Time after which it's assumed that the computer has been suspended since |
106 | // the task was posted. | ||||
107 | base::Time suspension_timeout_; | ||||
[email protected] | 995a7f1 | 2011-02-11 23:07:17 | [diff] [blame] | 108 | |
[email protected] | 21e2490 | 2013-08-06 20:23:48 | [diff] [blame] | 109 | bool suspended_; |
110 | |||||
[email protected] | 53cf496 | 2013-10-10 21:57:22 | [diff] [blame] | 111 | #if defined(OS_CHROMEOS) |
112 | FILE* tty_file_; | ||||
113 | #endif | ||||
114 | |||||
sohan.jyoti | cf28abf | 2015-02-18 03:13:42 | [diff] [blame] | 115 | #if defined(USE_X11) |
116 | XDisplay* display_; | ||||
117 | gfx::AcceleratedWidget window_; | ||||
118 | XAtom atom_; | ||||
119 | #endif | ||||
120 | |||||
mohan.reddy | 6993ff9 | 2014-09-12 09:52:40 | [diff] [blame] | 121 | base::WeakPtrFactory<GpuWatchdogThread> weak_factory_; |
122 | |||||
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 123 | DISALLOW_COPY_AND_ASSIGN(GpuWatchdogThread); |
124 | }; | ||||
125 | |||||
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 126 | } // namespace content |
127 | |||||
[email protected] | 623c0bd | 2011-03-12 01:00:41 | [diff] [blame] | 128 | #endif // CONTENT_GPU_GPU_WATCHDOG_THREAD_H_ |