1 // Copyright (c) 2012 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.
5 // The thread pool used in the POSIX 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. Unless blocking termination is requested, worker threads
9 // are not joined during process shutdown. This means that potentially long
10 // running tasks (such as DNS lookup) do not block process shutdown, but also
11 // means that process shutdown may "leak" objects. Note that although
12 // PosixDynamicThreadPool spawns the worker threads and manages the task queue,
13 // it does not own the worker threads. The worker threads ask the
14 // PosixDynamicThreadPool for work and eventually clean themselves up. The
15 // worker threads all maintain scoped_refptrs to the PosixDynamicThreadPool
16 // instance, which prevents PosixDynamicThreadPool from disappearing before all
17 // worker threads exit. The owner of PosixDynamicThreadPool should likewise
18 // maintain a scoped_refptr to the PosixDynamicThreadPool instance.
20 // NOTE: The classes defined in this file are only meant for use by the POSIX
21 // implementation of WorkerPool. No one else should be using these classes.
22 // These symbols are exported in a header purely for testing purposes.
24 #ifndef BASE_THREADING_WORKER_POOL_POSIX_H_
25 #define BASE_THREADING_WORKER_POOL_POSIX_H_
31 #include "base/basictypes.h"
32 #include "base/callback_forward.h"
33 #include "base/location.h"
34 #include "base/memory/ref_counted.h"
35 #include "base/memory/scoped_ptr.h"
36 #include "base/pending_task.h"
37 #include "base/synchronization/condition_variable.h"
38 #include "base/synchronization/lock.h"
39 #include "base/threading/platform_thread.h"
40 #include "base/time/time.h"
41 #include "base/tracked_objects.h"
47 class BASE_EXPORT PosixDynamicThreadPool
48 : public RefCountedThreadSafe
<PosixDynamicThreadPool
> {
50 class PosixDynamicThreadPoolPeer
;
52 // All worker threads will share the same |name_prefix|. They will exit after
53 // |idle_time_before_exit|.
54 PosixDynamicThreadPool(const std::string
& name_prefix
,
55 TimeDelta idle_time_before_exit
);
57 // Indicates that the thread pool is going away. Stops handing out tasks to
58 // worker threads. Wakes up all the idle threads to let them exit. If
59 // |blocking| is set to true, the call returns after all worker threads have
61 // The second and subsequent calls to this method are ignored, regardless of
62 // the value of |blocking|.
63 void Terminate(bool blocking
);
65 // Adds |task| to the thread pool.
66 void PostTask(const tracked_objects::Location
& from_here
,
69 // Worker thread method to wait for up to |idle_time_before_exit| for more
70 // work from the thread pool. Returns an empty task if no work is available.
71 PendingTask
WaitForTask();
73 // Marks |worker| as dead and enqueues a cleanup task to join dead worker
74 // threads. Unlike tasks enqueued by PostTask(), cleanup tasks never cause new
75 // worker threads to be created.
76 void NotifyWorkerIsGoingAway(PlatformThreadHandle worker
);
79 friend class RefCountedThreadSafe
<PosixDynamicThreadPool
>;
81 ~PosixDynamicThreadPool();
83 // Adds pending_task to the thread pool. This function will clear
84 // |pending_task->task|.
85 void AddTaskNoLock(PendingTask
* pending_task
);
87 void CleanUpThreads();
89 const std::string name_prefix_
;
90 const TimeDelta idle_time_before_exit_
;
92 Lock lock_
; // Protects all the variables below.
94 // Signal()s worker threads to let them know more tasks are available.
95 // Also used for Broadcast()'ing to worker threads to let them know the pool
96 // is being deleted and they can exit.
97 ConditionVariable pending_tasks_available_cv_
;
98 size_t num_idle_threads_
;
99 bool has_pending_cleanup_task_
;
100 std::queue
<PendingTask
> pending_tasks_
;
103 std::vector
<PlatformThreadHandle
> threads_to_cleanup_
;
104 std::vector
<PlatformThreadHandle
> worker_threads_
;
106 // Signaled when idle thread count or living thread count is changed. Please
107 // note that it won't be signaled when Terminate() is called.
109 // Only used for tests to ensure correct thread ordering. It will always be
110 // NULL in non-test code.
111 scoped_ptr
<ConditionVariable
> num_threads_cv_
;
113 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool
);
118 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_