Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'file' into 'master'
[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 Comm;
20   friend kernel::activity::MailboxImpl;
21
22   kernel::activity::MailboxImpl* const pimpl_;
23
24   explicit Mailbox(kernel::activity::MailboxImpl * mbox) : pimpl_(mbox) {}
25   ~Mailbox() = default;
26
27 public:
28   /** private function, do not use. FIXME: make me protected */
29   kernel::activity::MailboxImpl* get_impl() const { return pimpl_; }
30
31   /** @brief Retrieves the name of that mailbox as a C++ string */
32   const xbt::string& get_name() const;
33   /** @brief Retrieves the name of that mailbox as a C string */
34   const char* get_cname() const;
35
36   /** Retrieve the mailbox associated to the given name */
37   static Mailbox* by_name(const std::string& name);
38
39   /** Returns whether the mailbox contains queued communications */
40   bool empty();
41
42   /** Check if there is a communication going on in a mailbox. */
43   bool listen();
44
45   /** Check if there is a communication ready to be consumed from a mailbox. */
46   bool ready();
47
48   /** Gets the first element in the queue (without dequeuing it), or nullptr if none is there */
49   kernel::activity::CommImplPtr front();
50
51   /** Declare that the specified actor is a permanent receiver on that mailbox
52    *
53    * It means that the communications sent to this mailbox will start flowing to
54    * its host even before it does a get(). This models the real behavior of TCP
55    * and MPI communications, amongst other. It will improve the accuracy of
56    * predictions, in particular if your application exhibits swarms of small messages.
57    *
58    * SimGrid does not enforces any kind of ownership over the mailbox. Even if a receiver
59    * was declared, any other actors can still get() data from the mailbox. The timings
60    * will then probably be off tracks, so you should strive on your side to not get data
61    * from someone else's mailbox.
62    *
63    * Note that being permanent receivers of a mailbox prevents actors to be garbage-collected.
64    * If your simulation creates many short-lived actors that marked as permanent receiver, you
65    * should call mailbox->set_receiver(nullptr) by the end of the actors so that their memory gets
66    * properly reclaimed. This call should be at the end of the actor's function, not in a on_exit
67    * callback.
68    */
69   void set_receiver(ActorPtr actor);
70
71   /** Return the actor declared as permanent receiver, or nullptr if none **/
72   ActorPtr get_receiver();
73
74   /** Creates (but don't start) a data transmission to that mailbox */
75   CommPtr put_init();
76   /** Creates (but don't start) a data transmission to that mailbox */
77   CommPtr put_init(void* data, uint64_t simulated_size_in_bytes);
78   /** Creates and start a data transmission to that mailbox */
79   CommPtr put_async(void* data, uint64_t simulated_size_in_bytes);
80
81   smx_activity_t iprobe(int type, int (*match_fun)(void*, void*, kernel::activity::CommImpl*), void* data);
82   /** Blocking data transmission */
83   void put(void* payload, uint64_t simulated_size_in_bytes);
84   /** Blocking data transmission with timeout */
85   void put(void* payload, uint64_t simulated_size_in_bytes, double timeout);
86
87   /** Creates (but don't start) a data reception onto that mailbox */
88   CommPtr get_init();
89   /** Creates and start an async data reception to that mailbox */
90   CommPtr get_async(void** data);
91
92   /** Blocking data reception */
93   void* get(); // FIXME: make a typed template version
94   /** Blocking data reception with timeout */
95   void* get(double timeout);
96 };
97
98 } // namespace s4u
99 } // namespace simgrid
100
101 #endif /* SIMGRID_S4U_MAILBOX_HPP */