| 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 "bin/thread_pool.h" | |
| 6 | |
| 7 #include "bin/thread.h" | |
| 8 | |
| 9 | |
| 10 bool ThreadPool::InsertTask(Task task) { | |
| 11 TaskQueueEntry* entry = new TaskQueueEntry(task); | |
| 12 MonitorLocker locker(&monitor_); | |
| 13 if (terminate_) return false; | |
| 14 if (head_ == NULL) { | |
| 15 head_ = entry; | |
| 16 tail_ = entry; | |
| 17 locker.Notify(); | |
| 18 } else { | |
| 19 tail_->set_next(entry); | |
| 20 tail_ = entry; | |
| 21 } | |
| 22 return true; | |
| 23 } | |
| 24 | |
| 25 | |
| 26 ThreadPool::TaskQueueEntry* ThreadPool::WaitForTask() { | |
| 27 MonitorLocker locker(&monitor_); | |
| 28 if (terminate_ && (drain_flag_ == kDoNotDrain || head_ == NULL)) { | |
| 29 return NULL; | |
| 30 } | |
| 31 TaskQueueEntry* result = head_; | |
| 32 while (result == NULL) { | |
| 33 locker.Wait(); | |
| 34 if (terminate_ && (drain_flag_ == kDoNotDrain || head_ == NULL)) { | |
| 35 return NULL; | |
| 36 } | |
| 37 result = head_; | |
| 38 } | |
| 39 head_ = result->next(); | |
| 40 ASSERT(head_ != NULL || tail_ == result); | |
| 41 return result; | |
| 42 } | |
| 43 | |
| 44 | |
| 45 void ThreadPool::Start() { | |
| 46 MonitorLocker locker(&monitor_); | |
| 47 terminate_ = false; | |
| 48 for (int i = 0; i < initial_number_of_threads_; i++) { | |
| 49 int result = dart::Thread::Start(&ThreadPool::Main, | |
| 50 reinterpret_cast<uword>(this)); | |
| 51 if (result != 0) { | |
| 52 FATAL1("Failed to start thread pool thread %d", result); | |
| 53 } | |
| 54 number_of_threads_++; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 | |
| 59 void ThreadPool::Shutdown(DrainFlag drain_flag) { | |
| 60 MonitorLocker locker(&monitor_); | |
| 61 terminate_ = true; | |
| 62 drain_flag_ = drain_flag; | |
| 63 locker.NotifyAll(); | |
| 64 int shutdown_count = 0; | |
| 65 while (number_of_threads_ > 0 && shutdown_count < 10) { | |
| 66 locker.Wait(1000); | |
| 67 shutdown_count++; | |
| 68 if (number_of_threads_ > 0) { | |
| 69 fprintf(stderr, | |
| 70 "Waiting for thread pool termination, %d running threads\n", | |
| 71 number_of_threads_); | |
| 72 } | |
| 73 } | |
| 74 if (number_of_threads_ > 0) { | |
| 75 fprintf(stderr, | |
| 76 "Failed thread pool termination, still %d running threads\n", | |
| 77 number_of_threads_); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 | |
| 82 void ThreadPool::ThreadTerminated() { | |
| 83 MonitorLocker locker(&monitor_); | |
| 84 number_of_threads_--; | |
| 85 locker.Notify(); | |
| 86 } | |
| 87 | |
| 88 | |
| 89 void ThreadPool::Main(uword args) { | |
| 90 if (Dart_IsVMFlagSet("trace_thread_pool")) { | |
| 91 printf("Thread pool thread started\n"); | |
| 92 } | |
| 93 ThreadPool* pool = reinterpret_cast<ThreadPool*>(args); | |
| 94 while (true) { | |
| 95 if (Dart_IsVMFlagSet("trace_thread_pool")) { | |
| 96 printf("Waiting for task\n"); | |
| 97 } | |
| 98 TaskQueueEntry* task = pool->WaitForTask(); | |
| 99 if (task == NULL) break; | |
| 100 (*(pool->task_handler_))(task->task()); | |
| 101 } | |
| 102 pool->ThreadTerminated(); | |
| 103 }; | |
| OLD | NEW |