Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'toufic' of github.com:Takishipp/simgrid
[simgrid.git] / src / kernel / activity / 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/kernel/activity/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 kernel {
26 namespace activity {
27 /** @brief Returns the mailbox of that name, or nullptr */
28 MailboxImpl* MailboxImpl::byNameOrNull(const char* name)
29 {
30   return static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
31 }
32 /** @brief Returns the mailbox of that name, newly created on need */
33 MailboxImpl* MailboxImpl::byNameOrCreate(const char* name)
34 {
35   xbt_assert(name, "Mailboxes must have a name");
36   /* two processes may have pushed the same mbox_create simcall at the same time */
37   smx_mailbox_t mbox = static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
38   if (!mbox) {
39     mbox = new MailboxImpl(name);
40     XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
41     xbt_dict_set(mailboxes, mbox->name_, mbox, nullptr);
42   }
43   return mbox;
44 }
45 /** @brief set the receiver of the mailbox to allow eager sends
46  *  \param actor The receiving dude
47  */
48 void MailboxImpl::setReceiver(s4u::ActorPtr actor)
49 {
50   this->permanent_receiver = actor.get()->getImpl();
51 }
52 /** @brief Pushes a communication activity into a mailbox
53  *  @param comm What to add
54  */
55 void MailboxImpl::push(activity::Comm* comm)
56 {
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 }
78 }