ericrk | 4e3aa5a | 2015-12-01 03:53:56 | [diff] [blame] | 1 | // Copyright 2015 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 CC_RASTER_SINGLE_THREAD_TASK_GRAPH_RUNNER_H_ |
| 6 | #define CC_RASTER_SINGLE_THREAD_TASK_GRAPH_RUNNER_H_ |
| 7 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 8 | #include <memory> |
Benoit Lize | c3fca2f9 | 2020-09-18 17:56:00 | [diff] [blame] | 9 | #include <string> |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 10 | |
ericrk | 4e3aa5a | 2015-12-01 03:53:56 | [diff] [blame] | 11 | #include "base/synchronization/condition_variable.h" |
Benoit Lize | c3fca2f9 | 2020-09-18 17:56:00 | [diff] [blame] | 12 | #include "base/thread_annotations.h" |
ericrk | 4e3aa5a | 2015-12-01 03:53:56 | [diff] [blame] | 13 | #include "base/threading/simple_thread.h" |
| 14 | #include "cc/raster/task_graph_runner.h" |
| 15 | #include "cc/raster/task_graph_work_queue.h" |
| 16 | |
| 17 | namespace base { |
| 18 | class SimpleThread; |
| 19 | } |
| 20 | |
| 21 | namespace cc { |
| 22 | |
| 23 | // Runs TaskGraphs asynchronously using a single worker thread. |
| 24 | class CC_EXPORT SingleThreadTaskGraphRunner |
| 25 | : public TaskGraphRunner, |
| 26 | public base::DelegateSimpleThread::Delegate { |
| 27 | public: |
| 28 | SingleThreadTaskGraphRunner(); |
| 29 | ~SingleThreadTaskGraphRunner() override; |
| 30 | |
| 31 | // Overridden from TaskGraphRunner: |
vmpstr | cfd2408 | 2016-10-20 22:33:51 | [diff] [blame] | 32 | NamespaceToken GenerateNamespaceToken() override; |
ericrk | 4e3aa5a | 2015-12-01 03:53:56 | [diff] [blame] | 33 | void ScheduleTasks(NamespaceToken token, TaskGraph* graph) override; |
| 34 | void WaitForTasksToFinishRunning(NamespaceToken token) override; |
| 35 | void CollectCompletedTasks(NamespaceToken token, |
| 36 | Task::Vector* completed_tasks) override; |
| 37 | |
| 38 | // Overridden from base::DelegateSimpleThread::Delegate: |
| 39 | void Run() override; |
| 40 | |
| 41 | void Start(const std::string& thread_name, |
| 42 | const base::SimpleThread::Options& thread_options); |
| 43 | void Shutdown(); |
| 44 | |
| 45 | private: |
ericrk | 5a1188c6 | 2015-12-18 22:04:04 | [diff] [blame] | 46 | // Returns true if there was a task to run. |
Benoit Lize | c3fca2f9 | 2020-09-18 17:56:00 | [diff] [blame] | 47 | bool RunTaskWithLockAcquired() EXCLUSIVE_LOCKS_REQUIRED(lock_); |
ericrk | 4e3aa5a | 2015-12-01 03:53:56 | [diff] [blame] | 48 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 49 | std::unique_ptr<base::SimpleThread> thread_; |
ericrk | 4e3aa5a | 2015-12-01 03:53:56 | [diff] [blame] | 50 | |
| 51 | // Lock to exclusively access all the following members that are used to |
| 52 | // implement the TaskRunner interfaces. |
| 53 | base::Lock lock_; |
| 54 | |
| 55 | // Stores the actual tasks to be run by this runner, sorted by priority. |
Benoit Lize | c3fca2f9 | 2020-09-18 17:56:00 | [diff] [blame] | 56 | TaskGraphWorkQueue work_queue_ GUARDED_BY(lock_); |
ericrk | 4e3aa5a | 2015-12-01 03:53:56 | [diff] [blame] | 57 | |
| 58 | // Condition variable that is waited on by Run() until new tasks are ready to |
| 59 | // run or shutdown starts. |
| 60 | base::ConditionVariable has_ready_to_run_tasks_cv_; |
| 61 | |
| 62 | // Condition variable that is waited on by origin threads until a namespace |
| 63 | // has finished running all associated tasks. |
| 64 | base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_; |
| 65 | |
| 66 | // Set during shutdown. Tells Run() to return when no more tasks are pending. |
Benoit Lize | c3fca2f9 | 2020-09-18 17:56:00 | [diff] [blame] | 67 | bool shutdown_ GUARDED_BY(lock_) = false; |
ericrk | 4e3aa5a | 2015-12-01 03:53:56 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | } // namespace cc |
| 71 | |
| 72 | #endif // CC_RASTER_SINGLE_THREAD_TASK_GRAPH_RUNNER_H_ |