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);
19 /******************************************************************************/
20 /* Rendez-Vous Points */
21 /******************************************************************************/
23 smx_mailbox_t SIMIX_mbox_create(const char* name)
25 xbt_assert(name, "Mailboxes must have a name");
26 /* two processes may have pushed the same mbox_create simcall at the same time */
27 smx_mailbox_t mbox = static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
29 mbox = new simgrid::simix::MailboxImpl(name);
30 XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
31 xbt_dict_set(mailboxes, mbox->name_, mbox, nullptr);
36 void SIMIX_mbox_free(void* data)
38 XBT_DEBUG("mbox free %p", data);
39 smx_mailbox_t mbox = static_cast<smx_mailbox_t>(data);
43 smx_mailbox_t SIMIX_mbox_get_by_name(const char* name)
45 return static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
49 * \brief set the receiver of the rendez vous point to allow eager sends
50 * \param mbox The rendez-vous point
51 * \param process The receiving process
53 void SIMIX_mbox_set_receiver(smx_mailbox_t mbox, smx_actor_t process)
55 mbox->permanent_receiver = process;
59 * \brief Pushes a communication synchro into a rendez-vous point
60 * \param mbox The mailbox
61 * \param synchro The communication synchro
63 void SIMIX_mbox_push(smx_mailbox_t mbox, smx_activity_t synchro)
65 simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
66 mbox->comm_queue.push_back(comm);
71 * \brief Removes a communication synchro from a rendez-vous point
72 * \param mbox The rendez-vous point
73 * \param synchro The communication synchro
75 void SIMIX_mbox_remove(smx_mailbox_t mbox, smx_activity_t synchro)
77 simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
80 for (auto it = mbox->comm_queue.begin(); it != mbox->comm_queue.end(); it++)
82 mbox->comm_queue.erase(it);
85 xbt_die("Cannot remove the comm %p that is not part of the mailbox %s", comm, mbox->name_);