Convert sync_unit_tests to run exclusively on Swarming
[chromium-blink-merge.git] / content / gpu / gpu_watchdog_thread.h
blob9dd184ab32675d37d9466cd0eac732c0184b8902
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_GPU_GPU_WATCHDOG_THREAD_H_
6 #define CONTENT_GPU_GPU_WATCHDOG_THREAD_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/power_monitor/power_observer.h"
12 #include "base/threading/thread.h"
13 #include "base/time/time.h"
14 #include "content/common/gpu/gpu_watchdog.h"
15 #include "ui/gfx/native_widget_types.h"
17 #if defined(USE_X11)
18 extern "C" {
19 #include <X11/Xlib.h>
20 #include <X11/Xatom.h>
22 #include <sys/poll.h>
23 #include "ui/base/x/x11_util.h"
24 #include "ui/gfx/x/x11_types.h"
26 #endif
28 namespace content {
30 // A thread that intermitently sends tasks to a group of watched message loops
31 // and deliberately crashes if one of them does not respond after a timeout.
32 class GpuWatchdogThread : public base::Thread,
33 public GpuWatchdog,
34 public base::PowerObserver,
35 public base::RefCountedThreadSafe<GpuWatchdogThread> {
36 public:
37 explicit GpuWatchdogThread(int timeout);
39 // Accessible on watched thread but only modified by watchdog thread.
40 bool armed() const { return armed_; }
41 void PostAcknowledge();
43 // Implement GpuWatchdog.
44 void CheckArmed() override;
46 // Must be called after a PowerMonitor has been created. Can be called from
47 // any thread.
48 void AddPowerObserver();
50 protected:
51 void Init() override;
52 void CleanUp() override;
54 private:
55 friend class base::RefCountedThreadSafe<GpuWatchdogThread>;
57 // An object of this type intercepts the reception and completion of all tasks
58 // on the watched thread and checks whether the watchdog is armed.
59 class GpuWatchdogTaskObserver : public base::MessageLoop::TaskObserver {
60 public:
61 explicit GpuWatchdogTaskObserver(GpuWatchdogThread* watchdog);
62 ~GpuWatchdogTaskObserver() override;
64 // Implements MessageLoop::TaskObserver.
65 void WillProcessTask(const base::PendingTask& pending_task) override;
66 void DidProcessTask(const base::PendingTask& pending_task) override;
68 private:
69 GpuWatchdogThread* watchdog_;
72 ~GpuWatchdogThread() override;
74 void OnAcknowledge();
75 void OnCheck(bool after_suspend);
76 void DeliberatelyTerminateToRecoverFromHang();
77 #if defined(USE_X11)
78 void SetupXServer();
79 void SetupXChangeProp();
80 bool MatchXEventAtom(XEvent* event);
81 #endif
83 void OnAddPowerObserver();
85 // Implement PowerObserver.
86 void OnSuspend() override;
87 void OnResume() override;
89 #if defined(OS_WIN)
90 base::TimeDelta GetWatchedThreadTime();
91 #endif
93 base::MessageLoop* watched_message_loop_;
94 base::TimeDelta timeout_;
95 volatile bool armed_;
96 GpuWatchdogTaskObserver task_observer_;
98 #if defined(OS_WIN)
99 void* watched_thread_handle_;
100 base::TimeDelta arm_cpu_time_;
101 #endif
103 // Time after which it's assumed that the computer has been suspended since
104 // the task was posted.
105 base::Time suspension_timeout_;
107 bool suspended_;
109 #if defined(OS_CHROMEOS)
110 FILE* tty_file_;
111 #endif
113 #if defined(USE_X11)
114 XDisplay* display_;
115 gfx::AcceleratedWidget window_;
116 XAtom atom_;
117 #endif
119 base::WeakPtrFactory<GpuWatchdogThread> weak_factory_;
121 DISALLOW_COPY_AND_ASSIGN(GpuWatchdogThread);
124 } // namespace content
126 #endif // CONTENT_GPU_GPU_WATCHDOG_THREAD_H_