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 #include "base/threading/worker_pool_posix.h"
8 #include "base/callback.h"
9 #include "base/lazy_instance.h"
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/threading/platform_thread.h"
14 #include "base/threading/thread_local.h"
15 #include "base/threading/worker_pool.h"
16 #include "base/trace_event/trace_event.h"
17 #include "base/tracked_objects.h"
19 using tracked_objects::TrackedTime
;
25 base::LazyInstance
<ThreadLocalBoolean
>::Leaky
26 g_worker_pool_running_on_this_thread
= LAZY_INSTANCE_INITIALIZER
;
28 const int kIdleSecondsBeforeExit
= 10 * 60;
30 class WorkerPoolImpl
{
35 void PostTask(const tracked_objects::Location
& from_here
,
36 const base::Closure
& task
, bool task_is_slow
);
39 scoped_refptr
<base::PosixDynamicThreadPool
> pool_
;
42 WorkerPoolImpl::WorkerPoolImpl()
43 : pool_(new base::PosixDynamicThreadPool("WorkerPool",
44 kIdleSecondsBeforeExit
)) {
47 WorkerPoolImpl::~WorkerPoolImpl() {
51 void WorkerPoolImpl::PostTask(const tracked_objects::Location
& from_here
,
52 const base::Closure
& task
, bool task_is_slow
) {
53 pool_
->PostTask(from_here
, task
);
56 base::LazyInstance
<WorkerPoolImpl
> g_lazy_worker_pool
=
57 LAZY_INSTANCE_INITIALIZER
;
59 class WorkerThread
: public PlatformThread::Delegate
{
61 WorkerThread(const std::string
& name_prefix
,
62 base::PosixDynamicThreadPool
* pool
)
63 : name_prefix_(name_prefix
),
66 void ThreadMain() override
;
69 const std::string name_prefix_
;
70 scoped_refptr
<base::PosixDynamicThreadPool
> pool_
;
72 DISALLOW_COPY_AND_ASSIGN(WorkerThread
);
75 void WorkerThread::ThreadMain() {
76 g_worker_pool_running_on_this_thread
.Get().Set(true);
77 const std::string name
= base::StringPrintf(
78 "%s/%d", name_prefix_
.c_str(), PlatformThread::CurrentId());
79 // Note |name.c_str()| must remain valid for for the whole life of the thread.
80 PlatformThread::SetName(name
);
83 PendingTask pending_task
= pool_
->WaitForTask();
84 if (pending_task
.task
.is_null())
86 TRACE_EVENT2("toplevel", "WorkerThread::ThreadMain::Run",
87 "src_file", pending_task
.posted_from
.file_name(),
88 "src_func", pending_task
.posted_from
.function_name());
90 tracked_objects::TaskStopwatch stopwatch
;
92 pending_task
.task
.Run();
95 tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking(
96 pending_task
.birth_tally
, pending_task
.time_posted
, stopwatch
);
99 // The WorkerThread is non-joinable, so it deletes itself.
106 bool WorkerPool::PostTask(const tracked_objects::Location
& from_here
,
107 const base::Closure
& task
, bool task_is_slow
) {
108 g_lazy_worker_pool
.Pointer()->PostTask(from_here
, task
, task_is_slow
);
113 bool WorkerPool::RunsTasksOnCurrentThread() {
114 return g_worker_pool_running_on_this_thread
.Get().Get();
117 PosixDynamicThreadPool::PosixDynamicThreadPool(const std::string
& name_prefix
,
118 int idle_seconds_before_exit
)
119 : name_prefix_(name_prefix
),
120 idle_seconds_before_exit_(idle_seconds_before_exit
),
121 pending_tasks_available_cv_(&lock_
),
122 num_idle_threads_(0),
123 terminated_(false) {}
125 PosixDynamicThreadPool::~PosixDynamicThreadPool() {
126 while (!pending_tasks_
.empty())
127 pending_tasks_
.pop();
130 void PosixDynamicThreadPool::Terminate() {
132 AutoLock
locked(lock_
);
133 DCHECK(!terminated_
) << "Thread pool is already terminated.";
136 pending_tasks_available_cv_
.Broadcast();
139 void PosixDynamicThreadPool::PostTask(
140 const tracked_objects::Location
& from_here
,
141 const base::Closure
& task
) {
142 PendingTask
pending_task(from_here
, task
);
143 AddTask(&pending_task
);
146 void PosixDynamicThreadPool::AddTask(PendingTask
* pending_task
) {
147 AutoLock
locked(lock_
);
148 DCHECK(!terminated_
) <<
149 "This thread pool is already terminated. Do not post new tasks.";
151 pending_tasks_
.push(*pending_task
);
152 pending_task
->task
.Reset();
154 // We have enough worker threads.
155 if (static_cast<size_t>(num_idle_threads_
) >= pending_tasks_
.size()) {
156 pending_tasks_available_cv_
.Signal();
158 // The new PlatformThread will take ownership of the WorkerThread object,
159 // which will delete itself on exit.
160 WorkerThread
* worker
=
161 new WorkerThread(name_prefix_
, this);
162 PlatformThread::CreateNonJoinable(0, worker
);
166 PendingTask
PosixDynamicThreadPool::WaitForTask() {
167 AutoLock
locked(lock_
);
170 return PendingTask(FROM_HERE
, base::Closure());
172 if (pending_tasks_
.empty()) { // No work available, wait for work.
174 if (num_idle_threads_cv_
.get())
175 num_idle_threads_cv_
->Signal();
176 pending_tasks_available_cv_
.TimedWait(
177 TimeDelta::FromSeconds(idle_seconds_before_exit_
));
179 if (num_idle_threads_cv_
.get())
180 num_idle_threads_cv_
->Signal();
181 if (pending_tasks_
.empty()) {
182 // We waited for work, but there's still no work. Return NULL to signal
183 // the thread to terminate.
184 return PendingTask(FROM_HERE
, base::Closure());
188 PendingTask pending_task
= pending_tasks_
.front();
189 pending_tasks_
.pop();