Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Allocate Actors on the heap and return ActorPtr
[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/unordered_map.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
31 private:
32   Mailbox(const char*name, smx_mailbox_t inferior);
33 public:
34   ~Mailbox();
35   
36 protected:
37   smx_mailbox_t getInferior() { return pimpl_; }
38
39 public:
40   /** Get the name of that mailbox */
41   const char *getName();
42   /** Retrieve the mailbox associated to the given string */
43   static Mailbox *byName(const char *name);
44   /** Returns whether the mailbox contains queued communications */
45   bool empty();
46
47   /** Declare that the specified process is a permanent receiver on that mailbox
48    *
49    * It means that the communications sent to this mailbox will start flowing to its host even before he does a recv().
50    * This models the real behavior of TCP and MPI communications, amongst other.
51    */
52   void setReceiver(Actor* process);
53   /** Return the process declared as permanent receiver, or nullptr if none **/
54   Actor& receiver();
55
56 private:
57   std::string name_;
58   smx_mailbox_t pimpl_;
59   static boost::unordered_map<std::string, Mailbox *> *mailboxes;
60   friend s4u::Engine;
61 };
62 }} // namespace simgrid::s4u
63
64 XBT_PUBLIC(sg_mbox_t) sg_mbox_by_name(const char*name);
65 XBT_PUBLIC(int) sg_mbox_is_empty(sg_mbox_t mbox);
66 XBT_PUBLIC(void)sg_mbox_setReceiver(sg_mbox_t mbox, smx_process_t process);
67 XBT_PUBLIC(smx_process_t) sg_mbox_receiver(sg_mbox_t mbox);
68
69 #endif /* SIMGRID_S4U_MAILBOX_HPP */