Chromium Code Reviews| Index: runtime/bin/thread_pool_linux.cc |
| diff --git a/runtime/bin/thread_pool_linux.cc b/runtime/bin/thread_pool_linux.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..11339160e472a8300b7953055fb442da14f283d8 |
| --- /dev/null |
| +++ b/runtime/bin/thread_pool_linux.cc |
| @@ -0,0 +1,71 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +#include <pthread.h> |
| + |
| +#include "bin/thread_pool.h" |
| + |
| +TaskQueue::TaskQueue() : head_(NULL), tail_(NULL) { |
| + int result; |
| + |
| + result = pthread_mutex_init(data_.mutex(), NULL); |
| + if (result != 0) { |
| + FATAL("pthread_mutex_init failed"); |
| + } |
| + |
| + result = pthread_cond_init(data_.cond(), NULL); |
| + if (result != 0) { |
| + FATAL("pthread_cond_init failed"); |
| + } |
| +} |
| + |
| + |
| +void TaskQueue::Insert(TaskQueueEntry* entry) { |
| + pthread_mutex_lock(data_.mutex()); |
| + if (head_ == NULL) { |
| + ASSERT(tail_ == NULL); |
|
Mads Ager (google)
2012/01/04 14:32:46
Maybe not assert here? We don't really care about
Søren Gjesse
2012/01/04 15:15:07
Right, removed the asserts.
|
| + head_ = entry; |
| + tail_ = entry; |
| + pthread_cond_signal(data_.cond()); |
| + } else { |
| + ASSERT(tail_ != NULL); |
| + tail_->set_next(entry); |
| + tail_ = entry; |
| + } |
| + pthread_mutex_unlock(data_.mutex()); |
| +} |
| + |
| + |
| +TaskQueueEntry* TaskQueue::Remove() { |
| + pthread_mutex_lock(data_.mutex()); |
| + TaskQueueEntry* result = head_; |
| + while (result == NULL) { |
| + pthread_cond_wait(data_.cond(), data_.mutex()); |
| + result = head_; |
| + } |
| + head_ = result->next(); |
| +#ifdef DEBUG |
|
Mads Ager (google)
2012/01/04 14:32:46
Maybe just
ASSERT(head_ != NULL || tail_ == resul
Søren Gjesse
2012/01/04 15:15:07
Done.
|
| + if (head_ == NULL) { |
| + ASSERT(tail_ == result); |
| + tail_ = NULL; |
| + } |
| +#endif |
| + pthread_mutex_unlock(data_.mutex()); |
| + return result; |
| +} |
| + |
| + |
| +void ThreadPool::Start() { |
| + for (int i = 0; i < size_; i++) { |
| + pthread_t handler_thread; |
| + int result = pthread_create(&handler_thread, |
| + NULL, |
| + &ThreadPool::Main, |
| + this); |
| + if (result != 0) { |
| + FATAL("Create and start thread pool thread"); |
| + } |
| + data_.threads()[i] = handler_thread; |
| + } |
| +} |