1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 // 5 // The thread pool used in the Linux implementation of WorkerPool dynamically 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, 8 // the threads exit. This thread pool uses non-joinable threads, therefore 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 11 // shutdown, but also means that process shutdown may "leak" objects. Note that 12 // although LinuxDynamicThreadPool spawns the worker threads and manages the 13 // task queue, it does not own the worker threads. The worker threads ask the 14 // LinuxDynamicThreadPool for work and eventually clean themselves up. The 15 // worker threads all maintain scoped_refptrs to the LinuxDynamicThreadPool 16 // instance, which prevents LinuxDynamicThreadPool from disappearing before all 17 // worker threads exit. The owner of LinuxDynamicThreadPool should likewise 18 // maintain a scoped_refptr to the LinuxDynamicThreadPool instance. 19 // 20 // NOTE: The classes defined in this file are only meant for use by the Linux 21 // implementation of WorkerPool. No one else should be using these classes. 22 // These symbols are exported in a header purely for testing purposes. 23 24 #ifndef BASE_WORKER_POOL_LINUX_H_ 25 #define BASE_WORKER_POOL_LINUX_H_ 26 27 #include <queue> 28 #include <string> 29 30 #include "base/basictypes.h" 31 #include "base/condition_variable.h" 32 #include "base/lock.h" 33 #include "base/platform_thread.h" 34 #include "base/ref_counted.h" 35 #include "base/scoped_ptr.h" 36 37 class Task; 38 39 namespace base { 40 41 class LinuxDynamicThreadPool 42 : public RefCountedThreadSafe<LinuxDynamicThreadPool> { 43 public: 44 class LinuxDynamicThreadPoolPeer; 45 46 // All worker threads will share the same |name_prefix|. They will exit after 47 // |idle_seconds_before_exit|. 48 LinuxDynamicThreadPool(const std::string& name_prefix, 49 int idle_seconds_before_exit); 50 ~LinuxDynamicThreadPool(); 51 52 // Indicates that the thread pool is going away. Stops handing out tasks to 53 // worker threads. Wakes up all the idle threads to let them exit. 54 void Terminate(); 55 56 // Adds |task| to the thread pool. LinuxDynamicThreadPool assumes ownership 57 // of |task|. 58 void PostTask(Task* task); 59 60 // Worker thread method to wait for up to |idle_seconds_before_exit| for more 61 // work from the thread pool. Returns NULL if no work is available. 62 Task* WaitForTask(); 63 64 private: 65 friend class LinuxDynamicThreadPoolPeer; 66 67 const std::string name_prefix_; 68 const int idle_seconds_before_exit_; 69 70 Lock lock_; // Protects all the variables below. 71 72 // Signal()s worker threads to let them know more tasks are available. 73 // Also used for Broadcast()'ing to worker threads to let them know the pool 74 // is being deleted and they can exit. 75 ConditionVariable tasks_available_cv_; 76 int num_idle_threads_; 77 std::queue<Task*> tasks_; 78 bool terminated_; 79 // Only used for tests to ensure correct thread ordering. It will always be 80 // NULL in non-test code. 81 scoped_ptr<ConditionVariable> num_idle_threads_cv_; 82 83 DISALLOW_COPY_AND_ASSIGN(LinuxDynamicThreadPool); 84 }; 85 86 } // namespace base 87 88 #endif // BASE_WORKER_POOL_LINUX_H_ 89