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