blob: ba531132f91927cb427ceb56ae19ba1e3d1e3a6a [file] [log] [blame]
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001#include "runtime.h"
2#include "stl_util.h"
3#include "thread.h"
4#include "thread_pool.h"
5
6namespace art {
7
8ThreadPoolWorker::ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name,
9 size_t stack_size)
10 : thread_pool_(thread_pool),
11 name_(name),
12 stack_size_(stack_size) {
13 const char* reason = "new thread pool worker thread";
Brian Carlstrombcc29262012-11-02 11:36:03 -070014 pthread_attr_t attr;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070015 CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), reason);
16 CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), reason);
17 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, &attr, &Callback, this), reason);
18 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), reason);
19}
20
21ThreadPoolWorker::~ThreadPoolWorker() {
22 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "thread pool worker shutdown");
23}
24
25void ThreadPoolWorker::Run() {
26 Thread* self = Thread::Current();
27 Closure* task = NULL;
28 while ((task = thread_pool_->GetTask(self)) != NULL) {
29 task->Run(self);
30 }
31}
32
33void* ThreadPoolWorker::Callback(void* arg) {
34 ThreadPoolWorker* worker = reinterpret_cast<ThreadPoolWorker*>(arg);
35 Runtime* runtime = Runtime::Current();
36 CHECK(runtime->AttachCurrentThread(worker->name_.c_str(), true, NULL));
37 // Do work until its time to shut down.
38 worker->Run();
39 runtime->DetachCurrentThread();
40 return NULL;
41}
42
43void ThreadPool::AddTask(Thread* self, Closure* task){
44 MutexLock mu(self, task_queue_lock_);
45 tasks_.push_back(task);
46 // If we have any waiters, signal one.
47 if (waiting_count_ != 0) {
48 task_queue_condition_.Signal(self);
49 }
50}
51
52void ThreadPool::AddThread(size_t stack_size) {
53 threads_.push_back(
54 new ThreadPoolWorker(
55 this,
56 StringPrintf("Thread pool worker %d", static_cast<int>(GetThreadCount())),
57 stack_size));
58}
59
60ThreadPool::ThreadPool(size_t num_threads)
61 : task_queue_lock_("task queue lock"),
62 task_queue_condition_("task queue condition", task_queue_lock_),
63 completion_condition_("task completion condition", task_queue_lock_),
64 started_(false),
65 shutting_down_(false),
66 waiting_count_(0) {
67 while (GetThreadCount() < num_threads) {
68 AddThread(ThreadPoolWorker::kDefaultStackSize);
69 }
70}
71
72ThreadPool::~ThreadPool() {
Mathieu Chartiere46cd752012-10-31 16:56:18 -070073 {
74 Thread* self = Thread::Current();
75 MutexLock mu(self, task_queue_lock_);
76 // Tell any remaining workers to shut down.
77 shutting_down_ = true;
78 android_memory_barrier();
79 // Broadcast to everyone waiting.
80 task_queue_condition_.Broadcast(self);
81 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070082 // Wait for the threads to finish.
83 STLDeleteElements(&threads_);
84}
85
86void ThreadPool::StartWorkers(Thread* self) {
87 MutexLock mu(self, task_queue_lock_);
88 started_ = true;
89 android_memory_barrier();
90 task_queue_condition_.Broadcast(self);
91}
92
93void ThreadPool::StopWorkers(Thread* self) {
94 MutexLock mu(self, task_queue_lock_);
95 started_ = false;
96 android_memory_barrier();
97}
98
99Closure* ThreadPool::GetTask(Thread* self) {
100 MutexLock mu(self, task_queue_lock_);
101 while (!shutting_down_) {
102 if (started_ && !tasks_.empty()) {
103 Closure* task = tasks_.front();
104 tasks_.pop_front();
105 return task;
106 }
107
108 waiting_count_++;
109 if (waiting_count_ == GetThreadCount() && tasks_.empty()) {
110 // We may be done, lets broadcast to the completion condition.
111 completion_condition_.Broadcast(self);
112 }
113 task_queue_condition_.Wait(self);
114 waiting_count_--;
115 }
116
117 // We are shutting down, return NULL to tell the worker thread to stop looping.
118 return NULL;
119}
120
121void ThreadPool::Wait(Thread* self) {
122 MutexLock mu(self, task_queue_lock_);
123 // Wait until each thread is waiting and the task list is empty.
124 while (waiting_count_ != GetThreadCount() || !tasks_.empty()) {
125 completion_condition_.Wait(self);
126 }
127}
128
129} // namespace art