1 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_S4U_MAILBOX_HPP
7 #define SIMGRID_S4U_MAILBOX_HPP
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Actor.hpp>
11 #include <simgrid/s4u/Comm.hpp>
12 #include <smpi/forward.hpp>
13 #include <xbt/string.hpp>
21 /** @brief Mailboxes: Network rendez-vous points. */
22 class XBT_PUBLIC Mailbox {
25 friend kernel::activity::MailboxImpl;
27 kernel::activity::MailboxImpl* const pimpl_;
29 explicit Mailbox(kernel::activity::MailboxImpl * mbox) : pimpl_(mbox) {}
33 kernel::activity::MailboxImpl* get_impl() const { return pimpl_; }
36 /** @brief Retrieves the name of that mailbox as a C++ string */
37 const xbt::string& get_name() const;
38 /** @brief Retrieves the name of that mailbox as a C string */
39 const char* get_cname() const;
41 /** Retrieve the mailbox associated to the given name. Mailboxes are created on demand. */
42 static Mailbox* by_name(const std::string& name);
44 /** Returns whether the mailbox contains queued communications */
47 /** Check if there is a communication going on in a mailbox. */
50 /** Look if there is a communication going on in a mailbox and return the PID of the sender actor */
51 aid_t listen_from() const;
53 /** Check if there is a communication ready to be consumed from a mailbox. */
56 /** Gets the first element in the queue (without dequeuing it), or nullptr if none is there */
57 kernel::activity::CommImplPtr front() const;
59 /** Declare that the specified actor is a permanent receiver on that mailbox
61 * It means that the communications sent to this mailbox will start flowing to
62 * its host even before it does a get(). This models the real behavior of TCP
63 * and MPI communications, amongst other. It will improve the accuracy of
64 * predictions, in particular if your application exhibits swarms of small messages.
66 * SimGrid does not enforces any kind of ownership over the mailbox. Even if a receiver
67 * was declared, any other actors can still get() data from the mailbox. The timings
68 * will then probably be off tracks, so you should strive on your side to not get data
69 * from someone else's mailbox.
71 * Note that being permanent receivers of a mailbox prevents actors to be garbage-collected.
72 * If your simulation creates many short-lived actors that marked as permanent receiver, you
73 * should call mailbox->set_receiver(nullptr) by the end of the actors so that their memory gets
74 * properly reclaimed. This call should be at the end of the actor's function, not in an on_exit
77 void set_receiver(ActorPtr actor);
79 /** Return the actor declared as permanent receiver, or nullptr if none **/
80 ActorPtr get_receiver() const;
82 /** Creates (but don't start) a data transmission to that mailbox */
84 /** Creates (but don't start) a data transmission to that mailbox */
85 CommPtr put_init(void* data, uint64_t simulated_size_in_bytes);
86 /** Creates and start a data transmission to that mailbox */
87 CommPtr put_async(void* data, uint64_t simulated_size_in_bytes);
89 kernel::activity::ActivityImplPtr iprobe(int type, bool (*match_fun)(void*, void*, kernel::activity::CommImpl*),
91 /** Blocking data transmission */
92 void put(void* payload, uint64_t simulated_size_in_bytes);
93 /** Blocking data transmission with timeout */
94 void put(void* payload, uint64_t simulated_size_in_bytes, double timeout);
96 /** Creates (but don't start) a data reception onto that mailbox */
98 /** Creates and start an async data reception to that mailbox */
99 XBT_ATTRIB_DEPRECATED_v331("Please use typed template Mailbox::get_async<>()") CommPtr get_async(void** data);
100 template <typename T> CommPtr get_async(T** data);
102 /** Blocking data reception */
103 template <typename T> T* get();
104 XBT_ATTRIB_DEPRECATED_v331("Please use typed template Mailbox::get<>()") void* get();
105 template <typename T> std::unique_ptr<T> get_unique() { return std::unique_ptr<T>(get<T>()); }
107 /** Blocking data reception with timeout */
108 template <typename T> T* get(double timeout);
109 XBT_ATTRIB_DEPRECATED_v331("Please use typed template Mailbox::get<>()") void* get(double timeout);
110 template <typename T> std::unique_ptr<T> get_unique(double timeout) { return std::unique_ptr<T>(get<T>(timeout)); }
113 template <typename T> CommPtr Mailbox::get_async(T** data)
115 CommPtr res = get_init();
116 res->set_dst_data(reinterpret_cast<void**>(data), sizeof(void*));
117 res->vetoable_start();
121 template <typename T> T* Mailbox::get()
124 get_async<T>(&res)->wait();
128 template <typename T> T* Mailbox::get(double timeout)
131 get_async<T>(&res)->wait_for(timeout);
135 inline CommPtr Mailbox::get_async(void** data) // XBT_ATTRIB_DEPRECATED_v331
137 return get_async<void>(data);
139 inline void* Mailbox::get() // XBT_ATTRIB_DEPRECATED_v331
143 inline void* Mailbox::get(double timeout) // XBT_ATTRIB_DEPRECATED_v331
145 return get<void>(timeout);
149 } // namespace simgrid
151 #endif /* SIMGRID_S4U_MAILBOX_HPP */