forked from sony/nmos-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpplx_utils.cpp
178 lines (152 loc) · 6.12 KB
/
pplx_utils.cpp
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
#include "pplx/pplx_utils.h"
#if (defined(_MSC_VER) && (_MSC_VER >= 1800)) && !CPPREST_FORCE_PPLX
#include <agents.h>
namespace Concurrency // since namespace pplx = Concurrency
{
pplx::task<void> complete_after(unsigned int milliseconds, const pplx::cancellation_token& token)
{
// just yielding the current thread for the duration by pplx::wait(milliseconds) probably isn't good enough, so instead
// construct a task that completes after an asynchronous wait on a timer
task_completion_event<void> tce;
// namespace concurrency = Concurrency too...
auto timer = std::make_shared<concurrency::timer<int>>(milliseconds, 0);
auto callback = std::make_shared<concurrency::call<int>>([tce](int)
{
tce.set();
});
timer->link_target(callback.get());
timer->start();
auto result = pplx::create_task(tce, token);
// when the token is canceled, cancel the timer
if (token.is_cancelable())
{
auto registration = token.register_callback([timer, tce]
{
timer->stop();
// calling tce.set_exception(pplx::task_canceled()) does not have the right effect, it results in a call
// to wait on the task throwing rather than returning pplx::canceled
if (!tce._IsTriggered())
{
tce._Cancel();
}
});
result.then([token, registration](pplx::task<void>)
{
token.deregister_callback(registration);
});
}
// capture the timer and callback so they definitely don't get deleted too soon
result.then([timer, callback]{});
return result;
}
}
#else
#include <boost/asio/basic_waitable_timer.hpp>
#include "pplx/threadpool.h"
namespace pplx
{
pplx::task<void> complete_after(unsigned int milliseconds, const pplx::cancellation_token& token)
{
// construct a task that completes after an asynchronous wait on a timer
pplx::task_completion_event<void> tce;
typedef boost::asio::basic_waitable_timer<std::chrono::steady_clock> steady_timer;
// when the current <see cref="set_ambient_scheduler Function">ambient scheduler</see> has been changed from the default
// using the shared threadpool perhaps isn't appropriate, but given the scheduler_interface, the alternative is unclear...
auto timer = std::make_shared<steady_timer>(crossplat::threadpool::shared_instance().service());
timer->expires_from_now(std::chrono::duration_cast<steady_timer::duration>(std::chrono::milliseconds(milliseconds)));
timer->async_wait([tce](const boost::system::error_code& ec)
{
if (ec == boost::asio::error::operation_aborted)
{
// calling tce.set_exception(pplx::task_canceled()) does not have the right effect, it results in a call
// to wait on the task throwing rather than returning pplx::canceled
if (!tce._IsTriggered())
{
tce._Cancel();
}
}
else
{
tce.set();
}
});
auto result = pplx::create_task(tce, token);
// when the token is canceled, cancel the timer
if (token.is_cancelable())
{
auto registration = token.register_callback([timer]
{
boost::system::error_code ignored;
timer->cancel(ignored);
});
result.then([token, registration](pplx::task<void>)
{
token.deregister_callback(registration);
});
}
// capture the timer so it definitely doesn't get deleted too soon
result.then([timer]{});
return result;
}
}
#endif
#if (defined(_MSC_VER) && (_MSC_VER >= 1800)) && !CPPREST_FORCE_PPLX
namespace Concurrency // since namespace pplx = Concurrency
#else
namespace pplx
#endif
{
namespace details
{
void propagate_exception(pplx::task_completion_event<void> event, const pplx::task<void>& task)
{
task.then([event](pplx::task<void> completed_or_canceled)
{
try
{
completed_or_canceled.get();
}
catch (const pplx::task_canceled&)
{
if (!event._IsTriggered())
{
event._Cancel();
}
}
catch (...)
{
if (!event._IsTriggered())
{
event.set_exception(std::current_exception());
}
}
});
}
void do_while_iteration(pplx::task_completion_event<void> event, const std::function<pplx::task<bool>()>& create_iteration_task, const pplx::cancellation_token& token)
{
propagate_exception(event, create_iteration_task().then([event, create_iteration_task, token](pplx::task<bool> condition)
{
if (condition.get())
{
do_while_iteration(event, create_iteration_task, token);
}
else
{
event.set();
}
}, token));
}
}
pplx::task<void> do_while(const std::function<pplx::task<bool>()>& create_iteration_task, const pplx::cancellation_token& token)
{
pplx::task_completion_event<void> event;
// create the result task before starting the 'loop', as a workaround for a bug in pplx::task_completion_event::_RegisterTask
// that a task created from an event cancelled without a user exception (using _Cancel as above) isn't marked as cancelled
auto result = pplx::create_task(event, token);
details::propagate_exception(event, pplx::create_task([event, create_iteration_task, token]
{
details::do_while_iteration(event, create_iteration_task, token);
}, token));
return result;
}
}