1 /* Copyright (c) 2016. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <xbt/future.hpp>
12 #include <simgrid/simix.hpp>
13 #include <simgrid/simix/blocking_simcall.hpp>
14 #include <simgrid/kernel/future.hpp>
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "my log messages");
21 /** Execute the code in the kernel at some time
23 * @param date when we should execute the code
24 * @param code code to execute
25 * @return future with the result of the call
28 auto kernel_defer(double date, F code) -> simgrid::kernel::Future<decltype(code())>
30 typedef decltype(code()) T;
31 auto promise = std::make_shared<simgrid::kernel::Promise<T>>();
32 auto future = promise->get_future();
33 SIMIX_timer_set(date, [promise, code] {
34 simgrid::xbt::fulfillPromise(*promise, std::move(code));
39 static int master(int argc, char *argv[])
41 // Test the simple immediate execution:
43 simgrid::simix::kernel([] {
46 XBT_INFO("kernel, returned");
48 // Synchronize on a successful Future<void>:
49 simgrid::simix::blocking_simcall([&] {
50 return kernel_defer(10, [] {
51 XBT_INFO("blocking_simcall with void");
54 XBT_INFO("blocking_simcall with void, returned");
56 // Synchronize on a failing Future<void>:
58 simgrid::simix::blocking_simcall([&] {
59 return kernel_defer(20, [] {
60 throw std::runtime_error("Exception throwed from kernel_defer");
63 XBT_ERROR("No exception caught!");
65 catch(std::runtime_error& e) {
66 XBT_INFO("Exception caught: %s", e.what());
69 // Synchronize on a successul Future<int> and get the value:
70 int res = simgrid::simix::blocking_simcall([&] {
71 return kernel_defer(30, [] {
72 XBT_INFO("blocking_simcall with value");
76 XBT_INFO("blocking_simcall with value returned with %i", res);
78 // Synchronize on a successul Future<int> and get the value:
79 simgrid::simix::Future<int> future = simgrid::simix::asynchronous_simcall([&] {
80 return kernel_defer(50, [] {
81 XBT_INFO("asynchronous_simcall with value");
86 XBT_INFO("asynchronous_simcall with value returned with %i", res);
93 int main(int argc, char *argv[])
95 SIMIX_global_init(&argc, argv);
96 xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
97 SIMIX_function_register("master", example::master);
98 SIMIX_create_environment(argv[1]);
99 simcall_process_create("master", example::master, NULL, "Tremblay", -1, 0, NULL, NULL, 0);