Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include <pthread.h> | |
| 6 | |
| 7 #include "bin/thread_pool.h" | |
| 8 | |
| 9 TaskQueue::TaskQueue() : 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 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.
| |
| 28 head_ = entry; | |
| 29 tail_ = entry; | |
| 30 pthread_cond_signal(data_.cond()); | |
| 31 } else { | |
| 32 ASSERT(tail_ != NULL); | |
| 33 tail_->set_next(entry); | |
| 34 tail_ = entry; | |
| 35 } | |
| 36 pthread_mutex_unlock(data_.mutex()); | |
| 37 } | |
| 38 | |
| 39 | |
| 40 TaskQueueEntry* TaskQueue::Remove() { | |
| 41 pthread_mutex_lock(data_.mutex()); | |
| 42 TaskQueueEntry* result = head_; | |
| 43 while (result == NULL) { | |
| 44 pthread_cond_wait(data_.cond(), data_.mutex()); | |
| 45 result = head_; | |
| 46 } | |
| 47 head_ = result->next(); | |
| 48 #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.
| |
| 49 if (head_ == NULL) { | |
| 50 ASSERT(tail_ == result); | |
| 51 tail_ = NULL; | |
| 52 } | |
| 53 #endif | |
| 54 pthread_mutex_unlock(data_.mutex()); | |
| 55 return result; | |
| 56 } | |
| 57 | |
| 58 | |
| 59 void ThreadPool::Start() { | |
| 60 for (int i = 0; i < size_; i++) { | |
| 61 pthread_t handler_thread; | |
| 62 int result = pthread_create(&handler_thread, | |
| 63 NULL, | |
| 64 &ThreadPool::Main, | |
| 65 this); | |
| 66 if (result != 0) { | |
| 67 FATAL("Create and start thread pool thread"); | |
| 68 } | |
| 69 data_.threads()[i] = handler_thread; | |
| 70 } | |
| 71 } | |
| OLD | NEW |