Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(433)

Side by Side Diff: gin/v8_platform.cc

Issue 2478813002: When an Isolate is configured to use lockers, foreground tasks need to lock (Closed)
Patch Set: updates Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gin/per_isolate_data.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/threading/worker_pool.h"
11 #include "base/trace_event/trace_event.h" 11 #include "base/trace_event/trace_event.h"
12 #include "gin/per_isolate_data.h" 12 #include "gin/per_isolate_data.h"
13 13
14 namespace gin { 14 namespace gin {
15 15
16 namespace { 16 namespace {
17 17
18 base::LazyInstance<V8Platform>::Leaky g_v8_platform = LAZY_INSTANCE_INITIALIZER; 18 base::LazyInstance<V8Platform>::Leaky g_v8_platform = LAZY_INSTANCE_INITIALIZER;
19 19
20 void RunWithLocker(v8::Isolate* isolate, v8::Task* task) {
21 v8::Locker lock(isolate);
22 task->Run();
23 }
24
25 class IdleTaskWithLocker : public v8::IdleTask {
26 public:
27 IdleTaskWithLocker(v8::Isolate* isolate, v8::IdleTask* task)
28 : isolate_(isolate), task_(task) {}
29
30 ~IdleTaskWithLocker() override = default;
31
32 // v8::IdleTask implementation.
33 void Run(double deadline_in_seconds) override {
34 v8::Locker lock(isolate_);
35 task_->Run(deadline_in_seconds);
36 }
37
38 private:
39 v8::Isolate* isolate_;
40 std::unique_ptr<v8::IdleTask> task_;
41
42 DISALLOW_COPY_AND_ASSIGN(IdleTaskWithLocker);
43 };
44
20 } // namespace 45 } // namespace
21 46
22 // static 47 // static
23 V8Platform* V8Platform::Get() { return g_v8_platform.Pointer(); } 48 V8Platform* V8Platform::Get() { return g_v8_platform.Pointer(); }
24 49
25 V8Platform::V8Platform() {} 50 V8Platform::V8Platform() {}
26 51
27 V8Platform::~V8Platform() {} 52 V8Platform::~V8Platform() {}
28 53
29 size_t V8Platform::NumberOfAvailableBackgroundThreads() { 54 size_t V8Platform::NumberOfAvailableBackgroundThreads() {
(...skipping 12 matching lines...) Expand all
42 void V8Platform::CallOnBackgroundThread( 67 void V8Platform::CallOnBackgroundThread(
43 v8::Task* task, 68 v8::Task* task,
44 v8::Platform::ExpectedRuntime expected_runtime) { 69 v8::Platform::ExpectedRuntime expected_runtime) {
45 base::WorkerPool::PostTask( 70 base::WorkerPool::PostTask(
46 FROM_HERE, 71 FROM_HERE,
47 base::Bind(&v8::Task::Run, base::Owned(task)), 72 base::Bind(&v8::Task::Run, base::Owned(task)),
48 expected_runtime == v8::Platform::kLongRunningTask); 73 expected_runtime == v8::Platform::kLongRunningTask);
49 } 74 }
50 75
51 void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) { 76 void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) {
52 PerIsolateData::From(isolate)->task_runner()->PostTask( 77 PerIsolateData* data = PerIsolateData::From(isolate);
53 FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task))); 78 if (data->access_mode() == IsolateHolder::kUseLocker) {
79 data->task_runner()->PostTask(
80 FROM_HERE, base::Bind(RunWithLocker, base::Unretained(isolate),
81 base::Owned(task)));
82 } else {
83 data->task_runner()->PostTask(
84 FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task)));
85 }
54 } 86 }
55 87
56 void V8Platform::CallDelayedOnForegroundThread(v8::Isolate* isolate, 88 void V8Platform::CallDelayedOnForegroundThread(v8::Isolate* isolate,
57 v8::Task* task, 89 v8::Task* task,
58 double delay_in_seconds) { 90 double delay_in_seconds) {
59 PerIsolateData::From(isolate)->task_runner()->PostDelayedTask( 91 PerIsolateData* data = PerIsolateData::From(isolate);
60 FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task)), 92 if (data->access_mode() == IsolateHolder::kUseLocker) {
61 base::TimeDelta::FromSecondsD(delay_in_seconds)); 93 data->task_runner()->PostDelayedTask(
94 FROM_HERE,
95 base::Bind(RunWithLocker, base::Unretained(isolate), base::Owned(task)),
96 base::TimeDelta::FromSecondsD(delay_in_seconds));
97 } else {
98 data->task_runner()->PostDelayedTask(
99 FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task)),
100 base::TimeDelta::FromSecondsD(delay_in_seconds));
101 }
62 } 102 }
63 103
64 void V8Platform::CallIdleOnForegroundThread(v8::Isolate* isolate, 104 void V8Platform::CallIdleOnForegroundThread(v8::Isolate* isolate,
65 v8::IdleTask* task) { 105 v8::IdleTask* task) {
66 DCHECK(PerIsolateData::From(isolate)->idle_task_runner()); 106 PerIsolateData* data = PerIsolateData::From(isolate);
67 PerIsolateData::From(isolate)->idle_task_runner()->PostIdleTask(task); 107 DCHECK(data->idle_task_runner());
108 if (data->access_mode() == IsolateHolder::kUseLocker) {
109 data->idle_task_runner()->PostIdleTask(
110 new IdleTaskWithLocker(isolate, task));
111 } else {
112 data->idle_task_runner()->PostIdleTask(task);
113 }
68 } 114 }
69 115
70 bool V8Platform::IdleTasksEnabled(v8::Isolate* isolate) { 116 bool V8Platform::IdleTasksEnabled(v8::Isolate* isolate) {
71 return PerIsolateData::From(isolate)->idle_task_runner() != nullptr; 117 return PerIsolateData::From(isolate)->idle_task_runner() != nullptr;
72 } 118 }
73 119
74 double V8Platform::MonotonicallyIncreasingTime() { 120 double V8Platform::MonotonicallyIncreasingTime() {
75 return base::TimeTicks::Now().ToInternalValue() / 121 return base::TimeTicks::Now().ToInternalValue() /
76 static_cast<double>(base::Time::kMicrosecondsPerSecond); 122 static_cast<double>(base::Time::kMicrosecondsPerSecond);
77 } 123 }
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 v8::Platform::TraceStateObserver* observer) { 251 v8::Platform::TraceStateObserver* observer) {
206 g_trace_state_dispatcher.Get().AddObserver(observer); 252 g_trace_state_dispatcher.Get().AddObserver(observer);
207 } 253 }
208 254
209 void V8Platform::RemoveTraceStateObserver( 255 void V8Platform::RemoveTraceStateObserver(
210 v8::Platform::TraceStateObserver* observer) { 256 v8::Platform::TraceStateObserver* observer) {
211 g_trace_state_dispatcher.Get().RemoveObserver(observer); 257 g_trace_state_dispatcher.Get().RemoveObserver(observer);
212 } 258 }
213 259
214 } // namespace gin 260 } // namespace gin
OLDNEW
« no previous file with comments | « gin/per_isolate_data.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698