Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3c910f66328e3971547d21d0c59e6ee511eefd67
[simgrid.git] / include / simgrid / s4u / Mailbox.hpp
1 /* Copyright (c) 2006-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 #ifndef SIMGRID_S4U_MAILBOX_HPP
7 #define SIMGRID_S4U_MAILBOX_HPP
8
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Actor.hpp>
11 #include <simgrid/s4u/Comm.hpp>
12 #include <smpi/forward.hpp>
13
14 #include <memory>
15 #include <string>
16
17 namespace simgrid {
18 namespace s4u {
19
20 /** @brief Mailboxes: Network rendez-vous points. */
21 class XBT_PUBLIC Mailbox {
22 #ifndef DOXYGEN
23   friend Comm;
24   friend smpi::Request;
25   friend kernel::activity::MailboxImpl;
26 #endif
27
28   kernel::activity::MailboxImpl* const pimpl_;
29
30   explicit Mailbox(kernel::activity::MailboxImpl * mbox) : pimpl_(mbox) {}
31   ~Mailbox() = default;
32
33 protected:
34   kernel::activity::MailboxImpl* get_impl() const { return pimpl_; }
35
36 public:
37   /** @brief Retrieves the name of that mailbox as a C++ string */
38   const std::string& get_name() const;
39   /** @brief Retrieves the name of that mailbox as a C string */
40   const char* get_cname() const;
41
42   /** Retrieve the mailbox associated to the given name. Mailboxes are created on demand. */
43   static Mailbox* by_name(const std::string& name);
44
45   /** Returns whether the mailbox contains queued communications */
46   bool empty() const;
47
48   /* Returns the number of queued communications */
49   size_t size() const;
50
51   /** Check if there is a communication going on in a mailbox. */
52   bool listen() const;
53
54   /** Look if there is a communication going on in a mailbox and return the PID of the sender actor */
55   aid_t listen_from() const;
56
57   /** Check if there is a communication ready to be consumed from a mailbox.
58    * \beginrst
59    *  See :ref:`this example <s4u_ex_mailbox_ready>`.
60    * \endrst
61    */
62   bool ready() const;
63
64   /** Gets the first element in the queue (without dequeuing it), or nullptr if none is there */
65   kernel::activity::CommImplPtr front() const;
66
67   /** Declare that the specified actor is a permanent receiver on that mailbox
68    *
69    * It means that the communications sent to this mailbox will start flowing to
70    * its host even before it does a get(). This models the real behavior of TCP
71    * and MPI communications, amongst other. It will improve the accuracy of
72    * predictions, in particular if your application exhibits swarms of small messages.
73    *
74    * SimGrid does not enforces any kind of ownership over the mailbox. Even if a receiver
75    * was declared, any other actors can still get() data from the mailbox. The timings
76    * will then probably be off tracks, so you should strive on your side to not get data
77    * from someone else's mailbox.
78    *
79    * Note that being permanent receivers of a mailbox prevents actors to be garbage-collected.
80    * If your simulation creates many short-lived actors that marked as permanent receiver, you
81    * should call mailbox->set_receiver(nullptr) by the end of the actors so that their memory gets
82    * properly reclaimed. This call should be at the end of the actor's function, not in an on_exit
83    * callback.
84    */
85   void set_receiver(ActorPtr actor);
86
87   /** Return the actor declared as permanent receiver, or nullptr if none **/
88   ActorPtr get_receiver() const;
89
90   /** Creates (but don't start) a data transmission to that mailbox */
91   CommPtr put_init();
92   /** Creates (but don't start) a data transmission to that mailbox.
93    *
94    * Please note that if you send a pointer to some data, you must ensure that your data remains live during the
95    * communication, or the receiver will get a pointer to a garbled memory area.
96    */
97   CommPtr put_init(void* data, uint64_t simulated_size_in_bytes);
98   /** Creates and start a data transmission to that mailbox.
99    *
100    * Please note that if you send a pointer to some data, you must ensure that your data remains live during the
101    * communication, or the receiver will get a pointer to a garbled memory area.
102    */
103   CommPtr put_async(void* data, uint64_t simulated_size_in_bytes);
104
105   kernel::activity::ActivityImplPtr
106   iprobe(int type, const std::function<bool(void*, void*, kernel::activity::CommImpl*)>& match_fun, void* data);
107   /** Blocking data transmission.
108    *
109    * Please note that if you send a pointer to some data, you must ensure that your data remains live during the
110    * communication, or the receiver will get a pointer to a garbled memory area.
111    */
112   void put(void* payload, uint64_t simulated_size_in_bytes);
113   /** Blocking data transmission with timeout */
114   void put(void* payload, uint64_t simulated_size_in_bytes, double timeout);
115
116   /** Creates (but don't start) a data reception onto that mailbox.
117    * @verbatim embed:rst:inline You probably want to use :cpp:func:`simgrid::s4u::Comm::set_dst_data` and friends before
118    * starting that activity. @endverbatim  */
119   CommPtr get_init();
120   /** Creates and start an async data reception to that mailbox */
121   template <typename T> CommPtr get_async(T** data);
122
123   /** Blocking data reception */
124   template <typename T> T* get();
125   template <typename T> std::unique_ptr<T> get_unique() { return std::unique_ptr<T>(get<T>()); }
126
127   /** Blocking data reception with timeout */
128   template <typename T> T* get(double timeout);
129   template <typename T> std::unique_ptr<T> get_unique(double timeout) { return std::unique_ptr<T>(get<T>(timeout)); }
130
131   void clear();
132 };
133
134 template <typename T> CommPtr Mailbox::get_async(T** data)
135 {
136   CommPtr res = get_init()->set_dst_data(reinterpret_cast<void**>(data), sizeof(void*));
137   res->vetoable_start();
138   return res;
139 }
140
141 template <typename T> T* Mailbox::get()
142 {
143   T* res = nullptr;
144   get_async<T>(&res)->wait();
145   return res;
146 }
147
148 template <typename T> T* Mailbox::get(double timeout)
149 {
150   T* res = nullptr;
151   get_async<T>(&res)->wait_for(timeout);
152   return res;
153 }
154 } // namespace s4u
155 } // namespace simgrid
156
157 #endif /* SIMGRID_S4U_MAILBOX_HPP */