Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use const& for the parameters of type std::string not affected by previous commit.
[simgrid.git] / include / simgrid / s4u / Mailbox.hpp
1 /* Copyright (c) 2006-2019. 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 <xbt/string.hpp>
10 #include <simgrid/s4u/Actor.hpp>
11
12 #include <string>
13
14 namespace simgrid {
15 namespace s4u {
16
17 /** @brief Mailboxes: Network rendez-vous points. */
18 class XBT_PUBLIC Mailbox {
19   friend simgrid::s4u::Comm;
20   friend simgrid::kernel::activity::MailboxImpl;
21
22   simgrid::kernel::activity::MailboxImpl* const pimpl_;
23
24   explicit Mailbox(kernel::activity::MailboxImpl * mbox) : pimpl_(mbox) {}
25
26   /** private function to manage the mailboxes' lifetime (see @ref s4u_raii) */
27   friend void intrusive_ptr_add_ref(Mailbox*) {}
28   /** private function to manage the mailboxes' lifetime (see @ref s4u_raii) */
29   friend void intrusive_ptr_release(Mailbox*) {}
30
31 public:
32   /** private function, do not use. FIXME: make me protected */
33   kernel::activity::MailboxImpl* get_impl() { return pimpl_; }
34
35   /** @brief Retrieves the name of that mailbox as a C++ string */
36   const simgrid::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 */
41   static MailboxPtr by_name(const std::string& name);
42
43   /** Returns whether the mailbox contains queued communications */
44   bool empty();
45
46   /** Check if there is a communication going on in a mailbox. */
47   bool listen();
48
49   /** Check if there is a communication ready to be consumed from a mailbox. */
50   bool ready();
51
52   /** Gets the first element in the queue (without dequeuing it), or nullptr if none is there */
53   smx_activity_t front();
54
55   /** Declare that the specified actor is a permanent receiver on that mailbox
56    *
57    * It means that the communications sent to this mailbox will start flowing to
58    * its host even before it does a get(). This models the real behavior of TCP
59    * and MPI communications, amongst other. It will improve the accuracy of
60    * predictions, in particular if your application exhibits swarms of small messages.
61    *
62    * SimGrid does not enforces any kind of ownership over the mailbox. Even if a receiver
63    * was declared, any other actors can still get() data from the mailbox. The timings
64    * will then probably be off tracks, so you should strive on your side to not get data
65    * from someone else's mailbox.
66    *
67    * Note that being permanent receivers of a mailbox prevents actors to be garbage-collected.
68    * If your simulation creates many short-lived actors that marked as permanent receiver, you
69    * should call mailbox->set_receiver(nullptr) by the end of the actors so that their memory gets
70    * properly reclaimed. This call should be at the end of the actor's function, not in a on_exit
71    * callback.
72    */
73   void set_receiver(ActorPtr actor);
74
75   /** Return the actor declared as permanent receiver, or nullptr if none **/
76   ActorPtr get_receiver();
77
78   /** Creates (but don't start) a data transmission to that mailbox */
79   CommPtr put_init();
80   /** Creates (but don't start) a data transmission to that mailbox */
81   CommPtr put_init(void* data, uint64_t simulated_size_in_bytes);
82   /** Creates and start a data transmission to that mailbox */
83   CommPtr put_async(void* data, uint64_t simulated_size_in_bytes);
84
85   /** Blocking data transmission */
86   void put(void* payload, uint64_t simulated_size_in_bytes);
87   /** Blocking data transmission with timeout */
88   void put(void* payload, uint64_t simulated_size_in_bytes, double timeout);
89
90   /** Creates (but don't start) a data reception onto that mailbox */
91   CommPtr get_init();
92   /** Creates and start an async data reception to that mailbox */
93   CommPtr get_async(void** data);
94
95   /** Blocking data reception */
96   void* get(); // FIXME: make a typed template version
97   /** Blocking data reception with timeout */
98   void* get(double timeout);
99
100   // Deprecated functions
101 #ifndef DOXYGEN
102   /** @deprecated Mailbox::set_receiver() */
103   XBT_ATTRIB_DEPRECATED_v323("Please use Mailbox::set_receiver()") void setReceiver(ActorPtr actor)
104   {
105     set_receiver(actor);
106   }
107   /** @deprecated Mailbox::get_receiver() */
108   XBT_ATTRIB_DEPRECATED_v323("Please use Mailbox::get_receiver()") ActorPtr getReceiver() { return get_receiver(); }
109   /** @deprecated Mailbox::get_name() */
110   XBT_ATTRIB_DEPRECATED_v323("Please use Mailbox::get_name()") const simgrid::xbt::string& getName() const
111   {
112     return get_name();
113   }
114   /** @deprecated Mailbox::get_cname() */
115   XBT_ATTRIB_DEPRECATED_v323("Please use Mailbox::get_cname()") const char* getCname() const { return get_cname(); }
116   /** @deprecated Mailbox::get_impl() */
117   XBT_ATTRIB_DEPRECATED_v323("Please use Mailbox::get_impl()") kernel::activity::MailboxImpl* getImpl()
118   {
119     return get_impl();
120   }
121   /** @deprecated Mailbox::by_name() */
122   XBT_ATTRIB_DEPRECATED_v323("Please use Mailbox::by_name()") static MailboxPtr byName(const char* name)
123   {
124     return by_name(name);
125   }
126   /** @deprecated Mailbox::by_name() */
127   XBT_ATTRIB_DEPRECATED_v323("Please use Mailbox::by_name()") static MailboxPtr byName(std::string name)
128   {
129     return by_name(name);
130   }
131 #endif
132 };
133
134 }} // namespace simgrid::s4u
135
136 #endif /* SIMGRID_S4U_MAILBOX_HPP */