Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define and use a more specific exception.
[simgrid.git] / teshsuite / simix / generic_simcalls / generic_simcalls.cpp
1 /* Copyright (c) 2016-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <memory>
8 #include <stdexcept>
9
10 #include <xbt/future.hpp>
11
12 #include <simgrid/simix.hpp>
13 #include <simgrid/simix/blocking_simcall.hpp>
14 #include <simgrid/kernel/future.hpp>
15 #include <xbt/log.h>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "my log messages");
18
19 namespace example {
20
21 class exception : public std::runtime_error {
22   using std::runtime_error::runtime_error;
23 };
24
25 /** Create a future which becomes ready when the date is reached */
26 static
27 simgrid::kernel::Future<void> kernel_wait_until(double date)
28 {
29   auto promise = std::make_shared<simgrid::kernel::Promise<void>>();
30   auto future = promise->get_future();
31   SIMIX_timer_set(date, [promise] {
32     promise->set_value();
33   });
34   return future;
35 }
36
37 static int master(int argc, char *argv[])
38 {
39   // Test the simple immediate execution:
40   XBT_INFO("Start");
41   simgrid::simix::kernelImmediate([] {
42     XBT_INFO("kernel");
43   });
44   XBT_INFO("kernel, returned");
45
46   // Synchronize on a successful Future<void>:
47   simgrid::simix::kernelSync([] {
48     return kernel_wait_until(10).then([](simgrid::kernel::Future<void> future) {
49       future.get();
50       XBT_INFO("kernelSync with void");
51     });
52   });
53   XBT_INFO("kernelSync with void, returned");
54
55   // Synchronize on a failing Future<void>:
56   try {
57     simgrid::simix::kernelSync([] {
58       return kernel_wait_until(20).then([](simgrid::kernel::Future<void> future) {
59         future.get();
60         throw example::exception("Exception throwed from kernel_defer");
61       });
62     });
63     XBT_ERROR("No exception caught!");
64   } catch (const example::exception& e) {
65     XBT_INFO("Exception caught: %s", e.what());
66   }
67
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) {
71       future.get();
72       XBT_INFO("kernelSync with value");
73       return 42;
74     });
75   });
76   XBT_INFO("kernelSync with value returned with %i", res);
77
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) {
81       future.get();
82       XBT_INFO("kernelAsync with value");
83       return 43;
84     });
85   });
86   res = future.get();
87   XBT_INFO("kernelAsync with value returned with %i", res);
88
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) {
92       future.get();
93       XBT_INFO("kernelAsync with value");
94       return 43;
95     });
96   });
97   XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
98   future.wait();
99   XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
100   res = future.get();
101   XBT_INFO("kernelAsync with value returned with %i", res);
102
103   return 0;
104 }
105
106 }
107
108 int main(int argc, char *argv[])
109 {
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);
115   SIMIX_run();
116   return 0;
117 }