michael@0: // Copyright (c) 2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: // The thread pool used in the Linux implementation of WorkerPool dynamically michael@0: // adds threads as necessary to handle all tasks. It keeps old threads around michael@0: // for a period of time to allow them to be reused. After this waiting period, michael@0: // the threads exit. This thread pool uses non-joinable threads, therefore michael@0: // worker threads are not joined during process shutdown. This means that michael@0: // potentially long running tasks (such as DNS lookup) do not block process michael@0: // shutdown, but also means that process shutdown may "leak" objects. Note that michael@0: // although LinuxDynamicThreadPool spawns the worker threads and manages the michael@0: // task queue, it does not own the worker threads. The worker threads ask the michael@0: // LinuxDynamicThreadPool for work and eventually clean themselves up. The michael@0: // worker threads all maintain scoped_refptrs to the LinuxDynamicThreadPool michael@0: // instance, which prevents LinuxDynamicThreadPool from disappearing before all michael@0: // worker threads exit. The owner of LinuxDynamicThreadPool should likewise michael@0: // maintain a scoped_refptr to the LinuxDynamicThreadPool instance. michael@0: // michael@0: // NOTE: The classes defined in this file are only meant for use by the Linux michael@0: // implementation of WorkerPool. No one else should be using these classes. michael@0: // These symbols are exported in a header purely for testing purposes. michael@0: michael@0: #ifndef BASE_WORKER_POOL_LINUX_H_ michael@0: #define BASE_WORKER_POOL_LINUX_H_ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/condition_variable.h" michael@0: #include "base/lock.h" michael@0: #include "base/platform_thread.h" michael@0: #include "base/ref_counted.h" michael@0: #include "base/scoped_ptr.h" michael@0: michael@0: class Task; michael@0: michael@0: namespace base { michael@0: michael@0: class LinuxDynamicThreadPool michael@0: : public RefCountedThreadSafe { michael@0: public: michael@0: class LinuxDynamicThreadPoolPeer; michael@0: michael@0: // All worker threads will share the same |name_prefix|. They will exit after michael@0: // |idle_seconds_before_exit|. michael@0: LinuxDynamicThreadPool(const std::string& name_prefix, michael@0: int idle_seconds_before_exit); michael@0: ~LinuxDynamicThreadPool(); michael@0: michael@0: // Indicates that the thread pool is going away. Stops handing out tasks to michael@0: // worker threads. Wakes up all the idle threads to let them exit. michael@0: void Terminate(); michael@0: michael@0: // Adds |task| to the thread pool. LinuxDynamicThreadPool assumes ownership michael@0: // of |task|. michael@0: void PostTask(Task* task); michael@0: michael@0: // Worker thread method to wait for up to |idle_seconds_before_exit| for more michael@0: // work from the thread pool. Returns NULL if no work is available. michael@0: Task* WaitForTask(); michael@0: michael@0: private: michael@0: friend class LinuxDynamicThreadPoolPeer; michael@0: michael@0: const std::string name_prefix_; michael@0: const int idle_seconds_before_exit_; michael@0: michael@0: Lock lock_; // Protects all the variables below. michael@0: michael@0: // Signal()s worker threads to let them know more tasks are available. michael@0: // Also used for Broadcast()'ing to worker threads to let them know the pool michael@0: // is being deleted and they can exit. michael@0: ConditionVariable tasks_available_cv_; michael@0: int num_idle_threads_; michael@0: std::queue tasks_; michael@0: bool terminated_; michael@0: // Only used for tests to ensure correct thread ordering. It will always be michael@0: // NULL in non-test code. michael@0: scoped_ptr num_idle_threads_cv_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(LinuxDynamicThreadPool); michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_WORKER_POOL_LINUX_H_