blob: 4370d831168e234eb39bdf187e57f7dca617c9aa [file] [log] [blame]
[email protected]5f99f45c32012-01-30 22:21:441// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e09cee42010-11-09 01:50:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]623c0bd2011-03-12 01:00:415#ifndef CONTENT_GPU_GPU_WATCHDOG_THREAD_H_
6#define CONTENT_GPU_GPU_WATCHDOG_THREAD_H_
[email protected]e09cee42010-11-09 01:50:087
avi66a07722015-12-25 23:38:128#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/ref_counted.h"
[email protected]35a5b752011-11-17 23:58:5810#include "base/memory/weak_ptr.h"
[email protected]e55f5642013-07-18 00:22:5411#include "base/message_loop/message_loop.h"
[email protected]21e24902013-08-06 20:23:4812#include "base/power_monitor/power_observer.h"
[email protected]34b99632011-01-01 01:01:0613#include "base/threading/thread.h"
[email protected]abb522162013-06-28 01:54:1614#include "base/time/time.h"
avi66a07722015-12-25 23:38:1215#include "build/build_config.h"
[email protected]2dcf7022011-04-15 19:20:4116#include "content/common/gpu/gpu_watchdog.h"
sohan.jyoticf28abf2015-02-18 03:13:4217#include "ui/gfx/native_widget_types.h"
18
19#if defined(USE_X11)
20extern "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]e09cee42010-11-09 01:50:0829
[email protected]eb398192012-10-22 20:16:1930namespace content {
31
[email protected]e09cee42010-11-09 01:50:0832// 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.
34class GpuWatchdogThread : public base::Thread,
[email protected]2dcf7022011-04-15 19:20:4135 public GpuWatchdog,
[email protected]21e24902013-08-06 20:23:4836 public base::PowerObserver,
[email protected]e09cee42010-11-09 01:50:0837 public base::RefCountedThreadSafe<GpuWatchdogThread> {
38 public:
[email protected]981c1c52010-12-01 20:09:2439 explicit GpuWatchdogThread(int timeout);
[email protected]e09cee42010-11-09 01:50:0840
[email protected]49eab482010-11-24 00:07:4341 // Accessible on watched thread but only modified by watchdog thread.
42 bool armed() const { return armed_; }
43 void PostAcknowledge();
[email protected]2dcf7022011-04-15 19:20:4144
45 // Implement GpuWatchdog.
dchenge933b3eb2014-10-21 11:44:0946 void CheckArmed() override;
[email protected]49eab482010-11-24 00:07:4347
[email protected]21e24902013-08-06 20:23:4848 // Must be called after a PowerMonitor has been created. Can be called from
49 // any thread.
50 void AddPowerObserver();
51
[email protected]e09cee42010-11-09 01:50:0852 protected:
dchenge933b3eb2014-10-21 11:44:0953 void Init() override;
54 void CleanUp() override;
[email protected]e09cee42010-11-09 01:50:0855
56 private:
[email protected]69d5c512012-04-28 06:36:1557 friend class base::RefCountedThreadSafe<GpuWatchdogThread>;
[email protected]49eab482010-11-24 00:07:4358
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]dd32b1272013-05-04 14:17:1161 class GpuWatchdogTaskObserver : public base::MessageLoop::TaskObserver {
[email protected]49eab482010-11-24 00:07:4362 public:
63 explicit GpuWatchdogTaskObserver(GpuWatchdogThread* watchdog);
dchenge933b3eb2014-10-21 11:44:0964 ~GpuWatchdogTaskObserver() override;
[email protected]49eab482010-11-24 00:07:4365
66 // Implements MessageLoop::TaskObserver.
dchenge933b3eb2014-10-21 11:44:0967 void WillProcessTask(const base::PendingTask& pending_task) override;
68 void DidProcessTask(const base::PendingTask& pending_task) override;
[email protected]49eab482010-11-24 00:07:4369
70 private:
[email protected]49eab482010-11-24 00:07:4371 GpuWatchdogThread* watchdog_;
72 };
73
dchenge933b3eb2014-10-21 11:44:0974 ~GpuWatchdogThread() override;
[email protected]69d5c512012-04-28 06:36:1575
[email protected]e09cee42010-11-09 01:50:0876 void OnAcknowledge();
[email protected]25cb8c452013-01-03 02:22:1877 void OnCheck(bool after_suspend);
[email protected]f9a7e08f2011-08-18 21:20:1678 void DeliberatelyTerminateToRecoverFromHang();
sohan.jyoticf28abf2015-02-18 03:13:4279#if defined(USE_X11)
80 void SetupXServer();
81 void SetupXChangeProp();
82 bool MatchXEventAtom(XEvent* event);
83#endif
[email protected]e09cee42010-11-09 01:50:0884
[email protected]21e24902013-08-06 20:23:4885 void OnAddPowerObserver();
86
87 // Implement PowerObserver.
dchenge933b3eb2014-10-21 11:44:0988 void OnSuspend() override;
89 void OnResume() override;
[email protected]21e24902013-08-06 20:23:4890
[email protected]006d3aa2013-04-11 18:09:0091#if defined(OS_WIN)
92 base::TimeDelta GetWatchedThreadTime();
93#endif
94
[email protected]dd32b1272013-05-04 14:17:1195 base::MessageLoop* watched_message_loop_;
[email protected]5f99f45c32012-01-30 22:21:4496 base::TimeDelta timeout_;
[email protected]49eab482010-11-24 00:07:4397 volatile bool armed_;
98 GpuWatchdogTaskObserver task_observer_;
[email protected]e09cee42010-11-09 01:50:0899
[email protected]006d3aa2013-04-11 18:09:00100#if defined(OS_WIN)
101 void* watched_thread_handle_;
102 base::TimeDelta arm_cpu_time_;
103#endif
104
[email protected]25cb8c452013-01-03 02:22:18105 // 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]995a7f12011-02-11 23:07:17108
[email protected]21e24902013-08-06 20:23:48109 bool suspended_;
110
[email protected]53cf4962013-10-10 21:57:22111#if defined(OS_CHROMEOS)
112 FILE* tty_file_;
113#endif
114
sohan.jyoticf28abf2015-02-18 03:13:42115#if defined(USE_X11)
116 XDisplay* display_;
117 gfx::AcceleratedWidget window_;
118 XAtom atom_;
119#endif
120
mohan.reddy6993ff92014-09-12 09:52:40121 base::WeakPtrFactory<GpuWatchdogThread> weak_factory_;
122
[email protected]e09cee42010-11-09 01:50:08123 DISALLOW_COPY_AND_ASSIGN(GpuWatchdogThread);
124};
125
[email protected]eb398192012-10-22 20:16:19126} // namespace content
127
[email protected]623c0bd2011-03-12 01:00:41128#endif // CONTENT_GPU_GPU_WATCHDOG_THREAD_H_