Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Side by Side Diff: base/task_scheduler/scheduler_single_thread_task_runner_manager.h

Issue 2902753003: Implement Shared SingleThreadTaskRunners in the Task Scheduler (Closed)
Patch Set: Change Dependent Change Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 using SingleThreadTaskRunnerThreadMode::SHARED, are
fdoray 2017/05/25 17:02:59 no comma
robliao 2017/05/30 20:01:06 Removed.
44 // backed by shared SchedulerWorkers for each COM+task environment combination.
45 // These workers are only reclaimed during JoinForTesting().
gab 2017/05/25 18:47:00 "These workers are lazily instantiated and then on
robliao 2017/05/30 20:01:06 Done.
40 // 46 //
41 // No threads are created (and hence no tasks can run) before Start() is called. 47 // No threads are created (and hence no tasks can run) before Start() is called.
42 // 48 //
43 // This class is thread-safe. 49 // This class is thread-safe.
44 class BASE_EXPORT SchedulerSingleThreadTaskRunnerManager final { 50 class BASE_EXPORT SchedulerSingleThreadTaskRunnerManager final {
45 public: 51 public:
46 SchedulerSingleThreadTaskRunnerManager( 52 SchedulerSingleThreadTaskRunnerManager(
47 TaskTracker* task_tracker, 53 TaskTracker* task_tracker,
48 DelayedTaskManager* delayed_task_manager); 54 DelayedTaskManager* delayed_task_manager);
49 ~SchedulerSingleThreadTaskRunnerManager(); 55 ~SchedulerSingleThreadTaskRunnerManager();
50 56
51 // Starts threads for existing SingleThreadTaskRunners and allows threads to 57 // Starts threads for existing SingleThreadTaskRunners and allows threads to
52 // be started when SingleThreadTaskRunners are created in the future. 58 // be started when SingleThreadTaskRunners are created in the future.
53 void Start(); 59 void Start();
54 60
55 // Creates a SingleThreadTaskRunner which runs tasks with |traits| on a 61 // Creates a SingleThreadTaskRunner which runs tasks with |traits| on a thread
56 // dedicated thread named "TaskSchedulerSingleThread" + |name| + index. 62 // named "TaskSchedulerSingleThread[Shared]" + |name| +
gab 2017/05/25 18:47:00 I'd say TaskScheduler[Shared]SingleThread so that
robliao 2017/05/30 20:01:07 This is an artifact of preserving TaskSchedulerSin
57 // |priority_hint| is the preferred thread priority; the actual thread 63 // kEnvironmentParams[GetEnvironmentIndexForTraits(traits)].name_suffix +
58 // priority depends on shutdown state and platform capabilities. 64 // index. "Shared" will be in the thread name when |thread_mode| is SHARED. If
65 // |thread_mode| is DEDICATED, a thread will be dedicated to the returned task
66 // runner, otherwise it could be shared with other SingleThreadTaskRunners.
gab 2017/05/25 18:47:00 I think the rest of this comment is redundant star
robliao 2017/05/30 20:01:06 I certainly agree with this sentiment. I would pre
59 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( 67 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits(
60 const std::string& name, 68 const std::string& name,
61 ThreadPriority priority_hint, 69 const TaskTraits& traits,
62 const TaskTraits& traits); 70 SingleThreadTaskRunnerThreadMode thread_mode);
63 71
64 #if defined(OS_WIN) 72 #if defined(OS_WIN)
65 // Creates a SingleThreadTaskRunner which runs tasks with |traits| on a 73 // Creates a SingleThreadTaskRunner which runs tasks with |traits| on a COM
66 // dedicated COM STA thread named "TaskSchedulerSingleThreadCOMSTA" + |name| + 74 // STA thread named "TaskSchedulerSingleThreadCOMSTA[Shared]" + |name| +
67 // index. |priority_hint| is the preferred thread priority; the actual thread 75 // kEnvironmentParams[GetEnvironmentIndexForTraits(traits)].name_suffix +
68 // priority depends on shutdown state and platform capabilities. 76 // index. "Shared" will be in the thread name when |thread_mode| is SHARED. If
77 // |thread_mode| is DEDICATED, a thread will be dedicated to the returned task
78 // runner, otherwise it could be shared with other SingleThreadTaskRunners.
gab 2017/05/25 18:47:00 ditto from \"Shared\"
robliao 2017/05/30 20:01:06 Agreed. See above.
69 scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunnerWithTraits( 79 scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunnerWithTraits(
70 const std::string& name, 80 const std::string& name,
71 ThreadPriority priority_hint, 81 const TaskTraits& traits,
72 const TaskTraits& traits); 82 SingleThreadTaskRunnerThreadMode thread_mode);
73 #endif // defined(OS_WIN) 83 #endif // defined(OS_WIN)
74 84
75 void JoinForTesting(); 85 void JoinForTesting();
76 86
77 private: 87 private:
78 class SchedulerSingleThreadTaskRunner; 88 class SchedulerSingleThreadTaskRunner;
79 89
80 template <typename DelegateType> 90 template <typename DelegateType>
81 scoped_refptr<SingleThreadTaskRunner> 91 scoped_refptr<SchedulerSingleThreadTaskRunner> CreateTaskRunnerWithTraitsImpl(
82 CreateSingleThreadTaskRunnerWithDelegate(const std::string& name, 92 const std::string& name,
83 ThreadPriority priority_hint, 93 const TaskTraits& traits,
84 const TaskTraits& traits); 94 SingleThreadTaskRunnerThreadMode thread_mode);
85 95
86 template <typename DelegateType> 96 template <typename DelegateType>
87 std::unique_ptr<SchedulerWorkerDelegate> CreateSchedulerWorkerDelegate( 97 std::unique_ptr<SchedulerWorkerDelegate> CreateSchedulerWorkerDelegate(
88 const std::string& name, 98 const std::string& name,
89 int id); 99 int id);
90 100
91 template <typename DelegateType> 101 template <typename DelegateType>
92 SchedulerWorker* CreateAndRegisterSchedulerWorker( 102 SchedulerWorker* CreateAndRegisterSchedulerWorker(
93 const std::string& name, 103 const std::string& name,
94 ThreadPriority priority_hint); 104 ThreadPriority priority_hint);
95 105
106 template <typename DelegateType>
107 SchedulerWorker*& GetSharedSchedulerWorkerForTraits(const TaskTraits& traits);
gab 2017/05/25 18:47:00 ** is more common and less surprising than *& IMO,
robliao 2017/05/30 20:01:06 I wanted to enforce the non-nullable property for
108
96 void UnregisterSchedulerWorker(SchedulerWorker* worker); 109 void UnregisterSchedulerWorker(SchedulerWorker* worker);
97 110
111 void ReleaseSharedSchedulerWorkers();
112
98 TaskTracker* const task_tracker_; 113 TaskTracker* const task_tracker_;
99 DelayedTaskManager* const delayed_task_manager_; 114 DelayedTaskManager* const delayed_task_manager_;
100 115
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() 116 #if DCHECK_IS_ON()
110 subtle::Atomic32 workers_unregistered_during_join_ = 0; 117 subtle::Atomic32 workers_unregistered_during_join_ = 0;
111 #endif 118 #endif
112 119
120 // Synchronizes access to all members below.
121 SchedulerLock lock_;
122 std::vector<scoped_refptr<SchedulerWorker>> workers_;
123 int next_worker_id_ = 0;
124
125 SchedulerWorker* shared_scheduler_workers_[4];
fdoray 2017/05/25 17:02:59 s/4/ENVIRONMENT_COUNT/
gab 2017/05/25 18:47:00 = {}; to zero-initialize inline
robliao 2017/05/30 20:01:06 Done.
126
127 #if defined(OS_WIN)
128 SchedulerWorker* shared_com_scheduler_workers_[4];
fdoray 2017/05/25 17:02:59 s/4/ENVIRONMENT_COUNT/
gab 2017/05/25 18:47:00 ditto
robliao 2017/05/30 20:01:06 Done.
129 #endif // defined(OS_WIN)
130
131 // Set to true when Start() is called.
132 bool started_ = false;
133
113 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunnerManager); 134 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunnerManager);
114 }; 135 };
115 136
116 } // namespace internal 137 } // namespace internal
117 } // namespace base 138 } // namespace base
118 139
119 #endif // BASE_TASK_SCHEDULER_SCHEDULER_SINGLE_THREAD_TASK_RUNNER_MANAGER_H_ 140 #endif // BASE_TASK_SCHEDULER_SCHEDULER_SINGLE_THREAD_TASK_RUNNER_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698