Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / gpu / gpu_watchdog_thread.h
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.
4
5 #ifndef CONTENT_GPU_GPU_WATCHDOG_THREAD_H_
6 #define CONTENT_GPU_GPU_WATCHDOG_THREAD_H_
7
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
16 namespace content {
17
18 // A thread that intermitently sends tasks to a group of watched message loops
19 // and deliberately crashes if one of them does not respond after a timeout.
20 class GpuWatchdogThread : public base::Thread,
21                           public GpuWatchdog,
22                           public base::PowerObserver,
23                           public base::RefCountedThreadSafe<GpuWatchdogThread> {
24  public:
25   explicit GpuWatchdogThread(int timeout);
26
27   // Accessible on watched thread but only modified by watchdog thread.
28   bool armed() const { return armed_; }
29   void PostAcknowledge();
30
31   // Implement GpuWatchdog.
32   void CheckArmed() override;
33
34   // Must be called after a PowerMonitor has been created. Can be called from
35   // any thread.
36   void AddPowerObserver();
37
38  protected:
39   void Init() override;
40   void CleanUp() override;
41
42  private:
43   friend class base::RefCountedThreadSafe<GpuWatchdogThread>;
44
45   // An object of this type intercepts the reception and completion of all tasks
46   // on the watched thread and checks whether the watchdog is armed.
47   class GpuWatchdogTaskObserver : public base::MessageLoop::TaskObserver {
48    public:
49     explicit GpuWatchdogTaskObserver(GpuWatchdogThread* watchdog);
50     ~GpuWatchdogTaskObserver() override;
51
52     // Implements MessageLoop::TaskObserver.
53     void WillProcessTask(const base::PendingTask& pending_task) override;
54     void DidProcessTask(const base::PendingTask& pending_task) override;
55
56    private:
57     GpuWatchdogThread* watchdog_;
58   };
59
60   ~GpuWatchdogThread() override;
61
62   void OnAcknowledge();
63   void OnCheck(bool after_suspend);
64   void DeliberatelyTerminateToRecoverFromHang();
65
66   void OnAddPowerObserver();
67
68   // Implement PowerObserver.
69   void OnSuspend() override;
70   void OnResume() override;
71
72 #if defined(OS_WIN)
73   base::TimeDelta GetWatchedThreadTime();
74 #endif
75
76   base::MessageLoop* watched_message_loop_;
77   base::TimeDelta timeout_;
78   volatile bool armed_;
79   GpuWatchdogTaskObserver task_observer_;
80
81 #if defined(OS_WIN)
82   void* watched_thread_handle_;
83   base::TimeDelta arm_cpu_time_;
84 #endif
85
86   // Time after which it's assumed that the computer has been suspended since
87   // the task was posted.
88   base::Time suspension_timeout_;
89
90   bool suspended_;
91
92 #if defined(OS_CHROMEOS)
93   FILE* tty_file_;
94 #endif
95
96   base::WeakPtrFactory<GpuWatchdogThread> weak_factory_;
97
98   DISALLOW_COPY_AND_ASSIGN(GpuWatchdogThread);
99 };
100
101 }  // namespace content
102
103 #endif  // CONTENT_GPU_GPU_WATCHDOG_THREAD_H_