1 /* Copyright (c) 2007-2017. 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/SynchroComm.hpp"
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_mailbox, simix, "Mailbox implementation");
11 static xbt_dict_t mailboxes = xbt_dict_new_homogeneous([](void* data) {
12 delete static_cast<smx_mailbox_t>(data);
15 void SIMIX_mailbox_exit()
17 xbt_dict_free(&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 return static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
32 /** @brief Returns the mailbox of that name, newly created on need */
33 MailboxImpl* MailboxImpl::byNameOrCreate(const char* name)
35 xbt_assert(name, "Mailboxes must have a name");
36 /* two processes may have pushed the same mbox_create simcall at the same time */
37 smx_mailbox_t mbox = static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
39 mbox = new MailboxImpl(name);
40 XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
41 xbt_dict_set(mailboxes, mbox->name_, mbox, nullptr);
45 /** @brief set the receiver of the mailbox to allow eager sends
46 * \param actor The receiving dude
48 void MailboxImpl::setReceiver(s4u::ActorPtr actor)
50 this->permanent_receiver = actor.get()->getImpl();
52 /** @brief Pushes a communication activity into a mailbox
53 * @param comm What to add
55 void MailboxImpl::push(activity::Comm* comm)
57 this->comm_queue.push_back(comm);
61 /** @brief Removes a communication activity from a mailbox
62 * @param activity What to remove
64 void MailboxImpl::remove(smx_activity_t activity)
66 simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(activity);
69 for (auto it = this->comm_queue.begin(); it != this->comm_queue.end(); it++)
71 this->comm_queue.erase(it);
74 xbt_die("Cannot remove the comm %p that is not part of the mailbox %s", comm, this->name_);