From: Gabriel Corona Date: Mon, 20 Jun 2016 13:25:53 +0000 (+0200) Subject: [simix] .wait() and .is_ready() on simix::Future X-Git-Tag: v3_14~931^2~4 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/0a41f5d471ce7d2afcaf32a96e653c2ae397d7fb?ds=sidebyside [simix] .wait() and .is_ready() on simix::Future --- diff --git a/include/simgrid/kernel/future.hpp b/include/simgrid/kernel/future.hpp index a11c8ec40c..ffd1a33e29 100644 --- a/include/simgrid/kernel/future.hpp +++ b/include/simgrid/kernel/future.hpp @@ -261,7 +261,8 @@ public: Future(Future&& that) : state_(std::move(that.state_)) {} Future& operator=(Future&& that) { - state_ = std::move(that.stat_); + state_ = std::move(that.state_); + return *this; } /** Whether the future is valid:. diff --git a/include/simgrid/simix/blocking_simcall.hpp b/include/simgrid/simix/blocking_simcall.hpp index ee12510869..c89b2738f1 100644 --- a/include/simgrid/simix/blocking_simcall.hpp +++ b/include/simgrid/simix/blocking_simcall.hpp @@ -100,7 +100,33 @@ public: }); return result.get(); } - // TODO, wait() + bool is_ready() const + { + if (!valid()) + throw std::future_error(std::future_errc::no_state); + return future_.is_ready(); + } + void wait() + { + if (!valid()) + throw std::future_error(std::future_errc::no_state); + std::exception_ptr exception; + smx_process_t self = SIMIX_process_self(); + simcall_run_blocking([this, &exception, self]{ + try { + // When the kernel future is ready... + this->future_.then([this, self](simgrid::kernel::Future value) { + // ...store it the simix kernel and wake up. + this->future_ = std::move(value); + simgrid::simix::unblock(self); + }); + } + catch (...) { + exception = std::current_exception(); + simgrid::simix::unblock(self); + } + }); + } // TODO, wait_for() // TODO, wait_until() private: diff --git a/teshsuite/simix/generic_simcalls/generic_simcalls.cpp b/teshsuite/simix/generic_simcalls/generic_simcalls.cpp index b2525f5adf..1156e9b9c8 100644 --- a/teshsuite/simix/generic_simcalls/generic_simcalls.cpp +++ b/teshsuite/simix/generic_simcalls/generic_simcalls.cpp @@ -85,6 +85,19 @@ static int master(int argc, char *argv[]) res = future.get(); XBT_INFO("kernelAsync with value returned with %i", res); + // Synchronize on a successul Future and get the value: + future = simgrid::simix::kernelAsync([&] { + return kernel_defer(60, [] { + XBT_INFO("kernelAsync with value"); + return 43; + }); + }); + XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready"); + future.wait(); + XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready"); + res = future.get(); + XBT_INFO("kernelAsync with value returned with %i", res); + return 0; } diff --git a/teshsuite/simix/generic_simcalls/generic_simcalls.tesh b/teshsuite/simix/generic_simcalls/generic_simcalls.tesh index 25c22ef0a5..0bb08af698 100644 --- a/teshsuite/simix/generic_simcalls/generic_simcalls.tesh +++ b/teshsuite/simix/generic_simcalls/generic_simcalls.tesh @@ -9,3 +9,7 @@ $ ${bindir:=.}/generic_simcalls --cfg=contexts/stack-size:96 ${srcdir:=.}/exampl > [Tremblay:master:(0) 30.000000] [test/INFO] kernelSync with value returned with 42 > [50.000000] [test/INFO] kernelAsync with value > [Tremblay:master:(0) 50.000000] [test/INFO] kernelAsync with value returned with 43 +> [Tremblay:master:(0) 50.000000] [test/INFO] The future is not ready +> [60.000000] [test/INFO] kernelAsync with value +> [Tremblay:master:(0) 60.000000] [test/INFO] The future is ready +> [Tremblay:master:(0) 60.000000] [test/INFO] kernelAsync with value returned with 43 \ No newline at end of file