Logo AND Algorithmique Numérique Distribuée

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