ipc/chromium/src/base/worker_pool_linux.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial