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

Unified Diff: src/Scheduler.h

Issue 29367003: Issue #4711 - Rewrite legacy thread facilities
Patch Set: Created Dec. 7, 2016, 4:44 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/Scheduler.h
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/Scheduler.h
@@ -0,0 +1,78 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if !defined(ADBLOCKPLUS_SCHEDULER_H)
+#define ADBLOCKPLUS_SCHEDULER_H
+
+#include <memory>
+#include <functional>
+
+namespace AdblockPlus
+{
+ /*
+ * Adapter class for heap allocated function objects
+ *
+ * The legacy behavior of the original threading regime combined task life
+ * cycle and execution. Previously, tasks were allocated by the user and
+ * deleted at the end of execution internally. This adapter class allows
+ * separation of these concerns. It allows the legacy allocation behavior
+ * without burdening the new scheduler with any concern over task life cycle.
+ */
+ template<class T>
+ class HeapFunction
+ {
+ std::shared_ptr<T> body;
+
+ public:
+ HeapFunction(std::shared_ptr<T> body)
+ :body(body)
+ {}
+
+ void operator()()
+ {
+ if (body)
+ {
+ body->operator()();
+ }
+ }
+ };
+
+ /*
+ * Utility function uses type inference to simplify expressions at point of use.
+ */
+ template<class T>
+ HeapFunction<T> MakeHeapFunction(std::shared_ptr<T> body)
sergei 2017/01/16 14:50:13 It seems it can be achieved in a simpler way, e.g.
Eric 2017/01/16 15:20:16 Later on, the type HeapFunction gets used explicit
hub 2017/05/12 14:45:39 If this is needed for something we want to remove,
+ {
+ return HeapFunction<T>(body);
+ }
+
+ /*
+ * Scheduler for the execution of tasks.
+ *
+ * The present version is nothing more than a rewrite of the legacy behavior,
+ * which was to create a new thread for each task.
+ */
+ class Scheduler
+ {
+ public:
+ /*
+ * Execute a task immediately in a thread created for this task only
+ */
+ static void StartImmediatelyInSingleUseThread(std::function<void()> task);
sergei 2017/01/16 14:50:13 It would be better to use const reference here.
Eric 2017/01/16 15:20:16 It's a transient function. We can change it, but i
hub 2017/05/12 14:45:39 Here we could just take a std::unique_ptr<T>&& as
+ };
+}
+#endif

Powered by Google App Engine
This is Rietveld