Logo AND Algorithmique Numérique Distribuée

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