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" |
11 #include "base/logging.h" | 15 #include "base/logging.h" |
12 #include "base/macros.h" | 16 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
14 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
15 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
16 #include "base/task_scheduler/delayed_task_manager.h" | 20 #include "base/task_scheduler/delayed_task_manager.h" |
| 21 #include "base/task_scheduler/scheduler_thread_pool_impl.h" |
17 #include "base/task_scheduler/sequence.h" | 22 #include "base/task_scheduler/sequence.h" |
18 #include "base/task_scheduler/task_scheduler.h" | 23 #include "base/task_scheduler/task_scheduler.h" |
19 #include "base/task_scheduler/task_tracker.h" | 24 #include "base/task_scheduler/task_tracker.h" |
20 #include "base/task_scheduler/task_traits.h" | 25 #include "base/task_scheduler/task_traits.h" |
| 26 #include "base/threading/platform_thread.h" |
21 | 27 |
22 namespace base { | 28 namespace base { |
23 namespace internal { | 29 namespace internal { |
24 | 30 |
25 class SchedulerServiceThread; | 31 class SchedulerServiceThread; |
26 class SchedulerThreadPoolImpl; | 32 class SchedulerThreadPoolImpl; |
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 // Delegate interface for TaskSchedulerImpl. |
32 // do so (never returns null). | 38 class BASE_EXPORT Delegate { |
33 static std::unique_ptr<TaskSchedulerImpl> Create(); | 39 public: |
| 40 struct ThreadPoolCreationArgs { |
| 41 // Name of the pool. Use to label the pool's threads. |
| 42 std::string name; |
| 43 |
| 44 // Priority of the pool's threads. |
| 45 ThreadPriority thread_priority; |
| 46 |
| 47 // Whether IO is allowed in the pool. |
| 48 SchedulerThreadPoolImpl::IORestriction io_restriction; |
| 49 |
| 50 // Maximum number of threads in the pool. |
| 51 size_t max_threads; |
| 52 }; |
| 53 |
| 54 virtual ~Delegate() = default; |
| 55 |
| 56 // Returns the number of pools to create. |
| 57 virtual size_t GetNumThreadPools() = 0; |
| 58 |
| 59 // Returns creation arguments for the pool at index |pool_index|. |
| 60 virtual ThreadPoolCreationArgs GetCreationArgsForThreadPool( |
| 61 size_t pool_index) = 0; |
| 62 |
| 63 // Returns the index of the pool in which a task with |traits| should run. |
| 64 virtual size_t GetThreadPoolIndexForTraits(const TaskTraits& traits) = 0; |
| 65 }; |
| 66 |
| 67 // Creates and returns an initialized TaskSchedulerImpl. CHECKs on failure |
| 68 // (never returns null). |
| 69 static std::unique_ptr<TaskSchedulerImpl> Create( |
| 70 std::unique_ptr<Delegate> delegate); |
34 | 71 |
35 // Destroying a TaskSchedulerImpl is not allowed in production; it is always | 72 // Destroying a TaskSchedulerImpl is not allowed in production; it is always |
36 // leaked. In tests, it can only be destroyed after JoinForTesting() has | 73 // leaked. In tests, it can only be destroyed after JoinForTesting() has |
37 // returned. | 74 // returned. |
38 ~TaskSchedulerImpl() override; | 75 ~TaskSchedulerImpl() override; |
39 | 76 |
40 // TaskScheduler: | 77 // TaskScheduler: |
41 void PostTaskWithTraits(const tracked_objects::Location& from_here, | 78 void PostTaskWithTraits(const tracked_objects::Location& from_here, |
42 const TaskTraits& traits, | 79 const TaskTraits& traits, |
43 const Closure& task) override; | 80 const Closure& task) override; |
44 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 81 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
45 const TaskTraits& traits, | 82 const TaskTraits& traits, |
46 ExecutionMode execution_mode) override; | 83 ExecutionMode execution_mode) override; |
47 void Shutdown() override; | 84 void Shutdown() override; |
48 | 85 |
49 // Joins all threads of this scheduler. Tasks that are already running are | 86 // Joins all threads of this scheduler. Tasks that are already running are |
50 // allowed to complete their execution. This can only be called once. | 87 // allowed to complete their execution. This can only be called once. |
51 void JoinForTesting(); | 88 void JoinForTesting(); |
52 | 89 |
53 private: | 90 private: |
54 TaskSchedulerImpl(); | 91 TaskSchedulerImpl(std::unique_ptr<Delegate> delegate); |
55 | 92 |
56 void Initialize(); | 93 void Initialize(); |
57 | 94 |
58 // Returns the thread pool that runs Tasks with |traits|. | 95 // Returns the thread pool that runs Tasks with |traits|. |
59 SchedulerThreadPool* GetThreadPoolForTraits(const TaskTraits& traits); | 96 SchedulerThreadPool* GetThreadPoolForTraits(const TaskTraits& traits); |
60 | 97 |
61 // Callback invoked when a non-single-thread |sequence| isn't empty after a | 98 // Callback invoked when a non-single-thread |sequence| isn't empty after a |
62 // worker thread pops a Task from it. | 99 // worker thread pops a Task from it. |
63 void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence); | 100 void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence); |
64 | 101 |
65 // Callback invoked when the delayed run time is changed from the | 102 // Callback invoked when the delayed run time is changed from the |
66 // DelayedTaskManager. | 103 // DelayedTaskManager. |
67 void OnDelayedRunTimeUpdated(); | 104 void OnDelayedRunTimeUpdated(); |
68 | 105 |
| 106 std::unique_ptr<Delegate> delegate_; |
69 TaskTracker task_tracker_; | 107 TaskTracker task_tracker_; |
70 DelayedTaskManager delayed_task_manager_; | 108 DelayedTaskManager delayed_task_manager_; |
71 | 109 std::vector<std::unique_ptr<SchedulerThreadPoolImpl>> thread_pools_; |
72 // Thread pool for BACKGROUND Tasks without file I/O. | |
73 std::unique_ptr<SchedulerThreadPoolImpl> background_thread_pool_; | |
74 | |
75 // Thread pool for BACKGROUND Tasks with file I/O. | |
76 std::unique_ptr<SchedulerThreadPoolImpl> background_file_io_thread_pool_; | |
77 | |
78 // Thread pool for USER_VISIBLE and USER_BLOCKING Tasks without file I/O. | |
79 std::unique_ptr<SchedulerThreadPoolImpl> normal_thread_pool_; | |
80 | |
81 // Thread pool for USER_VISIBLE and USER_BLOCKING Tasks with file I/O. | |
82 std::unique_ptr<SchedulerThreadPoolImpl> normal_file_io_thread_pool_; | |
83 | |
84 std::unique_ptr<SchedulerServiceThread> service_thread_; | 110 std::unique_ptr<SchedulerServiceThread> service_thread_; |
85 | 111 |
86 #if DCHECK_IS_ON() | 112 #if DCHECK_IS_ON() |
87 // Signaled once JoinForTesting() has returned. | 113 // Signaled once JoinForTesting() has returned. |
88 WaitableEvent join_for_testing_returned_; | 114 WaitableEvent join_for_testing_returned_; |
89 #endif | 115 #endif |
90 | 116 |
91 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImpl); | 117 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImpl); |
92 }; | 118 }; |
93 | 119 |
94 } // namespace internal | 120 } // namespace internal |
95 } // namespace base | 121 } // namespace base |
96 | 122 |
97 #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_ | 123 #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_ |
OLD | NEW |