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

Side by Side Diff: src/JsEngineInternal.h

Issue 29370568: Issue #4692 - Move responsibility for engine reference from tasks to scheduler
Patch Set: Created Dec. 31, 2016, 10:37 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
« no previous file with comments | « src/JsEngine.cpp ('k') | src/Scheduler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 #if !defined(ADBLOCK_PLUS_JS_ENGINE_INTERNAL_H) 2 #if !defined(ADBLOCK_PLUS_JS_ENGINE_INTERNAL_H)
3 #define ADBLOCK_PLUS_JS_ENGINE_INTERNAL_H 3 #define ADBLOCK_PLUS_JS_ENGINE_INTERNAL_H
4 4
5 #include <AdblockPlus/JsEngine.h> 5 #include <AdblockPlus/JsEngine.h>
6 #include <v8.h> 6 #include <v8.h>
7 #include <array> 7 #include <array>
8 #include "AllocatedArray.h" 8 #include "AllocatedArray.h"
9 #include "Scheduler.h"
9 #include "V8Upgrade.h" 10 #include "V8Upgrade.h"
10 11
11 class PersistentValueArray 12 class PersistentValueArray
12 : public AllocatedArray<V8PersistentNG<v8::Value>> 13 : public AllocatedArray<V8PersistentNG<v8::Value>>
13 { 14 {
14 typedef AllocatedArray<V8PersistentNG<v8::Value>> Base; 15 typedef AllocatedArray<V8PersistentNG<v8::Value>> Base;
15 /** 16 /**
16 * Copy constructor deleted. Allocation is unique to single object. 17 * Copy constructor deleted. Allocation is unique to single object.
17 */ 18 */
18 PersistentValueArray(const PersistentValueArray&); // = delete; 19 PersistentValueArray(const PersistentValueArray&); // = delete;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 const auto length = Base::Size(); 54 const auto length = Base::Size();
54 AllocatedArray<v8::Local<v8::Value>> locals(length); 55 AllocatedArray<v8::Local<v8::Value>> locals(length);
55 for (size_t i = 0; i < length; ++i) 56 for (size_t i = 0; i < length; ++i)
56 { 57 {
57 locals[i] = (*this)[i].Get(isolate); 58 locals[i] = (*this)[i].Get(isolate);
58 } 59 }
59 return std::move(locals); 60 return std::move(locals);
60 } 61 }
61 }; 62 };
62 63
64 struct ImmediateSingleUseThreadType {}; ///< Marker class for scheduling policy
65 extern const ImmediateSingleUseThreadType ImmediateSingleUseThread; ///< Dummy c onstant for scheduling policy
66
63 /** 67 /**
64 * \par Implementation Notes 68 * \par Implementation Notes
65 */ 69 */
66 class JsEngineInternal 70 class JsEngineInternal
67 : public AdblockPlus::JsEngine 71 : public AdblockPlus::JsEngine
68 { 72 {
69 /** 73 /**
70 * Unique context associated with this isolate. 74 * Unique context associated with this isolate.
71 */ 75 */
72 v8::Persistent<v8::Context> context; 76 v8::Persistent<v8::Context> context;
73 77
78 /**
79 * A task within our scheduler.
80 *
81 * TODO: Move this class into the scheduler proper.
82 */
83 class Task
84 {
85 /**
86 * Shared pointer to our engine keeps it in existence
87 * for the duration of the execution of this task.
88 */
89 AdblockPlus::JsEnginePtr engine;
90 /**
91 * The body of the task, containing its main function.
92 * This is the class where tasks are heap-allocated for polymorphism.
93 */
94 std::shared_ptr<TaskFunctionInterface> body;
95
96 public:
97 /**
98 * Constructor makes a new shared pointer to the engine
99 * but copies the task body.
100 */
101 Task(JsEngineInternal* engine, std::shared_ptr<TaskFunctionInterface>&& body )
102 : engine(engine->shared_from_this()), body(body)
103 {}
104
105 /*
106 * Call operator allows conversion to std::function<void()> so that
107 * we don't have to change the scheduler just yet.
108 */
109 void operator()()
110 {
111 if (body)
112 {
113 body->operator()();
114 }
115 }
116 };
117
118 /**
119 * Scheduler for tasks executed under the current engine.
120 */
121 SchedulerT<SingleUseWorker> scheduler;
122
123 /**
124 * Schedule a task, internal version.
125 *
126 * This version relies upon the external version of this function
127 * to allocate a shared pointer to the task.
128 *
129 * @param task
130 * Task to execute. All v8 handles must refer to the present engine.
131 * @param ImmediateSingleUseThreadType
132 * The schedule policy--create a new thread and discard it afterwards.
133 */
134 void ScheduleTaskInternal(std::shared_ptr<TaskFunctionInterface>&& body);
135
74 public: 136 public:
75 JsEngineInternal(const AdblockPlus::ScopedV8IsolatePtr& isolate); 137 JsEngineInternal(const AdblockPlus::ScopedV8IsolatePtr& isolate);
76 138
77 /** 139 /**
78 * Retrieve our persistent v8 context as a local handle. 140 * Retrieve our persistent v8 context as a local handle.
79 */ 141 */
80 v8::Local<v8::Context> GetContextAsLocal() const; 142 v8::Local<v8::Context> GetContextAsLocal() const;
81 143
82 /** 144 /**
83 * Retrieve the global object of our context. 145 * Retrieve the global object of our context.
84 */ 146 */
85 v8::Local<v8::Object> GetGlobalObject(); 147 v8::Local<v8::Object> GetGlobalObject();
86 148
87 /** 149 /**
150 * Schedule a task with these policies:
151 * - timing policy: start execution immediately.
152 * - threading policy: run the task in a single-use thread.
153 *
154 * @param task
155 * Task to execute. All v8 handles must refer to the present engine.
156 * @param ImmediateSingleUseThreadType
157 * The schedule policy--create a new thread and discard it afterwards.
158 */
159 template<class T>
160 void ScheduleTask(T&& task, ImmediateSingleUseThreadType)
161 {
162 ScheduleTaskInternal(std::static_pointer_cast<TaskFunctionInterface>(std::ma ke_shared<T>(task)));
163 }
164
165 /**
166 * Block until there are no more tasks running in the scheduler.
167 *
168 * Note: this function does not prevent new tasks from being scheduled.
169 */
170 void WaitForQuietScheduler();
171
172 /**
88 * Create a JavaScript function that binds to a C++ callback. 173 * Create a JavaScript function that binds to a C++ callback.
89 * 174 *
90 * We save a copy of a pointer to the present instance when we 175 * We save a copy of a pointer to the present instance when we
91 * create the JS function inside the v8 isolate. 176 * create the JS function inside the v8 isolate.
92 * This pointer is accessible through the `v8::Arguments` class, 177 * This pointer is accessible through the `v8::Arguments` class,
93 * which is the argument of the callback function. 178 * which is the argument of the callback function.
94 * The isolate pointer is already available; 179 * The isolate pointer is already available;
95 * the engine pointer is available if its services are needed. 180 * the engine pointer is available if its services are needed.
96 * 181 *
97 * @param callback C++ callback to invoke. 182 * @param callback C++ callback to invoke.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 */ 252 */
168 const v8::HandleScope handleScope; 253 const v8::HandleScope handleScope;
169 const v8::Context::Scope contextScope; 254 const v8::Context::Scope contextScope;
170 public: 255 public:
171 V8ExecutionScope(JsEngineInternal* engine); 256 V8ExecutionScope(JsEngineInternal* engine);
172 }; 257 };
173 258
174 259
175 260
176 #endif 261 #endif
OLDNEW
« no previous file with comments | « src/JsEngine.cpp ('k') | src/Scheduler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld