1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
#ifndef MBGL_UTIL_RUN_LOOP
#define MBGL_UTIL_RUN_LOOP
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/util/work_task.hpp>
#include <mbgl/util/work_request.hpp>
#include <functional>
#include <utility>
#include <queue>
#include <mutex>
#include <atomic>
namespace mbgl {
namespace util {
typedef void * LOOP_HANDLE;
class RunLoop : private util::noncopyable {
public:
enum class Type : uint8_t {
Default,
New,
};
enum class Event : uint8_t {
None = 0,
Read = 1,
Write = 2,
ReadWrite = Read | Write,
};
RunLoop(Type type = Type::Default);
~RunLoop();
static RunLoop* Get();
static LOOP_HANDLE getLoopHandle();
void run();
void runOnce();
void stop();
void ref();
void unref();
// So far only needed by the libcurl backend.
void addWatch(int fd, Event, std::function<void(int, Event)>&& callback);
void removeWatch(int fd);
// Invoke fn(args...) on this RunLoop.
template <class Fn, class... Args>
void invoke(Fn&& fn, Args&&... args) {
auto tuple = std::make_tuple(std::move(args)...);
auto task = std::make_shared<Invoker<Fn, decltype(tuple)>>(
std::move(fn),
std::move(tuple));
push(task);
}
// Post the cancellable work fn(args...) to this RunLoop.
template <class Fn, class... Args>
std::unique_ptr<WorkRequest>
invokeCancellable(Fn&& fn, Args&&... args) {
auto flag = std::make_shared<std::atomic<bool>>();
*flag = false;
auto tuple = std::make_tuple(std::move(args)...);
auto task = std::make_shared<Invoker<Fn, decltype(tuple)>>(
std::move(fn),
std::move(tuple),
flag);
push(task);
return std::make_unique<WorkRequest>(task);
}
// Invoke fn(args...) on this RunLoop, then invoke callback(results...) on the current RunLoop.
template <class Fn, class Cb, class... Args>
std::unique_ptr<WorkRequest>
invokeWithCallback(Fn&& fn, Cb&& callback, Args&&... args) {
auto flag = std::make_shared<std::atomic<bool>>();
*flag = false;
// Create a lambda L1 that invokes another lambda L2 on the current RunLoop R, that calls
// the callback C. Both lambdas check the flag before proceeding. L1 needs to check the flag
// because if the request was cancelled, then R might have been destroyed. L2 needs to check
// the flag because the request may have been cancelled after L2 was invoked but before it
// began executing.
auto after = [flag, current = RunLoop::Get(), callback1 = std::move(callback)] (auto&&... results1) {
if (!*flag) {
current->invoke([flag, callback2 = std::move(callback1)] (auto&&... results2) {
if (!*flag) {
callback2(std::move(results2)...);
}
}, std::move(results1)...);
}
};
auto tuple = std::make_tuple(std::move(args)..., after);
auto task = std::make_shared<Invoker<Fn, decltype(tuple)>>(
std::move(fn),
std::move(tuple),
flag);
push(task);
return std::make_unique<WorkRequest>(task);
}
private:
MBGL_STORE_THREAD(tid)
template <class F, class P>
class Invoker : public WorkTask {
public:
Invoker(F&& f, P&& p, std::shared_ptr<std::atomic<bool>> canceled_ = nullptr)
: canceled(std::move(canceled_)),
func(std::move(f)),
params(std::move(p)) {
}
void operator()() override {
// Lock the mutex while processing so that cancel() will block.
std::lock_guard<std::recursive_mutex> lock(mutex);
if (!canceled || !*canceled) {
invoke(std::make_index_sequence<std::tuple_size<P>::value>{});
}
}
// If the task has not yet begun, this will cancel it.
// If the task is in progress, this will block until it completed. (Currently
// necessary because of shared state, but should be removed.) It will also
// cancel the after callback.
// If the task has completed, but the after callback has not executed, this
// will cancel the after callback.
// If the task has completed and the after callback has executed, this will
// do nothing.
void cancel() override {
std::lock_guard<std::recursive_mutex> lock(mutex);
*canceled = true;
}
private:
template <std::size_t... I>
void invoke(std::index_sequence<I...>) {
func(std::get<I>(std::forward<P>(params))...);
}
std::recursive_mutex mutex;
std::shared_ptr<std::atomic<bool>> canceled;
F func;
P params;
};
using Queue = std::queue<std::shared_ptr<WorkTask>>;
void push(std::shared_ptr<WorkTask>);
void withMutex(std::function<void()>&& fn) {
std::lock_guard<std::mutex> lock(mutex);
fn();
}
void process() {
Queue queue_;
withMutex([&] { queue_.swap(queue); });
while (!queue_.empty()) {
(*(queue_.front()))();
queue_.pop();
}
}
Queue queue;
std::mutex mutex;
class Impl;
std::unique_ptr<Impl> impl;
};
} // namespace util
} // namespace mbgl
#endif
|