Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'actor-startkilltime' of https://github.com/Takishipp/simgrid into Takis...
[simgrid.git] / include / simgrid / s4u / Mailbox.hpp
1 /* Copyright (c) 2006-2017. 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 <string>
10
11 #include <xbt/base.h>
12 #include <xbt/string.hpp>
13
14 #include <simgrid/s4u/forward.hpp>
15 #include <simgrid/s4u/Actor.hpp>
16
17 namespace simgrid {
18 namespace s4u {
19
20 /** @brief Mailboxes: Network rendez-vous points.
21  *  @ingroup s4u_api
22  *
23  * @tableofcontents
24  *
25  * @section s4u_mb_what What are mailboxes
26  *
27  * Rendez-vous point for network communications, similar to URLs on
28  * which you could post and retrieve data. Actually, the mailboxes are
29  * not involved in the communication once it starts, but only to find
30  * the contact with which you want to communicate.
31
32  * Here are some mechanisms similar to the mailbox in other
33  * communication systems: The phone number, which allows the caller to
34  * find the receiver. The twitter hashtag, which help senders and
35  * receivers to find each others. In TCP, the pair {host name, host
36  * port} to which you can connect to find your interlocutor. In HTTP,
37  * URLs through which the clients can connect to the servers.
38  *
39  * One big difference with most of these systems is that usually, no
40  * actor is the exclusive owner of a mailbox, neither in sending nor
41  * in receiving. Many actors can send into and/or receive from the
42  * same mailbox.  This is a big difference to the socket ports for
43  * example, that are definitely exclusive in receiving.
44  *
45  * A big difference with twitter hashtags is that SimGrid does not
46  * offer easy support to broadcast a given message to many
47  * receivers. So that would be like a twitter tag where each message
48  * is consumed by the first coming receiver.
49  *
50  * The mailboxes are not located on the network, and you can access
51  * them without any latency. The network delay are only related to the
52  * location of the sender and receiver once the match between them is
53  * done on the mailbox. This is just like the phone number that you
54  * can use locally, and the geographical distance only comes into play
55  * once you start the communication by dialling this number.
56  *
57  * @section s4u_mb_howto How to use mailboxes?
58  *
59  * Any existing mailbox can be retrieve from its name (which are
60  * unique strings, just like with twitter tags). This results in a
61  * versatile mechanism that can be used to build many different
62  * situations.
63  *
64  * For something close to classical socket communications, use
65  * "hostname:port" as mailbox names, and make sure that only one actor
66  * reads into that mailbox. It's hard to build a prefectly realistic
67  * model of the TCP sockets, but most of the time, this system is too
68  * cumbersome for your simulations anyway. You probably want something
69  * simpler, that turns our to be easy to build with the mailboxes.
70  *
71  * Many SimGrid examples use a sort of yellow page system where the
72  * mailbox names are the name of the service (such as "worker",
73  * "master" or "reducer"). That way, you don't have to know where your
74  * peer is located to contact it. You don't even need its name. Its
75  * function is enough for that. This also gives you some sort of load
76  * balancing for free if more than one actor pulls from the mailbox:
77  * the first relevant actor that can deal with the request will handle
78  * it.
79  *
80  * @section s4u_mb_matching How are sends and receives matched?
81  *
82  * The matching algorithm is as simple as a first come, first
83  * serve. When a new send arrives, it matches the oldest enqueued
84  * receive. If no receive is currently enqueued, then the incomming
85  * send is enqueued. As you can see, the mailbox cannot contain both
86  * send and receive requests: all enqueued requests must be of the
87  * same sort.
88  *
89  * @section s4u_mb_receiver Declaring a receiving actor
90  *
91  * The last twist is that by default in the simulator, the data starts
92  * to be exchanged only when both the sender and the receiver are
93  * declared while in real systems (such as TCP or MPI), the data
94  * starts to flow as soon as the sender posts it, even if the receiver
95  * did not post its recv() yet. This can obviously lead to bad
96  * simulation timings, as the simulated communications do not start at
97  * the exact same time than the real ones.
98  *
99  * If the simulation timings are very important to you, you can
100  * declare a specific receiver to a given mailbox (with the function
101  * setReceiver()). That way, any send() posted to that mailbox will
102  * start as soon as possible, and the data will already be there on
103  * the receiver host when the receiver actor posts its receive().
104  *
105  * @section s4u_mb_api The API
106  */
107 XBT_PUBLIC_CLASS Mailbox {
108   friend Comm;
109   friend simgrid::s4u::Engine;
110   friend simgrid::kernel::activity::MailboxImpl;
111
112   simgrid::kernel::activity::MailboxImpl* pimpl_;
113
114   explicit Mailbox(kernel::activity::MailboxImpl * mbox) : pimpl_(mbox) {}
115
116   /** private function to manage the mailboxes' lifetime (see @ref s4u_raii) */
117   friend void intrusive_ptr_add_ref(Mailbox*) {}
118   /** private function to manage the mailboxes' lifetime (see @ref s4u_raii) */
119   friend void intrusive_ptr_release(Mailbox*) {}
120 public:
121   /** private function, do not use. FIXME: make me protected */
122   kernel::activity::MailboxImpl* getImpl() { return pimpl_; }
123
124   /** @brief Retrieves the name of that mailbox as a C++ string */
125   const simgrid::xbt::string& getName() const;
126   /** @brief Retrieves the name of that mailbox as a C string */
127   const char* getCname() const;
128
129   /** Retrieve the mailbox associated to the given C string */
130   static MailboxPtr byName(const char *name);
131
132   /** Retrieve the mailbox associated to the given C++ string */
133   static MailboxPtr byName(std::string name);
134
135   /** Returns whether the mailbox contains queued communications */
136   bool empty();
137
138   /** Check if there is a communication going on in a mailbox. */
139   bool listen();
140
141   /** Gets the first element in the queue (without dequeuing it), or nullptr if none is there */
142   smx_activity_t front();
143
144   /** Declare that the specified actor is a permanent receiver on that mailbox
145    *
146    * It means that the communications sent to this mailbox will start flowing to
147    * its host even before he does a recv(). This models the real behavior of TCP
148    * and MPI communications, amongst other.
149    */
150   void setReceiver(ActorPtr actor);
151
152   /** Return the actor declared as permanent receiver, or nullptr if none **/
153   ActorPtr getReceiver();
154
155   /** Creates (but don't start) a data emission to that mailbox */
156   CommPtr put_init();
157   /** Creates (but don't start) a data emission to that mailbox */
158   CommPtr put_init(void* data, uint64_t simulatedSizeInBytes);
159   /** Creates and start a data emission to that mailbox */
160   CommPtr put_async(void* data, uint64_t simulatedSizeInBytes);
161
162   /** Blocking data emission */
163   void put(void* payload, uint64_t simulatedSizeInBytes);
164   /** Blocking data emission with timeout */
165   void put(void* payload, uint64_t simulatedSizeInBytes, double timeout);
166
167   /** Creates (but don't start) a data reception onto that mailbox */
168   CommPtr get_init();
169   /** Creates and start an async data reception to that mailbox */
170   CommPtr get_async(void** data);
171
172   /** Blocking data reception */
173   void* get();
174   /** Blocking data reception with timeout */
175   void* get(double timeout);
176 };
177
178 }} // namespace simgrid::s4u
179
180 #endif /* SIMGRID_S4U_MAILBOX_HPP */