1 /* Copyright (c) 2006-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
8 #include "src/msg/msg_private.h"
9 #include "src/simix/smx_network_private.h"
10 #include "src/simix/smx_process_private.h"
12 #include "simgrid/s4u/mailbox.hpp"
14 XBT_LOG_EXTERNAL_CATEGORY(s4u);
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_channel,s4u,"S4U Communication Mailboxes");
20 boost::unordered_map <std::string, s4u::Mailbox *> *s4u::Mailbox::mailboxes = new boost::unordered_map<std::string, s4u::Mailbox*> ();
22 Mailbox::Mailbox(const char*name, smx_mailbox_t inferior) {
25 mailboxes->insert({name, this});
27 const char *Mailbox::getName() {
30 Mailbox *Mailbox::byName(const char*name) {
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 mbox.
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_mbox_create(name));
40 res = mailboxes->at(name); // Use the stored one, even if it's not the one I created myself.
45 bool Mailbox::empty() {
46 return nullptr == simcall_mbox_front(pimpl_);
49 void Mailbox::setReceiver(Actor* actor) {
50 simcall_mbox_set_receiver(pimpl_, actor == nullptr ? nullptr : actor->pimpl_);
53 /** @brief get the receiver (process associated to the mailbox) */
54 Actor& Mailbox::receiver() {
55 return pimpl_->permanent_receiver->actor();
61 /*------- C functions -------*/
63 sg_mbox_t sg_mbox_by_name(const char*name){
64 return simgrid::s4u::Mailbox::byName(name);
66 int sg_mbox_is_empty(sg_mbox_t mbox) {
69 void sg_mbox_setReceiver(sg_mbox_t mbox, smx_process_t process) {
70 mbox->setReceiver(&process->actor());
72 smx_process_t sg_mbox_receiver(sg_mbox_t mbox) {
73 return mbox->receiver().getInferior();