Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 5 | #ifndef BASE_TASK_THREAD_POOL_TASK_SOURCE_H_ |
| 6 | #define BASE_TASK_THREAD_POOL_TASK_SOURCE_H_ |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 7 | |
| 8 | #include <stddef.h> |
| 9 | |
| 10 | #include "base/base_export.h" |
Etienne Pierre-doray | f7f59c3 | 2019-05-24 16:50:53 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 12 | #include "base/macros.h" |
| 13 | #include "base/memory/ref_counted.h" |
| 14 | #include "base/optional.h" |
Etienne Pierre-doray | 31246215 | 2019-03-19 16:10:17 | [diff] [blame] | 15 | #include "base/sequence_token.h" |
Gabriel Charette | d3564838 | 2019-04-30 21:10:59 | [diff] [blame] | 16 | #include "base/task/common/checked_lock.h" |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 17 | #include "base/task/common/intrusive_heap.h" |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 18 | #include "base/task/task_traits.h" |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 19 | #include "base/task/thread_pool/sequence_sort_key.h" |
| 20 | #include "base/task/thread_pool/task.h" |
Etienne Pierre-doray | 31246215 | 2019-03-19 16:10:17 | [diff] [blame] | 21 | #include "base/threading/sequence_local_storage_map.h" |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 22 | |
| 23 | namespace base { |
| 24 | namespace internal { |
| 25 | |
Etienne Pierre-doray | f7f59c3 | 2019-05-24 16:50:53 | [diff] [blame] | 26 | class TaskTracker; |
| 27 | |
Etienne Pierre-doray | 45fd5d5 | 2019-03-28 15:19:55 | [diff] [blame] | 28 | enum class TaskSourceExecutionMode { |
| 29 | kParallel, |
| 30 | kSequenced, |
| 31 | kSingleThread, |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 32 | kJob, |
| 33 | kMax = kJob, |
Etienne Pierre-doray | 45fd5d5 | 2019-03-28 15:19:55 | [diff] [blame] | 34 | }; |
| 35 | |
Etienne Pierre-doray | 31246215 | 2019-03-19 16:10:17 | [diff] [blame] | 36 | struct BASE_EXPORT ExecutionEnvironment { |
| 37 | SequenceToken token; |
| 38 | SequenceLocalStorageMap* sequence_local_storage; |
| 39 | }; |
| 40 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 41 | // A TaskSource is a virtual class that provides a series of Tasks that must be |
| 42 | // executed. |
| 43 | // |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 44 | // In order to execute a task from this TaskSource, a worker should first make |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 45 | // sure that a task can run with WillRunTask() which returns a RunIntent. |
| 46 | // TakeTask() can then be called to access the next Task, and DidProcessTask() |
| 47 | // must be called after the task was processed. Many overlapping chains of |
| 48 | // WillRunTask(), TakeTask(), run and DidProcessTask() can run concurrently, as |
| 49 | // permitted by WillRunTask(). This ensure that the number of workers |
| 50 | // concurrently running tasks never go over the intended concurrency. |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 51 | // |
| 52 | // In comments below, an "empty TaskSource" is a TaskSource with no Task. |
| 53 | // |
| 54 | // Note: there is a known refcounted-ownership cycle in the Scheduler |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 55 | // architecture: TaskSource -> TaskRunner -> TaskSource -> ... This is okay so |
| 56 | // long as the other owners of TaskSource (PriorityQueue and WorkerThread in |
| 57 | // alternation and ThreadGroupImpl::WorkerThreadDelegateImpl::GetWork() |
| 58 | // temporarily) keep running it (and taking Tasks from it as a result). A |
| 59 | // dangling reference cycle would only occur should they release their reference |
| 60 | // to it while it's not empty. In other words, it is only correct for them to |
| 61 | // release it when DidProcessTask() returns false. |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 62 | // |
| 63 | // This class is thread-safe. |
| 64 | class BASE_EXPORT TaskSource : public RefCountedThreadSafe<TaskSource> { |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 65 | protected: |
| 66 | // Indicates whether a TaskSource has reached its maximum intended concurrency |
| 67 | // and may not run any additional tasks. |
| 68 | enum class Saturated { |
| 69 | kYes, |
| 70 | kNo, |
| 71 | }; |
| 72 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 73 | public: |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 74 | // Result of WillRunTask(). A single task associated with a RunIntent may be |
Etienne Pierre-doray | 3bca492 | 2019-08-13 20:56:56 | [diff] [blame] | 75 | // accessed with TakeTask() and run, or the task source may be cleared with |
| 76 | // Clear() iff this evaluates to true. |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 77 | class BASE_EXPORT RunIntent { |
| 78 | public: |
| 79 | RunIntent() = default; |
| 80 | RunIntent(RunIntent&&) noexcept; |
| 81 | ~RunIntent(); |
| 82 | |
| 83 | RunIntent& operator=(RunIntent&&); |
| 84 | |
| 85 | operator bool() const { return !!task_source_; } |
| 86 | |
| 87 | // Returns true iff the TaskSource from which this RunIntent was obtained |
| 88 | // may not run any additional tasks beyond this RunIntent as it has reached |
| 89 | // its maximum concurrency. This indicates that the TaskSource no longer |
| 90 | // needs to be queued. |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 91 | bool IsSaturated() const { return is_saturated_ == Saturated::kYes; } |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 92 | |
| 93 | const TaskSource* task_source() const { return task_source_; } |
| 94 | |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 95 | void ReleaseForTesting() { |
| 96 | DCHECK(task_source_); |
| 97 | task_source_ = nullptr; |
| 98 | } |
| 99 | |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 100 | private: |
| 101 | friend class TaskSource; |
| 102 | |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 103 | // Indicates the step of a run intent chain. |
| 104 | enum class State { |
| 105 | kInitial, // After WillRunTask(). |
| 106 | kTaskAcquired, // After TakeTask(). |
| 107 | kCompleted, // After DidProcessTask(). |
| 108 | }; |
| 109 | |
| 110 | RunIntent(const TaskSource* task_source, Saturated is_saturated); |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 111 | |
| 112 | void Release() { |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 113 | DCHECK_EQ(run_step_, State::kCompleted); |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 114 | DCHECK(task_source_); |
| 115 | task_source_ = nullptr; |
| 116 | } |
| 117 | |
| 118 | const TaskSource* task_source_ = nullptr; |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 119 | State run_step_ = State::kInitial; |
| 120 | Saturated is_saturated_ = Saturated::kYes; |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 121 | }; |
| 122 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 123 | // A Transaction can perform multiple operations atomically on a |
| 124 | // TaskSource. While a Transaction is alive, it is guaranteed that nothing |
| 125 | // else will access the TaskSource; the TaskSource's lock is held for the |
| 126 | // lifetime of the Transaction. |
| 127 | class BASE_EXPORT Transaction { |
| 128 | public: |
| 129 | Transaction(Transaction&& other); |
| 130 | ~Transaction(); |
| 131 | |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 132 | operator bool() const { return !!task_source_; } |
| 133 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 134 | // Returns the next task to run from this TaskSource. This should be called |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 135 | // only with a valid |intent|. Cannot be called on an empty TaskSource. |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 136 | // |
| 137 | // Because this method cannot be called on an empty TaskSource, the returned |
| 138 | // Optional<Task> is never nullptr. An Optional is used in preparation for |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 139 | // the merge between ThreadPool and TaskQueueManager (in Blink). |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 140 | // https://crbug.com/783309 |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 141 | Optional<Task> TakeTask(RunIntent* intent) WARN_UNUSED_RESULT; |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 142 | |
Etienne Pierre-doray | 3bca492 | 2019-08-13 20:56:56 | [diff] [blame] | 143 | // Returns a task that clears this TaskSource to make it empty. This should |
| 144 | // be called only with a valid |intent|, but may be called for each valid |
| 145 | // outstanding RunIntent. |
| 146 | Optional<Task> Clear(RunIntent intent) WARN_UNUSED_RESULT; |
| 147 | |
| 148 | // Must be called once the task was run. Cannot be called on an empty |
| 149 | // TaskSource. Returns true if the TaskSource should be queued after this |
| 150 | // operation. |
| 151 | bool DidProcessTask(RunIntent intent); |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 152 | |
| 153 | // Returns a SequenceSortKey representing the priority of the TaskSource. |
| 154 | // Cannot be called on an empty TaskSource. |
| 155 | SequenceSortKey GetSortKey() const; |
| 156 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 157 | // Sets TaskSource priority to |priority|. |
| 158 | void UpdatePriority(TaskPriority priority); |
| 159 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 160 | // Returns the traits of all Tasks in the TaskSource. |
| 161 | TaskTraits traits() const { return task_source_->traits_; } |
| 162 | |
| 163 | TaskSource* task_source() const { return task_source_; } |
| 164 | |
| 165 | protected: |
| 166 | explicit Transaction(TaskSource* task_source); |
| 167 | |
| 168 | private: |
| 169 | friend class TaskSource; |
| 170 | |
| 171 | TaskSource* task_source_; |
| 172 | |
| 173 | DISALLOW_COPY_AND_ASSIGN(Transaction); |
| 174 | }; |
| 175 | |
| 176 | // |traits| is metadata that applies to all Tasks in the TaskSource. |
Etienne Pierre-doray | 45fd5d5 | 2019-03-28 15:19:55 | [diff] [blame] | 177 | // |task_runner| is a reference to the TaskRunner feeding this TaskSource. |
| 178 | // |task_runner| can be nullptr only for tasks with no TaskRunner, in which |
| 179 | // case |execution_mode| must be kParallel. Otherwise, |execution_mode| is the |
| 180 | // execution mode of |task_runner|. |
| 181 | TaskSource(const TaskTraits& traits, |
| 182 | TaskRunner* task_runner, |
| 183 | TaskSourceExecutionMode execution_mode); |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 184 | |
| 185 | // Begins a Transaction. This method cannot be called on a thread which has an |
| 186 | // active TaskSource::Transaction. |
Etienne Pierre-doray | f7f59c3 | 2019-05-24 16:50:53 | [diff] [blame] | 187 | Transaction BeginTransaction() WARN_UNUSED_RESULT; |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 188 | |
Etienne Pierre-doray | 31246215 | 2019-03-19 16:10:17 | [diff] [blame] | 189 | virtual ExecutionEnvironment GetExecutionEnvironment() = 0; |
| 190 | |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 191 | // Informs this TaskSource that an additional Task could be run. Returns a |
| 192 | // RunIntent that evaluates to true if this operation is allowed (TakeTask() |
Etienne Pierre-doray | 3bca492 | 2019-08-13 20:56:56 | [diff] [blame] | 193 | // or Clear() can be called), or false otherwise. |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 194 | virtual RunIntent WillRunTask() = 0; |
| 195 | |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 196 | // Thread-safe but the returned value may immediately be obsolete. As such |
| 197 | // this should only be used as a best-effort guess of how many more workers |
Etienne Pierre-doray | 3bca492 | 2019-08-13 20:56:56 | [diff] [blame] | 198 | // are needed. This may be called on an empty task source. |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 199 | virtual size_t GetRemainingConcurrency() const = 0; |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 200 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 201 | // Support for IntrusiveHeap. |
| 202 | void SetHeapHandle(const HeapHandle& handle); |
| 203 | void ClearHeapHandle(); |
Chris Hamilton | 9c9ce50 | 2019-08-22 20:53:18 | [diff] [blame^] | 204 | HeapHandle GetHeapHandle() const { return heap_handle_; } |
| 205 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 206 | HeapHandle heap_handle() const { return heap_handle_; } |
| 207 | |
| 208 | // Returns the shutdown behavior of all Tasks in the TaskSource. Can be |
| 209 | // accessed without a Transaction because it is never mutated. |
| 210 | TaskShutdownBehavior shutdown_behavior() const { |
| 211 | return traits_.shutdown_behavior(); |
| 212 | } |
| 213 | |
Etienne Pierre-doray | 45fd5d5 | 2019-03-28 15:19:55 | [diff] [blame] | 214 | // A reference to TaskRunner is only retained between PushTask() and when |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 215 | // DidProcessTask() returns false, guaranteeing it is safe to dereference this |
Etienne Pierre-doray | 45fd5d5 | 2019-03-28 15:19:55 | [diff] [blame] | 216 | // pointer. Otherwise, the caller should guarantee such TaskRunner still |
| 217 | // exists before dereferencing. |
| 218 | TaskRunner* task_runner() const { return task_runner_; } |
| 219 | |
| 220 | TaskSourceExecutionMode execution_mode() const { return execution_mode_; } |
| 221 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 222 | protected: |
| 223 | virtual ~TaskSource(); |
| 224 | |
| 225 | virtual Optional<Task> TakeTask() = 0; |
| 226 | |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 227 | // Informs this TaskSource that a task was processed. |was_run| indicates |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 228 | // whether the task executed or not. Returns true if the TaskSource |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 229 | // should be queued after this operation. |
Etienne Pierre-doray | 3bca492 | 2019-08-13 20:56:56 | [diff] [blame] | 230 | virtual bool DidProcessTask() = 0; |
Etienne Pierre-doray | 45fd5d5 | 2019-03-28 15:19:55 | [diff] [blame] | 231 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 232 | virtual SequenceSortKey GetSortKey() const = 0; |
| 233 | |
Etienne Pierre-doray | 3bca492 | 2019-08-13 20:56:56 | [diff] [blame] | 234 | // This may be called for each outstanding RunIntent. If applicable, the |
| 235 | // implementation needs to support this being called multiple times. |
| 236 | virtual Optional<Task> Clear() = 0; |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 237 | |
| 238 | // Sets TaskSource priority to |priority|. |
| 239 | void UpdatePriority(TaskPriority priority); |
| 240 | |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 241 | // Constructs and returns a RunIntent, where |is_saturated| indicates that the |
| 242 | // TaskSource has reached its maximum concurrency. |
Etienne Pierre-doray | 36afadeb | 2019-07-12 21:19:41 | [diff] [blame] | 243 | RunIntent MakeRunIntent(Saturated is_saturated) const; |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 244 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 245 | // The TaskTraits of all Tasks in the TaskSource. |
| 246 | TaskTraits traits_; |
| 247 | |
| 248 | private: |
| 249 | friend class RefCountedThreadSafe<TaskSource>; |
| 250 | |
| 251 | // Synchronizes access to all members. |
Gabriel Charette | d3564838 | 2019-04-30 21:10:59 | [diff] [blame] | 252 | mutable CheckedLock lock_{UniversalPredecessor()}; |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 253 | |
| 254 | // The TaskSource's position in its current PriorityQueue. Access is protected |
| 255 | // by the PriorityQueue's lock. |
| 256 | HeapHandle heap_handle_; |
| 257 | |
Etienne Pierre-doray | 45fd5d5 | 2019-03-28 15:19:55 | [diff] [blame] | 258 | // A pointer to the TaskRunner that posts to this TaskSource, if any. The |
Etienne Pierre-doray | abfecf7 | 2019-05-09 20:41:48 | [diff] [blame] | 259 | // derived class is responsible for calling AddRef() when a TaskSource from |
| 260 | // which no Task is executing becomes non-empty and Release() when |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 261 | // DidProcessTask() returns false. |
Etienne Pierre-doray | 45fd5d5 | 2019-03-28 15:19:55 | [diff] [blame] | 262 | TaskRunner* task_runner_; |
| 263 | |
| 264 | TaskSourceExecutionMode execution_mode_; |
| 265 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 266 | DISALLOW_COPY_AND_ASSIGN(TaskSource); |
| 267 | }; |
| 268 | |
Etienne Pierre-doray | f7f59c3 | 2019-05-24 16:50:53 | [diff] [blame] | 269 | // Wrapper around TaskSource to signify the intent to queue and run it. A |
| 270 | // RegisteredTaskSource can only be created with TaskTracker. |
| 271 | class BASE_EXPORT RegisteredTaskSource { |
| 272 | public: |
| 273 | RegisteredTaskSource(); |
| 274 | RegisteredTaskSource(std::nullptr_t); |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 275 | RegisteredTaskSource(RegisteredTaskSource&& other) noexcept; |
Etienne Pierre-doray | f7f59c3 | 2019-05-24 16:50:53 | [diff] [blame] | 276 | ~RegisteredTaskSource(); |
| 277 | |
| 278 | RegisteredTaskSource& operator=(RegisteredTaskSource&& other); |
| 279 | |
| 280 | operator bool() const { return task_source_ != nullptr; } |
| 281 | |
| 282 | TaskSource* operator->() const { return task_source_.get(); } |
| 283 | TaskSource* get() const { return task_source_.get(); } |
| 284 | |
| 285 | static RegisteredTaskSource CreateForTesting( |
| 286 | scoped_refptr<TaskSource> task_source, |
| 287 | TaskTracker* task_tracker = nullptr); |
| 288 | |
| 289 | scoped_refptr<TaskSource> Unregister(); |
| 290 | |
| 291 | private: |
| 292 | friend class TaskTracker; |
Etienne Pierre-doray | f7f59c3 | 2019-05-24 16:50:53 | [diff] [blame] | 293 | RegisteredTaskSource(scoped_refptr<TaskSource> task_source, |
| 294 | TaskTracker* task_tracker); |
| 295 | |
| 296 | scoped_refptr<TaskSource> task_source_; |
| 297 | TaskTracker* task_tracker_; |
| 298 | |
| 299 | DISALLOW_COPY_AND_ASSIGN(RegisteredTaskSource); |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 300 | }; |
| 301 | |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 302 | // Base implementation for TransactionWith[Owned/Registered]TaskSource (with |
| 303 | // Transaction as the decorator) and RunIntentWithRegisteredTaskSource (with |
| 304 | // RunIntent as the decorator). |
| 305 | template <class Decorator, class T> |
| 306 | class BASE_EXPORT DecoratorWithTaskSource : public Decorator { |
| 307 | public: |
| 308 | DecoratorWithTaskSource() = default; |
| 309 | DecoratorWithTaskSource(std::nullptr_t) : DecoratorWithTaskSource() {} |
| 310 | DecoratorWithTaskSource(T task_source_in, Decorator decorator) |
| 311 | : Decorator(std::move(decorator)), |
| 312 | task_source_(std::move(task_source_in)) { |
| 313 | DCHECK_EQ(task_source_.get(), this->task_source()); |
Etienne Pierre-doray | f7f59c3 | 2019-05-24 16:50:53 | [diff] [blame] | 314 | } |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 315 | DecoratorWithTaskSource(DecoratorWithTaskSource&& other) = default; |
| 316 | ~DecoratorWithTaskSource() = default; |
Etienne Pierre-doray | f7f59c3 | 2019-05-24 16:50:53 | [diff] [blame] | 317 | |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 318 | DecoratorWithTaskSource& operator=(DecoratorWithTaskSource&&) = default; |
Etienne Pierre-doray | f7f59c3 | 2019-05-24 16:50:53 | [diff] [blame] | 319 | |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 320 | T take_task_source() { return std::move(task_source_); } |
| 321 | |
| 322 | protected: |
| 323 | T task_source_; |
| 324 | |
| 325 | DISALLOW_COPY_AND_ASSIGN(DecoratorWithTaskSource); |
Etienne Pierre-doray | f7f59c3 | 2019-05-24 16:50:53 | [diff] [blame] | 326 | }; |
| 327 | |
Etienne Pierre-doray | a57964d | 2019-07-08 22:04:55 | [diff] [blame] | 328 | // A RunIntent with an additional RegisteredTaskSource member. |
| 329 | using RunIntentWithRegisteredTaskSource = |
| 330 | DecoratorWithTaskSource<TaskSource::RunIntent, RegisteredTaskSource>; |
| 331 | |
| 332 | template <class T> |
| 333 | struct BASE_EXPORT BasicTransactionWithTaskSource |
| 334 | : public DecoratorWithTaskSource<TaskSource::Transaction, T> { |
| 335 | using DecoratorWithTaskSource<TaskSource::Transaction, |
| 336 | T>::DecoratorWithTaskSource; |
| 337 | |
| 338 | static BasicTransactionWithTaskSource FromTaskSource(T task_source) { |
| 339 | auto transaction = task_source->BeginTransaction(); |
| 340 | return BasicTransactionWithTaskSource(std::move(task_source), |
| 341 | std::move(transaction)); |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | // A Transaction with an additional scoped_refptr<TaskSource> member. Useful to |
| 346 | // carry ownership of a TaskSource with an associated Transaction. |
| 347 | using TransactionWithOwnedTaskSource = |
| 348 | BasicTransactionWithTaskSource<scoped_refptr<TaskSource>>; |
| 349 | |
| 350 | // A Transaction with an additional RegisteredTaskSource member. Useful to carry |
| 351 | // a RegisteredTaskSource with an associated Transaction. |
| 352 | using TransactionWithRegisteredTaskSource = |
| 353 | BasicTransactionWithTaskSource<RegisteredTaskSource>; |
Etienne Pierre-doray | f7f59c3 | 2019-05-24 16:50:53 | [diff] [blame] | 354 | |
Etienne Pierre-doray | b38e0fd | 2019-03-18 19:35:38 | [diff] [blame] | 355 | } // namespace internal |
| 356 | } // namespace base |
| 357 | |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 358 | #endif // BASE_TASK_THREAD_POOL_TASK_SOURCE_H_ |