forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraster_worker_pool.h
148 lines (119 loc) · 5.73 KB
/
raster_worker_pool.h
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2015 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 CONTENT_RENDERER_RASTER_WORKER_POOL_H_
#define CONTENT_RENDERER_RASTER_WORKER_POOL_H_
#include <memory>
#include "base/callback.h"
#include "base/containers/hash_tables.h"
#include "base/macros.h"
#include "base/sequenced_task_runner.h"
#include "base/synchronization/condition_variable.h"
#include "base/task_runner.h"
#include "base/threading/simple_thread.h"
#include "cc/raster/task_category.h"
#include "cc/raster/task_graph_runner.h"
#include "cc/raster/task_graph_work_queue.h"
#include "content/common/content_export.h"
namespace content {
// A pool of threads used to run raster work.
// Work can be scheduled on the threads using different interfaces.
// The pool itself implements TaskRunner interface and tasks posted via that
// interface might run in parallel.
// CreateSequencedTaskRunner creates a sequenced task runner that might run in
// parallel with other instances of sequenced task runners.
// It's also possible to get the underlying TaskGraphRunner to schedule a graph
// of tasks with their dependencies.
class CONTENT_EXPORT RasterWorkerPool : public base::TaskRunner,
public cc::TaskGraphRunner {
public:
RasterWorkerPool();
// Overridden from base::TaskRunner:
bool PostDelayedTask(const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay) override;
bool RunsTasksOnCurrentThread() const override;
// Overridden from cc::TaskGraphRunner:
cc::NamespaceToken GetNamespaceToken() override;
void ScheduleTasks(cc::NamespaceToken token, cc::TaskGraph* graph) override;
void WaitForTasksToFinishRunning(cc::NamespaceToken token) override;
void CollectCompletedTasks(cc::NamespaceToken token,
cc::Task::Vector* completed_tasks) override;
// Runs a task from one of the provided categories. Categories listed first
// have higher priority.
void Run(const std::vector<cc::TaskCategory>& categories,
base::ConditionVariable* has_ready_to_run_tasks_cv);
void FlushForTesting();
// Spawn |num_threads| number of threads and start running work on the
// worker threads.
void Start(int num_threads);
// Finish running all the posted tasks (and nested task posted by those tasks)
// of all the associated task runners.
// Once all the tasks are executed the method blocks until the threads are
// terminated.
void Shutdown();
cc::TaskGraphRunner* GetTaskGraphRunner() { return this; }
// Create a new sequenced task graph runner.
scoped_refptr<base::SequencedTaskRunner> CreateSequencedTaskRunner();
protected:
~RasterWorkerPool() override;
private:
class RasterWorkerPoolSequencedTaskRunner;
friend class RasterWorkerPoolSequencedTaskRunner;
// Simple Task for the TaskGraphRunner that wraps a closure.
// This class is used to schedule TaskRunner tasks on the
// |task_graph_runner_|.
class ClosureTask : public cc::Task {
public:
explicit ClosureTask(const base::Closure& closure);
// Overridden from cc::Task:
void RunOnWorkerThread() override;
protected:
~ClosureTask() override;
private:
base::Closure closure_;
DISALLOW_COPY_AND_ASSIGN(ClosureTask);
};
void ScheduleTasksWithLockAcquired(cc::NamespaceToken token,
cc::TaskGraph* graph);
void CollectCompletedTasksWithLockAcquired(cc::NamespaceToken token,
cc::Task::Vector* completed_tasks);
// Runs a task from one of the provided categories. Categories listed first
// have higher priority. Returns false if there were no tasks to run.
bool RunTaskWithLockAcquired(const std::vector<cc::TaskCategory>& categories);
// Run next task for the given category. Caller must acquire |lock_| prior to
// calling this function and make sure at least one task is ready to run.
void RunTaskInCategoryWithLockAcquired(cc::TaskCategory category);
// Helper function which signals worker threads if tasks are ready to run.
void SignalHasReadyToRunTasksWithLockAcquired();
// Determines if we should run a new task for the given category. This factors
// in whether a task is available and whether the count of running tasks is
// low enough to start a new one.
bool ShouldRunTaskForCategoryWithLockAcquired(cc::TaskCategory category);
// The actual threads where work is done.
std::vector<std::unique_ptr<base::SimpleThread>> threads_;
// Lock to exclusively access all the following members that are used to
// implement the TaskRunner and TaskGraphRunner interfaces.
base::Lock lock_;
// Stores the tasks to be run, sorted by priority.
cc::TaskGraphWorkQueue work_queue_;
// Namespace used to schedule tasks in the task graph runner.
cc::NamespaceToken namespace_token_;
// List of tasks currently queued up for execution.
cc::Task::Vector tasks_;
// Graph object used for scheduling tasks.
cc::TaskGraph graph_;
// Cached vector to avoid allocation when getting the list of complete
// tasks.
cc::Task::Vector completed_tasks_;
// Condition variables for foreground and background tasks.
base::ConditionVariable has_ready_to_run_foreground_tasks_cv_;
base::ConditionVariable has_ready_to_run_background_tasks_cv_;
// Condition variable that is waited on by origin threads until a namespace
// has finished running all associated tasks.
base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_;
// Set during shutdown. Tells Run() to return when no more tasks are pending.
bool shutdown_;
};
} // namespace content
#endif // CONTENT_RENDERER_RASTER_WORKER_POOL_H_