Logo AND Algorithmique Numérique Distribuée

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