OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_ | 5 #ifndef BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_ |
6 #define BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_ | 6 #define BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_ |
7 | 7 |
8 #include <stddef.h> | |
9 | |
8 #include <memory> | 10 #include <memory> |
11 #include <string> | |
12 #include <vector> | |
9 | 13 |
10 #include "base/base_export.h" | 14 #include "base/base_export.h" |
15 #include "base/callback.h" | |
11 #include "base/logging.h" | 16 #include "base/logging.h" |
12 #include "base/macros.h" | 17 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
14 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
15 #include "base/task_runner.h" | 20 #include "base/task_runner.h" |
16 #include "base/task_scheduler/delayed_task_manager.h" | 21 #include "base/task_scheduler/delayed_task_manager.h" |
22 #include "base/task_scheduler/scheduler_worker_pool_impl.h" | |
17 #include "base/task_scheduler/sequence.h" | 23 #include "base/task_scheduler/sequence.h" |
18 #include "base/task_scheduler/task_scheduler.h" | 24 #include "base/task_scheduler/task_scheduler.h" |
19 #include "base/task_scheduler/task_tracker.h" | 25 #include "base/task_scheduler/task_tracker.h" |
20 #include "base/task_scheduler/task_traits.h" | 26 #include "base/task_scheduler/task_traits.h" |
27 #include "base/threading/thread.h" | |
21 | 28 |
22 namespace base { | 29 namespace base { |
23 namespace internal { | 30 namespace internal { |
24 | 31 |
25 class SchedulerServiceThread; | 32 class SchedulerServiceThread; |
26 class SchedulerWorkerPoolImpl; | |
27 | 33 |
28 // Default TaskScheduler implementation. This class is thread-safe. | 34 // Default TaskScheduler implementation. This class is thread-safe. |
29 class BASE_EXPORT TaskSchedulerImpl : public TaskScheduler { | 35 class BASE_EXPORT TaskSchedulerImpl : public TaskScheduler { |
30 public: | 36 public: |
31 // Creates and returns an initialized TaskSchedulerImpl. CHECKs on failure to | 37 struct WorkerPoolCreationArgs { |
32 // do so (never returns null). | 38 // Name of the pool. Used to label the pool's threads. |
33 static std::unique_ptr<TaskSchedulerImpl> Create(); | 39 std::string name; |
40 | |
41 // Priority of the pool's threads. | |
42 ThreadPriority thread_priority; | |
43 | |
44 // Whether I/O is allowed in the pool. | |
45 SchedulerWorkerPoolImpl::IORestriction io_restriction; | |
46 | |
47 // Maximum number of threads in the pool. | |
48 size_t max_threads; | |
49 }; | |
50 | |
51 // Returns the index of the worker pool in which a task with |traits| should | |
52 // run. This should be coded in a future-proof way: new traits should | |
53 // gracefully map to a default pool. | |
54 using WorkerPoolIndexForTraitsCallback = | |
55 Callback<size_t(const TaskTraits& traits)>; | |
56 | |
57 // Creates and returns an initialized TaskSchedulerImpl. CHECKs on failure. | |
58 // |worker_pools| describes the worker pools to create. | |
59 // |worker_pool_index_for_traits_callback| returns the index in |worker_pools| | |
60 // of the worker pool in which a task with given traits should run. | |
61 static std::unique_ptr<TaskSchedulerImpl> Create( | |
62 const std::vector<WorkerPoolCreationArgs>& worker_pools, | |
63 const WorkerPoolIndexForTraitsCallback& | |
64 worker_pool_index_for_traits_callback); | |
34 | 65 |
35 // Destroying a TaskSchedulerImpl is not allowed in production; it is always | 66 // Destroying a TaskSchedulerImpl is not allowed in production; it is always |
36 // leaked. In tests, it can only be destroyed after JoinForTesting() has | 67 // leaked. In tests, it can only be destroyed after JoinForTesting() has |
37 // returned. | 68 // returned. |
38 ~TaskSchedulerImpl() override; | 69 ~TaskSchedulerImpl() override; |
39 | 70 |
40 // TaskScheduler: | 71 // TaskScheduler: |
41 void PostTaskWithTraits(const tracked_objects::Location& from_here, | 72 void PostTaskWithTraits(const tracked_objects::Location& from_here, |
42 const TaskTraits& traits, | 73 const TaskTraits& traits, |
43 const Closure& task) override; | 74 const Closure& task) override; |
44 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 75 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
45 const TaskTraits& traits, | 76 const TaskTraits& traits, |
46 ExecutionMode execution_mode) override; | 77 ExecutionMode execution_mode) override; |
47 void Shutdown() override; | 78 void Shutdown() override; |
48 | 79 |
49 // Joins all threads of this scheduler. Tasks that are already running are | 80 // Joins all threads of this scheduler. Tasks that are already running are |
50 // allowed to complete their execution. This can only be called once. | 81 // allowed to complete their execution. This can only be called once. |
51 void JoinForTesting(); | 82 void JoinForTesting(); |
52 | 83 |
53 private: | 84 private: |
54 TaskSchedulerImpl(); | 85 TaskSchedulerImpl(const WorkerPoolIndexForTraitsCallback& |
danakj
2016/06/28 22:58:33
explicit
fdoray
2016/07/04 20:57:29
Done here https://codereview.chromium.org/21246230
| |
86 worker_pool_index_for_traits_callback); | |
55 | 87 |
56 void Initialize(); | 88 void Initialize(const std::vector<WorkerPoolCreationArgs>& worker_pools); |
57 | 89 |
58 // Returns the worker pool that runs Tasks with |traits|. | 90 // Returns the worker pool that runs Tasks with |traits|. |
59 SchedulerWorkerPool* GetWorkerPoolForTraits(const TaskTraits& traits); | 91 SchedulerWorkerPool* GetWorkerPoolForTraits(const TaskTraits& traits); |
60 | 92 |
61 // Callback invoked when a non-single-thread |sequence| isn't empty after a | 93 // Callback invoked when a non-single-thread |sequence| isn't empty after a |
62 // worker pops a Task from it. | 94 // worker pops a Task from it. |
63 void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence); | 95 void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence); |
64 | 96 |
65 // Callback invoked when the delayed run time is changed from the | 97 // Callback invoked when the delayed run time is changed from the |
66 // DelayedTaskManager. | 98 // DelayedTaskManager. |
67 void OnDelayedRunTimeUpdated(); | 99 void OnDelayedRunTimeUpdated(); |
68 | 100 |
69 TaskTracker task_tracker_; | 101 TaskTracker task_tracker_; |
70 DelayedTaskManager delayed_task_manager_; | 102 DelayedTaskManager delayed_task_manager_; |
71 | 103 const WorkerPoolIndexForTraitsCallback worker_pool_index_for_traits_callback_; |
72 // Worker pool for BACKGROUND Tasks without file I/O. | 104 std::vector<std::unique_ptr<SchedulerWorkerPoolImpl>> worker_pools_; |
73 std::unique_ptr<SchedulerWorkerPoolImpl> background_worker_pool_; | |
74 | |
75 // Worker pool for BACKGROUND Tasks with file I/O. | |
76 std::unique_ptr<SchedulerWorkerPoolImpl> background_file_io_worker_pool_; | |
77 | |
78 // Worker pool for USER_VISIBLE and USER_BLOCKING Tasks without file I/O. | |
79 std::unique_ptr<SchedulerWorkerPoolImpl> normal_worker_pool_; | |
80 | |
81 // Worker pool for USER_VISIBLE and USER_BLOCKING Tasks with file I/O. | |
82 std::unique_ptr<SchedulerWorkerPoolImpl> normal_file_io_worker_pool_; | |
83 | |
84 std::unique_ptr<SchedulerServiceThread> service_thread_; | 105 std::unique_ptr<SchedulerServiceThread> service_thread_; |
85 | 106 |
86 #if DCHECK_IS_ON() | 107 #if DCHECK_IS_ON() |
87 // Signaled once JoinForTesting() has returned. | 108 // Signaled once JoinForTesting() has returned. |
88 WaitableEvent join_for_testing_returned_; | 109 WaitableEvent join_for_testing_returned_; |
89 #endif | 110 #endif |
90 | 111 |
91 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImpl); | 112 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImpl); |
92 }; | 113 }; |
93 | 114 |
94 } // namespace internal | 115 } // namespace internal |
95 } // namespace base | 116 } // namespace base |
96 | 117 |
97 #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_ | 118 #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_ |
OLD | NEW |