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/simix/MailboxImpl.hpp"
7 #include "src/kernel/activity/SynchroComm.hpp"
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_mailbox, simix, "Mailbox implementation");
11 static void SIMIX_mbox_free(void* data);
12 static xbt_dict_t mailboxes = xbt_dict_new_homogeneous(SIMIX_mbox_free);
14 void SIMIX_mailbox_exit()
16 xbt_dict_free(&mailboxes);
18 void SIMIX_mbox_free(void* data)
20 XBT_DEBUG("mbox free %p", data);
21 smx_mailbox_t mbox = static_cast<smx_mailbox_t>(data);
25 /******************************************************************************/
26 /* Rendez-Vous Points */
27 /******************************************************************************/
30 * \brief set the receiver of the rendez vous point to allow eager sends
31 * \param mbox The rendez-vous point
32 * \param process The receiving process
34 void SIMIX_mbox_set_receiver(smx_mailbox_t mbox, smx_actor_t process)
36 mbox->permanent_receiver = process;
41 /** @brief Returns the mailbox of that name, or nullptr */
42 MailboxImpl* MailboxImpl::byNameOrNull(const char* name)
44 return static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
46 /** @brief Returns the mailbox of that name, newly created on need */
47 MailboxImpl* MailboxImpl::byNameOrCreate(const char* name)
49 xbt_assert(name, "Mailboxes must have a name");
50 /* two processes may have pushed the same mbox_create simcall at the same time */
51 smx_mailbox_t mbox = static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
53 mbox = new simgrid::simix::MailboxImpl(name);
54 XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
55 xbt_dict_set(mailboxes, mbox->name_, mbox, nullptr);
59 /** @brief Pushes a communication activity into a mailbox
60 * @param activity What to add
62 void MailboxImpl::push(smx_activity_t synchro)
64 simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
65 this->comm_queue.push_back(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::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(activity);
77 for (auto it = this->comm_queue.begin(); it != this->comm_queue.end(); it++)
79 this->comm_queue.erase(it);
82 xbt_die("Cannot remove the comm %p that is not part of the mailbox %s", comm, this->name_);