Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
90233b5e76ba18da24c6308dfd6abe0acb185753
[simgrid.git] / src / s4u / s4u_mailbox.cpp
1 /* Copyright (c) 2006-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/log.h"
8
9 #include "../msg/msg_private.hpp"
10 #include "src/simix/ActorImpl.hpp"
11 #include "src/simix/smx_network_private.h"
12 #include "simgrid/s4u/Mailbox.hpp"
13
14 XBT_LOG_EXTERNAL_CATEGORY(s4u);
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_channel,s4u,"S4U Communication Mailboxes");
16
17 namespace simgrid {
18 namespace s4u {
19
20 const char *Mailbox::getName() {
21   return pimpl_->name;
22 }
23
24 MailboxPtr Mailbox::byName(const char*name)
25 {
26   // FIXME: there is a race condition here where two actors run Mailbox::byName
27   // on a non-existent mailbox during the same scheduling round. Both will be
28   // interrupted in the simcall creating the underlying simix mbox.
29   // Only one simix object will be created, but two S4U objects will be created.
30   // Only one S4U object will be stored in the hashmap and used, and the other
31   // one will be leaked.
32   smx_mailbox_t mbox = SIMIX_mbox_get_by_name(name);
33   if (mbox == nullptr)
34     mbox = simcall_mbox_create(name);
35   return MailboxPtr(&mbox->piface_, true);
36 }
37
38 MailboxPtr Mailbox::byName(std::string name)
39 {
40   return byName(name.c_str());
41 }
42
43 bool Mailbox::empty()
44 {
45   return pimpl_->comm_queue.empty();
46 }
47
48 smx_activity_t Mailbox::front()
49 {
50   return pimpl_->comm_queue.empty() ? nullptr : pimpl_->comm_queue.front();
51 }
52
53 void Mailbox::setReceiver(ActorPtr actor) {
54   simcall_mbox_set_receiver(pimpl_, actor == nullptr ? nullptr : actor->pimpl_);
55 }
56
57 /** @brief get the receiver (process associated to the mailbox) */
58 ActorPtr Mailbox::receiver() {
59   if(pimpl_->permanent_receiver == nullptr)
60     return ActorPtr();
61   return ActorPtr(&pimpl_->permanent_receiver->getIface());
62 }
63
64 }
65 }