| 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() : terminate_(false), head_(NULL), tail_(NULL) { | |
| 10 int result; | |
| 11 | |
| 12 result = pthread_mutex_init(data_.mutex(), NULL); | |
| 13 if (result != 0) { | |
| 14 FATAL("pthread_mutex_init failed"); | |
| 15 } | |
| 16 | |
| 17 result = pthread_cond_init(data_.cond(), NULL); | |
| 18 if (result != 0) { | |
| 19 FATAL("pthread_cond_init failed"); | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 | |
| 24 void TaskQueue::Insert(TaskQueueEntry* entry) { | |
| 25 pthread_mutex_lock(data_.mutex()); | |
| 26 if (head_ == NULL) { | |
| 27 head_ = entry; | |
| 28 tail_ = entry; | |
| 29 pthread_cond_signal(data_.cond()); | |
| 30 } else { | |
| 31 tail_->set_next(entry); | |
| 32 tail_ = entry; | |
| 33 } | |
| 34 pthread_mutex_unlock(data_.mutex()); | |
| 35 } | |
| 36 | |
| 37 | |
| 38 TaskQueueEntry* TaskQueue::Remove() { | |
| 39 pthread_mutex_lock(data_.mutex()); | |
| 40 TaskQueueEntry* result = head_; | |
| 41 while (result == NULL) { | |
| 42 if (terminate_) { | |
| 43 pthread_mutex_unlock(data_.mutex()); | |
| 44 return NULL; | |
| 45 } | |
| 46 pthread_cond_wait(data_.cond(), data_.mutex()); | |
| 47 if (terminate_) { | |
| 48 pthread_mutex_unlock(data_.mutex()); | |
| 49 return NULL; | |
| 50 } | |
| 51 result = head_; | |
| 52 } | |
| 53 head_ = result->next(); | |
| 54 ASSERT(head_ != NULL || tail_ == result); | |
| 55 pthread_mutex_unlock(data_.mutex()); | |
| 56 return result; | |
| 57 } | |
| 58 | |
| 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 | |
| 68 void ThreadPool::Start() { | 9 void ThreadPool::Start() { |
| 69 pthread_t* threads | 10 pthread_t* threads |
| 70 = reinterpret_cast<pthread_t*>(calloc(size_, sizeof(pthread_t*))); // NOL
INT | 11 = reinterpret_cast<pthread_t*>(calloc(size_, sizeof(pthread_t*))); // NOL
INT |
| 71 data_.set_threads(threads); | 12 data_.set_threads(threads); |
| 72 for (int i = 0; i < size_; i++) { | 13 for (int i = 0; i < size_; i++) { |
| 73 pthread_t handler_thread; | 14 pthread_t handler_thread; |
| 74 int result = pthread_create(&handler_thread, | 15 int result = pthread_create(&handler_thread, |
| 75 NULL, | 16 NULL, |
| 76 &ThreadPool::Main, | 17 &ThreadPool::Main, |
| 77 this); | 18 this); |
| 78 if (result != 0) { | 19 if (result != 0) { |
| 79 FATAL("Create and start thread pool thread"); | 20 FATAL("Create and start thread pool thread"); |
| 80 } | 21 } |
| 81 data_.threads()[i] = handler_thread; | 22 data_.threads()[i] = handler_thread; |
| 82 } | 23 } |
| 83 } | 24 } |
| 84 | 25 |
| 85 | 26 |
| 86 void ThreadPool::Shutdown() { | 27 void ThreadPool::Shutdown() { |
| 87 terminate_ = true; | 28 terminate_ = true; |
| 88 queue_.Shutdown(); | 29 queue_.Shutdown(); |
| 89 for (int i = 0; i < size_; i++) { | 30 for (int i = 0; i < size_; i++) { |
| 90 pthread_join(data_.threads()[i], NULL); | 31 pthread_join(data_.threads()[i], NULL); |
| 91 } | 32 } |
| 92 } | 33 } |
| OLD | NEW |