| 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 <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/task_runner.h" |
| 17 | 18 |
| 18 namespace tracked_objects { | 19 namespace tracked_objects { |
| 19 class Location; | 20 class Location; |
| 20 } // namespace tracked_objects | 21 } // namespace tracked_objects |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 | 24 |
| 24 // A worker thread pool that enforces ordering between sets of tasks. It also | 25 // A worker thread pool that enforces ordering between sets of tasks. It also |
| 25 // allows you to specify what should happen to your tasks on shutdown. | 26 // allows you to specify what should happen to your tasks on shutdown. |
| 26 // | 27 // |
| (...skipping 17 matching lines...) Expand all Loading... |
| 44 // | 45 // |
| 45 // This class is designed to be leaked on shutdown to allow the | 46 // This class is designed to be leaked on shutdown to allow the |
| 46 // CONTINUE_ON_SHUTDOWN behavior to be implemented. To enforce the | 47 // CONTINUE_ON_SHUTDOWN behavior to be implemented. To enforce the |
| 47 // BLOCK_SHUTDOWN behavior, you must call Shutdown() which will wait until | 48 // BLOCK_SHUTDOWN behavior, you must call Shutdown() which will wait until |
| 48 // the necessary tasks have completed. | 49 // the necessary tasks have completed. |
| 49 // | 50 // |
| 50 // Implementation note: This does not use a base::WorkerPool since that does | 51 // Implementation note: This does not use a base::WorkerPool since that does |
| 51 // not enforce shutdown semantics or allow us to specify how many worker | 52 // not enforce shutdown semantics or allow us to specify how many worker |
| 52 // threads to run. For the typical use case of random background work, we don't | 53 // threads to run. For the typical use case of random background work, we don't |
| 53 // necessarily want to be super aggressive about creating threads. | 54 // necessarily want to be super aggressive about creating threads. |
| 54 class BASE_EXPORT SequencedWorkerPool | 55 // |
| 55 : public RefCountedThreadSafe<SequencedWorkerPool> { | 56 // Note that SequencedWorkerPool is RefCountedThreadSafe (inherited |
| 57 // from TaskRunner). |
| 58 class BASE_EXPORT SequencedWorkerPool : public TaskRunner { |
| 56 public: | 59 public: |
| 57 // Defines what should happen to a task posted to the worker pool on shutdown. | 60 // Defines what should happen to a task posted to the worker pool on shutdown. |
| 58 enum WorkerShutdown { | 61 enum WorkerShutdown { |
| 59 // Tasks posted with this mode which have not run at shutdown will be | 62 // Tasks posted with this mode which have not run at shutdown will be |
| 60 // deleted rather than run, and any tasks with this mode running at | 63 // deleted rather than run, and any tasks with this mode running at |
| 61 // shutdown will be ignored (the worker thread will not be joined). | 64 // shutdown will be ignored (the worker thread will not be joined). |
| 62 // | 65 // |
| 63 // This option provides a nice way to post stuff you don't want blocking | 66 // This option provides a nice way to post stuff you don't want blocking |
| 64 // shutdown. For example, you might be doing a slow DNS lookup and if it's | 67 // shutdown. For example, you might be doing a slow DNS lookup and if it's |
| 65 // blocked on the OS, you may not want to stop shutdown, since the result | 68 // 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... |
| 184 const Closure& task); | 187 const Closure& task); |
| 185 | 188 |
| 186 // Same as PostSequencedWorkerTask but allows specification of the shutdown | 189 // Same as PostSequencedWorkerTask but allows specification of the shutdown |
| 187 // behavior. | 190 // behavior. |
| 188 bool PostSequencedWorkerTaskWithShutdownBehavior( | 191 bool PostSequencedWorkerTaskWithShutdownBehavior( |
| 189 SequenceToken sequence_token, | 192 SequenceToken sequence_token, |
| 190 const tracked_objects::Location& from_here, | 193 const tracked_objects::Location& from_here, |
| 191 const Closure& task, | 194 const Closure& task, |
| 192 WorkerShutdown shutdown_behavior); | 195 WorkerShutdown shutdown_behavior); |
| 193 | 196 |
| 197 // TaskRunner implementation. Forwards to PostWorkerTask(). |
| 198 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 199 const Closure& task, |
| 200 int64 delay_ms) OVERRIDE; |
| 201 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 202 const Closure& task, |
| 203 TimeDelta delay) OVERRIDE; |
| 204 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
| 205 |
| 194 // Blocks until all pending tasks are complete. This should only be called in | 206 // Blocks until all pending tasks are complete. This should only be called in |
| 195 // unit tests when you want to validate something that should have happened. | 207 // unit tests when you want to validate something that should have happened. |
| 196 // | 208 // |
| 197 // Note that calling this will not prevent other threads from posting work to | 209 // Note that calling this will not prevent other threads from posting work to |
| 198 // the queue while the calling thread is waiting on Flush(). In this case, | 210 // the queue while the calling thread is waiting on Flush(). In this case, |
| 199 // Flush will return only when there's no more work in the queue. Normally, | 211 // Flush will return only when there's no more work in the queue. Normally, |
| 200 // this doesn't come up sine in a test, all the work is being posted from | 212 // this doesn't come up sine in a test, all the work is being posted from |
| 201 // the main thread. | 213 // the main thread. |
| 202 void FlushForTesting(); | 214 void FlushForTesting(); |
| 203 | 215 |
| 204 // Implements the worker pool shutdown. This should be called during app | 216 // Implements the worker pool shutdown. This should be called during app |
| 205 // shutdown, and will discard/join with appropriate tasks before returning. | 217 // shutdown, and will discard/join with appropriate tasks before returning. |
| 206 // After this call, subsequent calls to post tasks will fail. | 218 // After this call, subsequent calls to post tasks will fail. |
| 207 void Shutdown(); | 219 void Shutdown(); |
| 208 | 220 |
| 209 // Called by tests to set the testing observer. This is NULL by default | 221 // Called by tests to set the testing observer. This is NULL by default |
| 210 // and ownership of the pointer is kept with the caller. | 222 // and ownership of the pointer is kept with the caller. |
| 211 void SetTestingObserver(TestingObserver* observer); | 223 void SetTestingObserver(TestingObserver* observer); |
| 212 | 224 |
| 225 protected: |
| 226 virtual ~SequencedWorkerPool(); |
| 227 |
| 213 private: | 228 private: |
| 214 friend class RefCountedThreadSafe<SequencedWorkerPool>; | 229 friend class RefCountedThreadSafe<SequencedWorkerPool>; |
| 215 | 230 |
| 216 class Inner; | 231 class Inner; |
| 217 class Worker; | 232 class Worker; |
| 218 | 233 |
| 219 ~SequencedWorkerPool(); | |
| 220 | |
| 221 // Avoid pulling in too many headers by putting everything into | 234 // Avoid pulling in too many headers by putting everything into |
| 222 // |inner_|. | 235 // |inner_|. |
| 223 const scoped_ptr<Inner> inner_; | 236 const scoped_ptr<Inner> inner_; |
| 224 | 237 |
| 225 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 238 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
| 226 }; | 239 }; |
| 227 | 240 |
| 228 } // namespace base | 241 } // namespace base |
| 229 | 242 |
| 230 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 243 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| OLD | NEW |