Logo AND Algorithmique Numérique Distribuée

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