Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
(partially) convert simix::MailboxImpl to C++
[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 void SIMIX_mbox_free(void* data);
12 static xbt_dict_t mailboxes = xbt_dict_new_homogeneous(SIMIX_mbox_free);
13
14 void SIMIX_mailbox_exit()
15 {
16   xbt_dict_free(&mailboxes);
17 }
18 void SIMIX_mbox_free(void* data)
19 {
20   XBT_DEBUG("mbox free %p", data);
21   smx_mailbox_t mbox = static_cast<smx_mailbox_t>(data);
22   delete mbox;
23 }
24
25 /******************************************************************************/
26 /*                           Rendez-Vous Points                               */
27 /******************************************************************************/
28
29 /**
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
33  */
34 void SIMIX_mbox_set_receiver(smx_mailbox_t mbox, smx_actor_t process)
35 {
36   mbox->permanent_receiver = process;
37 }
38
39 namespace simgrid {
40 namespace simix {
41 /** @brief Returns the mailbox of that name, or nullptr */
42 MailboxImpl* MailboxImpl::byNameOrNull(const char* name)
43 {
44   return static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
45 }
46 /** @brief Returns the mailbox of that name, newly created on need */
47 MailboxImpl* MailboxImpl::byNameOrCreate(const char* name)
48 {
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));
52   if (!mbox) {
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);
56   }
57   return mbox;
58 }
59 /** @brief Pushes a communication activity into a mailbox
60  *  @param activity What to add
61  */
62 void MailboxImpl::push(smx_activity_t synchro)
63 {
64   simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
65   this->comm_queue.push_back(comm);
66   comm->mbox = this;
67 }
68
69 /** @brief Removes a communication activity from a mailbox
70  *  @param activity What to remove
71  */
72 void MailboxImpl::remove(smx_activity_t activity)
73 {
74   simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(activity);
75
76   comm->mbox = nullptr;
77   for (auto it = this->comm_queue.begin(); it != this->comm_queue.end(); it++)
78     if (*it == comm) {
79       this->comm_queue.erase(it);
80       return;
81     }
82   xbt_die("Cannot remove the comm %p that is not part of the mailbox %s", comm, this->name_);
83 }
84 }
85 }