Index: runtime/bin/thread_pool_macos.cc |
diff --git a/runtime/bin/thread_pool_macos.cc b/runtime/bin/thread_pool_macos.cc |
index 962bb0bfbd23264b05a6f0c93cea220bab79a480..f0e37342d5c2d0d4c1adac680efdb9e121453fae 100644 |
--- a/runtime/bin/thread_pool_macos.cc |
+++ b/runtime/bin/thread_pool_macos.cc |
@@ -6,7 +6,7 @@ |
#include "bin/thread_pool.h" |
-TaskQueue::TaskQueue() : head_(NULL), tail_(NULL) { |
+TaskQueue::TaskQueue() : terminate_(false), head_(NULL), tail_(NULL) { |
int result; |
result = pthread_mutex_init(data_.mutex(), NULL); |
@@ -39,7 +39,15 @@ TaskQueueEntry* TaskQueue::Remove() { |
pthread_mutex_lock(data_.mutex()); |
TaskQueueEntry* result = head_; |
while (result == NULL) { |
+ if (terminate_) { |
+ pthread_mutex_unlock(data_.mutex()); |
+ return NULL; |
+ } |
pthread_cond_wait(data_.cond(), data_.mutex()); |
+ if (terminate_) { |
+ pthread_mutex_unlock(data_.mutex()); |
+ return NULL; |
+ } |
result = head_; |
} |
head_ = result->next(); |
@@ -49,9 +57,17 @@ TaskQueueEntry* TaskQueue::Remove() { |
} |
+void TaskQueue::Shutdown() { |
+ pthread_mutex_lock(data_.mutex()); |
+ terminate_ = true; |
+ pthread_cond_broadcast(data_.cond()); |
+ pthread_mutex_unlock(data_.mutex()); |
+} |
+ |
+ |
void ThreadPool::Start() { |
pthread_t* threads |
- = reinterpret_cast<pthread_t*>(calloc(size_, sizeof(pthread_t*))); |
+ = reinterpret_cast<pthread_t*>(calloc(size_, sizeof(pthread_t*))); // NOLINT |
data_.set_threads(threads); |
for (int i = 0; i < size_; i++) { |
pthread_t handler_thread; |
@@ -65,3 +81,12 @@ void ThreadPool::Start() { |
data_.threads()[i] = handler_thread; |
} |
} |
+ |
+ |
+void ThreadPool::Shutdown() { |
+ terminate_ = true; |
+ queue_.Shutdown(); |
+ for (int i = 0; i < size_; i++) { |
+ pthread_join(data_.threads()[i], NULL); |
+ } |
+} |