#include <string>
-#include <boost/unordered_map.hpp>
+#include <boost/intrusive_ptr.hpp>
#include <xbt/base.h>
*/
XBT_PUBLIC_CLASS Mailbox {
friend Comm;
+ friend simgrid::s4u::Engine;
+ friend simgrid::simix::Mailbox;
+
+ smx_mailbox_t pimpl_;
+
+ Mailbox(smx_mailbox_t mbox): pimpl_(mbox) {}
-private:
- Mailbox(const char*name, smx_mailbox_t inferior);
-public:
- ~Mailbox();
-
protected:
smx_mailbox_t getInferior() { return pimpl_; }
public:
+
+ // We don't have to manage the lifetime of mailboxes:
+ friend void intrusive_ptr_add_ref(Mailbox*) {}
+ friend void intrusive_ptr_release(Mailbox*) {}
+ using Ptr = boost::intrusive_ptr<Mailbox>;
+
/** Get the name of that mailbox */
const char *getName();
+
/** Retrieve the mailbox associated to the given string */
- static Mailbox *byName(const char *name);
+ static Ptr byName(const char *name);
+
/** Returns whether the mailbox contains queued communications */
bool empty();
* This models the real behavior of TCP and MPI communications, amongst other.
*/
void setReceiver(Actor* process);
+
/** Return the process declared as permanent receiver, or nullptr if none **/
Actor& receiver();
-
-private:
- std::string name_;
- smx_mailbox_t pimpl_;
- static boost::unordered_map<std::string, Mailbox *> *mailboxes;
- friend s4u::Engine;
};
+
+using MailboxPtr = Mailbox::Ptr;
+
}} // namespace simgrid::s4u
XBT_PUBLIC(sg_mbox_t) sg_mbox_by_name(const char*name);
namespace simgrid {
namespace s4u {
-boost::unordered_map <std::string, s4u::Mailbox *> *s4u::Mailbox::mailboxes = new boost::unordered_map<std::string, s4u::Mailbox*> ();
-
-Mailbox::Mailbox(const char*name, smx_mailbox_t inferior) {
- pimpl_ = inferior;
- name_ = name;
- mailboxes->insert({name, this});
-}
const char *Mailbox::getName() {
- return name_.c_str();
+ return pimpl_->name;
}
-Mailbox *Mailbox::byName(const char*name) {
- s4u::Mailbox *res;
- try {
- res = mailboxes->at(name);
- } catch (std::out_of_range& e) {
- // FIXME: there is a potential race condition here where two actors run Mailbox::byName on a non-existent mailbox
- // during the same scheduling round. Both will be interrupted in the simcall creating the underlying simix mbox.
- // Only one simix object will be created, but two S4U objects will be created.
- // Only one S4U object will be stored in the hashmap and used, and the other one will be leaked.
- new Mailbox(name,simcall_mbox_create(name));
- res = mailboxes->at(name); // Use the stored one, even if it's not the one I created myself.
- }
- return res;
+
+MailboxPtr Mailbox::byName(const char*name) {
+ // FIXME: there is a race condition here where two actors run Mailbox::byName
+ // on a non-existent mailbox during the same scheduling round. Both will be
+ // interrupted in the simcall creating the underlying simix mbox.
+ // Only one simix object will be created, but two S4U objects will be created.
+ // Only one S4U object will be stored in the hashmap and used, and the other
+ // one will be leaked.
+ smx_mailbox_t mbox = simcall_mbox_get_by_name(name);
+ if (mbox == nullptr)
+ mbox = simcall_mbox_create(name);
+ return MailboxPtr(&mbox->mbox_, true);
}
bool Mailbox::empty() {
/*------- C functions -------*/
sg_mbox_t sg_mbox_by_name(const char*name){
- return simgrid::s4u::Mailbox::byName(name);
+ return simgrid::s4u::Mailbox::byName(name).get();
}
int sg_mbox_is_empty(sg_mbox_t mbox) {
return mbox->empty();
xbt_assert(name, "Mailboxes must have a name");
/* two processes may have pushed the same mbox_create simcall at the same time */
smx_mailbox_t mbox = (smx_mailbox_t) xbt_dict_get_or_null(mailboxes, name);
-
if (!mbox) {
- mbox = new s_smx_mailbox_t();
- mbox->name = xbt_strdup(name);
- mbox->comm_queue = new std::deque<smx_synchro_t>();
- mbox->done_comm_queue = nullptr; // Allocated on need only
- mbox->permanent_receiver=nullptr;
-
+ mbox = new simgrid::simix::Mailbox(name);
XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
xbt_dict_set(mailboxes, mbox->name, mbox, nullptr);
}
{
XBT_DEBUG("mbox free %p", data);
smx_mailbox_t mbox = (smx_mailbox_t) data;
- xbt_free(mbox->name);
- delete mbox->comm_queue;
- delete mbox->done_comm_queue;
delete mbox;
}
*/
void SIMIX_mbox_set_receiver(smx_mailbox_t mbox, smx_process_t process)
{
- mbox->permanent_receiver=process;
- if (mbox->done_comm_queue == nullptr)
- mbox->done_comm_queue = new std::deque<smx_synchro_t>();
+ mbox->permanent_receiver = process;
}
/**
static inline void SIMIX_mbox_push(smx_mailbox_t mbox, smx_synchro_t synchro)
{
simgrid::simix::Comm *comm = static_cast<simgrid::simix::Comm*>(synchro);
-
- mbox->comm_queue->push_back(comm);
+ mbox->comm_queue.push_back(comm);
comm->mbox = mbox;
}
simgrid::simix::Comm *comm = static_cast<simgrid::simix::Comm*>(synchro);
comm->mbox = nullptr;
- for (auto it = mbox->comm_queue->begin(); it != mbox->comm_queue->end(); it++)
+ for (auto it = mbox->comm_queue.begin(); it != mbox->comm_queue.end(); it++)
if (*it == comm) {
- mbox->comm_queue->erase(it);
+ mbox->comm_queue. erase(it);
return;
}
xbt_die("Cannot remove this comm that is not part of the mailbox");
*
* If it is not found then push our communication into the rendez-vous point */
smx_synchro_t other_synchro =
- _find_matching_comm(mbox->comm_queue, SIMIX_COMM_RECEIVE, match_fun, data, this_synchro, /*remove_matching*/true);
+ _find_matching_comm(&mbox->comm_queue, SIMIX_COMM_RECEIVE, match_fun, data, this_synchro, /*remove_matching*/true);
simgrid::simix::Comm *other_comm = static_cast<simgrid::simix::Comm*>(other_synchro);
other_synchro->state = SIMIX_READY;
other_comm->dst_proc=mbox->permanent_receiver.get();
other_comm->ref();
- mbox->done_comm_queue->push_back(other_synchro);
+ mbox->done_comm_queue.push_back(other_synchro);
other_comm->mbox=mbox;
XBT_DEBUG("pushing a message into the permanent receive fifo %p, comm %p", mbox, &(other_comm));
void (*copy_data_fun)(smx_synchro_t, void*, size_t), // used to copy data if not default one
void *data, double rate)
{
- XBT_DEBUG("recv from %p %p", mbox, mbox->comm_queue);
+ XBT_DEBUG("recv from %p %p", mbox, &mbox->comm_queue);
simgrid::simix::Comm* this_synchro = new simgrid::simix::Comm(SIMIX_COMM_RECEIVE);
smx_synchro_t other_synchro;
//communication already done, get it inside the fifo of completed comms
- if (mbox->permanent_receiver && ! mbox->done_comm_queue->empty()) {
+ if (mbox->permanent_receiver != nullptr && ! mbox->done_comm_queue.empty()) {
XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
//find a match in the already received fifo
- other_synchro = _find_matching_comm(mbox->done_comm_queue, SIMIX_COMM_SEND, match_fun, data, this_synchro,/*remove_matching*/true);
+ other_synchro = _find_matching_comm(&mbox->done_comm_queue, SIMIX_COMM_SEND, match_fun, data, this_synchro,/*remove_matching*/true);
//if not found, assume the receiver came first, register it to the mailbox in the classical way
if (!other_synchro) {
XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request into fifo");
* ourself so that the other side also gets a chance of choosing if it wants to match with us.
*
* If it is not found then push our communication into the rendez-vous point */
- other_synchro = _find_matching_comm(mbox->comm_queue, SIMIX_COMM_SEND, match_fun, data, this_synchro,/*remove_matching*/true);
+ other_synchro = _find_matching_comm(&mbox->comm_queue, SIMIX_COMM_SEND, match_fun, data, this_synchro,/*remove_matching*/true);
if (!other_synchro) {
- XBT_DEBUG("Receive pushed first %zu", mbox->comm_queue->size());
+ XBT_DEBUG("Receive pushed first %zu", mbox->comm_queue.size());
other_synchro = this_synchro;
SIMIX_mbox_push(mbox, this_synchro);
} else {
smx_synchro_t SIMIX_comm_iprobe(smx_process_t dst_proc, smx_mailbox_t mbox, int type, int src,
int tag, int (*match_fun)(void *, void *, smx_synchro_t), void *data)
{
- XBT_DEBUG("iprobe from %p %p", mbox, mbox->comm_queue);
+ XBT_DEBUG("iprobe from %p %p", mbox, &mbox->comm_queue);
simgrid::simix::Comm* this_comm;
int smx_type;
if(type == 1){
smx_type = SIMIX_COMM_SEND;
}
smx_synchro_t other_synchro=nullptr;
- if(mbox->permanent_receiver && ! mbox->done_comm_queue->empty()){
+ if (mbox->permanent_receiver != nullptr && !mbox->done_comm_queue.empty()) {
XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
- other_synchro =
- _find_matching_comm(mbox->done_comm_queue, (e_smx_comm_type_t) smx_type, match_fun, data, this_comm,/*remove_matching*/false);
+ other_synchro = _find_matching_comm(&mbox->done_comm_queue,
+ (e_smx_comm_type_t) smx_type, match_fun, data, this_comm,/*remove_matching*/false);
}
if (!other_synchro){
XBT_DEBUG("check if we have more luck in the normal mailbox");
- other_synchro = _find_matching_comm(mbox->comm_queue, (e_smx_comm_type_t) smx_type, match_fun, data, this_comm,/*remove_matching*/false);
+ other_synchro = _find_matching_comm(&mbox->comm_queue,
+ (e_smx_comm_type_t) smx_type, match_fun, data, this_comm,/*remove_matching*/false);
}
if(other_synchro)