| 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 "include/libplatform/libplatform.h" | 10 #include "include/libplatform/libplatform.h" |
| 11 #include "src/base/logging.h" | 11 #include "src/base/logging.h" |
| 12 #include "src/base/platform/platform.h" | 12 #include "src/base/platform/platform.h" |
| 13 #include "src/base/platform/time.h" | 13 #include "src/base/platform/time.h" |
| 14 #include "src/base/sys-info.h" | 14 #include "src/base/sys-info.h" |
| 15 #include "src/libplatform/worker-thread.h" | 15 #include "src/libplatform/worker-thread.h" |
| 16 | 16 |
| 17 namespace v8 { | 17 namespace v8 { |
| 18 namespace platform { | 18 namespace platform { |
| 19 | 19 |
| 20 | 20 v8::Platform* CreateDefaultPlatform(int thread_pool_size, |
| 21 v8::Platform* CreateDefaultPlatform(int thread_pool_size) { | 21 IdleTaskSupport idle_task_support) { |
| 22 DefaultPlatform* platform = new DefaultPlatform(); | 22 DefaultPlatform* platform = new DefaultPlatform(idle_task_support); |
| 23 platform->SetThreadPoolSize(thread_pool_size); | 23 platform->SetThreadPoolSize(thread_pool_size); |
| 24 platform->EnsureInitialized(); | 24 platform->EnsureInitialized(); |
| 25 return platform; | 25 return platform; |
| 26 } | 26 } |
| 27 | 27 |
| 28 | 28 |
| 29 bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate) { | 29 bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate) { |
| 30 return reinterpret_cast<DefaultPlatform*>(platform)->PumpMessageLoop(isolate); | 30 return reinterpret_cast<DefaultPlatform*>(platform)->PumpMessageLoop(isolate); |
| 31 } | 31 } |
| 32 | 32 |
| 33 void RunIdleTasks(v8::Platform* platform, v8::Isolate* isolate, | 33 void RunIdleTasks(v8::Platform* platform, v8::Isolate* isolate, |
| 34 double idle_time_in_seconds) { | 34 double idle_time_in_seconds) { |
| 35 reinterpret_cast<DefaultPlatform*>(platform)->RunIdleTasks( | 35 reinterpret_cast<DefaultPlatform*>(platform)->RunIdleTasks( |
| 36 isolate, idle_time_in_seconds); | 36 isolate, idle_time_in_seconds); |
| 37 } | 37 } |
| 38 | 38 |
| 39 void SetTracingController( | 39 void SetTracingController( |
| 40 v8::Platform* platform, | 40 v8::Platform* platform, |
| 41 v8::platform::tracing::TracingController* tracing_controller) { | 41 v8::platform::tracing::TracingController* tracing_controller) { |
| 42 return reinterpret_cast<DefaultPlatform*>(platform)->SetTracingController( | 42 return reinterpret_cast<DefaultPlatform*>(platform)->SetTracingController( |
| 43 tracing_controller); | 43 tracing_controller); |
| 44 } | 44 } |
| 45 | 45 |
| 46 const int DefaultPlatform::kMaxThreadPoolSize = 8; | 46 const int DefaultPlatform::kMaxThreadPoolSize = 8; |
| 47 | 47 |
| 48 DefaultPlatform::DefaultPlatform() | 48 DefaultPlatform::DefaultPlatform(IdleTaskSupport idle_task_support) |
| 49 : initialized_(false), thread_pool_size_(0) {} | 49 : initialized_(false), |
| 50 thread_pool_size_(0), |
| 51 idle_task_support_(idle_task_support) {} |
| 50 | 52 |
| 51 DefaultPlatform::~DefaultPlatform() { | 53 DefaultPlatform::~DefaultPlatform() { |
| 52 if (tracing_controller_) { | 54 if (tracing_controller_) { |
| 53 tracing_controller_->StopTracing(); | 55 tracing_controller_->StopTracing(); |
| 54 tracing_controller_.reset(); | 56 tracing_controller_.reset(); |
| 55 } | 57 } |
| 56 | 58 |
| 57 base::LockGuard<base::Mutex> guard(&lock_); | 59 base::LockGuard<base::Mutex> guard(&lock_); |
| 58 queue_.Terminate(); | 60 queue_.Terminate(); |
| 59 if (initialized_) { | 61 if (initialized_) { |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 return false; | 160 return false; |
| 159 } | 161 } |
| 160 } | 162 } |
| 161 task->Run(); | 163 task->Run(); |
| 162 delete task; | 164 delete task; |
| 163 return true; | 165 return true; |
| 164 } | 166 } |
| 165 | 167 |
| 166 void DefaultPlatform::RunIdleTasks(v8::Isolate* isolate, | 168 void DefaultPlatform::RunIdleTasks(v8::Isolate* isolate, |
| 167 double idle_time_in_seconds) { | 169 double idle_time_in_seconds) { |
| 170 DCHECK(IdleTaskSupport::kEnabled == idle_task_support_); |
| 168 double deadline_in_seconds = | 171 double deadline_in_seconds = |
| 169 MonotonicallyIncreasingTime() + idle_time_in_seconds; | 172 MonotonicallyIncreasingTime() + idle_time_in_seconds; |
| 170 while (deadline_in_seconds > MonotonicallyIncreasingTime()) { | 173 while (deadline_in_seconds > MonotonicallyIncreasingTime()) { |
| 171 { | 174 { |
| 172 IdleTask* task; | 175 IdleTask* task; |
| 173 { | 176 { |
| 174 base::LockGuard<base::Mutex> guard(&lock_); | 177 base::LockGuard<base::Mutex> guard(&lock_); |
| 175 task = PopTaskInMainThreadIdleQueue(isolate); | 178 task = PopTaskInMainThreadIdleQueue(isolate); |
| 176 } | 179 } |
| 177 if (task == nullptr) return; | 180 if (task == nullptr) return; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 201 double deadline = MonotonicallyIncreasingTime() + delay_in_seconds; | 204 double deadline = MonotonicallyIncreasingTime() + delay_in_seconds; |
| 202 main_thread_delayed_queue_[isolate].push(std::make_pair(deadline, task)); | 205 main_thread_delayed_queue_[isolate].push(std::make_pair(deadline, task)); |
| 203 } | 206 } |
| 204 | 207 |
| 205 void DefaultPlatform::CallIdleOnForegroundThread(Isolate* isolate, | 208 void DefaultPlatform::CallIdleOnForegroundThread(Isolate* isolate, |
| 206 IdleTask* task) { | 209 IdleTask* task) { |
| 207 base::LockGuard<base::Mutex> guard(&lock_); | 210 base::LockGuard<base::Mutex> guard(&lock_); |
| 208 main_thread_idle_queue_[isolate].push(task); | 211 main_thread_idle_queue_[isolate].push(task); |
| 209 } | 212 } |
| 210 | 213 |
| 211 bool DefaultPlatform::IdleTasksEnabled(Isolate* isolate) { return true; } | 214 bool DefaultPlatform::IdleTasksEnabled(Isolate* isolate) { |
| 215 return idle_task_support_ == IdleTaskSupport::kEnabled; |
| 216 } |
| 212 | 217 |
| 213 double DefaultPlatform::MonotonicallyIncreasingTime() { | 218 double DefaultPlatform::MonotonicallyIncreasingTime() { |
| 214 return base::TimeTicks::HighResolutionNow().ToInternalValue() / | 219 return base::TimeTicks::HighResolutionNow().ToInternalValue() / |
| 215 static_cast<double>(base::Time::kMicrosecondsPerSecond); | 220 static_cast<double>(base::Time::kMicrosecondsPerSecond); |
| 216 } | 221 } |
| 217 | 222 |
| 218 uint64_t DefaultPlatform::AddTraceEvent( | 223 uint64_t DefaultPlatform::AddTraceEvent( |
| 219 char phase, const uint8_t* category_enabled_flag, const char* name, | 224 char phase, const uint8_t* category_enabled_flag, const char* name, |
| 220 const char* scope, uint64_t id, uint64_t bind_id, int num_args, | 225 const char* scope, uint64_t id, uint64_t bind_id, int num_args, |
| 221 const char** arg_names, const uint8_t* arg_types, | 226 const char** arg_names, const uint8_t* arg_types, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 tracing_controller_->AddTraceStateObserver(observer); | 273 tracing_controller_->AddTraceStateObserver(observer); |
| 269 } | 274 } |
| 270 | 275 |
| 271 void DefaultPlatform::RemoveTraceStateObserver(TraceStateObserver* observer) { | 276 void DefaultPlatform::RemoveTraceStateObserver(TraceStateObserver* observer) { |
| 272 if (!tracing_controller_) return; | 277 if (!tracing_controller_) return; |
| 273 tracing_controller_->RemoveTraceStateObserver(observer); | 278 tracing_controller_->RemoveTraceStateObserver(observer); |
| 274 } | 279 } |
| 275 | 280 |
| 276 } // namespace platform | 281 } // namespace platform |
| 277 } // namespace v8 | 282 } // namespace v8 |
| OLD | NEW |