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