Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: base/threading/worker_pool_posix.cc

Issue 8565024: base: Refactor PendingTask out of MessageLoop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Style and build fix. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/threading/worker_pool_posix.h" 5 #include "base/threading/worker_pool_posix.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/debug/trace_event.h" 8 #include "base/debug/trace_event.h"
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "base/task.h" 13 #include "base/task.h"
14 #include "base/threading/platform_thread.h" 14 #include "base/threading/platform_thread.h"
15 #include "base/threading/worker_pool.h" 15 #include "base/threading/worker_pool.h"
16 #include "base/tracked_objects.h" 16 #include "base/tracked_objects.h"
17 17
18 using tracked_objects::TrackedTime;
19
18 namespace base { 20 namespace base {
19 21
20 namespace { 22 namespace {
21 23
22 const int kIdleSecondsBeforeExit = 10 * 60; 24 const int kIdleSecondsBeforeExit = 10 * 60;
23 // A stack size of 64 KB is too small for the CERT_PKIXVerifyCert 25 // A stack size of 64 KB is too small for the CERT_PKIXVerifyCert
24 // function of NSS because of NSS bug 439169. 26 // function of NSS because of NSS bug 439169.
25 const int kWorkerThreadStackSize = 128 * 1024; 27 const int kWorkerThreadStackSize = 128 * 1024;
26 28
27 class WorkerPoolImpl { 29 class WorkerPoolImpl {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 DISALLOW_COPY_AND_ASSIGN(WorkerThread); 77 DISALLOW_COPY_AND_ASSIGN(WorkerThread);
76 }; 78 };
77 79
78 void WorkerThread::ThreadMain() { 80 void WorkerThread::ThreadMain() {
79 const std::string name = base::StringPrintf( 81 const std::string name = base::StringPrintf(
80 "%s/%d", name_prefix_.c_str(), PlatformThread::CurrentId()); 82 "%s/%d", name_prefix_.c_str(), PlatformThread::CurrentId());
81 // Note |name.c_str()| must remain valid for for the whole life of the thread. 83 // Note |name.c_str()| must remain valid for for the whole life of the thread.
82 PlatformThread::SetName(name.c_str()); 84 PlatformThread::SetName(name.c_str());
83 85
84 for (;;) { 86 for (;;) {
85 PosixDynamicThreadPool::PendingTask pending_task = pool_->WaitForTask(); 87 PendingTask pending_task = pool_->WaitForTask();
86 if (pending_task.task.is_null()) 88 if (pending_task.task.is_null())
87 break; 89 break;
88 UNSHIPPED_TRACE_EVENT2("task", "WorkerThread::ThreadMain::Run", 90 UNSHIPPED_TRACE_EVENT2("task", "WorkerThread::ThreadMain::Run",
89 "src_file", pending_task.posted_from.file_name(), 91 "src_file", pending_task.posted_from.file_name(),
90 "src_func", pending_task.posted_from.function_name()); 92 "src_func", pending_task.posted_from.function_name());
91 93
92 tracked_objects::TrackedTime start_time = 94 TrackedTime start_time =
93 tracked_objects::ThreadData::NowForStartOfRun(); 95 tracked_objects::ThreadData::NowForStartOfRun();
94 96
95 pending_task.task.Run(); 97 pending_task.task.Run();
96 98
97 tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking( 99 tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking(
98 pending_task.birth_tally, pending_task.time_posted, 100 pending_task.birth_tally, TrackedTime(pending_task.time_posted),
99 start_time, tracked_objects::ThreadData::NowForEndOfRun()); 101 start_time, tracked_objects::ThreadData::NowForEndOfRun());
100 } 102 }
101 103
102 // The WorkerThread is non-joinable, so it deletes itself. 104 // The WorkerThread is non-joinable, so it deletes itself.
103 delete this; 105 delete this;
104 } 106 }
105 107
106 } // namespace 108 } // namespace
107 109
108 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, 110 bool WorkerPool::PostTask(const tracked_objects::Location& from_here,
109 Task* task, bool task_is_slow) { 111 Task* task, bool task_is_slow) {
110 g_lazy_worker_pool.Pointer()->PostTask(from_here, task, task_is_slow); 112 g_lazy_worker_pool.Pointer()->PostTask(from_here, task, task_is_slow);
111 return true; 113 return true;
112 } 114 }
113 115
114 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, 116 bool WorkerPool::PostTask(const tracked_objects::Location& from_here,
115 const base::Closure& task, bool task_is_slow) { 117 const base::Closure& task, bool task_is_slow) {
116 g_lazy_worker_pool.Pointer()->PostTask(from_here, task, task_is_slow); 118 g_lazy_worker_pool.Pointer()->PostTask(from_here, task, task_is_slow);
117 return true; 119 return true;
118 } 120 }
119 121
120 PosixDynamicThreadPool::PendingTask::PendingTask(
121 const tracked_objects::Location& posted_from,
122 const base::Closure& task)
123 : posted_from(posted_from),
124 task(task) {
125 birth_tally = tracked_objects::ThreadData::TallyABirthIfActive(posted_from);
126 time_posted = tracked_objects::ThreadData::Now();
127 }
128
129 PosixDynamicThreadPool::PendingTask::~PendingTask() {
130 }
131
132 PosixDynamicThreadPool::PosixDynamicThreadPool( 122 PosixDynamicThreadPool::PosixDynamicThreadPool(
133 const std::string& name_prefix, 123 const std::string& name_prefix,
134 int idle_seconds_before_exit) 124 int idle_seconds_before_exit)
135 : name_prefix_(name_prefix), 125 : name_prefix_(name_prefix),
136 idle_seconds_before_exit_(idle_seconds_before_exit), 126 idle_seconds_before_exit_(idle_seconds_before_exit),
137 pending_tasks_available_cv_(&lock_), 127 pending_tasks_available_cv_(&lock_),
138 num_idle_threads_(0), 128 num_idle_threads_(0),
139 terminated_(false), 129 terminated_(false),
140 num_idle_threads_cv_(NULL) {} 130 num_idle_threads_cv_(NULL) {}
141 131
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 pending_tasks_available_cv_.Signal(); 180 pending_tasks_available_cv_.Signal();
191 } else { 181 } else {
192 // The new PlatformThread will take ownership of the WorkerThread object, 182 // The new PlatformThread will take ownership of the WorkerThread object,
193 // which will delete itself on exit. 183 // which will delete itself on exit.
194 WorkerThread* worker = 184 WorkerThread* worker =
195 new WorkerThread(name_prefix_, this); 185 new WorkerThread(name_prefix_, this);
196 PlatformThread::CreateNonJoinable(kWorkerThreadStackSize, worker); 186 PlatformThread::CreateNonJoinable(kWorkerThreadStackSize, worker);
197 } 187 }
198 } 188 }
199 189
200 PosixDynamicThreadPool::PendingTask PosixDynamicThreadPool::WaitForTask() { 190 PendingTask PosixDynamicThreadPool::WaitForTask() {
201 AutoLock locked(lock_); 191 AutoLock locked(lock_);
202 192
203 if (terminated_) 193 if (terminated_)
204 return PendingTask(FROM_HERE, base::Closure()); 194 return PendingTask(FROM_HERE, base::Closure());
205 195
206 if (pending_tasks_.empty()) { // No work available, wait for work. 196 if (pending_tasks_.empty()) { // No work available, wait for work.
207 num_idle_threads_++; 197 num_idle_threads_++;
208 if (num_idle_threads_cv_.get()) 198 if (num_idle_threads_cv_.get())
209 num_idle_threads_cv_->Signal(); 199 num_idle_threads_cv_->Signal();
210 pending_tasks_available_cv_.TimedWait( 200 pending_tasks_available_cv_.TimedWait(
211 TimeDelta::FromSeconds(idle_seconds_before_exit_)); 201 TimeDelta::FromSeconds(idle_seconds_before_exit_));
212 num_idle_threads_--; 202 num_idle_threads_--;
213 if (num_idle_threads_cv_.get()) 203 if (num_idle_threads_cv_.get())
214 num_idle_threads_cv_->Signal(); 204 num_idle_threads_cv_->Signal();
215 if (pending_tasks_.empty()) { 205 if (pending_tasks_.empty()) {
216 // We waited for work, but there's still no work. Return NULL to signal 206 // We waited for work, but there's still no work. Return NULL to signal
217 // the thread to terminate. 207 // the thread to terminate.
218 return PendingTask(FROM_HERE, base::Closure()); 208 return PendingTask(FROM_HERE, base::Closure());
219 } 209 }
220 } 210 }
221 211
222 PendingTask pending_task = pending_tasks_.front(); 212 PendingTask pending_task = pending_tasks_.front();
223 pending_tasks_.pop(); 213 pending_tasks_.pop();
224 return pending_task; 214 return pending_task;
225 } 215 }
226 216
227 } // namespace base 217 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698