blob: b0812a64d196dee5ca018ceeddd5fb5e84975e5f [file] [log] [blame]
[email protected]623c0bd2011-03-12 01:00:411// Copyright (c) 2011 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
[email protected]49eab482010-11-24 00:07:438#include "base/message_loop.h"
[email protected]e09cee42010-11-09 01:50:089#include "base/ref_counted.h"
10#include "base/scoped_ptr.h"
11#include "base/task.h"
[email protected]34b99632011-01-01 01:01:0612#include "base/threading/thread.h"
[email protected]995a7f12011-02-11 23:07:1713#include "base/time.h"
[email protected]e09cee42010-11-09 01:50:0814
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.
17class GpuWatchdogThread : public base::Thread,
18 public base::RefCountedThreadSafe<GpuWatchdogThread> {
19 public:
[email protected]981c1c52010-12-01 20:09:2420 explicit GpuWatchdogThread(int timeout);
[email protected]e09cee42010-11-09 01:50:0821 virtual ~GpuWatchdogThread();
22
[email protected]49eab482010-11-24 00:07:4323 // Accessible on watched thread but only modified by watchdog thread.
24 bool armed() const { return armed_; }
25 void PostAcknowledge();
26
[email protected]e09cee42010-11-09 01:50:0827 protected:
28 virtual void Init();
29 virtual void CleanUp();
30
31 private:
[email protected]49eab482010-11-24 00:07:4332
33 // An object of this type intercepts the reception and completion of all tasks
34 // on the watched thread and checks whether the watchdog is armed.
35 class GpuWatchdogTaskObserver : public MessageLoop::TaskObserver {
36 public:
37 explicit GpuWatchdogTaskObserver(GpuWatchdogThread* watchdog);
38 virtual ~GpuWatchdogTaskObserver();
39
40 // Implements MessageLoop::TaskObserver.
41 virtual void WillProcessTask(const Task* task);
42 virtual void DidProcessTask(const Task* task);
43
44 private:
45 void CheckArmed();
46 GpuWatchdogThread* watchdog_;
47 };
48
[email protected]e09cee42010-11-09 01:50:0849 void OnAcknowledge();
50 void OnCheck();
[email protected]cff2ac8e2011-02-25 22:08:4951 void DeliberatelyCrashingToRecoverFromHang();
[email protected]e09cee42010-11-09 01:50:0852 void Disable();
53
[email protected]981c1c52010-12-01 20:09:2454 int64 GetWatchedThreadTime();
55
[email protected]e09cee42010-11-09 01:50:0856 MessageLoop* watched_message_loop_;
57 int timeout_;
[email protected]49eab482010-11-24 00:07:4358 volatile bool armed_;
59 GpuWatchdogTaskObserver task_observer_;
[email protected]e09cee42010-11-09 01:50:0860
[email protected]981c1c52010-12-01 20:09:2461#if defined(OS_WIN)
62 void* watched_thread_handle_;
[email protected]995a7f12011-02-11 23:07:1763 int64 arm_cpu_time_;
[email protected]981c1c52010-12-01 20:09:2464#endif
65
[email protected]995a7f12011-02-11 23:07:1766 base::Time arm_absolute_time_;
67
[email protected]e09cee42010-11-09 01:50:0868 typedef ScopedRunnableMethodFactory<GpuWatchdogThread> MethodFactory;
69 scoped_ptr<MethodFactory> method_factory_;
70
71 DISALLOW_COPY_AND_ASSIGN(GpuWatchdogThread);
72};
73
[email protected]623c0bd2011-03-12 01:00:4174#endif // CONTENT_GPU_GPU_WATCHDOG_THREAD_H_