Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Introduce on_???_cb functions to shield the signals
[simgrid.git] / examples / cpp / actor-exiting / s4u-actor-exiting.cpp
1 /* Copyright (c) 2017-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 /* There is two very different ways of being informed when an actor exits.
7  *
8  * The this_actor::on_exit() function allows one to register a function to be
9  * executed when this very actor exits. The registered function will run
10  * when this actor terminates (either because its main function returns, or
11  * because it's killed in any way). No simcall are allowed here: your actor
12  * is dead already, so it cannot interact with its environment in any way
13  * (network, executions, disks, etc).
14  *
15  * Usually, the functions registered in this_actor::on_exit() are in charge
16  * of releasing any memory allocated by the actor during its execution.
17  *
18  * The other way of getting informed when an actor terminates is to connect a
19  * function in the Actor::on_termination signal, that is shared between
20  * all actors. Callbacks to this signal are executed for each terminating
21  * actors, no matter what. This is useful in many cases, in particular
22  * when developing SimGrid plugins.
23  *
24  * Finally, you can attach callbacks to the Actor::on_destruction signal.
25  * It is also shared between all actors, and gets fired when the actors
26  * are destroyed. A delay is possible between the termination of an actor
27  * (ie, when it terminates executing its code) and its destruction (ie,
28  * when it is not referenced anywhere in the simulation and can be collected).
29  *
30  * In both cases, you can stack more than one callback in the signal.
31  * They will all be executed in the registration order.
32  */
33
34 #include <simgrid/s4u.hpp>
35 namespace sg4 = simgrid::s4u;
36
37 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_exiting, "Messages specific for this s4u example");
38
39 static void actor_a()
40 {
41   // Register a lambda function to be executed once it stops
42   sg4::this_actor::on_exit([](bool /*failed*/) { XBT_INFO("I stop now"); });
43
44   sg4::this_actor::sleep_for(1);
45 }
46
47 static void actor_b()
48 {
49   sg4::this_actor::sleep_for(2);
50 }
51
52 static void actor_c()
53 {
54   // Register a lambda function to be executed once it stops
55   sg4::this_actor::on_exit([](bool failed) {
56     if (failed) {
57       XBT_INFO("I was killed!");
58       if (xbt_log_no_loc)
59         XBT_INFO("The backtrace would be displayed here if --log=no_loc would not have been passed");
60       else
61         xbt_backtrace_display_current();
62     } else
63       XBT_INFO("Exiting gracefully.");
64   });
65
66   sg4::this_actor::sleep_for(3);
67   XBT_INFO("And now, induce a deadlock by waiting for a message that will never come\n\n");
68   sg4::Mailbox::by_name("nobody")->get<void>();
69   xbt_die("Receiving is not supposed to succeed when nobody is sending");
70 }
71
72 int main(int argc, char* argv[])
73 {
74   sg4::Engine e(&argc, argv);
75   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
76
77   e.load_platform(argv[1]); /* - Load the platform description */
78
79   /* Register a callback in the Actor::on_termination signal. It will be called for every terminated actors */
80   sg4::Actor::on_termination_cb(
81       [](sg4::Actor const& actor) { XBT_INFO("Actor %s terminates now", actor.get_cname()); });
82   /* Register a callback in the Actor::on_destruction signal. It will be called for every destructed actors */
83   sg4::Actor::on_destruction_cb(
84       [](sg4::Actor const& actor) { XBT_INFO("Actor %s gets destroyed now", actor.get_cname()); });
85
86   /* Create some actors */
87   sg4::Actor::create("A", e.host_by_name("Tremblay"), actor_a);
88   sg4::Actor::create("B", e.host_by_name("Fafard"), actor_b);
89   sg4::Actor::create("C", e.host_by_name("Ginette"), actor_c);
90
91   e.run(); /* - Run the simulation */
92
93   return 0;
94 }