| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include <pthread.h> | 5 #include <pthread.h> |
| 6 | 6 |
| 7 #include "bin/thread_pool.h" | 7 #include "bin/thread_pool.h" |
| 8 | 8 |
| 9 TaskQueue::TaskQueue() : head_(NULL), tail_(NULL) { | 9 TaskQueue::TaskQueue() : terminate_(false), head_(NULL), tail_(NULL) { |
| 10 int result; | 10 int result; |
| 11 | 11 |
| 12 result = pthread_mutex_init(data_.mutex(), NULL); | 12 result = pthread_mutex_init(data_.mutex(), NULL); |
| 13 if (result != 0) { | 13 if (result != 0) { |
| 14 FATAL("pthread_mutex_init failed"); | 14 FATAL("pthread_mutex_init failed"); |
| 15 } | 15 } |
| 16 | 16 |
| 17 result = pthread_cond_init(data_.cond(), NULL); | 17 result = pthread_cond_init(data_.cond(), NULL); |
| 18 if (result != 0) { | 18 if (result != 0) { |
| 19 FATAL("pthread_cond_init failed"); | 19 FATAL("pthread_cond_init failed"); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 32 tail_ = entry; | 32 tail_ = entry; |
| 33 } | 33 } |
| 34 pthread_mutex_unlock(data_.mutex()); | 34 pthread_mutex_unlock(data_.mutex()); |
| 35 } | 35 } |
| 36 | 36 |
| 37 | 37 |
| 38 TaskQueueEntry* TaskQueue::Remove() { | 38 TaskQueueEntry* TaskQueue::Remove() { |
| 39 pthread_mutex_lock(data_.mutex()); | 39 pthread_mutex_lock(data_.mutex()); |
| 40 TaskQueueEntry* result = head_; | 40 TaskQueueEntry* result = head_; |
| 41 while (result == NULL) { | 41 while (result == NULL) { |
| 42 if (terminate_) { |
| 43 pthread_mutex_unlock(data_.mutex()); |
| 44 return NULL; |
| 45 } |
| 42 pthread_cond_wait(data_.cond(), data_.mutex()); | 46 pthread_cond_wait(data_.cond(), data_.mutex()); |
| 47 if (terminate_) { |
| 48 pthread_mutex_unlock(data_.mutex()); |
| 49 return NULL; |
| 50 } |
| 43 result = head_; | 51 result = head_; |
| 44 } | 52 } |
| 45 head_ = result->next(); | 53 head_ = result->next(); |
| 46 ASSERT(head_ != NULL || tail_ == result); | 54 ASSERT(head_ != NULL || tail_ == result); |
| 47 pthread_mutex_unlock(data_.mutex()); | 55 pthread_mutex_unlock(data_.mutex()); |
| 48 return result; | 56 return result; |
| 49 } | 57 } |
| 50 | 58 |
| 51 | 59 |
| 60 void TaskQueue::Shutdown() { |
| 61 pthread_mutex_lock(data_.mutex()); |
| 62 terminate_ = true; |
| 63 pthread_cond_broadcast(data_.cond()); |
| 64 pthread_mutex_unlock(data_.mutex()); |
| 65 } |
| 66 |
| 67 |
| 52 void ThreadPool::Start() { | 68 void ThreadPool::Start() { |
| 53 pthread_t* threads | 69 pthread_t* threads |
| 54 = reinterpret_cast<pthread_t*>(calloc(size_, sizeof(pthread_t*))); | 70 = reinterpret_cast<pthread_t*>(calloc(size_, sizeof(pthread_t*))); // NOL
INT |
| 55 data_.set_threads(threads); | 71 data_.set_threads(threads); |
| 56 for (int i = 0; i < size_; i++) { | 72 for (int i = 0; i < size_; i++) { |
| 57 pthread_t handler_thread; | 73 pthread_t handler_thread; |
| 58 int result = pthread_create(&handler_thread, | 74 int result = pthread_create(&handler_thread, |
| 59 NULL, | 75 NULL, |
| 60 &ThreadPool::Main, | 76 &ThreadPool::Main, |
| 61 this); | 77 this); |
| 62 if (result != 0) { | 78 if (result != 0) { |
| 63 FATAL("Create and start thread pool thread"); | 79 FATAL("Create and start thread pool thread"); |
| 64 } | 80 } |
| 65 data_.threads()[i] = handler_thread; | 81 data_.threads()[i] = handler_thread; |
| 66 } | 82 } |
| 67 } | 83 } |
| 84 |
| 85 |
| 86 void ThreadPool::Shutdown() { |
| 87 terminate_ = true; |
| 88 queue_.Shutdown(); |
| 89 for (int i = 0; i < size_; i++) { |
| 90 pthread_join(data_.threads()[i], NULL); |
| 91 } |
| 92 } |
| OLD | NEW |