Fix bug in load time stats.
[chromium-blink-merge.git] / content / gpu / gpu_watchdog_thread.h
blob16fc5be3a1e1597bdf5b7f681ee5d206f7653578
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.h"
11 #include "base/threading/thread.h"
12 #include "base/time.h"
13 #include "content/common/gpu/gpu_watchdog.h"
15 // A thread that intermitently sends tasks to a group of watched message loops
16 // and deliberately crashes if one of them does not respond after a timeout.
17 class GpuWatchdogThread : public base::Thread,
18 public GpuWatchdog,
19 public base::RefCountedThreadSafe<GpuWatchdogThread> {
20 public:
21 explicit GpuWatchdogThread(int timeout);
23 // Accessible on watched thread but only modified by watchdog thread.
24 bool armed() const { return armed_; }
25 void PostAcknowledge();
27 // Implement GpuWatchdog.
28 virtual void CheckArmed() OVERRIDE;
30 protected:
31 virtual void Init() OVERRIDE;
32 virtual void CleanUp() OVERRIDE;
34 private:
35 friend class base::RefCountedThreadSafe<GpuWatchdogThread>;
37 // An object of this type intercepts the reception and completion of all tasks
38 // on the watched thread and checks whether the watchdog is armed.
39 class GpuWatchdogTaskObserver : public MessageLoop::TaskObserver {
40 public:
41 explicit GpuWatchdogTaskObserver(GpuWatchdogThread* watchdog);
42 virtual ~GpuWatchdogTaskObserver();
44 // Implements MessageLoop::TaskObserver.
45 virtual void WillProcessTask(base::TimeTicks time_posted) OVERRIDE;
46 virtual void DidProcessTask(base::TimeTicks time_posted) OVERRIDE;
48 private:
49 GpuWatchdogThread* watchdog_;
52 virtual ~GpuWatchdogThread();
54 void OnAcknowledge();
55 void OnCheck();
56 void DeliberatelyTerminateToRecoverFromHang();
58 #if defined(OS_WIN)
59 base::TimeDelta GetWatchedThreadTime();
60 #endif
62 MessageLoop* watched_message_loop_;
63 base::TimeDelta timeout_;
64 volatile bool armed_;
65 GpuWatchdogTaskObserver task_observer_;
67 #if defined(OS_WIN)
68 void* watched_thread_handle_;
69 base::TimeDelta arm_cpu_time_;
70 #endif
72 base::Time arm_absolute_time_;
74 base::WeakPtrFactory<GpuWatchdogThread> weak_factory_;
76 DISALLOW_COPY_AND_ASSIGN(GpuWatchdogThread);
79 #endif // CONTENT_GPU_GPU_WATCHDOG_THREAD_H_