Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A few spelling mistakes and many replacements: [Ss]imgrid -> SimGrid.
[simgrid.git] / examples / cpp / maestro-set / s4u-maestro-set.cpp
1 /* Copyright (c) 2007-2023. 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 /** Switch the system thread hosting our maestro.
7  *
8  *  That's a very advanced example in which we move the maestro context to another system thread.
9  *  Not many users need it (maybe only one, actually), but this example is also a regression test.
10  *
11  *  This example is in C++ because we use C++11 threads to ensure that the feature is working as
12  *  expected. You can still use that feature from a C code.
13  */
14
15 #include "simgrid/Exception.hpp"
16 #include "simgrid/actor.h"
17 #include "simgrid/s4u.hpp"
18
19 #include <string>
20 #include <thread>
21
22 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
23 namespace sg4 = simgrid::s4u;
24
25 const std::thread::id root_id = std::this_thread::get_id();
26
27 static void ensure_root_tid()
28 {
29   std::thread::id this_id = std::this_thread::get_id();
30   xbt_assert(root_id == this_id, "I was supposed to be the main thread");
31   XBT_INFO("I am the main thread, as expected");
32 }
33 static void ensure_other_tid()
34 {
35   std::thread::id this_id = std::this_thread::get_id();
36   xbt_assert(this_id != root_id, "I was NOT supposed to be the main thread");
37   XBT_INFO("I am not the main thread, as expected");
38 }
39
40 static void sender()
41 {
42   ensure_root_tid();
43   auto* payload = new std::string("some message");
44   sg4::Mailbox::by_name("some mailbox")->put(payload, 10e8);
45 }
46
47 static void receiver()
48 {
49   ensure_other_tid();
50
51   sg4::Mailbox::by_name("some mailbox")->get_unique<std::string>();
52   XBT_INFO("Task received");
53 }
54
55 static void maestro(void* /* data */)
56 {
57   ensure_other_tid();
58   sg4::Actor::create("receiver", sg4::Host::by_name("Jupiter"), receiver);
59   sg4::Engine::get_instance()->run();
60 }
61
62 /** Main function */
63 int main(int argc, char* argv[])
64 {
65   /* Specify which code should be executed by maestro on another thread, once this current thread is affected to an
66    * actor by the subsequent sg_actor_attach(). This must be done before the creation of the engine. */
67   simgrid_set_maestro(maestro, nullptr);
68
69   sg4::Engine e(&argc, argv);
70
71   xbt_assert(argc == 2, "Usage: %s platform_file\n"
72                         "example: %s ../platforms/small_platform.xml\n",
73              argv[0], argv[0]);
74
75   e.load_platform(argv[1]);
76
77   /* Become one of the simulated actors (must be done after the platform creation, or the host won't exist). */
78   sg_actor_attach("sender", nullptr, e.host_by_name("Tremblay"), nullptr);
79
80   ensure_root_tid(); // Only useful in this test: we ensure that SimGrid is not broken and that this code is executed in
81                      // the correct system thread
82
83   // Execute the sender code. The root thread was actually turned into a regular actor
84   sender();
85
86   sg_actor_detach(); // The root thread becomes maestro again (as proved by the output)
87   XBT_INFO("Detached");
88   ensure_root_tid();
89
90   return 0;
91 }