blob: fa0cf794c4c2b706cf03feb870f02e958557d1f4 [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() {
72 // Tell any remaining workers to shut down.
73 shutting_down_ = true;
74 android_memory_barrier();
75 // Broadcast to everyone waiting.
76 task_queue_condition_.Broadcast(Thread::Current());
77 // Wait for the threads to finish.
78 STLDeleteElements(&threads_);
79}
80
81void ThreadPool::StartWorkers(Thread* self) {
82 MutexLock mu(self, task_queue_lock_);
83 started_ = true;
84 android_memory_barrier();
85 task_queue_condition_.Broadcast(self);
86}
87
88void ThreadPool::StopWorkers(Thread* self) {
89 MutexLock mu(self, task_queue_lock_);
90 started_ = false;
91 android_memory_barrier();
92}
93
94Closure* ThreadPool::GetTask(Thread* self) {
95 MutexLock mu(self, task_queue_lock_);
96 while (!shutting_down_) {
97 if (started_ && !tasks_.empty()) {
98 Closure* task = tasks_.front();
99 tasks_.pop_front();
100 return task;
101 }
102
103 waiting_count_++;
104 if (waiting_count_ == GetThreadCount() && tasks_.empty()) {
105 // We may be done, lets broadcast to the completion condition.
106 completion_condition_.Broadcast(self);
107 }
108 task_queue_condition_.Wait(self);
109 waiting_count_--;
110 }
111
112 // We are shutting down, return NULL to tell the worker thread to stop looping.
113 return NULL;
114}
115
116void ThreadPool::Wait(Thread* self) {
117 MutexLock mu(self, task_queue_lock_);
118 // Wait until each thread is waiting and the task list is empty.
119 while (waiting_count_ != GetThreadCount() || !tasks_.empty()) {
120 completion_condition_.Wait(self);
121 }
122}
123
124} // namespace art