Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2016 Eyeo GmbH | |
4 * | |
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 | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
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/>. | |
16 */ | |
17 | |
18 #if !defined(ADBLOCKPLUS_SCHEDULER_H) | |
19 #define ADBLOCKPLUS_SCHEDULER_H | |
20 | |
21 #include <memory> | |
22 #include <functional> | |
23 | |
24 namespace AdblockPlus | |
25 { | |
26 /* | |
27 * Adapter class for heap allocated function objects | |
28 * | |
29 * The legacy behavior of the original threading regime combined task life | |
30 * cycle and execution. Previously, tasks were allocated by the user and | |
31 * deleted at the end of execution internally. This adapter class allows | |
32 * separation of these concerns. It allows the legacy allocation behavior | |
33 * without burdening the new scheduler with any concern over task life cycle. | |
34 */ | |
35 template<class T> | |
36 class HeapFunction | |
37 { | |
38 std::shared_ptr<T> body; | |
39 | |
40 public: | |
41 HeapFunction(std::shared_ptr<T> body) | |
42 :body(body) | |
43 {} | |
44 | |
45 void operator()() | |
46 { | |
47 if (body) | |
48 { | |
49 body->operator()(); | |
50 } | |
51 } | |
52 }; | |
53 | |
54 /* | |
55 * Utility function uses type inference to simplify expressions at point of us e. | |
56 */ | |
57 template<class T> | |
58 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,
| |
59 { | |
60 return HeapFunction<T>(body); | |
61 } | |
62 | |
63 /* | |
64 * Scheduler for the execution of tasks. | |
65 * | |
66 * The present version is nothing more than a rewrite of the legacy behavior, | |
67 * which was to create a new thread for each task. | |
68 */ | |
69 class Scheduler | |
70 { | |
71 public: | |
72 /* | |
73 * Execute a task immediately in a thread created for this task only | |
74 */ | |
75 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
| |
76 }; | |
77 } | |
78 #endif | |
OLD | NEW |