1 /* Copyright (c) 2016-2019. 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/engine.h>
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 simgrid::simix::Timer::set(date, [promise] { promise->set_value(); });
36 // Test the simple immediate execution:
38 simgrid::kernel::actor::simcall([] { XBT_INFO("kernel"); });
39 XBT_INFO("kernel, returned");
41 // Synchronize on a successful Future<void>:
42 simgrid::simix::kernel_sync([] {
43 return kernel_wait_until(10).then([](simgrid::kernel::Future<void> f) {
45 XBT_INFO("kernel_sync with void");
48 XBT_INFO("kernel_sync with void, returned");
50 // Synchronize on a failing Future<void>:
52 simgrid::simix::kernel_sync([] {
53 return kernel_wait_until(20).then([](simgrid::kernel::Future<void> f) {
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::kernel_sync([] {
65 return kernel_wait_until(30).then([](simgrid::kernel::Future<void> f) {
67 XBT_INFO("kernel_sync with value");
71 XBT_INFO("kernel_sync 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::kernel_async([] {
75 return kernel_wait_until(50).then([](simgrid::kernel::Future<void> f) {
77 XBT_INFO("kernel_async with value");
82 XBT_INFO("kernel_async with value returned with %i", res);
84 // Synchronize on a successul Future<int> and get the value:
85 future = simgrid::simix::kernel_async([] {
86 return kernel_wait_until(60).then([](simgrid::kernel::Future<void> f) {
88 XBT_INFO("kernel_async 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("kernel_async with value returned with %i", res);
100 int main(int argc, char* argv[])
102 SIMIX_global_init(&argc, argv);
103 xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
104 simgrid_load_platform(argv[1]);
105 simcall_process_create("master", example::master, NULL, sg_host_by_name("Tremblay"), NULL);