Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c30d78496669e7c2b09110a2c2c23bb87dd70873
[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
19 /******************************************************************************/
20 /*                           Rendez-Vous Points                               */
21 /******************************************************************************/
22
23 smx_mailbox_t SIMIX_mbox_create(const char* name)
24 {
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));
28   if (!mbox) {
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);
32   }
33   return mbox;
34 }
35
36 void SIMIX_mbox_free(void* data)
37 {
38   XBT_DEBUG("mbox free %p", data);
39   smx_mailbox_t mbox = static_cast<smx_mailbox_t>(data);
40   delete mbox;
41 }
42
43 smx_mailbox_t SIMIX_mbox_get_by_name(const char* name)
44 {
45   return static_cast<smx_mailbox_t>(xbt_dict_get_or_null(mailboxes, name));
46 }
47
48 /**
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
52  */
53 void SIMIX_mbox_set_receiver(smx_mailbox_t mbox, smx_actor_t process)
54 {
55   mbox->permanent_receiver = process;
56 }
57
58 /**
59  *  \brief Pushes a communication synchro into a rendez-vous point
60  *  \param mbox The mailbox
61  *  \param synchro The communication synchro
62  */
63 void SIMIX_mbox_push(smx_mailbox_t mbox, smx_activity_t synchro)
64 {
65   simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
66   mbox->comm_queue.push_back(comm);
67   comm->mbox = mbox;
68 }
69
70 /**
71  *  \brief Removes a communication synchro from a rendez-vous point
72  *  \param mbox The rendez-vous point
73  *  \param synchro The communication synchro
74  */
75 void SIMIX_mbox_remove(smx_mailbox_t mbox, smx_activity_t synchro)
76 {
77   simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
78
79   comm->mbox = nullptr;
80   for (auto it = mbox->comm_queue.begin(); it != mbox->comm_queue.end(); it++)
81     if (*it == comm) {
82       mbox->comm_queue.erase(it);
83       return;
84     }
85   xbt_die("Cannot remove the comm %p that is not part of the mailbox %s", comm, mbox->name_);
86 }