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
3 // found in the LICENSE file.
5 #include "src/libplatform/default-platform.h"
10 #include "src/base/logging.h"
11 #include "src/base/platform/platform.h"
12 #include "src/base/platform/time.h"
13 #include "src/base/sys-info.h"
14 #include "src/libplatform/worker-thread.h"
20 v8::Platform* CreateDefaultPlatform(int thread_pool_size) {
21 DefaultPlatform* platform = new DefaultPlatform();
22 platform->SetThreadPoolSize(thread_pool_size);
23 platform->EnsureInitialized();
28 bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate) {
29 return reinterpret_cast<DefaultPlatform*>(platform)->PumpMessageLoop(isolate);
33 const int DefaultPlatform::kMaxThreadPoolSize = 4;
36 DefaultPlatform::DefaultPlatform()
37 : initialized_(false), thread_pool_size_(0) {}
40 DefaultPlatform::~DefaultPlatform() {
41 base::LockGuard<base::Mutex> guard(&lock_);
44 for (auto i = thread_pool_.begin(); i != thread_pool_.end(); ++i) {
48 for (auto i = main_thread_queue_.begin(); i != main_thread_queue_.end();
50 while (!i->second.empty()) {
51 delete i->second.front();
55 for (auto i = main_thread_delayed_queue_.begin();
56 i != main_thread_delayed_queue_.end(); ++i) {
57 while (!i->second.empty()) {
58 delete i->second.top().second;
65 void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) {
66 base::LockGuard<base::Mutex> guard(&lock_);
67 DCHECK(thread_pool_size >= 0);
68 if (thread_pool_size < 1) {
69 thread_pool_size = base::SysInfo::NumberOfProcessors();
72 std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1);
76 void DefaultPlatform::EnsureInitialized() {
77 base::LockGuard<base::Mutex> guard(&lock_);
78 if (initialized_) return;
81 for (int i = 0; i < thread_pool_size_; ++i)
82 thread_pool_.push_back(new WorkerThread(&queue_));
86 Task* DefaultPlatform::PopTaskInMainThreadQueue(v8::Isolate* isolate) {
87 auto it = main_thread_queue_.find(isolate);
88 if (it == main_thread_queue_.end() || it->second.empty()) {
91 Task* task = it->second.front();
97 Task* DefaultPlatform::PopTaskInMainThreadDelayedQueue(v8::Isolate* isolate) {
98 auto it = main_thread_delayed_queue_.find(isolate);
99 if (it == main_thread_delayed_queue_.end() || it->second.empty()) {
102 double now = MonotonicallyIncreasingTime();
103 std::pair<double, Task*> deadline_and_task = it->second.top();
104 if (deadline_and_task.first > now) {
108 return deadline_and_task.second;
112 bool DefaultPlatform::PumpMessageLoop(v8::Isolate* isolate) {
115 base::LockGuard<base::Mutex> guard(&lock_);
117 // Move delayed tasks that hit their deadline to the main queue.
118 task = PopTaskInMainThreadDelayedQueue(isolate);
119 while (task != NULL) {
120 main_thread_queue_[isolate].push(task);
121 task = PopTaskInMainThreadDelayedQueue(isolate);
124 task = PopTaskInMainThreadQueue(isolate);
136 void DefaultPlatform::CallOnBackgroundThread(Task *task,
137 ExpectedRuntime expected_runtime) {
143 void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) {
144 base::LockGuard<base::Mutex> guard(&lock_);
145 main_thread_queue_[isolate].push(task);
149 void DefaultPlatform::CallDelayedOnForegroundThread(Isolate* isolate,
151 double delay_in_seconds) {
152 base::LockGuard<base::Mutex> guard(&lock_);
153 double deadline = MonotonicallyIncreasingTime() + delay_in_seconds;
154 main_thread_delayed_queue_[isolate].push(std::make_pair(deadline, task));
158 void DefaultPlatform::CallIdleOnForegroundThread(Isolate* isolate,
164 bool DefaultPlatform::IdleTasksEnabled(Isolate* isolate) { return false; }
167 double DefaultPlatform::MonotonicallyIncreasingTime() {
168 return base::TimeTicks::HighResolutionNow().ToInternalValue() /
169 static_cast<double>(base::Time::kMicrosecondsPerSecond);
171 } } // namespace v8::platform