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>
15 #include <simgrid/simix/blocking_simcall.hpp>
18 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "my log messages");
22 class exception : public std::runtime_error {
23 using std::runtime_error::runtime_error;
26 /** Create a future which becomes ready when the date is reached */
27 static simgrid::kernel::Future<void> kernel_wait_until(double date)
29 auto promise = std::make_shared<simgrid::kernel::Promise<void>>();
30 auto future = promise->get_future();
31 simgrid::simix::Timer::set(date, [promise] { promise->set_value(); });
37 // Test the simple immediate execution:
39 simgrid::kernel::actor::simcall([] { XBT_INFO("kernel"); });
40 XBT_INFO("kernel, returned");
42 // Synchronize on a successful Future<void>:
43 simgrid::simix::kernel_sync([] {
44 return kernel_wait_until(10).then([](simgrid::kernel::Future<void> f) {
46 XBT_INFO("kernel_sync with void");
49 XBT_INFO("kernel_sync with void, returned");
51 // Synchronize on a failing Future<void>:
53 simgrid::simix::kernel_sync([] {
54 return kernel_wait_until(20).then([](simgrid::kernel::Future<void> f) {
56 throw example::exception("Exception thrown from kernel_defer");
59 XBT_ERROR("No exception caught!");
60 } catch (const example::exception& e) {
61 XBT_INFO("Exception caught: %s", e.what());
64 // Synchronize on a successful Future<int> and get the value:
65 int res = simgrid::simix::kernel_sync([] {
66 return kernel_wait_until(30).then([](simgrid::kernel::Future<void> f) {
68 XBT_INFO("kernel_sync with value");
72 XBT_INFO("kernel_sync with value returned with %i", res);
74 // Synchronize on a successful Future<int> and get the value:
75 simgrid::simix::Future<int> future = simgrid::simix::kernel_async([] {
76 return kernel_wait_until(50).then([](simgrid::kernel::Future<void> f) {
78 XBT_INFO("kernel_async with value");
83 XBT_INFO("kernel_async with value returned with %i", res);
85 // Synchronize on a successful Future<int> and get the value:
86 future = simgrid::simix::kernel_async([] {
87 return kernel_wait_until(60).then([](simgrid::kernel::Future<void> f) {
89 XBT_INFO("kernel_async with value");
93 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
95 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
97 XBT_INFO("kernel_async with value returned with %i", res);
101 int main(int argc, char* argv[])
103 simgrid::s4u::Engine e(&argc, argv);
104 xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
105 e.load_platform(argv[1]);
106 simgrid::s4u::Actor::create("master", e.host_by_name("Tremblay"), example::master);