| 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 #include "base/threading/sequenced_worker_pool.h" | 5 #include "base/threading/sequenced_worker_pool.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 tracked_objects::Location location; | 38 tracked_objects::Location location; |
| 39 Closure task; | 39 Closure task; |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 // Worker --------------------------------------------------------------------- | 44 // Worker --------------------------------------------------------------------- |
| 45 | 45 |
| 46 class SequencedWorkerPool::Worker : public SimpleThread { | 46 class SequencedWorkerPool::Worker : public SimpleThread { |
| 47 public: | 47 public: |
| 48 // Hold a ref to |worker_pool|, since we want to keep it around even | 48 // Hold a (cyclic) ref to |worker_pool|, since we want to keep it |
| 49 // if it doesn't join our thread. Note that this (deliberately) | 49 // around as long as we are running. |
| 50 // leaks on shutdown. | |
| 51 Worker(const scoped_refptr<SequencedWorkerPool>& worker_pool, | 50 Worker(const scoped_refptr<SequencedWorkerPool>& worker_pool, |
| 52 int thread_number, | 51 int thread_number, |
| 53 const std::string& thread_name_prefix); | 52 const std::string& thread_name_prefix); |
| 54 virtual ~Worker(); | 53 virtual ~Worker(); |
| 55 | 54 |
| 56 // SimpleThread implementation. This actually runs the background thread. | 55 // SimpleThread implementation. This actually runs the background thread. |
| 57 virtual void Run() OVERRIDE; | 56 virtual void Run() OVERRIDE; |
| 58 | 57 |
| 59 private: | 58 private: |
| 60 const scoped_refptr<SequencedWorkerPool> worker_pool_; | 59 scoped_refptr<SequencedWorkerPool> worker_pool_; |
| 61 | 60 |
| 62 DISALLOW_COPY_AND_ASSIGN(Worker); | 61 DISALLOW_COPY_AND_ASSIGN(Worker); |
| 63 }; | 62 }; |
| 64 | 63 |
| 65 // Inner ---------------------------------------------------------------------- | 64 // Inner ---------------------------------------------------------------------- |
| 66 | 65 |
| 67 class SequencedWorkerPool::Inner { | 66 class SequencedWorkerPool::Inner { |
| 68 public: | 67 public: |
| 69 // Take a raw pointer to |worker| to avoid cycles (since we're owned | 68 // Take a raw pointer to |worker| to avoid cycles (since we're owned |
| 70 // by it). | 69 // by it). |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 SequencedWorkerPool::Worker::~Worker() { | 218 SequencedWorkerPool::Worker::~Worker() { |
| 220 } | 219 } |
| 221 | 220 |
| 222 void SequencedWorkerPool::Worker::Run() { | 221 void SequencedWorkerPool::Worker::Run() { |
| 223 // Just jump back to the Inner object to run the thread, since it has all the | 222 // Just jump back to the Inner object to run the thread, since it has all the |
| 224 // tracking information and queues. It might be more natural to implement | 223 // tracking information and queues. It might be more natural to implement |
| 225 // using DelegateSimpleThread and have Inner implement the Delegate to avoid | 224 // using DelegateSimpleThread and have Inner implement the Delegate to avoid |
| 226 // having these worker objects at all, but that method lacks the ability to | 225 // having these worker objects at all, but that method lacks the ability to |
| 227 // send thread-specific information easily to the thread loop. | 226 // send thread-specific information easily to the thread loop. |
| 228 worker_pool_->inner_->ThreadLoop(this); | 227 worker_pool_->inner_->ThreadLoop(this); |
| 228 // Release our cyclic reference once we're done. |
| 229 worker_pool_ = NULL; |
| 229 } | 230 } |
| 230 | 231 |
| 231 // Inner definitions --------------------------------------------------------- | 232 // Inner definitions --------------------------------------------------------- |
| 232 | 233 |
| 233 SequencedWorkerPool::Inner::Inner( | 234 SequencedWorkerPool::Inner::Inner( |
| 234 SequencedWorkerPool* worker_pool, | 235 SequencedWorkerPool* worker_pool, |
| 235 size_t max_threads, | 236 size_t max_threads, |
| 236 const std::string& thread_name_prefix) | 237 const std::string& thread_name_prefix) |
| 237 : worker_pool_(worker_pool), | 238 : worker_pool_(worker_pool), |
| 238 last_sequence_number_(0), | 239 last_sequence_number_(0), |
| (...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 | 696 |
| 696 void SequencedWorkerPool::Shutdown() { | 697 void SequencedWorkerPool::Shutdown() { |
| 697 inner_->Shutdown(); | 698 inner_->Shutdown(); |
| 698 } | 699 } |
| 699 | 700 |
| 700 void SequencedWorkerPool::SetTestingObserver(TestingObserver* observer) { | 701 void SequencedWorkerPool::SetTestingObserver(TestingObserver* observer) { |
| 701 inner_->SetTestingObserver(observer); | 702 inner_->SetTestingObserver(observer); |
| 702 } | 703 } |
| 703 | 704 |
| 704 } // namespace base | 705 } // namespace base |
| OLD | NEW |