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"
20 #include <X11/Xatom.h>
23 #include "ui/base/x/x11_util.h"
24 #include "ui/gfx/x/x11_types.h"
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
,
34 public base::PowerObserver
,
35 public base::RefCountedThreadSafe
<GpuWatchdogThread
> {
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
48 void AddPowerObserver();
52 void CleanUp() override
;
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
{
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
;
69 GpuWatchdogThread
* watchdog_
;
72 ~GpuWatchdogThread() override
;
75 void OnCheck(bool after_suspend
);
76 void DeliberatelyTerminateToRecoverFromHang();
79 void SetupXChangeProp();
80 bool MatchXEventAtom(XEvent
* event
);
83 void OnAddPowerObserver();
85 // Implement PowerObserver.
86 void OnSuspend() override
;
87 void OnResume() override
;
90 base::TimeDelta
GetWatchedThreadTime();
93 base::MessageLoop
* watched_message_loop_
;
94 base::TimeDelta timeout_
;
96 GpuWatchdogTaskObserver task_observer_
;
99 void* watched_thread_handle_
;
100 base::TimeDelta arm_cpu_time_
;
103 // Time after which it's assumed that the computer has been suspended since
104 // the task was posted.
105 base::Time suspension_timeout_
;
109 #if defined(OS_CHROMEOS)
115 gfx::AcceleratedWidget window_
;
119 base::WeakPtrFactory
<GpuWatchdogThread
> weak_factory_
;
121 DISALLOW_COPY_AND_ASSIGN(GpuWatchdogThread
);
124 } // namespace content
126 #endif // CONTENT_GPU_GPU_WATCHDOG_THREAD_H_