| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_THREADING_SEQUENCED_WORKER_POOL_H_ | 5 #ifndef BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| 6 #define BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 6 #define BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <cstddef> | 10 #include <cstddef> |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 // | 41 // |
| 42 // - No two tasks with the same token will run at the same time. | 42 // - No two tasks with the same token will run at the same time. |
| 43 // | 43 // |
| 44 // - Given two tasks T1 and T2 with the same token such that T2 will | 44 // - Given two tasks T1 and T2 with the same token such that T2 will |
| 45 // run after T1, then T2 will start after T1 is destroyed. | 45 // run after T1, then T2 will start after T1 is destroyed. |
| 46 // | 46 // |
| 47 // - If T2 will run after T1, then all memory changes in T1 and T1's | 47 // - If T2 will run after T1, then all memory changes in T1 and T1's |
| 48 // destruction will be visible to T2. | 48 // destruction will be visible to T2. |
| 49 // | 49 // |
| 50 // Example: | 50 // Example: |
| 51 // SequencedWorkerPool::SequenceToken token = pool.GetSequenceToken(); | 51 // SequencedWorkerPool::SequenceToken token = |
| 52 // SequencedWorkerPool::GetSequenceToken(); |
| 52 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, | 53 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, |
| 53 // FROM_HERE, base::Bind(...)); | 54 // FROM_HERE, base::Bind(...)); |
| 54 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, | 55 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, |
| 55 // FROM_HERE, base::Bind(...)); | 56 // FROM_HERE, base::Bind(...)); |
| 56 // | 57 // |
| 57 // You can make named sequence tokens to make it easier to share a token | 58 // You can make named sequence tokens to make it easier to share a token |
| 58 // across different components. | 59 // across different components. |
| 59 // | 60 // |
| 60 // You can also post tasks to the pool without ordering using PostWorkerTask. | 61 // You can also post tasks to the pool without ordering using PostWorkerTask. |
| 61 // These will be executed in an unspecified order. The order of execution | 62 // These will be executed in an unspecified order. The order of execution |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 virtual void OnHasWork() = 0; | 158 virtual void OnHasWork() = 0; |
| 158 virtual void WillWaitForShutdown() = 0; | 159 virtual void WillWaitForShutdown() = 0; |
| 159 virtual void OnDestruct() = 0; | 160 virtual void OnDestruct() = 0; |
| 160 }; | 161 }; |
| 161 | 162 |
| 162 // Gets the SequencedToken of the current thread. | 163 // Gets the SequencedToken of the current thread. |
| 163 // If current thread is not a SequencedWorkerPool worker thread or is running | 164 // If current thread is not a SequencedWorkerPool worker thread or is running |
| 164 // an unsequenced task, returns an invalid SequenceToken. | 165 // an unsequenced task, returns an invalid SequenceToken. |
| 165 static SequenceToken GetSequenceTokenForCurrentThread(); | 166 static SequenceToken GetSequenceTokenForCurrentThread(); |
| 166 | 167 |
| 168 // Gets a SequencedTaskRunner for the current thread. If the current thread is |
| 169 // running an unsequenced task, a new SequenceToken will be generated and set, |
| 170 // so that the returned SequencedTaskRunner is guaranteed to run tasks after |
| 171 // the current task has finished running. |
| 172 static scoped_refptr<SequencedTaskRunner> |
| 173 GetSequencedTaskRunnerForCurrentThread(); |
| 174 |
| 175 // Returns a unique token that can be used to sequence tasks posted to |
| 176 // PostSequencedWorkerTask(). Valid tokens are always nonzero. |
| 177 // TODO(bauerb): Rename this to better differentiate from |
| 178 // GetSequenceTokenForCurrentThread(). |
| 179 static SequenceToken GetSequenceToken(); |
| 180 |
| 167 // Returns the SequencedWorkerPool that owns this thread, or null if the | 181 // Returns the SequencedWorkerPool that owns this thread, or null if the |
| 168 // current thread is not a SequencedWorkerPool worker thread. | 182 // current thread is not a SequencedWorkerPool worker thread. |
| 169 static scoped_refptr<SequencedWorkerPool> GetWorkerPoolForCurrentThread(); | 183 static scoped_refptr<SequencedWorkerPool> GetWorkerPoolForCurrentThread(); |
| 170 | 184 |
| 171 // Returns a unique token that can be used to sequence tasks posted to | |
| 172 // PostSequencedWorkerTask(). Valid tokens are always nonzero. | |
| 173 static SequenceToken GetSequenceToken(); | |
| 174 | |
| 175 // When constructing a SequencedWorkerPool, there must be a | 185 // When constructing a SequencedWorkerPool, there must be a |
| 176 // ThreadTaskRunnerHandle on the current thread unless you plan to | 186 // ThreadTaskRunnerHandle on the current thread unless you plan to |
| 177 // deliberately leak it. | 187 // deliberately leak it. |
| 178 | 188 |
| 179 // Pass the maximum number of threads (they will be lazily created as needed) | 189 // Pass the maximum number of threads (they will be lazily created as needed) |
| 180 // and a prefix for the thread name to aid in debugging. |task_priority| will | 190 // and a prefix for the thread name to aid in debugging. |task_priority| will |
| 181 // be used to hint base::TaskScheduler for an experiment in which all | 191 // be used to hint base::TaskScheduler for an experiment in which all |
| 182 // SequencedWorkerPool tasks will be redirected to it in processes where a | 192 // SequencedWorkerPool tasks will be redirected to it in processes where a |
| 183 // base::TaskScheduler was instantiated. | 193 // base::TaskScheduler was instantiated. |
| 184 SequencedWorkerPool(size_t max_threads, | 194 SequencedWorkerPool(size_t max_threads, |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 // TaskRunner implementation. Forwards to PostDelayedWorkerTask(). | 326 // TaskRunner implementation. Forwards to PostDelayedWorkerTask(). |
| 317 bool PostDelayedTask(const tracked_objects::Location& from_here, | 327 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 318 const Closure& task, | 328 const Closure& task, |
| 319 TimeDelta delay) override; | 329 TimeDelta delay) override; |
| 320 bool RunsTasksOnCurrentThread() const override; | 330 bool RunsTasksOnCurrentThread() const override; |
| 321 | 331 |
| 322 // Returns true if the current thread is processing a task with the given | 332 // Returns true if the current thread is processing a task with the given |
| 323 // sequence_token. | 333 // sequence_token. |
| 324 bool IsRunningSequenceOnCurrentThread(SequenceToken sequence_token) const; | 334 bool IsRunningSequenceOnCurrentThread(SequenceToken sequence_token) const; |
| 325 | 335 |
| 336 // Returns true if any thread is currently processing a task with the given |
| 337 // sequence token. Should only be called with a valid sequence token. |
| 338 bool IsRunningSequence(SequenceToken sequence_token) const; |
| 339 |
| 326 // Blocks until all pending tasks are complete. This should only be called in | 340 // Blocks until all pending tasks are complete. This should only be called in |
| 327 // unit tests when you want to validate something that should have happened. | 341 // unit tests when you want to validate something that should have happened. |
| 328 // This will not flush delayed tasks; delayed tasks get deleted. | 342 // This will not flush delayed tasks; delayed tasks get deleted. |
| 329 // | 343 // |
| 330 // Note that calling this will not prevent other threads from posting work to | 344 // Note that calling this will not prevent other threads from posting work to |
| 331 // the queue while the calling thread is waiting on Flush(). In this case, | 345 // the queue while the calling thread is waiting on Flush(). In this case, |
| 332 // Flush will return only when there's no more work in the queue. Normally, | 346 // Flush will return only when there's no more work in the queue. Normally, |
| 333 // this doesn't come up since in a test, all the work is being posted from | 347 // this doesn't come up since in a test, all the work is being posted from |
| 334 // the main thread. | 348 // the main thread. |
| 335 void FlushForTesting(); | 349 void FlushForTesting(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 // Avoid pulling in too many headers by putting (almost) everything | 388 // Avoid pulling in too many headers by putting (almost) everything |
| 375 // into |inner_|. | 389 // into |inner_|. |
| 376 const std::unique_ptr<Inner> inner_; | 390 const std::unique_ptr<Inner> inner_; |
| 377 | 391 |
| 378 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 392 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
| 379 }; | 393 }; |
| 380 | 394 |
| 381 } // namespace base | 395 } // namespace base |
| 382 | 396 |
| 383 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 397 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| OLD | NEW |