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/Timer.hpp>
12 #include <simgrid/kernel/future.hpp>
13 #include <simgrid/s4u/Actor.hpp>
14 #include <simgrid/s4u/Engine.hpp>
15 #include <simgrid/simix.hpp>
18 #include "blocking_simcall.hpp"
20 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "my log messages");
24 class exception : public std::runtime_error {
25 using std::runtime_error::runtime_error;
28 /** Create a future which becomes ready when the date is reached */
29 static simgrid::kernel::Future<void> kernel_wait_until(double date)
31 auto promise = std::make_shared<simgrid::kernel::Promise<void>>();
32 auto future = promise->get_future();
33 simgrid::kernel::timer::Timer::set(date, [promise] { promise->set_value(); });
39 // Test the simple immediate execution:
41 simgrid::kernel::actor::simcall([] { XBT_INFO("kernel"); });
42 XBT_INFO("kernel, returned");
44 // Synchronize on a successful Future<void>:
45 simgrid::simix::kernel_sync([] {
46 return kernel_wait_until(10).then([](simgrid::kernel::Future<void> f) {
48 XBT_INFO("kernel_sync with void");
51 XBT_INFO("kernel_sync with void, returned");
53 // Synchronize on a failing Future<void>:
55 simgrid::simix::kernel_sync([] {
56 return kernel_wait_until(20).then([](simgrid::kernel::Future<void> f) {
58 throw example::exception("Exception thrown from kernel_defer");
61 XBT_ERROR("No exception caught!");
62 } catch (const example::exception& e) {
63 XBT_INFO("Exception caught: %s", e.what());
66 // Synchronize on a successful Future<int> and get the value:
67 int res = simgrid::simix::kernel_sync([] {
68 return kernel_wait_until(30).then([](simgrid::kernel::Future<void> f) {
70 XBT_INFO("kernel_sync with value");
74 XBT_INFO("kernel_sync with value returned with %i", res);
76 // Synchronize on a successful Future<int> and get the value:
77 simgrid::simix::Future<int> future = simgrid::simix::kernel_async([] {
78 return kernel_wait_until(50).then([](simgrid::kernel::Future<void> f) {
80 XBT_INFO("kernel_async with value");
85 XBT_INFO("kernel_async with value returned with %i", res);
87 // Synchronize on a successful Future<int> and get the value:
88 future = simgrid::simix::kernel_async([] {
89 return kernel_wait_until(60).then([](simgrid::kernel::Future<void> f) {
91 XBT_INFO("kernel_async with value");
95 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
97 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
99 XBT_INFO("kernel_async with value returned with %i", res);
103 int main(int argc, char* argv[])
105 simgrid::s4u::Engine e(&argc, argv);
106 xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
107 e.load_platform(argv[1]);
108 simgrid::s4u::Actor::create("master", e.host_by_name("Tremblay"), example::master);