From 250103f6249e1f19ab14ff69f4356fee56833af8 Mon Sep 17 00:00:00 2001 From: "willchan@chromium.org" Date: Tue, 24 Mar 2009 23:41:53 +0000 Subject: Added a thread pool to WorkerPool for Linux that dynamically adds threads as needed. Review URL: http://codereview.chromium.org/39102 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@12414 0039d316-1c4b-4281-b951-d872f2087c98 --- base/worker_pool_linux.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 base/worker_pool_linux.h (limited to 'base/worker_pool_linux.h') diff --git a/base/worker_pool_linux.h b/base/worker_pool_linux.h new file mode 100644 index 0000000..a9cd894 --- /dev/null +++ b/base/worker_pool_linux.h @@ -0,0 +1,88 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// The thread pool used in the Linux implementation of WorkerPool dynamically +// adds threads as necessary to handle all tasks. It keeps old threads around +// for a period of time to allow them to be reused. After this waiting period, +// the threads exit. This thread pool uses non-joinable threads, therefore +// worker threads are not joined during process shutdown. This means that +// potentially long running tasks (such as DNS lookup) do not block process +// shutdown, but also means that process shutdown may "leak" objects. Note that +// although LinuxDynamicThreadPool spawns the worker threads and manages the +// task queue, it does not own the worker threads. The worker threads ask the +// LinuxDynamicThreadPool for work and eventually clean themselves up. The +// worker threads all maintain scoped_refptrs to the LinuxDynamicThreadPool +// instance, which prevents LinuxDynamicThreadPool from disappearing before all +// worker threads exit. The owner of LinuxDynamicThreadPool should likewise +// maintain a scoped_refptr to the LinuxDynamicThreadPool instance. +// +// NOTE: The classes defined in this file are only meant for use by the Linux +// implementation of WorkerPool. No one else should be using these classes. +// These symbols are exported in a header purely for testing purposes. + +#ifndef BASE_WORKER_POOL_LINUX_H_ +#define BASE_WORKER_POOL_LINUX_H_ + +#include +#include + +#include "base/basictypes.h" +#include "base/condition_variable.h" +#include "base/lock.h" +#include "base/platform_thread.h" +#include "base/ref_counted.h" +#include "base/scoped_ptr.h" + +class Task; + +namespace base { + +class LinuxDynamicThreadPool + : public RefCountedThreadSafe { + public: + class LinuxDynamicThreadPoolPeer; + + // All worker threads will share the same |name_prefix|. They will exit after + // |idle_seconds_before_exit|. + LinuxDynamicThreadPool(const std::string& name_prefix, + int idle_seconds_before_exit); + ~LinuxDynamicThreadPool(); + + // Indicates that the thread pool is going away. Stops handing out tasks to + // worker threads. Wakes up all the idle threads to let them exit. + void Terminate(); + + // Adds |task| to the thread pool. LinuxDynamicThreadPool assumes ownership + // of |task|. + void PostTask(Task* task); + + // Worker thread method to wait for up to |idle_seconds_before_exit| for more + // work from the thread pool. Returns NULL if no work is available. + Task* WaitForTask(); + + private: + friend class LinuxDynamicThreadPoolPeer; + + const std::string name_prefix_; + const int idle_seconds_before_exit_; + + Lock lock_; // Protects all the variables below. + + // Signal()s worker threads to let them know more tasks are available. + // Also used for Broadcast()'ing to worker threads to let them know the pool + // is being deleted and they can exit. + ConditionVariable tasks_available_cv_; + int num_idle_threads_; + std::queue tasks_; + bool terminated_; + // Only used for tests to ensure correct thread ordering. It will always be + // NULL in non-test code. + scoped_ptr num_idle_threads_cv_; + + DISALLOW_COPY_AND_ASSIGN(LinuxDynamicThreadPool); +}; + +} // namespace base + +#endif // BASE_WORKER_POOL_LINUX_H_ -- cgit v1.1