1 /* Copyright (c) 2007-2018. 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 #include "src/kernel/activity/MailboxImpl.hpp"
7 #include "src/kernel/activity/CommImpl.hpp"
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_mailbox, simix, "Mailbox implementation");
11 static std::map<std::string, smx_mailbox_t>* mailboxes = new std::map<std::string, smx_mailbox_t>;
13 void SIMIX_mailbox_exit()
15 for (auto const& elm : *mailboxes)
20 /******************************************************************************/
21 /* Rendez-Vous Points */
22 /******************************************************************************/
27 /** @brief Returns the mailbox of that name, or nullptr */
28 MailboxImpl* MailboxImpl::byNameOrNull(const char* name)
30 auto mbox = mailboxes->find(name);
31 if (mbox != mailboxes->end())
36 /** @brief Returns the mailbox of that name, newly created on need */
37 MailboxImpl* MailboxImpl::byNameOrCreate(const char* name)
39 xbt_assert(name, "Mailboxes must have a name");
40 /* two processes may have pushed the same mbox_create simcall at the same time */
41 auto m = mailboxes->find(name);
42 if (m == mailboxes->end()) {
43 smx_mailbox_t mbox = new MailboxImpl(name);
44 XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
45 (*mailboxes)[mbox->name_] = mbox;
50 /** @brief set the receiver of the mailbox to allow eager sends
51 * \param actor The receiving dude
53 void MailboxImpl::setReceiver(s4u::ActorPtr actor)
56 this->permanent_receiver = actor.get()->get_impl();
58 this->permanent_receiver = nullptr;
60 /** @brief Pushes a communication activity into a mailbox
61 * @param comm What to add
63 void MailboxImpl::push(activity::CommImplPtr comm)
66 this->comm_queue.push_back(std::move(comm));
69 /** @brief Removes a communication activity from a mailbox
70 * @param activity What to remove
72 void MailboxImpl::remove(smx_activity_t activity)
74 simgrid::kernel::activity::CommImplPtr comm =
75 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(activity);
77 xbt_assert(comm->mbox == this, "Comm %p is in mailbox %s, not mailbox %s", comm.get(),
78 (comm->mbox ? comm->mbox->get_cname() : "(null)"), this->get_cname());
80 for (auto it = this->comm_queue.begin(); it != this->comm_queue.end(); it++)
82 this->comm_queue.erase(it);
85 xbt_die("Comm %p not found in mailbox %s", comm.get(), this->get_cname());