Chromium Code Reviews| 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 <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
| 13 #include "base/base_export.h" | 13 #include "base/base_export.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/task_scheduler/environment_config.h" | |
| 17 #include "base/task_scheduler/scheduler_lock.h" | 18 #include "base/task_scheduler/scheduler_lock.h" |
| 19 #include "base/task_scheduler/single_thread_task_runner_thread_mode.h" | |
| 18 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
| 19 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 20 | 22 |
| 21 namespace base { | 23 namespace base { |
| 22 | 24 |
| 23 class TaskTraits; | 25 class TaskTraits; |
| 24 class SingleThreadTaskRunner; | 26 class SingleThreadTaskRunner; |
| 25 | 27 |
| 26 namespace internal { | 28 namespace internal { |
| 27 | 29 |
| 28 class DelayedTaskManager; | 30 class DelayedTaskManager; |
| 29 class SchedulerWorker; | 31 class SchedulerWorker; |
| 30 class TaskTracker; | 32 class TaskTracker; |
| 31 | 33 |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 34 class SchedulerWorkerDelegate; | 36 class SchedulerWorkerDelegate; |
| 35 | 37 |
| 36 } // namespace | 38 } // namespace |
| 37 | 39 |
| 38 // Manages a pool of threads which are each associated with one | 40 // Manages a pool of threads which are each associated with one or more |
| 39 // SingleThreadTaskRunner. | 41 // SingleThreadTaskRunners. |
| 42 // | |
| 43 // SingleThreadTaskRunners are shared by reusing a single dedicated | |
| 44 // SingleThreadTaskRunners for each COM+Trait combination. These | |
|
fdoray
2017/05/24 13:25:45
SingleThreadTaskRunnerThreadMode::SHARED (make it
robliao
2017/05/24 18:28:32
Went with
// SingleThreadTaskRunners using Single
| |
| 45 // SingleThreadTaskRunners are only reclaimed during JoinForTesting(). | |
| 46 // Otherwise, no resources are freed even if the shared SingleThreadTaskRunner | |
| 47 // reference count goes to 1. | |
| 40 // | 48 // |
| 41 // No threads are created (and hence no tasks can run) before Start() is called. | 49 // No threads are created (and hence no tasks can run) before Start() is called. |
| 42 // | 50 // |
| 43 // This class is thread-safe. | 51 // This class is thread-safe. |
| 44 class BASE_EXPORT SchedulerSingleThreadTaskRunnerManager final { | 52 class BASE_EXPORT SchedulerSingleThreadTaskRunnerManager final { |
| 45 public: | 53 public: |
| 46 SchedulerSingleThreadTaskRunnerManager( | 54 SchedulerSingleThreadTaskRunnerManager( |
| 47 TaskTracker* task_tracker, | 55 TaskTracker* task_tracker, |
| 48 DelayedTaskManager* delayed_task_manager); | 56 DelayedTaskManager* delayed_task_manager); |
| 49 ~SchedulerSingleThreadTaskRunnerManager(); | 57 ~SchedulerSingleThreadTaskRunnerManager(); |
| 50 | 58 |
| 51 // Starts threads for existing SingleThreadTaskRunners and allows threads to | 59 // Starts threads for existing SingleThreadTaskRunners and allows threads to |
| 52 // be started when SingleThreadTaskRunners are created in the future. | 60 // be started when SingleThreadTaskRunners are created in the future. |
| 53 void Start(); | 61 void Start(); |
| 54 | 62 |
| 55 // Creates a SingleThreadTaskRunner which runs tasks with |traits| on a | 63 // Creates a SingleThreadTaskRunner which runs tasks with |traits| on a thread |
| 56 // dedicated thread named "TaskSchedulerSingleThread" + |name| + index. | 64 // named "TaskSchedulerSingleThread[Shared]" + |name| + index. "Shared" will |
| 57 // |priority_hint| is the preferred thread priority; the actual thread | 65 // be in the thread name when |thread_mode| is SHARED. |priority_hint| is the |
| 58 // priority depends on shutdown state and platform capabilities. | 66 // preferred thread priority. The actual thread priority depends on shutdown |
| 67 // state and platform capabilities. If |thread_mode| is DEDICATED, a thread | |
| 68 // will be dedicated to the returned task runner, otherwise it could be shared | |
| 69 // with other SingleThreadTaskRunners. | |
| 59 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( | 70 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( |
| 60 const std::string& name, | 71 const std::string& name, |
| 61 ThreadPriority priority_hint, | 72 ThreadPriority priority_hint, |
| 62 const TaskTraits& traits); | 73 const TaskTraits& traits, |
| 74 SingleThreadTaskRunnerThreadMode thread_mode); | |
| 63 | 75 |
| 64 #if defined(OS_WIN) | 76 #if defined(OS_WIN) |
| 65 // Creates a SingleThreadTaskRunner which runs tasks with |traits| on a | 77 // Creates a SingleThreadTaskRunner which runs tasks with |traits| on a COM |
| 66 // dedicated COM STA thread named "TaskSchedulerSingleThreadCOMSTA" + |name| + | 78 // STA thread named "TaskSchedulerSingleThreadCOMSTA[Shared]" + |name| + |
| 67 // index. |priority_hint| is the preferred thread priority; the actual thread | 79 // index. "Shared" will be in the thread name when |thread_mode| is SHARED. |
| 68 // priority depends on shutdown state and platform capabilities. | 80 // |priority_hint| is the preferred thread priority. The actual thread |
| 81 // priority depends on shutdown state and platform capabilities. If | |
| 82 // |thread_mode| is DEDICATED, a thread will be dedicated to the returned task | |
| 83 // runner, otherwise it could be shared with other SingleThreadTaskRunners. | |
| 69 scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunnerWithTraits( | 84 scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunnerWithTraits( |
| 70 const std::string& name, | 85 const std::string& name, |
| 71 ThreadPriority priority_hint, | 86 ThreadPriority priority_hint, |
| 72 const TaskTraits& traits); | 87 const TaskTraits& traits, |
| 88 SingleThreadTaskRunnerThreadMode thread_mode); | |
| 73 #endif // defined(OS_WIN) | 89 #endif // defined(OS_WIN) |
| 74 | 90 |
| 75 void JoinForTesting(); | 91 void JoinForTesting(); |
| 76 | 92 |
| 77 private: | 93 private: |
| 78 class SchedulerSingleThreadTaskRunner; | 94 class SchedulerSingleThreadTaskRunner; |
| 79 | 95 |
| 80 template <typename DelegateType> | 96 template <typename DelegateType> |
| 81 scoped_refptr<SingleThreadTaskRunner> | 97 scoped_refptr<SchedulerSingleThreadTaskRunner> CreateTaskRunnerWithTraitsImpl( |
| 98 const std::string& name, | |
| 99 ThreadPriority priority_hint, | |
| 100 const TaskTraits& traits, | |
| 101 SingleThreadTaskRunnerThreadMode thread_mode); | |
| 102 | |
| 103 template <typename DelegateType> | |
| 104 scoped_refptr<SchedulerSingleThreadTaskRunner> | |
| 82 CreateSingleThreadTaskRunnerWithDelegate(const std::string& name, | 105 CreateSingleThreadTaskRunnerWithDelegate(const std::string& name, |
| 83 ThreadPriority priority_hint, | 106 ThreadPriority priority_hint, |
| 84 const TaskTraits& traits); | 107 const TaskTraits& traits); |
| 85 | 108 |
| 86 template <typename DelegateType> | 109 template <typename DelegateType> |
| 87 std::unique_ptr<SchedulerWorkerDelegate> CreateSchedulerWorkerDelegate( | 110 std::unique_ptr<SchedulerWorkerDelegate> CreateSchedulerWorkerDelegate( |
| 88 const std::string& name, | 111 const std::string& name, |
| 89 int id); | 112 int id); |
| 90 | 113 |
| 91 template <typename DelegateType> | 114 template <typename DelegateType> |
| 92 SchedulerWorker* CreateAndRegisterSchedulerWorker( | 115 SchedulerWorker* CreateAndRegisterSchedulerWorker( |
| 93 const std::string& name, | 116 const std::string& name, |
| 94 ThreadPriority priority_hint); | 117 ThreadPriority priority_hint); |
| 95 | 118 |
| 119 template <typename DelegateType> | |
| 120 scoped_refptr<SchedulerSingleThreadTaskRunner>& GetSharedTaskRunnerForTraits( | |
| 121 const TaskTraits& traits); | |
| 122 | |
| 96 void UnregisterSchedulerWorker(SchedulerWorker* worker); | 123 void UnregisterSchedulerWorker(SchedulerWorker* worker); |
| 97 | 124 |
| 125 void ReleaseSharedTaskRunners(); | |
| 126 | |
| 98 TaskTracker* const task_tracker_; | 127 TaskTracker* const task_tracker_; |
| 99 DelayedTaskManager* const delayed_task_manager_; | 128 DelayedTaskManager* const delayed_task_manager_; |
| 100 | 129 |
| 101 // Synchronizes access to |workers_|, |next_worker_id_| and |started_|. | |
| 102 SchedulerLock lock_; | |
| 103 std::vector<scoped_refptr<SchedulerWorker>> workers_; | |
| 104 int next_worker_id_ = 0; | |
| 105 | |
| 106 // Set to true when Start() is called. | |
| 107 bool started_ = false; | |
| 108 | |
| 109 #if DCHECK_IS_ON() | 130 #if DCHECK_IS_ON() |
| 110 subtle::Atomic32 workers_unregistered_during_join_ = 0; | 131 subtle::Atomic32 workers_unregistered_during_join_ = 0; |
| 111 #endif | 132 #endif |
| 112 | 133 |
| 134 // Synchronizes access to all members below. | |
| 135 SchedulerLock lock_; | |
| 136 std::vector<scoped_refptr<SchedulerWorker>> workers_; | |
| 137 int next_worker_id_ = 0; | |
| 138 | |
| 139 scoped_refptr<SchedulerSingleThreadTaskRunner> shared_task_runners_[4]; | |
| 140 | |
| 141 #if defined(OS_WIN) | |
| 142 scoped_refptr<SchedulerSingleThreadTaskRunner> shared_com_task_runners_[4]; | |
| 143 #endif // defined(OS_WIN) | |
| 144 | |
| 145 // Set to true when Start() is called. | |
| 146 bool started_ = false; | |
| 147 | |
| 113 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunnerManager); | 148 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunnerManager); |
| 114 }; | 149 }; |
| 115 | 150 |
| 116 } // namespace internal | 151 } // namespace internal |
| 117 } // namespace base | 152 } // namespace base |
| 118 | 153 |
| 119 #endif // BASE_TASK_SCHEDULER_SCHEDULER_SINGLE_THREAD_TASK_RUNNER_MANAGER_H_ | 154 #endif // BASE_TASK_SCHEDULER_SCHEDULER_SINGLE_THREAD_TASK_RUNNER_MANAGER_H_ |
| OLD | NEW |