1 /* Copyright (c) 2016-2017. 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/kernel/future.hpp>
13 #include <simgrid/simix.hpp>
14 #include <simgrid/simix/blocking_simcall.hpp>
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "my log messages");
21 class exception : public std::runtime_error {
22 using std::runtime_error::runtime_error;
25 /** Create a future which becomes ready when the date is reached */
26 static simgrid::kernel::Future<void> kernel_wait_until(double date)
28 auto promise = std::make_shared<simgrid::kernel::Promise<void>>();
29 auto future = promise->get_future();
30 SIMIX_timer_set(date, [promise] { promise->set_value(); });
34 static int master(int argc, char* argv[])
36 // Test the simple immediate execution:
38 simgrid::simix::kernelImmediate([] { XBT_INFO("kernel"); });
39 XBT_INFO("kernel, returned");
41 // Synchronize on a successful Future<void>:
42 simgrid::simix::kernelSync([] {
43 return kernel_wait_until(10).then([](simgrid::kernel::Future<void> future) {
45 XBT_INFO("kernelSync with void");
48 XBT_INFO("kernelSync with void, returned");
50 // Synchronize on a failing Future<void>:
52 simgrid::simix::kernelSync([] {
53 return kernel_wait_until(20).then([](simgrid::kernel::Future<void> future) {
55 throw example::exception("Exception throwed from kernel_defer");
58 XBT_ERROR("No exception caught!");
59 } catch (const example::exception& e) {
60 XBT_INFO("Exception caught: %s", e.what());
63 // Synchronize on a successul Future<int> and get the value:
64 int res = simgrid::simix::kernelSync([] {
65 return kernel_wait_until(30).then([](simgrid::kernel::Future<void> future) {
67 XBT_INFO("kernelSync with value");
71 XBT_INFO("kernelSync with value returned with %i", res);
73 // Synchronize on a successul Future<int> and get the value:
74 simgrid::simix::Future<int> future = simgrid::simix::kernelAsync([] {
75 return kernel_wait_until(50).then([](simgrid::kernel::Future<void> future) {
77 XBT_INFO("kernelAsync with value");
82 XBT_INFO("kernelAsync with value returned with %i", res);
84 // Synchronize on a successul Future<int> and get the value:
85 future = simgrid::simix::kernelAsync([] {
86 return kernel_wait_until(60).then([](simgrid::kernel::Future<void> future) {
88 XBT_INFO("kernelAsync with value");
92 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
94 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
96 XBT_INFO("kernelAsync with value returned with %i", res);
102 int main(int argc, char* argv[])
104 SIMIX_global_init(&argc, argv);
105 xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
106 SIMIX_function_register("master", example::master);
107 SIMIX_create_environment(argv[1]);
108 simcall_process_create("master", example::master, NULL, sg_host_by_name("Tremblay"), 0, NULL, NULL);