1 /* Copyright (c) 2016-2017. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <xbt/future.hpp>
12 #include <simgrid/simix.hpp>
13 #include <simgrid/simix/blocking_simcall.hpp>
14 #include <simgrid/kernel/future.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 */
27 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 SIMIX_timer_set(date, [promise] {
37 static int master(int argc, char *argv[])
39 // Test the simple immediate execution:
41 simgrid::simix::kernelImmediate([] {
44 XBT_INFO("kernel, returned");
46 // Synchronize on a successful Future<void>:
47 simgrid::simix::kernelSync([] {
48 return kernel_wait_until(10).then([](simgrid::kernel::Future<void> future) {
50 XBT_INFO("kernelSync with void");
53 XBT_INFO("kernelSync with void, returned");
55 // Synchronize on a failing Future<void>:
57 simgrid::simix::kernelSync([] {
58 return kernel_wait_until(20).then([](simgrid::kernel::Future<void> future) {
60 throw example::exception("Exception throwed from kernel_defer");
63 XBT_ERROR("No exception caught!");
64 } catch (const example::exception& e) {
65 XBT_INFO("Exception caught: %s", e.what());
68 // Synchronize on a successul Future<int> and get the value:
69 int res = simgrid::simix::kernelSync([] {
70 return kernel_wait_until(30).then([](simgrid::kernel::Future<void> future) {
72 XBT_INFO("kernelSync with value");
76 XBT_INFO("kernelSync with value returned with %i", res);
78 // Synchronize on a successul Future<int> and get the value:
79 simgrid::simix::Future<int> future = simgrid::simix::kernelAsync([] {
80 return kernel_wait_until(50).then([](simgrid::kernel::Future<void> future) {
82 XBT_INFO("kernelAsync with value");
87 XBT_INFO("kernelAsync with value returned with %i", res);
89 // Synchronize on a successul Future<int> and get the value:
90 future = simgrid::simix::kernelAsync([] {
91 return kernel_wait_until(60).then([](simgrid::kernel::Future<void> future) {
93 XBT_INFO("kernelAsync with value");
97 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
99 XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
101 XBT_INFO("kernelAsync with value returned with %i", res);
108 int main(int argc, char *argv[])
110 SIMIX_global_init(&argc, argv);
111 xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
112 SIMIX_function_register("master", example::master);
113 SIMIX_create_environment(argv[1]);
114 simcall_process_create("master", example::master, NULL, sg_host_by_name("Tremblay"), 0, NULL, NULL);