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"
8 #include "src/kernel/activity/CommImpl.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_mailbox, simix, "Mailbox implementation");
12 static xbt_dict_t mailboxes = xbt_dict_new_homogeneous([](void* data) {
13 delete static_cast<smx_mailbox_t>(data);
16 void SIMIX_mailbox_exit()
18 xbt_dict_free(&mailboxes);
21 /******************************************************************************/
22 /* Rendez-Vous Points */
23 /******************************************************************************/
28 /** @brief Returns the mailbox of that name, or nullptr */
29 MailboxImpl* MailboxImpl::byNameOrNull(const char* name)
31 return static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
33 /** @brief Returns the mailbox of that name, newly created on need */
34 MailboxImpl* MailboxImpl::byNameOrCreate(const char* name)
36 xbt_assert(name, "Mailboxes must have a name");
37 /* two processes may have pushed the same mbox_create simcall at the same time */
38 smx_mailbox_t mbox = static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
40 mbox = new MailboxImpl(name);
41 XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
42 xbt_dict_set(mailboxes, mbox->name_, mbox, nullptr);
46 /** @brief set the receiver of the mailbox to allow eager sends
47 * \param actor The receiving dude
49 void MailboxImpl::setReceiver(s4u::ActorPtr actor)
51 this->permanent_receiver = actor.get()->getImpl();
53 /** @brief Pushes a communication activity into a mailbox
54 * @param comm What to add
56 void MailboxImpl::push(activity::CommImpl* comm)
58 this->comm_queue.push_back(comm);
62 /** @brief Removes a communication activity from a mailbox
63 * @param activity What to remove
65 void MailboxImpl::remove(smx_activity_t activity)
67 simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(activity);
70 for (auto it = this->comm_queue.begin(); it != this->comm_queue.end(); it++)
72 this->comm_queue.erase(it);
75 xbt_die("Cannot remove the comm %p that is not part of the mailbox %s", comm, this->name_);