X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/467a0c53018ee489de1dd7ae61a083d52048b8e8..c3afdfa7a603897d1fbca56e7d1705983038405e:/teshsuite/simix/generic_simcalls/generic_simcalls.cpp diff --git a/teshsuite/simix/generic_simcalls/generic_simcalls.cpp b/teshsuite/simix/generic_simcalls/generic_simcalls.cpp index f1743b585f..cf42eb708d 100644 --- a/teshsuite/simix/generic_simcalls/generic_simcalls.cpp +++ b/teshsuite/simix/generic_simcalls/generic_simcalls.cpp @@ -18,20 +18,14 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(test, "my log messages"); namespace example { -/** Execute the code in the kernel at some time - * - * @param date when we should execute the code - * @param code code to execute - * @return future with the result of the call - */ -template -auto kernel_defer(double date, F code) -> simgrid::kernel::Future +/** Create a future which becomes ready when the date is reached */ +static +simgrid::kernel::Future kernel_wait_until(double date) { - typedef decltype(code()) T; - auto promise = std::make_shared>(); + auto promise = std::make_shared>(); auto future = promise->get_future(); - SIMIX_timer_set(date, [promise, code] { - simgrid::xbt::fulfillPromise(*promise, std::move(code)); + SIMIX_timer_set(date, [promise] { + promise->set_value(); }); return future; } @@ -40,23 +34,25 @@ static int master(int argc, char *argv[]) { // Test the simple immediate execution: XBT_INFO("Start"); - simgrid::simix::kernel([] { + simgrid::simix::kernelImmediate([] { XBT_INFO("kernel"); }); XBT_INFO("kernel, returned"); // Synchronize on a successful Future: - simgrid::simix::blocking_simcall([&] { - return kernel_defer(10, [] { - XBT_INFO("blocking_simcall with void"); + simgrid::simix::kernelSync([&] { + return kernel_wait_until(10).then([](simgrid::kernel::Future future) { + future.get(); + XBT_INFO("kernelSync with void"); }); }); - XBT_INFO("blocking_simcall with void, returned"); + XBT_INFO("kernelSync with void, returned"); // Synchronize on a failing Future: try { - simgrid::simix::blocking_simcall([&] { - return kernel_defer(20, [] { + simgrid::simix::kernelSync([&] { + return kernel_wait_until(20).then([](simgrid::kernel::Future future) { + future.get(); throw std::runtime_error("Exception throwed from kernel_defer"); }); }); @@ -67,13 +63,39 @@ static int master(int argc, char *argv[]) } // Synchronize on a successul Future and get the value: - int res = simgrid::simix::blocking_simcall([&] { - return kernel_defer(30, [] { - XBT_INFO("blocking_simcall with value"); + int res = simgrid::simix::kernelSync([&] { + return kernel_wait_until(30).then([](simgrid::kernel::Future future) { + future.get(); + XBT_INFO("kernelSync with value"); return 42; }); }); - XBT_INFO("blocking_simcall with value returned with %i", res); + XBT_INFO("kernelSync with value returned with %i", res); + + // Synchronize on a successul Future and get the value: + simgrid::simix::Future future = simgrid::simix::kernelAsync([&] { + return kernel_wait_until(50).then([](simgrid::kernel::Future future) { + future.get(); + XBT_INFO("kernelAsync with value"); + return 43; + }); + }); + 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_wait_until(60).then([](simgrid::kernel::Future future) { + future.get(); + 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; }