| 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 "default-platform.h" | 5 #include "default-platform.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <queue> | 8 #include <queue> |
| 9 | 9 |
| 10 // TODO(jochen): We should have our own version of checks.h. | 10 // TODO(jochen): We should have our own version of checks.h. |
| 11 #include "../checks.h" | 11 #include "../checks.h" |
| 12 // TODO(jochen): Why is cpu.h not in platform/? | 12 #include "../platform.h" |
| 13 #include "../cpu.h" | |
| 14 #include "worker-thread.h" | 13 #include "worker-thread.h" |
| 15 | 14 |
| 16 namespace v8 { | 15 namespace v8 { |
| 17 namespace internal { | 16 namespace internal { |
| 18 | 17 |
| 19 | 18 |
| 20 const int DefaultPlatform::kMaxThreadPoolSize = 4; | 19 const int DefaultPlatform::kMaxThreadPoolSize = 4; |
| 21 | 20 |
| 22 | 21 |
| 23 DefaultPlatform::DefaultPlatform() | 22 DefaultPlatform::DefaultPlatform() |
| 24 : initialized_(false), thread_pool_size_(0) {} | 23 : initialized_(false), thread_pool_size_(0) {} |
| 25 | 24 |
| 26 | 25 |
| 27 DefaultPlatform::~DefaultPlatform() { | 26 DefaultPlatform::~DefaultPlatform() { |
| 28 LockGuard<Mutex> guard(&lock_); | 27 LockGuard<Mutex> guard(&lock_); |
| 29 queue_.Terminate(); | 28 queue_.Terminate(); |
| 30 if (initialized_) { | 29 if (initialized_) { |
| 31 for (std::vector<WorkerThread*>::iterator i = thread_pool_.begin(); | 30 for (std::vector<WorkerThread*>::iterator i = thread_pool_.begin(); |
| 32 i != thread_pool_.end(); ++i) { | 31 i != thread_pool_.end(); ++i) { |
| 33 delete *i; | 32 delete *i; |
| 34 } | 33 } |
| 35 } | 34 } |
| 36 } | 35 } |
| 37 | 36 |
| 38 | 37 |
| 39 void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) { | 38 void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) { |
| 40 LockGuard<Mutex> guard(&lock_); | 39 LockGuard<Mutex> guard(&lock_); |
| 41 ASSERT(thread_pool_size >= 0); | 40 ASSERT(thread_pool_size >= 0); |
| 42 if (thread_pool_size < 1) | 41 if (thread_pool_size < 1) |
| 43 thread_pool_size = CPU::NumberOfProcessorsOnline(); | 42 thread_pool_size = OS::NumberOfProcessorsOnline(); |
| 44 thread_pool_size_ = | 43 thread_pool_size_ = |
| 45 std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1); | 44 std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1); |
| 46 } | 45 } |
| 47 | 46 |
| 48 | 47 |
| 49 void DefaultPlatform::EnsureInitialized() { | 48 void DefaultPlatform::EnsureInitialized() { |
| 50 LockGuard<Mutex> guard(&lock_); | 49 LockGuard<Mutex> guard(&lock_); |
| 51 if (initialized_) return; | 50 if (initialized_) return; |
| 52 initialized_ = true; | 51 initialized_ = true; |
| 53 | 52 |
| 54 for (int i = 0; i < thread_pool_size_; ++i) | 53 for (int i = 0; i < thread_pool_size_; ++i) |
| 55 thread_pool_.push_back(new WorkerThread(&queue_)); | 54 thread_pool_.push_back(new WorkerThread(&queue_)); |
| 56 } | 55 } |
| 57 | 56 |
| 58 void DefaultPlatform::CallOnBackgroundThread(Task *task, | 57 void DefaultPlatform::CallOnBackgroundThread(Task *task, |
| 59 ExpectedRuntime expected_runtime) { | 58 ExpectedRuntime expected_runtime) { |
| 60 EnsureInitialized(); | 59 EnsureInitialized(); |
| 61 queue_.Append(task); | 60 queue_.Append(task); |
| 62 } | 61 } |
| 63 | 62 |
| 64 | 63 |
| 65 void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) { | 64 void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) { |
| 66 // TODO(jochen): implement. | 65 // TODO(jochen): implement. |
| 67 task->Run(); | 66 task->Run(); |
| 68 delete task; | 67 delete task; |
| 69 } | 68 } |
| 70 | 69 |
| 71 } } // namespace v8::internal | 70 } } // namespace v8::internal |
| OLD | NEW |