Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/JsEngine.cpp

Issue 29367507: Issue #3595 - Add an actual scheduler; use joined threads for file system
Patch Set: Created Dec. 14, 2016, 5:38 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <AdblockPlus.h> 18 #include <AdblockPlus.h>
19 #include "GlobalJsObject.h" 19 #include "GlobalJsObject.h"
20 #include "JsContext.h" 20 #include "JsContext.h"
21 #include "JsError.h" 21 #include "JsError.h"
22 #include "Scheduler.h"
22 #include "Utils.h" 23 #include "Utils.h"
23 24
25 const AdblockPlus::ImmediateSingleUseThreadType AdblockPlus::ImmediateSingleUseT hread = {};
26
27 class AdblockPlus::JsEngine::SchedulerImpl
28 : public SchedulerT<SingleUseWorker>
29 {
30 public:
31 SchedulerImpl() {} // = default;
sergei 2017/01/20 13:08:13 We don't need this line.
32 };
33
24 namespace 34 namespace
25 { 35 {
26 v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, 36 v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate,
27 const std::string& source, const std::string& filename) 37 const std::string& source, const std::string& filename)
28 { 38 {
29 using AdblockPlus::Utils::ToV8String; 39 using AdblockPlus::Utils::ToV8String;
30 const v8::Handle<v8::String> v8Source = ToV8String(isolate, source); 40 const v8::Handle<v8::String> v8Source = ToV8String(isolate, source);
31 if (filename.length()) 41 if (filename.length())
32 { 42 {
33 const v8::Handle<v8::String> v8Filename = ToV8String(isolate, filename); 43 const v8::Handle<v8::String> v8Filename = ToV8String(isolate, filename);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 isolate = v8::Isolate::New(); 80 isolate = v8::Isolate::New();
71 } 81 }
72 82
73 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() 83 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate()
74 { 84 {
75 isolate->Dispose(); 85 isolate->Dispose();
76 isolate = nullptr; 86 isolate = nullptr;
77 } 87 }
78 88
79 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate) 89 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate)
80 : isolate(isolate) 90 : isolate(isolate),
81 { 91 scheduler(new SchedulerImpl()) // use std::make_unique after we upgrade out of VS2012
sergei 2017/01/20 13:08:13 We don't need this comment about std::make_unique
Eric 2017/03/30 17:16:58 Is that because we're planning never to upgrade fr
sergei 2017/04/03 15:35:31 Maybe, though I think that std::make_shared are mu
82 } 92 {}
83 93
84 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons t ScopedV8IsolatePtr& isolate) 94 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons t ScopedV8IsolatePtr& isolate)
85 { 95 {
86 JsEnginePtr result(new JsEngine(isolate)); 96 JsEnginePtr result(new JsEngine(isolate));
87 97
88 const v8::Locker locker(result->GetIsolate()); 98 const v8::Locker locker(result->GetIsolate());
89 const v8::Isolate::Scope isolateScope(result->GetIsolate()); 99 const v8::Isolate::Scope isolateScope(result->GetIsolate());
90 const v8::HandleScope handleScope(result->GetIsolate()); 100 const v8::HandleScope handleScope(result->GetIsolate());
91 101
92 result->context.reset(new v8::Persistent<v8::Context>(result->GetIsolate(), 102 result->context.reset(new v8::Persistent<v8::Context>(result->GetIsolate(),
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 255
246 256
247 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, 257 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name,
248 AdblockPlus::JsValuePtr value) 258 AdblockPlus::JsValuePtr value)
249 { 259 {
250 auto global = GetGlobalObject(); 260 auto global = GetGlobalObject();
251 if (!global) 261 if (!global)
252 throw std::runtime_error("Global object cannot be null"); 262 throw std::runtime_error("Global object cannot be null");
253 global->SetProperty(name, value); 263 global->SetProperty(name, value);
254 } 264 }
265
266 void AdblockPlus::JsEngine::Schedule(std::function<void()> task,
267 AdblockPlus::ImmediateSingleUseThreadType)
268 {
269 // The present version of the scheduler only does immediate and single-use
270 // It does not, however, detach threads like the legacy thread behavior did.
271 scheduler->Run(task);
272 }
273
274 void AdblockPlus::JsEngine::WaitForQuietScheduler()
275 {
276 scheduler->JoinAll();
277 }
OLDNEW

Powered by Google App Engine
This is Rietveld