Chromium Code Reviews| 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 "bin/thread_pool.h" | 5 #include "bin/thread_pool.h" |
| 6 | 6 |
| 7 #include "bin/thread.h" | 7 #include "bin/thread.h" |
| 8 | 8 |
| 9 void TaskQueue::Insert(TaskQueueEntry* entry) { | 9 void TaskQueue::Insert(TaskQueueEntry* entry) { |
| 10 MonitorLocker monitor(&monitor_); | 10 MonitorLocker monitor(&monitor_); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 TaskQueueEntry* entry = queue_.Remove(); | 56 TaskQueueEntry* entry = queue_.Remove(); |
| 57 if (entry == NULL) { | 57 if (entry == NULL) { |
| 58 return NULL; | 58 return NULL; |
| 59 } | 59 } |
| 60 Task task = entry->task(); | 60 Task task = entry->task(); |
| 61 delete entry; | 61 delete entry; |
| 62 return task; | 62 return task; |
| 63 } | 63 } |
| 64 | 64 |
| 65 | 65 |
| 66 void* ThreadPool::Main(void* args) { | 66 void ThreadPool::Start() { |
| 67 threads_ | |
| 68 = reinterpret_cast<dart::ThreadHandle*>( | |
| 69 calloc(size_, sizeof(dart::ThreadHandle))); | |
|
Mads Ager (google)
2012/01/20 12:35:34
I think we usually use 4-space indent we we have t
Søren Gjesse
2012/01/20 13:25:45
Done.
| |
| 70 for (int i = 0; i < size_; i++) { | |
| 71 threads_[i] = dart::Thread::Start(&ThreadPool::Main, | |
| 72 reinterpret_cast<uword>(this)); | |
| 73 if (threads_[i] == dart::Thread::kInvalidThreadHandle) { | |
| 74 FATAL("Create and start thread pool thread"); | |
| 75 } | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 | |
| 80 void ThreadPool::Shutdown() { | |
| 81 terminate_ = true; | |
| 82 queue_.Shutdown(); | |
| 83 for (int i = 0; i < size_; i++) { | |
| 84 dart::Thread::Join(threads_[i]); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 | |
| 89 void ThreadPool::Main(uword args) { | |
| 67 if (Dart_IsVMFlagSet("trace_thread_pool")) { | 90 if (Dart_IsVMFlagSet("trace_thread_pool")) { |
| 68 printf("Thread pool thread started\n"); | 91 printf("Thread pool thread started\n"); |
| 69 } | 92 } |
| 70 ThreadPool* pool = reinterpret_cast<ThreadPool*>(args); | 93 ThreadPool* pool = reinterpret_cast<ThreadPool*>(args); |
| 71 while (!pool->terminate_) { | 94 while (!pool->terminate_) { |
| 72 if (Dart_IsVMFlagSet("trace_thread_pool")) { | 95 if (Dart_IsVMFlagSet("trace_thread_pool")) { |
| 73 printf("Waiting for task\n"); | 96 printf("Waiting for task\n"); |
| 74 } | 97 } |
| 75 Task task = pool->WaitForTask(); | 98 Task task = pool->WaitForTask(); |
| 76 if (pool->terminate_) return NULL; | 99 if (pool->terminate_) return; |
| 77 (*(pool->task_handler_))(task); | 100 (*(pool->task_handler_))(task); |
| 78 } | 101 } |
| 79 return NULL; | |
| 80 }; | 102 }; |
| OLD | NEW |