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