Logo AND Algorithmique Numérique Distribuée

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