OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_SCHEDULER_SINGLE_THREAD_TASK_RUNNER_MANAGER_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_SINGLE_THREAD_TASK_RUNNER_MANAGER_H_ |
6 #define BASE_TASK_SCHEDULER_SCHEDULER_SINGLE_THREAD_TASK_RUNNER_MANAGER_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_SINGLE_THREAD_TASK_RUNNER_MANAGER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
| 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
12 #include "base/base_export.h" | 13 #include "base/base_export.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
16 #include "base/task_scheduler/scheduler_lock.h" | 17 #include "base/task_scheduler/scheduler_lock.h" |
17 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 18 #include "base/threading/platform_thread.h" |
18 #include "base/task_scheduler/task_scheduler.h" | |
19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
20 | 20 |
21 namespace base { | 21 namespace base { |
22 | 22 |
23 class TaskTraits; | 23 class TaskTraits; |
24 class SingleThreadTaskRunner; | 24 class SingleThreadTaskRunner; |
25 | 25 |
26 namespace internal { | 26 namespace internal { |
27 | 27 |
28 class DelayedTaskManager; | 28 class DelayedTaskManager; |
29 class SchedulerWorker; | 29 class SchedulerWorker; |
30 class TaskTracker; | 30 class TaskTracker; |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 class SchedulerWorkerDelegate; | 34 class SchedulerWorkerDelegate; |
35 | 35 |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 // Manages a pool of threads which are each associated with one | 38 // Manages a pool of threads which are each associated with one |
39 // SingleThreadTaskRunner. | 39 // SingleThreadTaskRunner. |
40 // | 40 // |
41 // No threads are created (and hence no tasks can run) before Start() is called. | 41 // No threads are created (and hence no tasks can run) before Start() is called. |
42 // | 42 // |
43 // This class is thread-safe. | 43 // This class is thread-safe. |
44 class BASE_EXPORT SchedulerSingleThreadTaskRunnerManager final { | 44 class BASE_EXPORT SchedulerSingleThreadTaskRunnerManager final { |
45 public: | 45 public: |
46 SchedulerSingleThreadTaskRunnerManager( | 46 SchedulerSingleThreadTaskRunnerManager( |
47 const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector, | |
48 const TaskScheduler::WorkerPoolIndexForTraitsCallback& | |
49 worker_pool_index_for_traits_callback, | |
50 TaskTracker* task_tracker, | 47 TaskTracker* task_tracker, |
51 DelayedTaskManager* delayed_task_manager); | 48 DelayedTaskManager* delayed_task_manager); |
52 ~SchedulerSingleThreadTaskRunnerManager(); | 49 ~SchedulerSingleThreadTaskRunnerManager(); |
53 | 50 |
54 // Starts threads for existing SingleThreadTaskRunners and allows threads to | 51 // Starts threads for existing SingleThreadTaskRunners and allows threads to |
55 // be started when SingleThreadTaskRunners are created in the future. | 52 // be started when SingleThreadTaskRunners are created in the future. |
56 void Start(); | 53 void Start(); |
57 | 54 |
| 55 // Creates a SingleThreadTaskRunner which runs tasks with |traits| on a |
| 56 // dedicated thread named "TaskSchedulerSingleThread" + |name| + index. |
| 57 // |priority_hint| is the preferred thread priority; the actual thread |
| 58 // priority depends on shutdown state and platform capabilities. |
58 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( | 59 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( |
| 60 const std::string& name, |
| 61 ThreadPriority priority_hint, |
59 const TaskTraits& traits); | 62 const TaskTraits& traits); |
60 | 63 |
61 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
| 65 // Creates a SingleThreadTaskRunner which runs tasks with |traits| on a |
| 66 // dedicated COM STA thread named "TaskSchedulerSingleThreadCOMSTA" + |name| + |
| 67 // index. |priority_hint| is the preferred thread priority; the actual thread |
| 68 // priority depends on shutdown state and platform capabilities. |
62 scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunnerWithTraits( | 69 scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunnerWithTraits( |
| 70 const std::string& name, |
| 71 ThreadPriority priority_hint, |
63 const TaskTraits& traits); | 72 const TaskTraits& traits); |
64 #endif // defined(OS_WIN) | 73 #endif // defined(OS_WIN) |
65 | 74 |
66 void JoinForTesting(); | 75 void JoinForTesting(); |
67 | 76 |
68 private: | 77 private: |
69 class SchedulerSingleThreadTaskRunner; | 78 class SchedulerSingleThreadTaskRunner; |
70 | 79 |
71 template <typename DelegateType> | 80 template <typename DelegateType> |
72 scoped_refptr<SingleThreadTaskRunner> | 81 scoped_refptr<SingleThreadTaskRunner> |
73 CreateSingleThreadTaskRunnerWithDelegate(const TaskTraits& traits); | 82 CreateSingleThreadTaskRunnerWithDelegate(const std::string& name, |
| 83 ThreadPriority priority_hint, |
| 84 const TaskTraits& traits); |
74 | 85 |
75 template <typename DelegateType> | 86 template <typename DelegateType> |
76 std::unique_ptr<SchedulerWorkerDelegate> CreateSchedulerWorkerDelegate( | 87 std::unique_ptr<SchedulerWorkerDelegate> CreateSchedulerWorkerDelegate( |
77 const SchedulerWorkerPoolParams& params, | 88 const std::string& name, |
78 int id); | 89 int id); |
79 | 90 |
80 template <typename DelegateType> | 91 template <typename DelegateType> |
81 SchedulerWorker* CreateAndRegisterSchedulerWorker( | 92 SchedulerWorker* CreateAndRegisterSchedulerWorker( |
82 const SchedulerWorkerPoolParams& params); | 93 const std::string& name, |
| 94 ThreadPriority priority_hint); |
83 | 95 |
84 void UnregisterSchedulerWorker(SchedulerWorker* worker); | 96 void UnregisterSchedulerWorker(SchedulerWorker* worker); |
85 | 97 |
86 const std::vector<SchedulerWorkerPoolParams> worker_pool_params_vector_; | |
87 const TaskScheduler::WorkerPoolIndexForTraitsCallback | |
88 worker_pool_index_for_traits_callback_; | |
89 TaskTracker* const task_tracker_; | 98 TaskTracker* const task_tracker_; |
90 DelayedTaskManager* const delayed_task_manager_; | 99 DelayedTaskManager* const delayed_task_manager_; |
91 | 100 |
92 // Synchronizes access to |workers_|, |next_worker_id_| and |started_|. | 101 // Synchronizes access to |workers_|, |next_worker_id_| and |started_|. |
93 SchedulerLock lock_; | 102 SchedulerLock lock_; |
94 std::vector<scoped_refptr<SchedulerWorker>> workers_; | 103 std::vector<scoped_refptr<SchedulerWorker>> workers_; |
95 int next_worker_id_ = 0; | 104 int next_worker_id_ = 0; |
96 | 105 |
97 // Set to true when Start() is called. | 106 // Set to true when Start() is called. |
98 bool started_ = false; | 107 bool started_ = false; |
99 | 108 |
100 #if DCHECK_IS_ON() | 109 #if DCHECK_IS_ON() |
101 subtle::Atomic32 workers_unregistered_during_join_ = 0; | 110 subtle::Atomic32 workers_unregistered_during_join_ = 0; |
102 #endif | 111 #endif |
103 | 112 |
104 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunnerManager); | 113 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunnerManager); |
105 }; | 114 }; |
106 | 115 |
107 } // namespace internal | 116 } // namespace internal |
108 } // namespace base | 117 } // namespace base |
109 | 118 |
110 #endif // BASE_TASK_SCHEDULER_SCHEDULER_SINGLE_THREAD_TASK_RUNNER_MANAGER_H_ | 119 #endif // BASE_TASK_SCHEDULER_SCHEDULER_SINGLE_THREAD_TASK_RUNNER_MANAGER_H_ |
OLD | NEW |