gasync.cpp
8.4 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2019 Intel Corporation
#include <opencv2/gapi/gcomputation_async.hpp>
#include <opencv2/gapi/gcomputation.hpp>
#include <opencv2/gapi/gcompiled_async.hpp>
#include <opencv2/gapi/gcompiled.hpp>
#include <opencv2/gapi/gasync_context.hpp>
#include <opencv2/gapi/util/copy_through_move.hpp>
#include <condition_variable>
#include <future>
#include <condition_variable>
#include <stdexcept>
#include <queue>
namespace cv {
namespace gapi {
namespace wip {
namespace impl{
class async_service {
std::mutex mtx;
std::condition_variable cv;
std::queue<std::function<void()>> q;
std::atomic<bool> exiting = {false};
std::atomic<bool> thread_started = {false};
std::thread thrd;
async_service() = default ;
public:
// singleton
static async_service& instance()
{
static async_service the_ctx;
return the_ctx;
}
void add_task(std::function<void()>&& t){
if (!thread_started)
{
//thread has not been started yet, so start it
//try to Compare And Swap the flag, false -> true
//If there are multiple threads - only single one will succeed in changing the value.
bool expected = false;
if (thread_started.compare_exchange_strong(expected, true))
{
//have won (probable) race - so actually start the thread
thrd = std::thread {[this](){
//move the whole queue into local instance in order to minimize time the guarding lock is held
decltype(q) second_q;
while (!exiting){
std::unique_lock<std::mutex> lck{mtx};
if (q.empty())
{
//block current thread until arrival of exit request or new elements
cv.wait(lck, [&](){ return exiting || !q.empty();});
}
//usually swap for std::queue is plain pointers exchange, so relatively cheap
q.swap(second_q);
lck.unlock();
while (!second_q.empty())
{
auto& f = second_q.front();
f();
second_q.pop();
}
}
}};
}
}
std::unique_lock<std::mutex> lck{mtx};
bool first_task = q.empty();
q.push(std::move(t));
lck.unlock();
if (first_task)
{
//as the queue was empty before adding the task,
//the thread might be sleeping, so wake it up
cv.notify_one();
}
}
protected:
~async_service(){
if (thread_started && thrd.joinable())
{
exiting = true;
mtx.lock();
mtx.unlock();
cv.notify_one();
thrd.join();
}
}
};
}
namespace {
template<typename f_t, typename context_t>
std::exception_ptr call_and_catch(f_t&& f, context_t&& ctx){
if (std::forward<context_t>(ctx).isCanceled()){
return std::make_exception_ptr(GAsyncCanceled{});
}
std::exception_ptr eptr;
try {
std::forward<f_t>(f)();
} catch(...) {
eptr = std::current_exception();
}
return eptr;
}
struct DummyContext {
bool isCanceled() const {
return false;
}
};
template<typename f_t, typename callback_t, typename context_t>
void call_with_callback(f_t&& f, callback_t&& cb, context_t&& ctx){
auto eptr = call_and_catch(std::forward<f_t>(f), std::forward<context_t>(ctx));
std::forward<callback_t>(cb)(eptr);
}
template<typename f_t, typename context_t>
void call_with_future(f_t&& f, std::promise<void>& p, context_t&& ctx){
auto eptr = call_and_catch(std::forward<f_t>(f), std::forward<context_t>(ctx));
if (eptr){
p.set_exception(eptr);
}
else {
p.set_value();
}
}
}//namespace
bool GAsyncContext::cancel(){
bool expected = false;
bool updated = cancelation_requested.compare_exchange_strong(expected, true);
return updated;
}
bool GAsyncContext::isCanceled() const {
return cancelation_requested.load();
}
const char* GAsyncCanceled::what() const noexcept {
return "GAPI asynchronous operation was canceled";
}
//For now these async functions are simply wrapping serial version of apply/operator() into a functor.
//These functors are then serialized into single queue, which is processed by a devoted background thread.
void async_apply(GComputation& gcomp, std::function<void(std::exception_ptr)>&& callback, GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args){
//TODO: use copy_through_move_t for all args except gcomp
//TODO: avoid code duplication between versions of "async" functions
auto l = [=]() mutable {
auto apply_l = [&](){
gcomp.apply(std::move(ins), std::move(outs), std::move(args));
};
call_with_callback(apply_l,std::move(callback), DummyContext{});
};
impl::async_service::instance().add_task(l);
}
std::future<void> async_apply(GComputation& gcomp, GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args){
util::copy_through_move_t<std::promise<void>> prms{{}};
auto f = prms.value.get_future();
auto l = [=]() mutable {
auto apply_l = [&](){
gcomp.apply(std::move(ins), std::move(outs), std::move(args));
};
call_with_future(apply_l, prms.value, DummyContext{});
};
impl::async_service::instance().add_task(l);
return f;
}
void async_apply(GComputation& gcomp, std::function<void(std::exception_ptr)>&& callback, GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args, GAsyncContext& ctx){
//TODO: use copy_through_move_t for all args except gcomp
auto l = [=, &ctx]() mutable {
auto apply_l = [&](){
gcomp.apply(std::move(ins), std::move(outs), std::move(args));
};
call_with_callback(apply_l,std::move(callback), ctx);
};
impl::async_service::instance().add_task(l);
}
std::future<void> async_apply(GComputation& gcomp, GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args, GAsyncContext& ctx){
util::copy_through_move_t<std::promise<void>> prms{{}};
auto f = prms.value.get_future();
auto l = [=, &ctx]() mutable {
auto apply_l = [&](){
gcomp.apply(std::move(ins), std::move(outs), std::move(args));
};
call_with_future(apply_l, prms.value, ctx);
};
impl::async_service::instance().add_task(l);
return f;
}
void async(GCompiled& gcmpld, std::function<void(std::exception_ptr)>&& callback, GRunArgs &&ins, GRunArgsP &&outs){
auto l = [=]() mutable {
auto apply_l = [&](){
gcmpld(std::move(ins), std::move(outs));
};
call_with_callback(apply_l,std::move(callback), DummyContext{});
};
impl::async_service::instance().add_task(l);
}
void async(GCompiled& gcmpld, std::function<void(std::exception_ptr)>&& callback, GRunArgs &&ins, GRunArgsP &&outs, GAsyncContext& ctx){
auto l = [=, &ctx]() mutable {
auto apply_l = [&](){
gcmpld(std::move(ins), std::move(outs));
};
call_with_callback(apply_l,std::move(callback), ctx);
};
impl::async_service::instance().add_task(l);
}
std::future<void> async(GCompiled& gcmpld, GRunArgs &&ins, GRunArgsP &&outs){
util::copy_through_move_t<std::promise<void>> prms{{}};
auto f = prms.value.get_future();
auto l = [=]() mutable {
auto apply_l = [&](){
gcmpld(std::move(ins), std::move(outs));
};
call_with_future(apply_l, prms.value, DummyContext{});
};
impl::async_service::instance().add_task(l);
return f;
}
std::future<void> async(GCompiled& gcmpld, GRunArgs &&ins, GRunArgsP &&outs, GAsyncContext& ctx){
util::copy_through_move_t<std::promise<void>> prms{{}};
auto f = prms.value.get_future();
auto l = [=, &ctx]() mutable {
auto apply_l = [&](){
gcmpld(std::move(ins), std::move(outs));
};
call_with_future(apply_l, prms.value, ctx);
};
impl::async_service::instance().add_task(l);
return f;
}
}}} //namespace wip namespace gapi namespace cv