summaryrefslogtreecommitdiffstats
path: root/cc/base/worker_pool.h
blob: 8e00b6b8e541f7d9cc693cf94e105d106e117242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_BASE_WORKER_POOL_H_
#define CC_BASE_WORKER_POOL_H_

#include <string>

#include "base/cancelable_callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "cc/base/cc_export.h"
#include "cc/base/scoped_ptr_deque.h"

namespace cc {
struct RenderingStats;

namespace internal {

class WorkerPoolTask {
 public:
  virtual ~WorkerPoolTask();

  virtual bool IsCheap() = 0;

  virtual void Run(RenderingStats* rendering_stats) = 0;

  virtual void RunOnThread(
      RenderingStats* rendering_stats, unsigned thread_index) = 0;

  void DidComplete();

 protected:
  WorkerPoolTask(const base::Closure& reply);

  const base::Closure reply_;
};

}  // namespace internal

class CC_EXPORT WorkerPoolClient {
 public:
  virtual void DidFinishDispatchingWorkerPoolCompletionCallbacks() = 0;

 protected:
  virtual ~WorkerPoolClient() {}
};

// A worker thread pool that runs rendering tasks and guarantees completion
// of all pending tasks at shutdown.
class WorkerPool {
 public:
  typedef base::Callback<void(RenderingStats*)> Callback;

  virtual ~WorkerPool();

  static scoped_ptr<WorkerPool> Create(
      WorkerPoolClient* client,
      size_t num_threads,
      base::TimeDelta check_for_completed_tasks_delay,
      const std::string& thread_name_prefix) {
    return make_scoped_ptr(new WorkerPool(client,
                                          num_threads,
                                          check_for_completed_tasks_delay,
                                          thread_name_prefix));
  }

  // Tells the worker pool to shutdown and returns once all pending tasks have
  // completed.
  void Shutdown();

  // Posts |task| to worker pool. On completion, |reply|
  // is posted to the thread that called PostTaskAndReply().
  void PostTaskAndReply(const Callback& task, const base::Closure& reply);

  // Set time limit for running cheap tasks.
  void SetRunCheapTasksTimeLimit(base::TimeTicks run_cheap_tasks_time_limit);

  // Toggle rendering stats collection.
  void SetRecordRenderingStats(bool record_rendering_stats);

  // Collect rendering stats of all completed tasks.
  void GetRenderingStats(RenderingStats* stats);

 protected:
  WorkerPool(WorkerPoolClient* client,
             size_t num_threads,
             base::TimeDelta check_for_completed_tasks_delay,
             const std::string& thread_name_prefix);

  void PostTask(scoped_ptr<internal::WorkerPoolTask> task);

 private:
  class Inner;
  friend class Inner;

  void OnTaskCompleted();
  void OnIdle();
  void ScheduleCheckForCompletedTasks();
  void CheckForCompletedTasks();
  void CancelCheckForCompletedTasks();
  void DispatchCompletionCallbacks();
  void ScheduleRunCheapTasks();
  void RunCheapTasks();

  WorkerPoolClient* client_;
  scoped_refptr<base::MessageLoopProxy> origin_loop_;
  base::WeakPtrFactory<WorkerPool> weak_ptr_factory_;
  base::TimeTicks check_for_completed_tasks_time_;
  base::TimeDelta check_for_completed_tasks_delay_;
  base::CancelableClosure check_for_completed_tasks_callback_;
  bool check_for_completed_tasks_pending_;
  const base::Closure run_cheap_tasks_callback_;
  base::TimeTicks run_cheap_tasks_time_limit_;
  bool run_cheap_tasks_pending_;

  // Holds all completed tasks for which we have not yet dispatched
  // reply callbacks.
  ScopedPtrDeque<internal::WorkerPoolTask> completed_tasks_;

  // Hide the gory details of the worker pool in |inner_|.
  const scoped_ptr<Inner> inner_;

  DISALLOW_COPY_AND_ASSIGN(WorkerPool);
};

}  // namespace cc

#endif  // CC_BASE_WORKER_POOL_H_