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_ = reinterpret_cast<dart::ThreadHandle*>(calloc( | |
68 size_, sizeof(dart::ThreadHandle))); | |
69 for (int i = 0; i < size_; i++) { | |
70 threads_[i] = dart::Thread::Start(&ThreadPool::Main, | |
71 reinterpret_cast<uword>(this)); | |
72 if (threads_[i] == dart::Thread::kInvalidThreadHandle) { | |
73 FATAL("Create and start thread pool thread"); | |
74 } | |
75 } | |
76 } | |
77 | |
78 | |
79 void ThreadPool::Shutdown() { | |
80 terminate_ = true; | |
81 queue_.Shutdown(); | |
82 for (int i = 0; i < size_; i++) { | |
83 dart::Thread::Join(threads_[i]); | |
Ivan Posva
2012/01/20 16:45:55
This will not work, please see comment in thread.h
Søren Gjesse
2012/01/23 13:14:58
See reply in thread.h
| |
84 } | |
85 } | |
86 | |
87 | |
88 void ThreadPool::Main(uword args) { | |
67 if (Dart_IsVMFlagSet("trace_thread_pool")) { | 89 if (Dart_IsVMFlagSet("trace_thread_pool")) { |
68 printf("Thread pool thread started\n"); | 90 printf("Thread pool thread started\n"); |
69 } | 91 } |
70 ThreadPool* pool = reinterpret_cast<ThreadPool*>(args); | 92 ThreadPool* pool = reinterpret_cast<ThreadPool*>(args); |
71 while (!pool->terminate_) { | 93 while (!pool->terminate_) { |
72 if (Dart_IsVMFlagSet("trace_thread_pool")) { | 94 if (Dart_IsVMFlagSet("trace_thread_pool")) { |
73 printf("Waiting for task\n"); | 95 printf("Waiting for task\n"); |
74 } | 96 } |
75 Task task = pool->WaitForTask(); | 97 Task task = pool->WaitForTask(); |
76 if (pool->terminate_) return NULL; | 98 if (pool->terminate_) return; |
77 (*(pool->task_handler_))(task); | 99 (*(pool->task_handler_))(task); |
78 } | 100 } |
79 return NULL; | |
80 }; | 101 }; |
OLD | NEW |