Index: src/JsEngineInternal.h |
=================================================================== |
--- a/src/JsEngineInternal.h |
+++ b/src/JsEngineInternal.h |
@@ -76,49 +76,9 @@ |
v8::Persistent<v8::Context> context; |
/** |
- * A task within our scheduler. |
- * |
- * TODO: Move this class into the scheduler proper. |
- */ |
- class Task |
- { |
- /** |
- * Shared pointer to our engine keeps it in existence |
- * for the duration of the execution of this task. |
- */ |
- AdblockPlus::JsEnginePtr engine; |
- /** |
- * The body of the task, containing its main function. |
- * This is the class where tasks are heap-allocated for polymorphism. |
- */ |
- std::shared_ptr<TaskFunctionInterface> body; |
- |
- public: |
- /** |
- * Constructor makes a new shared pointer to the engine |
- * but copies the task body. |
- */ |
- Task(JsEngineInternal* engine, std::shared_ptr<TaskFunctionInterface>&& body) |
- : engine(engine->shared_from_this()), body(body) |
- {} |
- |
- /* |
- * Call operator allows conversion to std::function<void()> so that |
- * we don't have to change the scheduler just yet. |
- */ |
- void operator()() |
- { |
- if (body) |
- { |
- body->operator()(); |
- } |
- } |
- }; |
- |
- /** |
* Scheduler for tasks executed under the current engine. |
*/ |
- SchedulerT<SingleUseWorker> scheduler; |
+ SchedulerT<SingleUseWorker, JsEngine> scheduler; |
/** |
* Schedule a task, internal version. |
@@ -159,7 +119,29 @@ |
template<class T> |
void ScheduleTask(T&& task, ImmediateSingleUseThreadType) |
{ |
- ScheduleTaskInternal(std::static_pointer_cast<TaskFunctionInterface>(std::make_shared<T>(task))); |
+ ScheduleTaskInternal(std::static_pointer_cast<TaskFunctionInterface>(std::make_shared<T>(std::forward<T>(task)))); |
+ } |
+ |
+ /* |
+ * Note: This version shouldn't be necessary; it's a workaround for a defect in Visual Studio. |
+ * The problem is that make_shared insists on using a copy constructor even when a move constructor is available. |
+ * Tested in both toolset 12.0 (VS 2012) and 14.1 (VS 2017). |
+ * The workaround, used here, is to call make_shared in the caller. |
+ */ |
+ /** |
+ * Schedule a task with these policies: |
+ * - timing policy: start execution immediately. |
+ * - threading policy: run the task in a single-use thread. |
+ * |
+ * @param task |
+ * Task to execute. All v8 handles must refer to the present engine. |
+ * @param ImmediateSingleUseThreadType |
+ * The schedule policy--create a new thread and discard it afterwards. |
+ */ |
+ template<class T> |
+ void ScheduleTask(std::shared_ptr<T>&& task, ImmediateSingleUseThreadType) |
+ { |
+ ScheduleTaskInternal(std::static_pointer_cast<TaskFunctionInterface>(std::forward<std::shared_ptr<T>>(task))); |
} |
/** |