Significantly clean up mutual recursion workaround

This has much fewer moving parts, and it's probably more understandable.
There was also a race condition in the previous implementation, so
there's that.
This commit is contained in:
Robbert van der Helm
2020-12-23 15:31:32 +01:00
parent eae77d4dbf
commit 7a55fc3ec0
5 changed files with 146 additions and 191 deletions
+5 -27
View File
@@ -71,33 +71,11 @@ class MainContext {
*/
template <typename T, typename F>
std::future<T> run_in_context(F fn) {
std::promise<T> result{};
std::future<T> future = result.get_future();
boost::asio::dispatch(context,
[result = std::move(result), fn]() mutable {
result.set_value(fn());
});
std::packaged_task<T()> call_fn(std::move(fn));
std::future<T> response = call_fn.get_future();
boost::asio::dispatch(context, std::move(call_fn));
return future;
}
/**
* The same as the above, but without returning a value. This allows us to
* wait for the task to have been run.
*
* @overload
*/
template <typename F>
std::future<void> run_in_context(F fn) {
std::promise<void> result{};
std::future<void> future = result.get_future();
boost::asio::dispatch(context,
[result = std::move(result), fn]() mutable {
fn();
result.set_value();
});
return future;
return response;
}
/**
@@ -107,7 +85,7 @@ class MainContext {
*/
template <typename F>
void schedule_task(F fn) {
boost::asio::post(context, fn);
boost::asio::post(context, std::move(fn));
}
/**