Logo AND Algorithmique Numérique Distribuée

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