| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/libplatform/default-platform.h" | 5 #include "src/libplatform/default-platform.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <queue> | 8 #include <queue> |
| 9 | 9 |
| 10 #include "src/base/logging.h" | 10 #include "src/base/logging.h" |
| 11 #include "src/base/platform/platform.h" | 11 #include "src/base/platform/platform.h" |
| 12 #include "src/libplatform/worker-thread.h" | 12 #include "src/libplatform/worker-thread.h" |
| 13 | 13 |
| 14 namespace v8 { | 14 namespace v8 { |
| 15 namespace platform { | 15 namespace platform { |
| 16 | 16 |
| 17 | 17 |
| 18 v8::Platform* CreateDefaultPlatform(int thread_pool_size) { | 18 v8::Platform* CreateDefaultPlatform(int thread_pool_size) { |
| 19 DefaultPlatform* platform = new DefaultPlatform(); | 19 DefaultPlatform* platform = new DefaultPlatform(); |
| 20 platform->SetThreadPoolSize(thread_pool_size); | 20 platform->SetThreadPoolSize(thread_pool_size); |
| 21 platform->EnsureInitialized(); | 21 platform->EnsureInitialized(); |
| 22 return platform; | 22 return platform; |
| 23 } | 23 } |
| 24 | 24 |
| 25 | 25 |
| 26 bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate) { |
| 27 return reinterpret_cast<DefaultPlatform*>(platform)->PumpMessageLoop(isolate); |
| 28 } |
| 29 |
| 30 |
| 26 const int DefaultPlatform::kMaxThreadPoolSize = 4; | 31 const int DefaultPlatform::kMaxThreadPoolSize = 4; |
| 27 | 32 |
| 28 | 33 |
| 29 DefaultPlatform::DefaultPlatform() | 34 DefaultPlatform::DefaultPlatform() |
| 30 : initialized_(false), thread_pool_size_(0) {} | 35 : initialized_(false), thread_pool_size_(0) {} |
| 31 | 36 |
| 32 | 37 |
| 33 DefaultPlatform::~DefaultPlatform() { | 38 DefaultPlatform::~DefaultPlatform() { |
| 34 base::LockGuard<base::Mutex> guard(&lock_); | 39 base::LockGuard<base::Mutex> guard(&lock_); |
| 35 queue_.Terminate(); | 40 queue_.Terminate(); |
| 36 if (initialized_) { | 41 if (initialized_) { |
| 37 for (std::vector<WorkerThread*>::iterator i = thread_pool_.begin(); | 42 for (std::vector<WorkerThread*>::iterator i = thread_pool_.begin(); |
| 38 i != thread_pool_.end(); ++i) { | 43 i != thread_pool_.end(); ++i) { |
| 39 delete *i; | 44 delete *i; |
| 40 } | 45 } |
| 41 } | 46 } |
| 47 for (std::map<v8::Isolate*, std::queue<Task*> >::iterator i = |
| 48 main_thread_queue_.begin(); |
| 49 i != main_thread_queue_.end(); ++i) { |
| 50 while (!i->second.empty()) { |
| 51 delete i->second.front(); |
| 52 i->second.pop(); |
| 53 } |
| 54 } |
| 42 } | 55 } |
| 43 | 56 |
| 44 | 57 |
| 45 void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) { | 58 void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) { |
| 46 base::LockGuard<base::Mutex> guard(&lock_); | 59 base::LockGuard<base::Mutex> guard(&lock_); |
| 47 ASSERT(thread_pool_size >= 0); | 60 ASSERT(thread_pool_size >= 0); |
| 48 if (thread_pool_size < 1) | 61 if (thread_pool_size < 1) |
| 49 thread_pool_size = base::OS::NumberOfProcessorsOnline(); | 62 thread_pool_size = base::OS::NumberOfProcessorsOnline(); |
| 50 thread_pool_size_ = | 63 thread_pool_size_ = |
| 51 std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1); | 64 std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1); |
| 52 } | 65 } |
| 53 | 66 |
| 54 | 67 |
| 55 void DefaultPlatform::EnsureInitialized() { | 68 void DefaultPlatform::EnsureInitialized() { |
| 56 base::LockGuard<base::Mutex> guard(&lock_); | 69 base::LockGuard<base::Mutex> guard(&lock_); |
| 57 if (initialized_) return; | 70 if (initialized_) return; |
| 58 initialized_ = true; | 71 initialized_ = true; |
| 59 | 72 |
| 60 for (int i = 0; i < thread_pool_size_; ++i) | 73 for (int i = 0; i < thread_pool_size_; ++i) |
| 61 thread_pool_.push_back(new WorkerThread(&queue_)); | 74 thread_pool_.push_back(new WorkerThread(&queue_)); |
| 62 } | 75 } |
| 63 | 76 |
| 77 |
| 78 bool DefaultPlatform::PumpMessageLoop(v8::Isolate* isolate) { |
| 79 Task* task = NULL; |
| 80 { |
| 81 base::LockGuard<base::Mutex> guard(&lock_); |
| 82 std::map<v8::Isolate*, std::queue<Task*> >::iterator it = |
| 83 main_thread_queue_.find(isolate); |
| 84 if (it == main_thread_queue_.end() || it->second.empty()) { |
| 85 return false; |
| 86 } |
| 87 task = it->second.front(); |
| 88 it->second.pop(); |
| 89 } |
| 90 task->Run(); |
| 91 delete task; |
| 92 return true; |
| 93 } |
| 94 |
| 64 void DefaultPlatform::CallOnBackgroundThread(Task *task, | 95 void DefaultPlatform::CallOnBackgroundThread(Task *task, |
| 65 ExpectedRuntime expected_runtime) { | 96 ExpectedRuntime expected_runtime) { |
| 66 EnsureInitialized(); | 97 EnsureInitialized(); |
| 67 queue_.Append(task); | 98 queue_.Append(task); |
| 68 } | 99 } |
| 69 | 100 |
| 70 | 101 |
| 71 void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) { | 102 void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) { |
| 72 // TODO(jochen): implement. | 103 base::LockGuard<base::Mutex> guard(&lock_); |
| 73 task->Run(); | 104 main_thread_queue_[isolate].push(task); |
| 74 delete task; | |
| 75 } | 105 } |
| 76 | 106 |
| 77 } } // namespace v8::platform | 107 } } // namespace v8::platform |
| OLD | NEW |