Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f01330b53f892861110fd9812fe22b14d2412f8e
[simgrid.git] / include / simgrid / s4u / mailbox.hpp
1 /* Copyright (c) 2006-2015. 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 <boost/intrusive_ptr.hpp>
12
13 #include <xbt/base.h>
14
15 #include <simgrid/s4u/forward.hpp>
16 #include <simgrid/s4u/Actor.hpp>
17
18 namespace simgrid {
19 namespace s4u {
20
21 /** @brief Mailboxes
22  *
23  * Rendez-vous point for network communications, similar to URLs on which you could post and retrieve data.
24  * They are not network locations: you can post and retrieve on a given mailbox from anywhere on the network.
25  * You can access any mailbox without any latency. The network delay are only related to the location of the
26  * sender and receiver.
27  */
28 XBT_PUBLIC_CLASS Mailbox {
29   friend Comm;
30   friend simgrid::s4u::Engine;
31   friend simgrid::simix::Mailbox;
32
33   simgrid::simix::Mailbox *pimpl_;
34
35   Mailbox(smx_mailbox_t mbox): pimpl_(mbox) {}
36
37 public:
38   smx_mailbox_t getImpl() { return pimpl_; } // FIXME: make me protected
39
40   // We don't have to manage the lifetime of mailboxes:
41   friend void intrusive_ptr_add_ref(Mailbox*) {}
42   friend void intrusive_ptr_release(Mailbox*) {}
43   using Ptr = boost::intrusive_ptr<Mailbox>;
44
45   /** Get the name of that mailbox */
46   const char *getName();
47
48   /** Retrieve the mailbox associated to the given string */
49   static Ptr byName(const char *name);
50
51   /** Returns whether the mailbox contains queued communications */
52   bool empty();
53
54   /** Returns the first element in the queue, or nullptr if none is there */
55   smx_synchro_t front();
56
57   /** Declare that the specified process is a permanent receiver on that mailbox
58    *
59    * It means that the communications sent to this mailbox will start flowing to its host even before he does a recv().
60    * This models the real behavior of TCP and MPI communications, amongst other.
61    */
62   void setReceiver(ActorPtr process);
63
64   /** Return the process declared as permanent receiver, or nullptr if none **/
65   ActorPtr receiver();
66 };
67
68 using MailboxPtr = Mailbox::Ptr;
69
70 }} // namespace simgrid::s4u
71
72 XBT_PUBLIC(sg_mbox_t) sg_mbox_by_name(const char*name);
73 XBT_PUBLIC(int) sg_mbox_is_empty(sg_mbox_t mbox);
74 XBT_PUBLIC(void)sg_mbox_setReceiver(sg_mbox_t mbox, smx_process_t process);
75
76 #endif /* SIMGRID_S4U_MAILBOX_HPP */