| 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 // The thread pool used in the POSIX implementation of WorkerPool dynamically | 5 // The thread pool used in the POSIX implementation of WorkerPool dynamically |
| 6 // adds threads as necessary to handle all tasks. It keeps old threads around | 6 // adds threads as necessary to handle all tasks. It keeps old threads around |
| 7 // for a period of time to allow them to be reused. After this waiting period, | 7 // for a period of time to allow them to be reused. After this waiting period, |
| 8 // the threads exit. This thread pool uses non-joinable threads, therefore | 8 // the threads exit. This thread pool uses non-joinable threads, therefore |
| 9 // worker threads are not joined during process shutdown. This means that | 9 // worker threads are not joined during process shutdown. This means that |
| 10 // potentially long running tasks (such as DNS lookup) do not block process | 10 // potentially long running tasks (such as DNS lookup) do not block process |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 class BASE_EXPORT PosixDynamicThreadPool | 47 class BASE_EXPORT PosixDynamicThreadPool |
| 48 : public RefCountedThreadSafe<PosixDynamicThreadPool> { | 48 : public RefCountedThreadSafe<PosixDynamicThreadPool> { |
| 49 public: | 49 public: |
| 50 class PosixDynamicThreadPoolPeer; | 50 class PosixDynamicThreadPoolPeer; |
| 51 | 51 |
| 52 // All worker threads will share the same |name_prefix|. They will exit after | 52 // All worker threads will share the same |name_prefix|. They will exit after |
| 53 // |idle_seconds_before_exit|. | 53 // |idle_seconds_before_exit|. |
| 54 PosixDynamicThreadPool(const std::string& name_prefix, | 54 PosixDynamicThreadPool(const std::string& name_prefix, |
| 55 int idle_seconds_before_exit); | 55 int idle_seconds_before_exit); |
| 56 ~PosixDynamicThreadPool(); | |
| 57 | 56 |
| 58 // Indicates that the thread pool is going away. Stops handing out tasks to | 57 // Indicates that the thread pool is going away. Stops handing out tasks to |
| 59 // worker threads. Wakes up all the idle threads to let them exit. | 58 // worker threads. Wakes up all the idle threads to let them exit. |
| 60 void Terminate(); | 59 void Terminate(); |
| 61 | 60 |
| 62 // Adds |task| to the thread pool. | 61 // Adds |task| to the thread pool. |
| 63 void PostTask(const tracked_objects::Location& from_here, | 62 void PostTask(const tracked_objects::Location& from_here, |
| 64 const Closure& task); | 63 const Closure& task); |
| 65 | 64 |
| 66 // Worker thread method to wait for up to |idle_seconds_before_exit| for more | 65 // Worker thread method to wait for up to |idle_seconds_before_exit| for more |
| 67 // work from the thread pool. Returns NULL if no work is available. | 66 // work from the thread pool. Returns NULL if no work is available. |
| 68 PendingTask WaitForTask(); | 67 PendingTask WaitForTask(); |
| 69 | 68 |
| 70 private: | 69 private: |
| 70 friend class RefCountedThreadSafe<PosixDynamicThreadPool>; |
| 71 friend class PosixDynamicThreadPoolPeer; | 71 friend class PosixDynamicThreadPoolPeer; |
| 72 | 72 |
| 73 ~PosixDynamicThreadPool(); |
| 74 |
| 73 // Adds pending_task to the thread pool. This function will clear | 75 // Adds pending_task to the thread pool. This function will clear |
| 74 // |pending_task->task|. | 76 // |pending_task->task|. |
| 75 void AddTask(PendingTask* pending_task); | 77 void AddTask(PendingTask* pending_task); |
| 76 | 78 |
| 77 const std::string name_prefix_; | 79 const std::string name_prefix_; |
| 78 const int idle_seconds_before_exit_; | 80 const int idle_seconds_before_exit_; |
| 79 | 81 |
| 80 Lock lock_; // Protects all the variables below. | 82 Lock lock_; // Protects all the variables below. |
| 81 | 83 |
| 82 // Signal()s worker threads to let them know more tasks are available. | 84 // Signal()s worker threads to let them know more tasks are available. |
| 83 // Also used for Broadcast()'ing to worker threads to let them know the pool | 85 // Also used for Broadcast()'ing to worker threads to let them know the pool |
| 84 // is being deleted and they can exit. | 86 // is being deleted and they can exit. |
| 85 ConditionVariable pending_tasks_available_cv_; | 87 ConditionVariable pending_tasks_available_cv_; |
| 86 int num_idle_threads_; | 88 int num_idle_threads_; |
| 87 TaskQueue pending_tasks_; | 89 TaskQueue pending_tasks_; |
| 88 bool terminated_; | 90 bool terminated_; |
| 89 // Only used for tests to ensure correct thread ordering. It will always be | 91 // Only used for tests to ensure correct thread ordering. It will always be |
| 90 // NULL in non-test code. | 92 // NULL in non-test code. |
| 91 scoped_ptr<ConditionVariable> num_idle_threads_cv_; | 93 scoped_ptr<ConditionVariable> num_idle_threads_cv_; |
| 92 | 94 |
| 93 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool); | 95 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool); |
| 94 }; | 96 }; |
| 95 | 97 |
| 96 } // namespace base | 98 } // namespace base |
| 97 | 99 |
| 98 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_ | 100 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_ |
| OLD | NEW |