X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/d37695654af494c87f28017306376b8c229aaec0..dae3e1ff7494f78632ef7d90d97d0794d7e7bc1f:/include/simgrid/simix/blocking_simcall.hpp diff --git a/include/simgrid/simix/blocking_simcall.hpp b/include/simgrid/simix/blocking_simcall.hpp index 43565d3e62..c95e63f09f 100644 --- a/include/simgrid/simix/blocking_simcall.hpp +++ b/include/simgrid/simix/blocking_simcall.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2016-2019. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2016-2020. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -18,8 +18,6 @@ #include #include -XBT_PUBLIC void simcall_run_blocking(std::function const& code); - namespace simgrid { namespace simix { @@ -54,19 +52,20 @@ template auto kernel_sync(F code) -> decltype(code().get()) smx_actor_t self = SIMIX_process_self(); simgrid::xbt::Result result; - simcall_run_blocking([&result, self, &code]{ - try { - auto future = code(); - future.then_([&result, self](std::shared_ptr>&& value) { - simgrid::xbt::set_promise(result, simgrid::kernel::Future(value)); - simgrid::simix::unblock(self); - }); - } - catch (...) { - result.set_exception(std::current_exception()); - simgrid::simix::unblock(self); - } - }); + simcall_run_blocking( + [&result, self, &code] { + try { + auto future = code(); + future.then_([&result, self](std::shared_ptr> value) { + simgrid::xbt::set_promise(result, simgrid::kernel::Future(std::move(value))); + simgrid::simix::unblock(self); + }); + } catch (...) { + result.set_exception(std::current_exception()); + simgrid::simix::unblock(self); + } + }, + nullptr); return result.get(); } @@ -79,8 +78,10 @@ template auto kernel_sync(F code) -> decltype(code().get()) template class Future { public: - Future() { /* Nothing to do*/} + Future() = default; explicit Future(simgrid::kernel::Future future) : future_(std::move(future)) {} + Future(Future&&) noexcept = default; + Future& operator=(Future&&) noexcept = default; bool valid() const { return future_.valid(); } T get() @@ -89,20 +90,21 @@ public: throw std::future_error(std::future_errc::no_state); smx_actor_t self = SIMIX_process_self(); simgrid::xbt::Result result; - simcall_run_blocking([this, &result, self]{ - try { - // When the kernel future is ready... - this->future_.then_([&result, self](std::shared_ptr>&& value) { - // ... wake up the process with the result of the kernel future. - simgrid::xbt::set_promise(result, simgrid::kernel::Future(value)); - simgrid::simix::unblock(self); - }); - } - catch (...) { - result.set_exception(std::current_exception()); - simgrid::simix::unblock(self); - } - }); + simcall_run_blocking( + [this, &result, self] { + try { + // When the kernel future is ready... + this->future_.then_([&result, self](std::shared_ptr> value) { + // ... wake up the process with the result of the kernel future. + simgrid::xbt::set_promise(result, simgrid::kernel::Future(std::move(value))); + simgrid::simix::unblock(self); + }); + } catch (...) { + result.set_exception(std::current_exception()); + simgrid::simix::unblock(self); + } + }, + nullptr); return result.get(); } bool is_ready() const @@ -119,21 +121,23 @@ public: // The future is not ready. We have to delegate to the SimGrid kernel: std::exception_ptr exception; smx_actor_t self = SIMIX_process_self(); - simcall_run_blocking([this, &exception, self]{ - try { - // When the kernel future is ready... - this->future_.then_([this, self](std::shared_ptr>&& value) { - // ...store it the simix kernel and wake up. - this->future_ = std::move(simgrid::kernel::Future(value)); - simgrid::simix::unblock(self); - }); - } - catch (...) { - exception = std::current_exception(); - simgrid::simix::unblock(self); - } - }); + simcall_run_blocking( + [this, &exception, self] { + try { + // When the kernel future is ready... + this->future_.then_([this, self](std::shared_ptr> value) { + // ...store it the simix kernel and wake up. + this->future_ = simgrid::kernel::Future(std::move(value)); + simgrid::simix::unblock(self); + }); + } catch (...) { + exception = std::current_exception(); + simgrid::simix::unblock(self); + } + }, + nullptr); } + private: // We wrap an event-based kernel future: simgrid::kernel::Future future_; @@ -149,9 +153,9 @@ template auto kernel_async(F code) -> Future typedef decltype(code().get()) T; // Execute the code in the kernel and get the kernel future: - simgrid::kernel::Future future = simgrid::simix::simcall(std::move(code)); + simgrid::kernel::Future future = simgrid::kernel::actor::simcall(std::move(code)); - // Wrap the kernel future in a actor future: + // Wrap the kernel future in an actor future: return simgrid::simix::Future(std::move(future)); } }