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

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

Issue 2806413002: Separate the create and start phases in SchedulerSingleThreadTaskRunnerManager. (Closed)
Patch Set: self-review Created 3 years, 8 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 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_SCHEDULER_WORKER_H_ 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_
6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/base_export.h" 10 #include "base/base_export.h"
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 // SchedulerLock. 91 // SchedulerLock.
92 virtual void OnDetach() = 0; 92 virtual void OnDetach() = 0;
93 93
94 // Called by a thread right before the main function exits. 94 // Called by a thread right before the main function exits.
95 virtual void OnMainExit() {} 95 virtual void OnMainExit() {}
96 }; 96 };
97 97
98 enum class InitialState { ALIVE, DETACHED }; 98 enum class InitialState { ALIVE, DETACHED };
99 99
100 // Creates a SchedulerWorker that runs Tasks from Sequences returned by 100 // Creates a SchedulerWorker that runs Tasks from Sequences returned by
101 // |delegate|. |priority_hint| is the preferred thread priority; the actual 101 // |delegate|. No actual thread will be created for this SchedulerWorker
102 // thread priority depends on shutdown state and platform capabilities. 102 // before Start() is called. |priority_hint| is the preferred thread priority;
103 // |task_tracker| is used to handle shutdown behavior of Tasks. If 103 // the actual thread priority depends on shutdown state and platform
104 // |worker_state| is DETACHED, the thread will be created upon a WakeUp(). 104 // capabilities. |task_tracker| is used to handle shutdown behavior of Tasks.
105 // Returns nullptr if creating the underlying platform thread fails during 105 // |backward_compatibility| indicates whether backward compatibility is
106 // Create(). |backward_compatibility| indicates whether backward compatibility 106 // enabled. Either JoinForTesting() or Cleanup() must be called before
107 // is enabled. Either JoinForTesting() or Cleanup() must be called before
108 // releasing the last external reference. 107 // releasing the last external reference.
109 static scoped_refptr<SchedulerWorker> Create( 108 SchedulerWorker(ThreadPriority priority_hint,
110 ThreadPriority priority_hint, 109 std::unique_ptr<Delegate> delegate,
111 std::unique_ptr<Delegate> delegate, 110 TaskTracker* task_tracker,
112 TaskTracker* task_tracker, 111 SchedulerBackwardCompatibility backward_compatibility =
113 InitialState initial_state, 112 SchedulerBackwardCompatibility::DISABLED,
114 SchedulerBackwardCompatibility backward_compatibility = 113 InitialState initial_state = InitialState::ALIVE);
robliao 2017/04/18 21:08:02 Nit: Add initial_state back into the docs.
fdoray 2017/04/18 23:03:11 Done.
115 SchedulerBackwardCompatibility::DISABLED);
116 114
117 // Wakes up this SchedulerWorker if it wasn't already awake. After this 115 // Allows this SchedulerWorker to be backed by a thread. If
118 // is called, this SchedulerWorker will run Tasks from Sequences 116 // InitialState::ALIVE was passed to the constructor and Cleanup() wasn't
119 // returned by the GetWork() method of its delegate until it returns nullptr. 117 // called, a thread is created immediately (in a wait state pending a WakeUp()
120 // WakeUp() may fail if the worker is detached and it fails to allocate a new 118 // call). If InitialState::DETACHED was passed to the constructor and
121 // worker. If this happens, there will be no call to GetWork(). 119 // Cleanup() wasn't called, creation is delayed until the next WakeUp(). No
120 // thread will be created if Cleanup() was called. Returns true on success.
121 bool Start();
122
123 // Wakes up this SchedulerWorker if it wasn't already awake. After this is
124 // called, this SchedulerWorker will run Tasks from Sequences returned by the
125 // GetWork() method of its delegate until it returns nullptr. WakeUp() may
126 // fail if the worker is detached and it fails to allocate a new worker. If
127 // this happens, there will be no call to GetWork(). No-op if Start() wasn't
128 // called.
122 void WakeUp(); 129 void WakeUp();
123 130
124 SchedulerWorker::Delegate* delegate() { return delegate_.get(); } 131 SchedulerWorker::Delegate* delegate() { return delegate_.get(); }
125 132
126 // Joins this SchedulerWorker. If a Task is already running, it will be 133 // Joins this SchedulerWorker. If a Task is already running, it will be
127 // allowed to complete its execution. This can only be called once. 134 // allowed to complete its execution. This can only be called once.
128 // 135 //
129 // Note: A thread that detaches before JoinForTesting() is called may still be 136 // Note: A thread that detaches before JoinForTesting() is called may still be
130 // running after JoinForTesting() returns. However, it can't run tasks after 137 // running after JoinForTesting() returns. However, it can't run tasks after
131 // JoinForTesting() returns. 138 // JoinForTesting() returns.
(...skipping 16 matching lines...) Expand all
148 private: 155 private:
149 friend class RefCountedThreadSafe<SchedulerWorker>; 156 friend class RefCountedThreadSafe<SchedulerWorker>;
150 class Thread; 157 class Thread;
151 enum class DetachNotify { 158 enum class DetachNotify {
152 // Do not notify any component. 159 // Do not notify any component.
153 SILENT, 160 SILENT,
154 // Notify the delegate. 161 // Notify the delegate.
155 DELEGATE, 162 DELEGATE,
156 }; 163 };
157 164
158 SchedulerWorker(ThreadPriority thread_priority,
159 std::unique_ptr<Delegate> delegate,
160 TaskTracker* task_tracker,
161 SchedulerBackwardCompatibility backward_compatibility);
162 ~SchedulerWorker(); 165 ~SchedulerWorker();
163 166
164 // Returns ownership of the thread instance when appropriate so that it can be 167 // Returns ownership of the thread instance when appropriate so that it can be
165 // freed upon termination of the thread. If ownership transfer is not 168 // freed upon termination of the thread. If ownership transfer is not
166 // possible, returns nullptr. 169 // possible, returns nullptr.
167 std::unique_ptr<SchedulerWorker::Thread> DetachThreadObject( 170 std::unique_ptr<SchedulerWorker::Thread> DetachThreadObject(
168 DetachNotify detach_notify); 171 DetachNotify detach_notify);
169 172
170 void CreateThread(); 173 void CreateThread();
171
172 void CreateThreadAssertSynchronized();
173
174 bool ShouldExit(); 174 bool ShouldExit();
175 175
176 // Synchronizes access to |thread_| (read+write) as well as |should_exit_| 176 // Synchronizes access to |thread_| (read+write), |started_| (read+write) and
177 // (write-only). See Cleanup() for details. 177 // |should_exit_| (write-only). See Cleanup() for details.
178 mutable SchedulerLock thread_lock_; 178 mutable SchedulerLock thread_lock_;
179 179
180 AtomicFlag should_exit_;
181
182 // The underlying thread for this SchedulerWorker. 180 // The underlying thread for this SchedulerWorker.
183 // The thread object will be cleaned up by the running thread unless we join 181 // The thread object will be cleaned up by the running thread unless we join
184 // against the thread. Joining requires the thread object to remain alive for 182 // against the thread. Joining requires the thread object to remain alive for
185 // the Thread::Join() call. 183 // the Thread::Join() call.
186 std::unique_ptr<Thread> thread_; 184 std::unique_ptr<Thread> thread_;
187 185
186 bool started_ = false;
187 AtomicFlag should_exit_;
188
188 const ThreadPriority priority_hint_; 189 const ThreadPriority priority_hint_;
189 190
190 const std::unique_ptr<Delegate> delegate_; 191 const std::unique_ptr<Delegate> delegate_;
191 TaskTracker* const task_tracker_; 192 TaskTracker* const task_tracker_;
192 193
193 #if defined(OS_WIN) 194 #if defined(OS_WIN)
194 const SchedulerBackwardCompatibility backward_compatibility_; 195 const SchedulerBackwardCompatibility backward_compatibility_;
195 #endif 196 #endif
196 197
198 const InitialState initial_state_;
199
197 // Set once JoinForTesting() has been called. 200 // Set once JoinForTesting() has been called.
198 AtomicFlag join_called_for_testing_; 201 AtomicFlag join_called_for_testing_;
199 202
200 DISALLOW_COPY_AND_ASSIGN(SchedulerWorker); 203 DISALLOW_COPY_AND_ASSIGN(SchedulerWorker);
201 }; 204 };
202 205
203 } // namespace internal 206 } // namespace internal
204 } // namespace base 207 } // namespace base
205 208
206 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ 209 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_
OLDNEW
« no previous file with comments | « base/task_scheduler/scheduler_single_thread_task_runner_manager_unittest.cc ('k') | base/task_scheduler/scheduler_worker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698