Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add getName()/getCname() in kernel::activity::MailboxImpl.
[simgrid.git] / src / kernel / activity / MailboxImpl.cpp
1 /* Copyright (c) 2007-2017. 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 #include "src/kernel/activity/MailboxImpl.hpp"
7
8 #include "src/kernel/activity/CommImpl.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_mailbox, simix, "Mailbox implementation");
11
12 static std::map<std::string, smx_mailbox_t>* mailboxes = new std::map<std::string, smx_mailbox_t>;
13
14 void SIMIX_mailbox_exit()
15 {
16   for (auto const& elm : *mailboxes)
17     delete elm.second;
18   delete mailboxes;
19 }
20
21 /******************************************************************************/
22 /*                           Rendez-Vous Points                               */
23 /******************************************************************************/
24
25 namespace simgrid {
26 namespace kernel {
27 namespace activity {
28 /** @brief Returns the mailbox of that name, or nullptr */
29 MailboxImpl* MailboxImpl::byNameOrNull(const char* name)
30 {
31   auto mbox = mailboxes->find(name);
32   if (mbox != mailboxes->end())
33     return mbox->second;
34   else
35     return nullptr;
36 }
37 /** @brief Returns the mailbox of that name, newly created on need */
38 MailboxImpl* MailboxImpl::byNameOrCreate(const char* name)
39 {
40   xbt_assert(name, "Mailboxes must have a name");
41   /* two processes may have pushed the same mbox_create simcall at the same time */
42   auto m = mailboxes->find(name);
43   if (m == mailboxes->end()) {
44     smx_mailbox_t mbox = new MailboxImpl(name);
45     XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
46     (*mailboxes)[mbox->name_] = mbox;
47     return mbox;
48   } else
49     return m->second;
50 }
51 /** @brief set the receiver of the mailbox to allow eager sends
52  *  \param actor The receiving dude
53  */
54 void MailboxImpl::setReceiver(s4u::ActorPtr actor)
55 {
56   this->permanent_receiver = actor.get()->getImpl();
57 }
58 /** @brief Pushes a communication activity into a mailbox
59  *  @param comm What to add
60  */
61 void MailboxImpl::push(activity::CommImplPtr comm)
62 {
63   comm->mbox = this;
64   this->comm_queue.push_back(std::move(comm));
65 }
66
67 /** @brief Removes a communication activity from a mailbox
68  *  @param activity What to remove
69  */
70 void MailboxImpl::remove(smx_activity_t activity)
71 {
72   simgrid::kernel::activity::CommImplPtr comm =
73       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(activity);
74
75   xbt_assert(comm->mbox == this, "Comm %p is in mailbox %s, not mailbox %s", comm.get(),
76              (comm->mbox ? comm->mbox->getCname() : "(null)"), this->getCname());
77   comm->mbox = nullptr;
78   for (auto it = this->comm_queue.begin(); it != this->comm_queue.end(); it++)
79     if (*it == comm) {
80       this->comm_queue.erase(it);
81       return;
82     }
83   xbt_die("Comm %p not found in mailbox %s", comm.get(), this->getCname());
84 }
85 }
86 }
87 }