1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/worker_pool_linux.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +// Copyright (c) 2009 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 +// 1.8 +// The thread pool used in the Linux implementation of WorkerPool dynamically 1.9 +// adds threads as necessary to handle all tasks. It keeps old threads around 1.10 +// for a period of time to allow them to be reused. After this waiting period, 1.11 +// the threads exit. This thread pool uses non-joinable threads, therefore 1.12 +// worker threads are not joined during process shutdown. This means that 1.13 +// potentially long running tasks (such as DNS lookup) do not block process 1.14 +// shutdown, but also means that process shutdown may "leak" objects. Note that 1.15 +// although LinuxDynamicThreadPool spawns the worker threads and manages the 1.16 +// task queue, it does not own the worker threads. The worker threads ask the 1.17 +// LinuxDynamicThreadPool for work and eventually clean themselves up. The 1.18 +// worker threads all maintain scoped_refptrs to the LinuxDynamicThreadPool 1.19 +// instance, which prevents LinuxDynamicThreadPool from disappearing before all 1.20 +// worker threads exit. The owner of LinuxDynamicThreadPool should likewise 1.21 +// maintain a scoped_refptr to the LinuxDynamicThreadPool instance. 1.22 +// 1.23 +// NOTE: The classes defined in this file are only meant for use by the Linux 1.24 +// implementation of WorkerPool. No one else should be using these classes. 1.25 +// These symbols are exported in a header purely for testing purposes. 1.26 + 1.27 +#ifndef BASE_WORKER_POOL_LINUX_H_ 1.28 +#define BASE_WORKER_POOL_LINUX_H_ 1.29 + 1.30 +#include <queue> 1.31 +#include <string> 1.32 + 1.33 +#include "base/basictypes.h" 1.34 +#include "base/condition_variable.h" 1.35 +#include "base/lock.h" 1.36 +#include "base/platform_thread.h" 1.37 +#include "base/ref_counted.h" 1.38 +#include "base/scoped_ptr.h" 1.39 + 1.40 +class Task; 1.41 + 1.42 +namespace base { 1.43 + 1.44 +class LinuxDynamicThreadPool 1.45 + : public RefCountedThreadSafe<LinuxDynamicThreadPool> { 1.46 + public: 1.47 + class LinuxDynamicThreadPoolPeer; 1.48 + 1.49 + // All worker threads will share the same |name_prefix|. They will exit after 1.50 + // |idle_seconds_before_exit|. 1.51 + LinuxDynamicThreadPool(const std::string& name_prefix, 1.52 + int idle_seconds_before_exit); 1.53 + ~LinuxDynamicThreadPool(); 1.54 + 1.55 + // Indicates that the thread pool is going away. Stops handing out tasks to 1.56 + // worker threads. Wakes up all the idle threads to let them exit. 1.57 + void Terminate(); 1.58 + 1.59 + // Adds |task| to the thread pool. LinuxDynamicThreadPool assumes ownership 1.60 + // of |task|. 1.61 + void PostTask(Task* task); 1.62 + 1.63 + // Worker thread method to wait for up to |idle_seconds_before_exit| for more 1.64 + // work from the thread pool. Returns NULL if no work is available. 1.65 + Task* WaitForTask(); 1.66 + 1.67 + private: 1.68 + friend class LinuxDynamicThreadPoolPeer; 1.69 + 1.70 + const std::string name_prefix_; 1.71 + const int idle_seconds_before_exit_; 1.72 + 1.73 + Lock lock_; // Protects all the variables below. 1.74 + 1.75 + // Signal()s worker threads to let them know more tasks are available. 1.76 + // Also used for Broadcast()'ing to worker threads to let them know the pool 1.77 + // is being deleted and they can exit. 1.78 + ConditionVariable tasks_available_cv_; 1.79 + int num_idle_threads_; 1.80 + std::queue<Task*> tasks_; 1.81 + bool terminated_; 1.82 + // Only used for tests to ensure correct thread ordering. It will always be 1.83 + // NULL in non-test code. 1.84 + scoped_ptr<ConditionVariable> num_idle_threads_cv_; 1.85 + 1.86 + DISALLOW_COPY_AND_ASSIGN(LinuxDynamicThreadPool); 1.87 +}; 1.88 + 1.89 +} // namespace base 1.90 + 1.91 +#endif // BASE_WORKER_POOL_LINUX_H_