| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <cstddef> | 9 #include <cstddef> |
| 10 #include <list> | 10 #include <list> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <set> | 12 #include <set> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/atomicops.h" | 16 #include "base/atomicops.h" |
| 17 #include "base/base_export.h" | 17 #include "base/base_export.h" |
| 18 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
| 19 #include "base/callback.h" | 19 #include "base/callback.h" |
| 20 #include "base/compiler_specific.h" |
| 20 #include "base/memory/linked_ptr.h" | 21 #include "base/memory/linked_ptr.h" |
| 21 #include "base/memory/ref_counted.h" | |
| 22 #include "base/synchronization/condition_variable.h" | 22 #include "base/synchronization/condition_variable.h" |
| 23 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
| 24 #include "base/task_runner.h" |
| 24 #include "base/tracked_objects.h" | 25 #include "base/tracked_objects.h" |
| 25 | 26 |
| 26 namespace base { | 27 namespace base { |
| 27 | 28 |
| 28 // A worker thread pool that enforces ordering between sets of tasks. It also | 29 // A worker thread pool that enforces ordering between sets of tasks. It also |
| 29 // allows you to specify what should happen to your tasks on shutdown. | 30 // allows you to specify what should happen to your tasks on shutdown. |
| 30 // | 31 // |
| 31 // To enforce ordering, get a unique sequence token from the pool and post all | 32 // To enforce ordering, get a unique sequence token from the pool and post all |
| 32 // tasks you want to order with the token. All tasks with the same token are | 33 // tasks you want to order with the token. All tasks with the same token are |
| 33 // guaranteed to execute serially, though not necessarily on the same thread. | 34 // guaranteed to execute serially, though not necessarily on the same thread. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 48 // | 49 // |
| 49 // This class is designed to be leaked on shutdown to allow the | 50 // This class is designed to be leaked on shutdown to allow the |
| 50 // CONTINUE_ON_SHUTDOWN behavior to be implemented. To enforce the | 51 // CONTINUE_ON_SHUTDOWN behavior to be implemented. To enforce the |
| 51 // BLOCK_SHUTDOWN behavior, you must call Shutdown() which will wait until | 52 // BLOCK_SHUTDOWN behavior, you must call Shutdown() which will wait until |
| 52 // the necessary tasks have completed. | 53 // the necessary tasks have completed. |
| 53 // | 54 // |
| 54 // Implementation note: This does not use a base::WorkerPool since that does | 55 // Implementation note: This does not use a base::WorkerPool since that does |
| 55 // not enforce shutdown semantics or allow us to specify how many worker | 56 // not enforce shutdown semantics or allow us to specify how many worker |
| 56 // threads to run. For the typical use case of random background work, we don't | 57 // threads to run. For the typical use case of random background work, we don't |
| 57 // necessarily want to be super aggressive about creating threads. | 58 // necessarily want to be super aggressive about creating threads. |
| 58 class BASE_EXPORT SequencedWorkerPool | 59 // |
| 59 : public RefCountedThreadSafe<SequencedWorkerPool> { | 60 // Note that SequencedWorkerPool is RefCountedThreadSafe (inherited |
| 61 // from TaskRunner). |
| 62 class BASE_EXPORT SequencedWorkerPool : public TaskRunner { |
| 60 public: | 63 public: |
| 61 // Defines what should happen to a task posted to the worker pool on shutdown. | 64 // Defines what should happen to a task posted to the worker pool on shutdown. |
| 62 enum WorkerShutdown { | 65 enum WorkerShutdown { |
| 63 // Tasks posted with this mode which have not run at shutdown will be | 66 // Tasks posted with this mode which have not run at shutdown will be |
| 64 // deleted rather than run, and any tasks with this mode running at | 67 // deleted rather than run, and any tasks with this mode running at |
| 65 // shutdown will be ignored (the worker thread will not be joined). | 68 // shutdown will be ignored (the worker thread will not be joined). |
| 66 // | 69 // |
| 67 // This option provides a nice way to post stuff you don't want blocking | 70 // This option provides a nice way to post stuff you don't want blocking |
| 68 // shutdown. For example, you might be doing a slow DNS lookup and if it's | 71 // shutdown. For example, you might be doing a slow DNS lookup and if it's |
| 69 // blocked on the OS, you may not want to stop shutdown, since the result | 72 // blocked on the OS, you may not want to stop shutdown, since the result |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 const Closure& task); | 191 const Closure& task); |
| 189 | 192 |
| 190 // Same as PostSequencedWorkerTask but allows specification of the shutdown | 193 // Same as PostSequencedWorkerTask but allows specification of the shutdown |
| 191 // behavior. | 194 // behavior. |
| 192 bool PostSequencedWorkerTaskWithShutdownBehavior( | 195 bool PostSequencedWorkerTaskWithShutdownBehavior( |
| 193 SequenceToken sequence_token, | 196 SequenceToken sequence_token, |
| 194 const tracked_objects::Location& from_here, | 197 const tracked_objects::Location& from_here, |
| 195 const Closure& task, | 198 const Closure& task, |
| 196 WorkerShutdown shutdown_behavior); | 199 WorkerShutdown shutdown_behavior); |
| 197 | 200 |
| 201 // TaskRunner implementation. Forwards to PostWorkerTask(). |
| 202 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 203 const Closure& task, |
| 204 int64 delay_ms) OVERRIDE; |
| 205 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
| 206 |
| 198 // Blocks until all pending tasks are complete. This should only be called in | 207 // Blocks until all pending tasks are complete. This should only be called in |
| 199 // unit tests when you want to validate something that should have happened. | 208 // unit tests when you want to validate something that should have happened. |
| 200 // | 209 // |
| 201 // Note that calling this will not prevent other threads from posting work to | 210 // Note that calling this will not prevent other threads from posting work to |
| 202 // the queue while the calling thread is waiting on Flush(). In this case, | 211 // the queue while the calling thread is waiting on Flush(). In this case, |
| 203 // Flush will return only when there's no more work in the queue. Normally, | 212 // Flush will return only when there's no more work in the queue. Normally, |
| 204 // this doesn't come up sine in a test, all the work is being posted from | 213 // this doesn't come up sine in a test, all the work is being posted from |
| 205 // the main thread. | 214 // the main thread. |
| 206 void FlushForTesting(); | 215 void FlushForTesting(); |
| 207 | 216 |
| 208 // Implements the worker pool shutdown. This should be called during app | 217 // Implements the worker pool shutdown. This should be called during app |
| 209 // shutdown, and will discard/join with appropriate tasks before returning. | 218 // shutdown, and will discard/join with appropriate tasks before returning. |
| 210 // After this call, subsequent calls to post tasks will fail. | 219 // After this call, subsequent calls to post tasks will fail. |
| 211 void Shutdown(); | 220 void Shutdown(); |
| 212 | 221 |
| 213 // Called by tests to set the testing observer. This is NULL by default | 222 // Called by tests to set the testing observer. This is NULL by default |
| 214 // and ownership of the pointer is kept with the caller. | 223 // and ownership of the pointer is kept with the caller. |
| 215 void SetTestingObserver(TestingObserver* observer); | 224 void SetTestingObserver(TestingObserver* observer); |
| 216 | 225 |
| 226 protected: |
| 227 virtual ~SequencedWorkerPool(); |
| 228 |
| 217 private: | 229 private: |
| 218 friend class RefCountedThreadSafe<SequencedWorkerPool>; | |
| 219 class Worker; | 230 class Worker; |
| 220 | 231 |
| 221 struct SequencedTask { | 232 struct SequencedTask { |
| 222 SequencedTask(); | 233 SequencedTask(); |
| 223 ~SequencedTask(); | 234 ~SequencedTask(); |
| 224 | 235 |
| 225 int sequence_token_id; | 236 int sequence_token_id; |
| 226 WorkerShutdown shutdown_behavior; | 237 WorkerShutdown shutdown_behavior; |
| 227 tracked_objects::Location location; | 238 tracked_objects::Location location; |
| 228 Closure task; | 239 Closure task; |
| 229 }; | 240 }; |
| 230 | 241 |
| 231 ~SequencedWorkerPool(); | |
| 232 | |
| 233 // This function accepts a name and an ID. If the name is null, the | 242 // This function accepts a name and an ID. If the name is null, the |
| 234 // token ID is used. This allows us to implement the optional name lookup | 243 // token ID is used. This allows us to implement the optional name lookup |
| 235 // from a single function without having to enter the lock a separate time. | 244 // from a single function without having to enter the lock a separate time. |
| 236 bool PostTaskHelper(const std::string* optional_token_name, | 245 bool PostTaskHelper(const std::string* optional_token_name, |
| 237 SequenceToken sequence_token, | 246 SequenceToken sequence_token, |
| 238 WorkerShutdown shutdown_behavior, | 247 WorkerShutdown shutdown_behavior, |
| 239 const tracked_objects::Location& from_here, | 248 const tracked_objects::Location& from_here, |
| 240 const Closure& task); | 249 const Closure& task); |
| 241 | 250 |
| 242 // Runs the worker loop on the background thread. | 251 // Runs the worker loop on the background thread. |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 bool shutdown_called_; | 353 bool shutdown_called_; |
| 345 | 354 |
| 346 TestingObserver* testing_observer_; | 355 TestingObserver* testing_observer_; |
| 347 | 356 |
| 348 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 357 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
| 349 }; | 358 }; |
| 350 | 359 |
| 351 } // namespace base | 360 } // namespace base |
| 352 | 361 |
| 353 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 362 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| OLD | NEW |