1 /* Copyright (c) 2016-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include <xbt/future.hpp>
11 #include <simgrid/kernel/future.hpp>
12 #include <simgrid/simix.hpp>
13 #include <simgrid/simix/blocking_simcall.hpp>
16 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "my log messages");
20 class exception : public std::runtime_error {
21 using std::runtime_error::runtime_error;
24 /** Create a future which becomes ready when the date is reached */
25 static simgrid::kernel::Future<void> kernel_wait_until(double date)
27 auto promise = std::make_shared<simgrid::kernel::Promise<void>>();
28 auto future = promise->get_future();
29 SIMIX_timer_set(date, [promise] { promise->set_value(); });
33 static int master(int argc, char* argv[])
35 // Test the simple immediate execution:
37 simgrid::simix::simcall([] { XBT_INFO("kernel"); });
38 XBT_INFO("kernel, returned");
40 // Synchronize on a successful Future<void>:
41 simgrid::simix::kernel_sync([] {
42 return kernel_wait_until(10).then([](simgrid::kernel::Future<void> future) {
44 XBT_INFO("kernel_sync with void");
47 XBT_INFO("kernel_sync with void, returned");
49 // Synchronize on a failing Future<void>:
51 simgrid::simix::kernel_sync([] {
52 return kernel_wait_until(20).then([](simgrid::kernel::Future<void> future) {
54 throw example::exception("Exception throwed from kernel_defer");
57 XBT_ERROR("No exception caught!");
58 } catch (const example::exception& e) {
59 XBT_INFO("Exception caught: %s", e.what());
62 // Synchronize on a successul Future<int> and get the value:
63 int res = simgrid::simix::kernel_sync([] {
64 return kernel_wait_until(30).then([](simgrid::kernel::Future<void> future) {
66 XBT_INFO("kernel_sync with value");
70 XBT_INFO("kernel_sync with value returned with %i", res);
72 // Synchronize on a successul Future<int> and get the value:
73 simgrid::simix::Future<int> future = simgrid::simix::kernel_async([] {
74 return kernel_wait_until(50).then([](simgrid::kernel::Future<void> future) {
76 XBT_INFO("kernel_async with value");
81 XBT_INFO("kernel_async with value returned with %i", res);
83 // Synchronize on a successul Future<int> and get the value:
84 future = simgrid::simix::kernel_async([] {
85 return kernel_wait_until(60).then([](simgrid::kernel::Future<void> future) {
87 XBT_INFO("kernel_async with value");
91 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
93 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
95 XBT_INFO("kernel_async with value returned with %i", res);
101 int main(int argc, char* argv[])
103 SIMIX_global_init(&argc, argv);
104 xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
105 SIMIX_function_register("master", example::master);
106 SIMIX_create_environment(argv[1]);
107 simcall_process_create("master", example::master, NULL, sg_host_by_name("Tremblay"), 0, NULL, NULL);