OLD | NEW |
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 "Scheduler.h" |
10 #include "V8Upgrade.h" | 10 #include "V8Upgrade.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 */ | 69 */ |
70 class JsEngineInternal | 70 class JsEngineInternal |
71 : public AdblockPlus::JsEngine | 71 : public AdblockPlus::JsEngine |
72 { | 72 { |
73 /** | 73 /** |
74 * Unique context associated with this isolate. | 74 * Unique context associated with this isolate. |
75 */ | 75 */ |
76 v8::Persistent<v8::Context> context; | 76 v8::Persistent<v8::Context> context; |
77 | 77 |
78 /** | 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. | 79 * Scheduler for tasks executed under the current engine. |
120 */ | 80 */ |
121 SchedulerT<SingleUseWorker> scheduler; | 81 SchedulerT<SingleUseWorker, JsEngine> scheduler; |
122 | 82 |
123 /** | 83 /** |
124 * Schedule a task, internal version. | 84 * Schedule a task, internal version. |
125 * | 85 * |
126 * This version relies upon the external version of this function | 86 * This version relies upon the external version of this function |
127 * to allocate a shared pointer to the task. | 87 * to allocate a shared pointer to the task. |
128 * | 88 * |
129 * @param task | 89 * @param task |
130 * Task to execute. All v8 handles must refer to the present engine. | 90 * Task to execute. All v8 handles must refer to the present engine. |
131 * @param ImmediateSingleUseThreadType | 91 * @param ImmediateSingleUseThreadType |
(...skipping 20 matching lines...) Expand all Loading... |
152 * - threading policy: run the task in a single-use thread. | 112 * - threading policy: run the task in a single-use thread. |
153 * | 113 * |
154 * @param task | 114 * @param task |
155 * Task to execute. All v8 handles must refer to the present engine. | 115 * Task to execute. All v8 handles must refer to the present engine. |
156 * @param ImmediateSingleUseThreadType | 116 * @param ImmediateSingleUseThreadType |
157 * The schedule policy--create a new thread and discard it afterwards. | 117 * The schedule policy--create a new thread and discard it afterwards. |
158 */ | 118 */ |
159 template<class T> | 119 template<class T> |
160 void ScheduleTask(T&& task, ImmediateSingleUseThreadType) | 120 void ScheduleTask(T&& task, ImmediateSingleUseThreadType) |
161 { | 121 { |
162 ScheduleTaskInternal(std::static_pointer_cast<TaskFunctionInterface>(std::ma
ke_shared<T>(task))); | 122 ScheduleTaskInternal(std::static_pointer_cast<TaskFunctionInterface>(std::ma
ke_shared<T>(std::forward<T>(task)))); |
| 123 } |
| 124 |
| 125 /* |
| 126 * Note: This version shouldn't be necessary; it's a workaround for a defect i
n Visual Studio. |
| 127 * The problem is that make_shared insists on using a copy constructor even wh
en a move constructor is available. |
| 128 * Tested in both toolset 12.0 (VS 2012) and 14.1 (VS 2017). |
| 129 * The workaround, used here, is to call make_shared in the caller. |
| 130 */ |
| 131 /** |
| 132 * Schedule a task with these policies: |
| 133 * - timing policy: start execution immediately. |
| 134 * - threading policy: run the task in a single-use thread. |
| 135 * |
| 136 * @param task |
| 137 * Task to execute. All v8 handles must refer to the present engine. |
| 138 * @param ImmediateSingleUseThreadType |
| 139 * The schedule policy--create a new thread and discard it afterwards. |
| 140 */ |
| 141 template<class T> |
| 142 void ScheduleTask(std::shared_ptr<T>&& task, ImmediateSingleUseThreadType) |
| 143 { |
| 144 ScheduleTaskInternal(std::static_pointer_cast<TaskFunctionInterface>(std::fo
rward<std::shared_ptr<T>>(task))); |
163 } | 145 } |
164 | 146 |
165 /** | 147 /** |
166 * Block until there are no more tasks running in the scheduler. | 148 * Block until there are no more tasks running in the scheduler. |
167 * | 149 * |
168 * Note: this function does not prevent new tasks from being scheduled. | 150 * Note: this function does not prevent new tasks from being scheduled. |
169 */ | 151 */ |
170 void WaitForQuietScheduler(); | 152 void WaitForQuietScheduler(); |
171 | 153 |
172 /** | 154 /** |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 */ | 234 */ |
253 const v8::HandleScope handleScope; | 235 const v8::HandleScope handleScope; |
254 const v8::Context::Scope contextScope; | 236 const v8::Context::Scope contextScope; |
255 public: | 237 public: |
256 V8ExecutionScope(JsEngineInternal* engine); | 238 V8ExecutionScope(JsEngineInternal* engine); |
257 }; | 239 }; |
258 | 240 |
259 | 241 |
260 | 242 |
261 #endif | 243 #endif |
OLD | NEW |