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_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 Loading... |
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. |initial_state| determines whether the thread is created in |
107 // is enabled. Either JoinForTesting() or Cleanup() must be called before | 107 // Start() or in the first WakeUp() after Start(). Either JoinForTesting() or |
108 // releasing the last external reference. | 108 // Cleanup() must be called before releasing the last external reference. |
109 static scoped_refptr<SchedulerWorker> Create( | 109 SchedulerWorker(ThreadPriority priority_hint, |
110 ThreadPriority priority_hint, | 110 std::unique_ptr<Delegate> delegate, |
111 std::unique_ptr<Delegate> delegate, | 111 TaskTracker* task_tracker, |
112 TaskTracker* task_tracker, | 112 SchedulerBackwardCompatibility backward_compatibility = |
113 InitialState initial_state, | 113 SchedulerBackwardCompatibility::DISABLED, |
114 SchedulerBackwardCompatibility backward_compatibility = | 114 InitialState initial_state = InitialState::ALIVE); |
115 SchedulerBackwardCompatibility::DISABLED); | |
116 | 115 |
117 // Wakes up this SchedulerWorker if it wasn't already awake. After this | 116 // Allows this SchedulerWorker to be backed by a thread. If |
118 // is called, this SchedulerWorker will run Tasks from Sequences | 117 // InitialState::ALIVE was passed to the constructor and Cleanup() wasn't |
119 // returned by the GetWork() method of its delegate until it returns nullptr. | 118 // 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 | 119 // call). If InitialState::DETACHED was passed to the constructor and |
121 // worker. If this happens, there will be no call to GetWork(). | 120 // Cleanup() wasn't called, creation is delayed until the next WakeUp(). No |
| 121 // thread will be created if Cleanup() was called. Returns true on success. |
| 122 bool Start(); |
| 123 |
| 124 // Wakes up this SchedulerWorker if it wasn't already awake. After this is |
| 125 // called, this SchedulerWorker will run Tasks from Sequences returned by the |
| 126 // GetWork() method of its delegate until it returns nullptr. WakeUp() may |
| 127 // fail if the worker is detached and it fails to allocate a new worker. If |
| 128 // this happens, there will be no call to GetWork(). No-op if Start() wasn't |
| 129 // called. |
122 void WakeUp(); | 130 void WakeUp(); |
123 | 131 |
124 SchedulerWorker::Delegate* delegate() { return delegate_.get(); } | 132 SchedulerWorker::Delegate* delegate() { return delegate_.get(); } |
125 | 133 |
126 // Joins this SchedulerWorker. If a Task is already running, it will be | 134 // Joins this SchedulerWorker. If a Task is already running, it will be |
127 // allowed to complete its execution. This can only be called once. | 135 // allowed to complete its execution. This can only be called once. |
128 // | 136 // |
129 // Note: A thread that detaches before JoinForTesting() is called may still be | 137 // Note: A thread that detaches before JoinForTesting() is called may still be |
130 // running after JoinForTesting() returns. However, it can't run tasks after | 138 // running after JoinForTesting() returns. However, it can't run tasks after |
131 // JoinForTesting() returns. | 139 // JoinForTesting() returns. |
(...skipping 16 matching lines...) Expand all Loading... |
148 private: | 156 private: |
149 friend class RefCountedThreadSafe<SchedulerWorker>; | 157 friend class RefCountedThreadSafe<SchedulerWorker>; |
150 class Thread; | 158 class Thread; |
151 enum class DetachNotify { | 159 enum class DetachNotify { |
152 // Do not notify any component. | 160 // Do not notify any component. |
153 SILENT, | 161 SILENT, |
154 // Notify the delegate. | 162 // Notify the delegate. |
155 DELEGATE, | 163 DELEGATE, |
156 }; | 164 }; |
157 | 165 |
158 SchedulerWorker(ThreadPriority thread_priority, | |
159 std::unique_ptr<Delegate> delegate, | |
160 TaskTracker* task_tracker, | |
161 SchedulerBackwardCompatibility backward_compatibility); | |
162 ~SchedulerWorker(); | 166 ~SchedulerWorker(); |
163 | 167 |
164 // Returns ownership of the thread instance when appropriate so that it can be | 168 // 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 | 169 // freed upon termination of the thread. If ownership transfer is not |
166 // possible, returns nullptr. | 170 // possible, returns nullptr. |
167 std::unique_ptr<SchedulerWorker::Thread> DetachThreadObject( | 171 std::unique_ptr<SchedulerWorker::Thread> DetachThreadObject( |
168 DetachNotify detach_notify); | 172 DetachNotify detach_notify); |
169 | 173 |
170 void CreateThread(); | 174 void CreateThread(); |
171 | |
172 void CreateThreadAssertSynchronized(); | |
173 | |
174 bool ShouldExit(); | 175 bool ShouldExit(); |
175 | 176 |
176 // Synchronizes access to |thread_| (read+write) as well as |should_exit_| | 177 // Synchronizes access to |thread_| (read+write), |started_| (read+write) and |
177 // (write-only). See Cleanup() for details. | 178 // |should_exit_| (write-only). See Cleanup() for details. |
178 mutable SchedulerLock thread_lock_; | 179 mutable SchedulerLock thread_lock_; |
179 | 180 |
180 AtomicFlag should_exit_; | |
181 | |
182 // The underlying thread for this SchedulerWorker. | 181 // The underlying thread for this SchedulerWorker. |
183 // The thread object will be cleaned up by the running thread unless we join | 182 // 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 | 183 // against the thread. Joining requires the thread object to remain alive for |
185 // the Thread::Join() call. | 184 // the Thread::Join() call. |
186 std::unique_ptr<Thread> thread_; | 185 std::unique_ptr<Thread> thread_; |
187 | 186 |
| 187 bool started_ = false; |
| 188 AtomicFlag should_exit_; |
| 189 |
188 const ThreadPriority priority_hint_; | 190 const ThreadPriority priority_hint_; |
189 | 191 |
190 const std::unique_ptr<Delegate> delegate_; | 192 const std::unique_ptr<Delegate> delegate_; |
191 TaskTracker* const task_tracker_; | 193 TaskTracker* const task_tracker_; |
192 | 194 |
193 #if defined(OS_WIN) | 195 #if defined(OS_WIN) |
194 const SchedulerBackwardCompatibility backward_compatibility_; | 196 const SchedulerBackwardCompatibility backward_compatibility_; |
195 #endif | 197 #endif |
196 | 198 |
| 199 const InitialState initial_state_; |
| 200 |
197 // Set once JoinForTesting() has been called. | 201 // Set once JoinForTesting() has been called. |
198 AtomicFlag join_called_for_testing_; | 202 AtomicFlag join_called_for_testing_; |
199 | 203 |
200 DISALLOW_COPY_AND_ASSIGN(SchedulerWorker); | 204 DISALLOW_COPY_AND_ASSIGN(SchedulerWorker); |
201 }; | 205 }; |
202 | 206 |
203 } // namespace internal | 207 } // namespace internal |
204 } // namespace base | 208 } // namespace base |
205 | 209 |
206 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ | 210 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ |
OLD | NEW |