Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
allow to get the number of queued communications in a Mailbox
[simgrid.git] / include / simgrid / s4u / Mailbox.hpp
1 /* Copyright (c) 2006-2021. 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   friend Comm;
24   friend smpi::Request;
25   friend kernel::activity::MailboxImpl;
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 xbt::string& get_name() const;
38   /** @brief Retrieves the name of that mailbox as a C string */
39   const char* get_cname() const;
40
41   /** 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   unsigned int 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   bool ready() const;
58
59   /** Gets the first element in the queue (without dequeuing it), or nullptr if none is there */
60   kernel::activity::CommImplPtr front() const;
61
62   /** Declare that the specified actor is a permanent receiver on that mailbox
63    *
64    * It means that the communications sent to this mailbox will start flowing to
65    * its host even before it does a get(). This models the real behavior of TCP
66    * and MPI communications, amongst other. It will improve the accuracy of
67    * predictions, in particular if your application exhibits swarms of small messages.
68    *
69    * SimGrid does not enforces any kind of ownership over the mailbox. Even if a receiver
70    * was declared, any other actors can still get() data from the mailbox. The timings
71    * will then probably be off tracks, so you should strive on your side to not get data
72    * from someone else's mailbox.
73    *
74    * Note that being permanent receivers of a mailbox prevents actors to be garbage-collected.
75    * If your simulation creates many short-lived actors that marked as permanent receiver, you
76    * should call mailbox->set_receiver(nullptr) by the end of the actors so that their memory gets
77    * properly reclaimed. This call should be at the end of the actor's function, not in an on_exit
78    * callback.
79    */
80   void set_receiver(ActorPtr actor);
81
82   /** Return the actor declared as permanent receiver, or nullptr if none **/
83   ActorPtr get_receiver() const;
84
85   /** Creates (but don't start) a data transmission to that mailbox */
86   CommPtr put_init();
87   /** Creates (but don't start) a data transmission to that mailbox.
88    *
89    * Please note that if you send a pointer to some data, you must ensure that your data remains live during the
90    * communication, or the receiver will get a pointer to a garbled memory area.
91    */
92   CommPtr put_init(void* data, uint64_t simulated_size_in_bytes);
93   /** Creates and start a data transmission to that mailbox.
94    *
95    * Please note that if you send a pointer to some data, you must ensure that your data remains live during the
96    * communication, or the receiver will get a pointer to a garbled memory area.
97    */
98   CommPtr put_async(void* data, uint64_t simulated_size_in_bytes);
99
100   kernel::activity::ActivityImplPtr iprobe(int type, bool (*match_fun)(void*, void*, kernel::activity::CommImpl*),
101                                            void* data);
102   /** Blocking data transmission.
103    *
104    * Please note that if you send a pointer to some data, you must ensure that your data remains live during the
105    * communication, or the receiver will get a pointer to a garbled memory area.
106    */
107   void put(void* payload, uint64_t simulated_size_in_bytes);
108   /** Blocking data transmission with timeout */
109   void put(void* payload, uint64_t simulated_size_in_bytes, double timeout);
110
111   /** Creates (but don't start) a data reception onto that mailbox */
112   CommPtr get_init();
113   /** Creates and start an async data reception to that mailbox */
114   XBT_ATTRIB_DEPRECATED_v331("Please use typed template Mailbox::get_async<>()") CommPtr get_async(void** data);
115   template <typename T> CommPtr get_async(T** data);
116
117   /** Blocking data reception */
118   template <typename T> T* get();
119   XBT_ATTRIB_DEPRECATED_v331("Please use typed template Mailbox::get<>()") void* get();
120   template <typename T> std::unique_ptr<T> get_unique() { return std::unique_ptr<T>(get<T>()); }
121
122   /** Blocking data reception with timeout */
123   template <typename T> T* get(double timeout);
124   XBT_ATTRIB_DEPRECATED_v331("Please use typed template Mailbox::get<>()") void* get(double timeout);
125   template <typename T> std::unique_ptr<T> get_unique(double timeout) { return std::unique_ptr<T>(get<T>(timeout)); }
126 };
127
128 template <typename T> CommPtr Mailbox::get_async(T** data)
129 {
130   CommPtr res = get_init()->set_dst_data(reinterpret_cast<void**>(data), sizeof(void*));
131   res->vetoable_start();
132   return res;
133 }
134
135 template <typename T> T* Mailbox::get()
136 {
137   T* res = nullptr;
138   get_async<T>(&res)->wait();
139   return res;
140 }
141
142 template <typename T> T* Mailbox::get(double timeout)
143 {
144   T* res = nullptr;
145   get_async<T>(&res)->wait_for(timeout);
146   return res;
147 }
148
149 inline CommPtr Mailbox::get_async(void** data) // XBT_ATTRIB_DEPRECATED_v331
150 {
151   return get_async<void>(data);
152 }
153 inline void* Mailbox::get() // XBT_ATTRIB_DEPRECATED_v331
154 {
155   return get<void>();
156 }
157 inline void* Mailbox::get(double timeout) // XBT_ATTRIB_DEPRECATED_v331
158 {
159   return get<void>(timeout);
160 }
161
162 } // namespace s4u
163 } // namespace simgrid
164
165 #endif /* SIMGRID_S4U_MAILBOX_HPP */