| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium 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 "gin/public/v8_platform.h" | 5 #include "gin/public/v8_platform.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/debug/stack_trace.h" | 10 #include "base/debug/stack_trace.h" |
| 9 #include "base/location.h" | 11 #include "base/location.h" |
| 10 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 11 #include "base/threading/worker_pool.h" | 13 #include "base/task_scheduler/post_task.h" |
| 14 #include "base/task_scheduler/task_scheduler.h" |
| 12 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
| 13 #include "gin/per_isolate_data.h" | 16 #include "gin/per_isolate_data.h" |
| 14 | 17 |
| 15 namespace gin { | 18 namespace gin { |
| 16 | 19 |
| 17 namespace { | 20 namespace { |
| 18 | 21 |
| 22 constexpr base::TaskTraits kBackgroundThreadTaskTraits = { |
| 23 base::MayBlock(), base::TaskPriority::USER_VISIBLE, |
| 24 base::TaskShutdownBehavior::BLOCK_SHUTDOWN}; |
| 25 |
| 19 base::LazyInstance<V8Platform>::Leaky g_v8_platform = LAZY_INSTANCE_INITIALIZER; | 26 base::LazyInstance<V8Platform>::Leaky g_v8_platform = LAZY_INSTANCE_INITIALIZER; |
| 20 | 27 |
| 21 void RunWithLocker(v8::Isolate* isolate, v8::Task* task) { | 28 void RunWithLocker(v8::Isolate* isolate, v8::Task* task) { |
| 22 v8::Locker lock(isolate); | 29 v8::Locker lock(isolate); |
| 23 task->Run(); | 30 task->Run(); |
| 24 } | 31 } |
| 25 | 32 |
| 26 class IdleTaskWithLocker : public v8::IdleTask { | 33 class IdleTaskWithLocker : public v8::IdleTask { |
| 27 public: | 34 public: |
| 28 IdleTaskWithLocker(v8::Isolate* isolate, v8::IdleTask* task) | 35 IdleTaskWithLocker(v8::Isolate* isolate, v8::IdleTask* task) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 51 } // namespace | 58 } // namespace |
| 52 | 59 |
| 53 // static | 60 // static |
| 54 V8Platform* V8Platform::Get() { return g_v8_platform.Pointer(); } | 61 V8Platform* V8Platform::Get() { return g_v8_platform.Pointer(); } |
| 55 | 62 |
| 56 V8Platform::V8Platform() {} | 63 V8Platform::V8Platform() {} |
| 57 | 64 |
| 58 V8Platform::~V8Platform() {} | 65 V8Platform::~V8Platform() {} |
| 59 | 66 |
| 60 size_t V8Platform::NumberOfAvailableBackgroundThreads() { | 67 size_t V8Platform::NumberOfAvailableBackgroundThreads() { |
| 61 // WorkerPool will currently always create additional threads for posted | 68 return std::max(1, base::TaskScheduler::GetInstance() |
| 62 // background tasks, unless there are threads sitting idle (on posix). | 69 ->GetMaxConcurrentTasksWithTraitsDeprecated( |
| 63 // Indicate that V8 should create no more than the number of cores available, | 70 kBackgroundThreadTaskTraits)); |
| 64 // reserving one core for the main thread. | |
| 65 const size_t available_cores = | |
| 66 static_cast<size_t>(base::SysInfo::NumberOfProcessors()); | |
| 67 if (available_cores > 1) { | |
| 68 return available_cores - 1; | |
| 69 } | |
| 70 return 1; | |
| 71 } | 71 } |
| 72 | 72 |
| 73 void V8Platform::CallOnBackgroundThread( | 73 void V8Platform::CallOnBackgroundThread( |
| 74 v8::Task* task, | 74 v8::Task* task, |
| 75 v8::Platform::ExpectedRuntime expected_runtime) { | 75 v8::Platform::ExpectedRuntime expected_runtime) { |
| 76 base::WorkerPool::PostTask( | 76 base::PostTaskWithTraits(FROM_HERE, kBackgroundThreadTaskTraits, |
| 77 FROM_HERE, | 77 base::Bind(&v8::Task::Run, base::Owned(task))); |
| 78 base::Bind(&v8::Task::Run, base::Owned(task)), | |
| 79 expected_runtime == v8::Platform::kLongRunningTask); | |
| 80 } | 78 } |
| 81 | 79 |
| 82 void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) { | 80 void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) { |
| 83 PerIsolateData* data = PerIsolateData::From(isolate); | 81 PerIsolateData* data = PerIsolateData::From(isolate); |
| 84 if (data->access_mode() == IsolateHolder::kUseLocker) { | 82 if (data->access_mode() == IsolateHolder::kUseLocker) { |
| 85 data->task_runner()->PostTask( | 83 data->task_runner()->PostTask( |
| 86 FROM_HERE, base::Bind(RunWithLocker, base::Unretained(isolate), | 84 FROM_HERE, base::Bind(RunWithLocker, base::Unretained(isolate), |
| 87 base::Owned(task))); | 85 base::Owned(task))); |
| 88 } else { | 86 } else { |
| 89 data->task_runner()->PostTask( | 87 data->task_runner()->PostTask( |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 void V8Platform::RemoveTraceStateObserver( | 265 void V8Platform::RemoveTraceStateObserver( |
| 268 v8::Platform::TraceStateObserver* observer) { | 266 v8::Platform::TraceStateObserver* observer) { |
| 269 g_trace_state_dispatcher.Get().RemoveObserver(observer); | 267 g_trace_state_dispatcher.Get().RemoveObserver(observer); |
| 270 } | 268 } |
| 271 | 269 |
| 272 v8::Platform::StackTracePrinter V8Platform::GetStackTracePrinter() { | 270 v8::Platform::StackTracePrinter V8Platform::GetStackTracePrinter() { |
| 273 return PrintStackTrace; | 271 return PrintStackTrace; |
| 274 } | 272 } |
| 275 | 273 |
| 276 } // namespace gin | 274 } // namespace gin |
| OLD | NEW |