1 /* Copyright (c) 2006-2019. 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>
11 #include <simgrid/s4u/Actor.hpp>
12 #include <xbt/string.hpp>
19 /** @brief Mailboxes: Network rendez-vous points. */
20 class XBT_PUBLIC Mailbox {
22 friend kernel::activity::MailboxImpl;
24 kernel::activity::MailboxImpl* const pimpl_;
26 explicit Mailbox(kernel::activity::MailboxImpl * mbox) : pimpl_(mbox) {}
30 /** private function, do not use. FIXME: make me protected */
31 kernel::activity::MailboxImpl* get_impl() const { return pimpl_; }
33 /** @brief Retrieves the name of that mailbox as a C++ string */
34 const xbt::string& get_name() const;
35 /** @brief Retrieves the name of that mailbox as a C string */
36 const char* get_cname() const;
38 /** Retrieve the mailbox associated to the given name */
39 static Mailbox* by_name(const std::string& name);
41 /** Returns whether the mailbox contains queued communications */
44 /** Check if there is a communication going on in a mailbox. */
47 /** Check if there is a communication ready to be consumed from a mailbox. */
50 /** Gets the first element in the queue (without dequeuing it), or nullptr if none is there */
51 kernel::activity::CommImplPtr front();
53 /** Declare that the specified actor is a permanent receiver on that mailbox
55 * It means that the communications sent to this mailbox will start flowing to
56 * its host even before it does a get(). This models the real behavior of TCP
57 * and MPI communications, amongst other. It will improve the accuracy of
58 * predictions, in particular if your application exhibits swarms of small messages.
60 * SimGrid does not enforces any kind of ownership over the mailbox. Even if a receiver
61 * was declared, any other actors can still get() data from the mailbox. The timings
62 * will then probably be off tracks, so you should strive on your side to not get data
63 * from someone else's mailbox.
65 * Note that being permanent receivers of a mailbox prevents actors to be garbage-collected.
66 * If your simulation creates many short-lived actors that marked as permanent receiver, you
67 * should call mailbox->set_receiver(nullptr) by the end of the actors so that their memory gets
68 * properly reclaimed. This call should be at the end of the actor's function, not in a on_exit
71 void set_receiver(ActorPtr actor);
73 /** Return the actor declared as permanent receiver, or nullptr if none **/
74 ActorPtr get_receiver();
76 /** Creates (but don't start) a data transmission to that mailbox */
78 /** Creates (but don't start) a data transmission to that mailbox */
79 CommPtr put_init(void* data, uint64_t simulated_size_in_bytes);
80 /** Creates and start a data transmission to that mailbox */
81 CommPtr put_async(void* data, uint64_t simulated_size_in_bytes);
83 smx_activity_t iprobe(int type, int (*match_fun)(void*, void*, kernel::activity::CommImpl*), void* data);
84 /** Blocking data transmission */
85 void put(void* payload, uint64_t simulated_size_in_bytes);
86 /** Blocking data transmission with timeout */
87 void put(void* payload, uint64_t simulated_size_in_bytes, double timeout);
89 /** Creates (but don't start) a data reception onto that mailbox */
91 /** Creates and start an async data reception to that mailbox */
92 CommPtr get_async(void** data);
94 /** Blocking data reception */
95 void* get(); // FIXME: make a typed template version
96 /** Blocking data reception with timeout */
97 void* get(double timeout);
101 } // namespace simgrid
103 #endif /* SIMGRID_S4U_MAILBOX_HPP */