1 /* Copyright (c) 2016-2021. 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/promise.hpp>
11 #include <simgrid/kernel/future.hpp>
12 #include <simgrid/s4u/Actor.hpp>
13 #include <simgrid/s4u/Engine.hpp>
14 #include <simgrid/simix.hpp>
17 #include "blocking_simcall.hpp"
19 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "my log messages");
23 class exception : public std::runtime_error {
24 using std::runtime_error::runtime_error;
27 /** Create a future which becomes ready when the date is reached */
28 static simgrid::kernel::Future<void> kernel_wait_until(double date)
30 auto promise = std::make_shared<simgrid::kernel::Promise<void>>();
31 auto future = promise->get_future();
32 simgrid::simix::Timer::set(date, [promise] { promise->set_value(); });
38 // Test the simple immediate execution:
40 simgrid::kernel::actor::simcall([] { XBT_INFO("kernel"); });
41 XBT_INFO("kernel, returned");
43 // Synchronize on a successful Future<void>:
44 simgrid::simix::kernel_sync([] {
45 return kernel_wait_until(10).then([](simgrid::kernel::Future<void> f) {
47 XBT_INFO("kernel_sync with void");
50 XBT_INFO("kernel_sync with void, returned");
52 // Synchronize on a failing Future<void>:
54 simgrid::simix::kernel_sync([] {
55 return kernel_wait_until(20).then([](simgrid::kernel::Future<void> f) {
57 throw example::exception("Exception thrown from kernel_defer");
60 XBT_ERROR("No exception caught!");
61 } catch (const example::exception& e) {
62 XBT_INFO("Exception caught: %s", e.what());
65 // Synchronize on a successful Future<int> and get the value:
66 int res = simgrid::simix::kernel_sync([] {
67 return kernel_wait_until(30).then([](simgrid::kernel::Future<void> f) {
69 XBT_INFO("kernel_sync with value");
73 XBT_INFO("kernel_sync with value returned with %i", res);
75 // Synchronize on a successful Future<int> and get the value:
76 simgrid::simix::Future<int> future = simgrid::simix::kernel_async([] {
77 return kernel_wait_until(50).then([](simgrid::kernel::Future<void> f) {
79 XBT_INFO("kernel_async with value");
84 XBT_INFO("kernel_async with value returned with %i", res);
86 // Synchronize on a successful Future<int> and get the value:
87 future = simgrid::simix::kernel_async([] {
88 return kernel_wait_until(60).then([](simgrid::kernel::Future<void> f) {
90 XBT_INFO("kernel_async with value");
94 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
96 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
98 XBT_INFO("kernel_async with value returned with %i", res);
102 int main(int argc, char* argv[])
104 simgrid::s4u::Engine e(&argc, argv);
105 xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
106 e.load_platform(argv[1]);
107 simgrid::s4u::Actor::create("master", e.host_by_name("Tremblay"), example::master);