Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
587a72aad61047f9edace8b03dc86b405d4a0b67
[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 #include "src/msg/msg_private.h"
9 #include "src/msg/msg_mailbox.h"
10
11 #include "simgrid/s4u/mailbox.hpp"
12
13 XBT_LOG_EXTERNAL_CATEGORY(s4u);
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_channel,s4u,"S4U Communication Mailboxes");
15
16
17 using namespace simgrid;
18
19 boost::unordered_map <std::string, s4u::Mailbox *> *s4u::Mailbox::mailboxes = new boost::unordered_map<std::string, s4u::Mailbox*> ();
20
21
22 s4u::Mailbox::Mailbox(const char*name, smx_rdv_t inferior) {
23   p_inferior = inferior;
24   p_name = name;
25   mailboxes->insert({name, this});
26 }
27 const char *s4u::Mailbox::getName() {
28   return p_name.c_str();
29 }
30 s4u::Mailbox *s4u::Mailbox::byName(const char*name) {
31   s4u::Mailbox *res;
32   try {
33     res = mailboxes->at(name);
34   } catch (std::out_of_range& e) {
35     // FIXME: there is a potential race condition here where two actors run Mailbox::byName on a non-existent mailbox
36     // during the same scheduling round. Both will be interrupted in the simcall creating the underlying simix rdv.
37     // Only one simix object will be created, but two S4U objects will be created.
38     // Only one S4U object will be stored in the hashmap and used, and the other one will be leaked.
39     new Mailbox(name,simcall_rdv_create(name));
40     res = mailboxes->at(name); // Use the stored one, even if it's not the one I created myself.
41   }
42   return res;
43 }