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

Side by Side Diff: base/worker_pool_linux.cc

Issue 39102: Adding a new thread pool to SimpleThread. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 9 months 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/worker_pool.h" 5 #include "base/worker_pool.h"
6 #include "base/worker_pool_linux.h"
6 7
8 #include "base/lazy_instance.h"
7 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/platform_thread.h"
11 #include "base/ref_counted.h"
12 #include "base/string_util.h"
8 #include "base/task.h" 13 #include "base/task.h"
9 14
10 namespace { 15 namespace {
11 16
12 void* PThreadCallback(void* param) { 17 const int kIdleSecondsBeforeExit = 10 * 60;
13 Task* task = static_cast<Task*>(param); 18 const int kWorkerThreadStackSize = 64 * 1024;
14 task->Run(); 19
15 delete task; 20 class WorkerPoolImpl {
16 return 0; 21 public:
22 WorkerPoolImpl();
23 ~WorkerPoolImpl();
24
25 void PostTask(const tracked_objects::Location& from_here, Task* task,
26 bool task_is_slow);
27
28 private:
29 scoped_refptr<base::LinuxDynamicThreadPool> pool_;
30 };
31
32 WorkerPoolImpl::WorkerPoolImpl()
33 : pool_(new base::LinuxDynamicThreadPool(
34 "WorkerPool", kIdleSecondsBeforeExit)) {}
35
36 WorkerPoolImpl::~WorkerPoolImpl() {
37 pool_->Terminate();
38 }
39
40 void WorkerPoolImpl::PostTask(const tracked_objects::Location& from_here,
41 Task* task, bool task_is_slow) {
42 task->SetBirthPlace(from_here);
43 pool_->PostTask(task);
44 }
45
46 base::LazyInstance<WorkerPoolImpl> g_lazy_worker_pool(base::LINKER_INITIALIZED);
47
48 class WorkerThread : public PlatformThread::Delegate {
49 public:
50 WorkerThread(const std::string& name_prefix, int idle_seconds_before_exit,
51 base::LinuxDynamicThreadPool* pool)
52 : name_prefix_(name_prefix),
53 idle_seconds_before_exit_(idle_seconds_before_exit),
54 pool_(pool) {}
55
56 virtual void ThreadMain();
57
58 private:
59 const std::string name_prefix_;
60 const int idle_seconds_before_exit_;
61 scoped_refptr<base::LinuxDynamicThreadPool> pool_;
62
63 DISALLOW_COPY_AND_ASSIGN(WorkerThread);
64 };
65
66 void WorkerThread::ThreadMain() {
67 const std::string name =
68 StringPrintf("%s/%d", name_prefix_.c_str(),
69 IntToString(PlatformThread::CurrentId()).c_str());
70 PlatformThread::SetName(name.c_str());
71
72 for (;;) {
73 Task* task = pool_->WaitForTask();
74 if (!task)
75 break;
76 task->Run();
77 delete task;
78 }
79
80 // The WorkerThread is non-joinable, so it deletes itself.
81 delete this;
17 } 82 }
18 83
19 } // namespace 84 } // namespace
20 85
21 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, 86 bool WorkerPool::PostTask(const tracked_objects::Location& from_here,
22 Task* task, bool task_is_slow) { 87 Task* task, bool task_is_slow) {
23 task->SetBirthPlace(from_here); 88 g_lazy_worker_pool.Pointer()->PostTask(from_here, task, task_is_slow);
89 return true;
90 }
24 91
25 pthread_t thread; 92 namespace base {
26 pthread_attr_t attr;
27 93
28 // POSIX does not have a worker thread pool implementation. For now we just 94 LinuxDynamicThreadPool::LinuxDynamicThreadPool(
29 // create a thread for each task, and ignore |task_is_slow|. 95 const std::string& name_prefix,
30 // TODO(dsh): Implement thread reuse. 96 int idle_seconds_before_exit)
31 pthread_attr_init(&attr); 97 : name_prefix_(name_prefix),
32 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 98 idle_seconds_before_exit_(idle_seconds_before_exit),
99 tasks_available_cv_(&lock_),
100 num_idle_threads_(0),
101 terminated_(false),
102 num_idle_threads_cv_(NULL) {}
33 103
34 int err = pthread_create(&thread, &attr, PThreadCallback, task); 104 LinuxDynamicThreadPool::~LinuxDynamicThreadPool() {
35 pthread_attr_destroy(&attr); 105 while (!tasks_.empty()) {
36 if (err) { 106 Task* task = tasks_.front();
37 DLOG(ERROR) << "pthread_create failed: " << err; 107 tasks_.pop();
38 delete task; 108 delete task;
39 return false; 109 }
110 }
111
112 void LinuxDynamicThreadPool::Terminate() {
113 {
114 AutoLock locked(lock_);
115 DCHECK(!terminated_) << "Thread pool is already terminated.";
116 terminated_ = true;
117 }
118 tasks_available_cv_.Broadcast();
119 }
120
121 void LinuxDynamicThreadPool::PostTask(Task* task) {
122 AutoLock locked(lock_);
123 DCHECK(!terminated_) <<
124 "This thread pool is already terminated. Do not post new tasks.";
125
126 tasks_.push(task);
127
128 // We have enough worker threads.
129 if (static_cast<size_t>(num_idle_threads_) >= tasks_.size()) {
130 tasks_available_cv_.Signal();
131 } else {
132 // The new PlatformThread will take ownership of the WorkerThread object,
133 // which will delete itself on exit.
134 WorkerThread* worker =
135 new WorkerThread(name_prefix_, idle_seconds_before_exit_, this);
136 PlatformThread::CreateNonJoinable(kWorkerThreadStackSize, worker);
137 }
138 }
139
140 Task* LinuxDynamicThreadPool::WaitForTask() {
141 AutoLock locked(lock_);
142
143 if (terminated_)
144 return NULL;
145
146 if (tasks_.empty()) { // No work available, wait for work.
147 num_idle_threads_++;
148 if (num_idle_threads_cv_.get())
149 num_idle_threads_cv_->Signal();
150 tasks_available_cv_.TimedWait(
151 TimeDelta::FromSeconds(kIdleSecondsBeforeExit));
152 num_idle_threads_--;
153 if (num_idle_threads_cv_.get())
154 num_idle_threads_cv_->Signal();
155 if (tasks_.empty()) {
156 // We waited for work, but there's still no work. Return NULL to signal
157 // the thread to terminate.
158 return NULL;
159 }
40 } 160 }
41 161
42 return true; 162 Task* task = tasks_.front();
163 tasks_.pop();
164 return task;
43 } 165 }
166
167 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698