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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/sys_info.h" | 9 #include "base/sys_info.h" |
10 #include "base/task_scheduler/post_task.h" | 10 #include "base/threading/worker_pool.h" |
11 #include "base/task_scheduler/task_scheduler.h" | |
12 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
13 #include "gin/per_isolate_data.h" | 12 #include "gin/per_isolate_data.h" |
14 | 13 |
15 namespace gin { | 14 namespace gin { |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 base::LazyInstance<V8Platform>::Leaky g_v8_platform = LAZY_INSTANCE_INITIALIZER; | 18 base::LazyInstance<V8Platform>::Leaky g_v8_platform = LAZY_INSTANCE_INITIALIZER; |
20 | 19 |
21 void RunWithLocker(v8::Isolate* isolate, v8::Task* task) { | 20 void RunWithLocker(v8::Isolate* isolate, v8::Task* task) { |
(...skipping 14 matching lines...) Expand all Loading... |
36 task_->Run(deadline_in_seconds); | 35 task_->Run(deadline_in_seconds); |
37 } | 36 } |
38 | 37 |
39 private: | 38 private: |
40 v8::Isolate* isolate_; | 39 v8::Isolate* isolate_; |
41 std::unique_ptr<v8::IdleTask> task_; | 40 std::unique_ptr<v8::IdleTask> task_; |
42 | 41 |
43 DISALLOW_COPY_AND_ASSIGN(IdleTaskWithLocker); | 42 DISALLOW_COPY_AND_ASSIGN(IdleTaskWithLocker); |
44 }; | 43 }; |
45 | 44 |
46 base::TaskTraits GetBackgroundThreadTaskTraits() { | |
47 return base::TaskTraits().WithShutdownBehavior( | |
48 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN); | |
49 } | |
50 | |
51 } // namespace | 45 } // namespace |
52 | 46 |
53 // static | 47 // static |
54 V8Platform* V8Platform::Get() { return g_v8_platform.Pointer(); } | 48 V8Platform* V8Platform::Get() { return g_v8_platform.Pointer(); } |
55 | 49 |
56 V8Platform::V8Platform() {} | 50 V8Platform::V8Platform() {} |
57 | 51 |
58 V8Platform::~V8Platform() {} | 52 V8Platform::~V8Platform() {} |
59 | 53 |
60 size_t V8Platform::NumberOfAvailableBackgroundThreads() { | 54 size_t V8Platform::NumberOfAvailableBackgroundThreads() { |
61 return base::TaskScheduler::GetInstance() | 55 // WorkerPool will currently always create additional threads for posted |
62 ->GetMaxConcurrentTasksWithTraitsDeprecated( | 56 // background tasks, unless there are threads sitting idle (on posix). |
63 GetBackgroundThreadTaskTraits()); | 57 // Indicate that V8 should create no more than the number of cores available, |
| 58 // reserving one core for the main thread. |
| 59 const size_t available_cores = |
| 60 static_cast<size_t>(base::SysInfo::NumberOfProcessors()); |
| 61 if (available_cores > 1) { |
| 62 return available_cores - 1; |
| 63 } |
| 64 return 1; |
64 } | 65 } |
65 | 66 |
66 void V8Platform::CallOnBackgroundThread( | 67 void V8Platform::CallOnBackgroundThread( |
67 v8::Task* task, | 68 v8::Task* task, |
68 v8::Platform::ExpectedRuntime expected_runtime) { | 69 v8::Platform::ExpectedRuntime expected_runtime) { |
69 base::PostTaskWithTraits(FROM_HERE, GetBackgroundThreadTaskTraits(), | 70 base::WorkerPool::PostTask( |
70 base::Bind(&v8::Task::Run, base::Owned(task))); | 71 FROM_HERE, |
| 72 base::Bind(&v8::Task::Run, base::Owned(task)), |
| 73 expected_runtime == v8::Platform::kLongRunningTask); |
71 } | 74 } |
72 | 75 |
73 void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) { | 76 void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) { |
74 PerIsolateData* data = PerIsolateData::From(isolate); | 77 PerIsolateData* data = PerIsolateData::From(isolate); |
75 if (data->access_mode() == IsolateHolder::kUseLocker) { | 78 if (data->access_mode() == IsolateHolder::kUseLocker) { |
76 data->task_runner()->PostTask( | 79 data->task_runner()->PostTask( |
77 FROM_HERE, base::Bind(RunWithLocker, base::Unretained(isolate), | 80 FROM_HERE, base::Bind(RunWithLocker, base::Unretained(isolate), |
78 base::Owned(task))); | 81 base::Owned(task))); |
79 } else { | 82 } else { |
80 data->task_runner()->PostTask( | 83 data->task_runner()->PostTask( |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 v8::Platform::TraceStateObserver* observer) { | 257 v8::Platform::TraceStateObserver* observer) { |
255 g_trace_state_dispatcher.Get().AddObserver(observer); | 258 g_trace_state_dispatcher.Get().AddObserver(observer); |
256 } | 259 } |
257 | 260 |
258 void V8Platform::RemoveTraceStateObserver( | 261 void V8Platform::RemoveTraceStateObserver( |
259 v8::Platform::TraceStateObserver* observer) { | 262 v8::Platform::TraceStateObserver* observer) { |
260 g_trace_state_dispatcher.Get().RemoveObserver(observer); | 263 g_trace_state_dispatcher.Get().RemoveObserver(observer); |
261 } | 264 } |
262 | 265 |
263 } // namespace gin | 266 } // namespace gin |
OLD | NEW |