| 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 10 matching lines...) Expand all Loading... |
| 21 // implementation of WorkerPool. No one else should be using these classes. | 21 // implementation of WorkerPool. No one else should be using these classes. |
| 22 // These symbols are exported in a header purely for testing purposes. | 22 // These symbols are exported in a header purely for testing purposes. |
| 23 | 23 |
| 24 #ifndef BASE_THREADING_WORKER_POOL_POSIX_H_ | 24 #ifndef BASE_THREADING_WORKER_POOL_POSIX_H_ |
| 25 #define BASE_THREADING_WORKER_POOL_POSIX_H_ | 25 #define BASE_THREADING_WORKER_POOL_POSIX_H_ |
| 26 | 26 |
| 27 #include <memory> | 27 #include <memory> |
| 28 #include <queue> | 28 #include <queue> |
| 29 #include <string> | 29 #include <string> |
| 30 | 30 |
| 31 #include "base/callback_forward.h" | 31 #include "base/callback.h" |
| 32 #include "base/location.h" | 32 #include "base/location.h" |
| 33 #include "base/macros.h" | 33 #include "base/macros.h" |
| 34 #include "base/memory/ref_counted.h" | 34 #include "base/memory/ref_counted.h" |
| 35 #include "base/pending_task.h" | 35 #include "base/pending_task.h" |
| 36 #include "base/synchronization/condition_variable.h" | 36 #include "base/synchronization/condition_variable.h" |
| 37 #include "base/synchronization/lock.h" | 37 #include "base/synchronization/lock.h" |
| 38 #include "base/threading/platform_thread.h" | 38 #include "base/threading/platform_thread.h" |
| 39 #include "base/tracked_objects.h" | 39 #include "base/tracked_objects.h" |
| 40 | 40 |
| 41 namespace base { | 41 namespace base { |
| 42 | 42 |
| 43 class BASE_EXPORT PosixDynamicThreadPool | 43 class BASE_EXPORT PosixDynamicThreadPool |
| 44 : public RefCountedThreadSafe<PosixDynamicThreadPool> { | 44 : public RefCountedThreadSafe<PosixDynamicThreadPool> { |
| 45 public: | 45 public: |
| 46 class PosixDynamicThreadPoolPeer; | 46 class PosixDynamicThreadPoolPeer; |
| 47 | 47 |
| 48 // All worker threads will share the same |name_prefix|. They will exit after | 48 // All worker threads will share the same |name_prefix|. They will exit after |
| 49 // |idle_seconds_before_exit|. | 49 // |idle_seconds_before_exit|. |
| 50 PosixDynamicThreadPool(const std::string& name_prefix, | 50 PosixDynamicThreadPool(const std::string& name_prefix, |
| 51 int idle_seconds_before_exit); | 51 int idle_seconds_before_exit); |
| 52 | 52 |
| 53 // Adds |task| to the thread pool. | 53 // Adds |task| to the thread pool. |
| 54 void PostTask(const tracked_objects::Location& from_here, | 54 void PostTask(const tracked_objects::Location& from_here, Closure task); |
| 55 const Closure& task); | |
| 56 | 55 |
| 57 // Worker thread method to wait for up to |idle_seconds_before_exit| for more | 56 // Worker thread method to wait for up to |idle_seconds_before_exit| for more |
| 58 // work from the thread pool. Returns NULL if no work is available. | 57 // work from the thread pool. Returns NULL if no work is available. |
| 59 PendingTask WaitForTask(); | 58 PendingTask WaitForTask(); |
| 60 | 59 |
| 61 private: | 60 private: |
| 62 friend class RefCountedThreadSafe<PosixDynamicThreadPool>; | 61 friend class RefCountedThreadSafe<PosixDynamicThreadPool>; |
| 63 friend class PosixDynamicThreadPoolPeer; | 62 friend class PosixDynamicThreadPoolPeer; |
| 64 | 63 |
| 65 ~PosixDynamicThreadPool(); | 64 ~PosixDynamicThreadPool(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 82 // Only used for tests to ensure correct thread ordering. It will always be | 81 // Only used for tests to ensure correct thread ordering. It will always be |
| 83 // NULL in non-test code. | 82 // NULL in non-test code. |
| 84 std::unique_ptr<ConditionVariable> num_idle_threads_cv_; | 83 std::unique_ptr<ConditionVariable> num_idle_threads_cv_; |
| 85 | 84 |
| 86 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool); | 85 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool); |
| 87 }; | 86 }; |
| 88 | 87 |
| 89 } // namespace base | 88 } // namespace base |
| 90 | 89 |
| 91 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_ | 90 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_ |
| OLD | NEW |