Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
finish the conversion C++ of simix::MailboxImpl
[simgrid.git] / src / simix / MailboxImpl.cpp
1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/simix/MailboxImpl.hpp"
7 #include "src/kernel/activity/SynchroComm.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_mailbox, simix, "Mailbox implementation");
10
11 static xbt_dict_t mailboxes = xbt_dict_new_homogeneous([](void* data) {
12   delete static_cast<smx_mailbox_t>(data);
13 });
14
15 void SIMIX_mailbox_exit()
16 {
17   xbt_dict_free(&mailboxes);
18 }
19
20 /******************************************************************************/
21 /*                           Rendez-Vous Points                               */
22 /******************************************************************************/
23
24 namespace simgrid {
25 namespace simix {
26 /** @brief Returns the mailbox of that name, or nullptr */
27 MailboxImpl* MailboxImpl::byNameOrNull(const char* name)
28 {
29   return static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
30 }
31 /** @brief Returns the mailbox of that name, newly created on need */
32 MailboxImpl* MailboxImpl::byNameOrCreate(const char* name)
33 {
34   xbt_assert(name, "Mailboxes must have a name");
35   /* two processes may have pushed the same mbox_create simcall at the same time */
36   smx_mailbox_t mbox = static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
37   if (!mbox) {
38     mbox = new simgrid::simix::MailboxImpl(name);
39     XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
40     xbt_dict_set(mailboxes, mbox->name_, mbox, nullptr);
41   }
42   return mbox;
43 }
44 /** @brief set the receiver of the mailbox to allow eager sends
45  *  \param actor The receiving dude
46  */
47 void MailboxImpl::setReceiver(s4u::ActorPtr actor)
48 {
49   this->permanent_receiver = actor.get()->getImpl();
50 }
51 /** @brief Pushes a communication activity into a mailbox
52  *  @param activity What to add
53  */
54 void MailboxImpl::push(smx_activity_t synchro)
55 {
56   simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
57   this->comm_queue.push_back(comm);
58   comm->mbox = this;
59 }
60
61 /** @brief Removes a communication activity from a mailbox
62  *  @param activity What to remove
63  */
64 void MailboxImpl::remove(smx_activity_t activity)
65 {
66   simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(activity);
67
68   comm->mbox = nullptr;
69   for (auto it = this->comm_queue.begin(); it != this->comm_queue.end(); it++)
70     if (*it == comm) {
71       this->comm_queue.erase(it);
72       return;
73     }
74   xbt_die("Cannot remove the comm %p that is not part of the mailbox %s", comm, this->name_);
75 }
76 }
77 }