-/* Copyright (c) 2009-2015. The SimGrid Team.
- * All rights reserved. */
+/* Copyright (c) 2009-2016. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#include "mc/mc.h"
#include "src/mc/mc_replay.h"
#include "xbt/dict.h"
+#include "simgrid/s4u/mailbox.hpp"
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related synchronization");
-static xbt_dict_t mailboxes = NULL;
+static void SIMIX_mbox_free(void *data);
+static xbt_dict_t mailboxes = xbt_dict_new_homogeneous(SIMIX_mbox_free);
static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall);
static void SIMIX_comm_copy_data(smx_synchro_t comm);
static smx_synchro_t SIMIX_comm_new(e_smx_comm_type_t type);
static inline void SIMIX_mbox_push(smx_mailbox_t mbox, smx_synchro_t comm);
-static smx_synchro_t SIMIX_fifo_probe_comm(xbt_fifo_t fifo, e_smx_comm_type_t type,
- int (*match_fun)(void *, void *,smx_synchro_t),
- void *user_data, smx_synchro_t my_synchro);
-static smx_synchro_t SIMIX_fifo_get_comm(xbt_fifo_t fifo, e_smx_comm_type_t type,
- int (*match_fun)(void *, void *,smx_synchro_t),
- void *user_data, smx_synchro_t my_synchro);
-static void SIMIX_mbox_free(void *data);
+static smx_synchro_t _find_matching_comm(std::deque<smx_synchro_t> *deque, e_smx_comm_type_t type,
+ int (*match_fun)(void *, void *,smx_synchro_t), void *user_data, smx_synchro_t my_synchro, bool remove_matching);
static void SIMIX_comm_start(smx_synchro_t synchro);
-void SIMIX_network_init(void)
-{
- mailboxes = xbt_dict_new_homogeneous(SIMIX_mbox_free);
-}
-
-void SIMIX_network_exit(void)
+void SIMIX_mailbox_exit(void)
{
xbt_dict_free(&mailboxes);
}
smx_mailbox_t SIMIX_mbox_create(const char *name)
{
+ 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 = name ? (smx_mailbox_t) xbt_dict_get_or_null(mailboxes, name) : NULL;
+ smx_mailbox_t mbox = (smx_mailbox_t) xbt_dict_get_or_null(mailboxes, name);
if (!mbox) {
mbox = xbt_new0(s_smx_mailbox_t, 1);
- mbox->name = name ? xbt_strdup(name) : NULL;
- mbox->comm_fifo = xbt_fifo_new();
- mbox->done_comm_fifo = xbt_fifo_new();
+ 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=NULL;
XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name);
-
- if (mbox->name)
- xbt_dict_set(mailboxes, mbox->name, mbox, NULL);
+ xbt_dict_set(mailboxes, mbox->name, mbox, NULL);
}
return mbox;
}
-void SIMIX_mbox_destroy(smx_mailbox_t mbox)
-{
- if (mbox->name)
- xbt_dict_remove(mailboxes, mbox->name);
-}
-
void SIMIX_mbox_free(void *data)
{
XBT_DEBUG("mbox free %p", data);
smx_mailbox_t mbox = (smx_mailbox_t) data;
xbt_free(mbox->name);
- xbt_fifo_free(mbox->comm_fifo);
- xbt_fifo_free(mbox->done_comm_fifo);
+ delete mbox->comm_queue;
+ delete mbox->done_comm_queue;
xbt_free(mbox);
}
return (smx_mailbox_t) xbt_dict_get_or_null(mailboxes, name);
}
-int SIMIX_mbox_comm_count_by_host(smx_mailbox_t mbox, sg_host_t host)
-{
- smx_synchro_t comm = NULL;
- xbt_fifo_item_t item = NULL;
- int count = 0;
-
- xbt_fifo_foreach(mbox->comm_fifo, item, comm, smx_synchro_t) {
- if (comm->comm.src_proc->host == host)
- count++;
- }
-
- return count;
-}
-
smx_synchro_t SIMIX_mbox_get_head(smx_mailbox_t mbox)
{
- return (smx_synchro_t) xbt_fifo_get_item_content(
- xbt_fifo_get_first_item(mbox->comm_fifo));
+ return mbox->comm_queue->empty()? nullptr:mbox->comm_queue->front();
}
/**
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>();
}
/**
*/
static inline void SIMIX_mbox_push(smx_mailbox_t mbox, smx_synchro_t comm)
{
- xbt_fifo_push(mbox->comm_fifo, comm);
+ mbox->comm_queue->push_back(comm);
comm->comm.mbox = mbox;
}
*/
void SIMIX_mbox_remove(smx_mailbox_t mbox, smx_synchro_t comm)
{
- xbt_fifo_remove(mbox->comm_fifo, comm);
comm->comm.mbox = NULL;
+ for (auto it = mbox->comm_queue->begin(); it != mbox->comm_queue->end(); it++)
+ if (*it == comm) {
+ mbox->comm_queue->erase(it);
+ return;
+ }
+ xbt_die("Cannot remove this comm that is not part of the mailbox");
}
/**
- * \brief Checks if there is a communication synchro queued in a fifo matching our needs
+ * \brief Checks if there is a communication synchro queued in a deque matching our needs
* \param type The type of communication we are looking for (comm_send, comm_recv)
* \return The communication synchro if found, NULL otherwise
*/
-smx_synchro_t SIMIX_fifo_get_comm(xbt_fifo_t fifo, e_smx_comm_type_t type,
- int (*match_fun)(void *, void *,smx_synchro_t),
- void *this_user_data, smx_synchro_t my_synchro)
+static smx_synchro_t _find_matching_comm(std::deque<smx_synchro_t> *deque, e_smx_comm_type_t type,
+ int (*match_fun)(void *, void *,smx_synchro_t), void *this_user_data, smx_synchro_t my_synchro, bool remove_matching)
{
- smx_synchro_t synchro;
- xbt_fifo_item_t item;
void* other_user_data = NULL;
- xbt_fifo_foreach(fifo, item, synchro, smx_synchro_t) {
+ for(auto it = deque->begin(); it != deque->end(); it++){
+ smx_synchro_t synchro = *it;
if (synchro->comm.type == SIMIX_COMM_SEND) {
other_user_data = synchro->comm.src_data;
} else if (synchro->comm.type == SIMIX_COMM_RECEIVE) {
other_user_data = synchro->comm.dst_data;
}
if (synchro->comm.type == type &&
- (!match_fun || match_fun(this_user_data, other_user_data, synchro)) &&
+ (!match_fun || match_fun(this_user_data, other_user_data, synchro)) &&
(!synchro->comm.match_fun || synchro->comm.match_fun(other_user_data, this_user_data, my_synchro))) {
XBT_DEBUG("Found a matching communication synchro %p", synchro);
- xbt_fifo_remove_item(fifo, item);
- xbt_fifo_free_item(item);
+ if (remove_matching)
+ deque->erase(it);
synchro->comm.refcount++;
#if HAVE_MC
synchro->comm.mbox_cpy = synchro->comm.mbox;
return NULL;
}
-
-/**
- * \brief Checks if there is a communication synchro queued in a fifo matching our needs, but leave it there
- * \param type The type of communication we are looking for (comm_send, comm_recv)
- * \return The communication synchro if found, NULL otherwise
- */
-smx_synchro_t SIMIX_fifo_probe_comm(xbt_fifo_t fifo, e_smx_comm_type_t type,
- int (*match_fun)(void *, void *,smx_synchro_t),
- void *this_user_data, smx_synchro_t my_synchro)
-{
- smx_synchro_t synchro;
- xbt_fifo_item_t item;
- void* other_user_data = NULL;
-
- xbt_fifo_foreach(fifo, item, synchro, smx_synchro_t) {
- if (synchro->comm.type == SIMIX_COMM_SEND) {
- other_user_data = synchro->comm.src_data;
- } else if (synchro->comm.type == SIMIX_COMM_RECEIVE) {
- other_user_data = synchro->comm.dst_data;
- }
- if (synchro->comm.type == type &&
- (!match_fun || match_fun(this_user_data, other_user_data, synchro)) &&
- (!synchro->comm.match_fun || synchro->comm.match_fun(other_user_data, this_user_data, my_synchro))) {
- XBT_DEBUG("Found a matching communication synchro %p", synchro);
- synchro->comm.refcount++;
-
- return synchro;
- }
- XBT_DEBUG("Sorry, communication synchro %p does not match our needs:"
- " its type is %d but we are looking for a comm of type %d (or maybe the filtering didn't match)",
- synchro, (int)synchro->comm.type, (int)type);
- }
- XBT_DEBUG("No matching communication synchro found");
- return NULL;
-}
/******************************************************************************/
/* Communication synchros */
/******************************************************************************/
* 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 */
- smx_synchro_t other_synchro = SIMIX_fifo_get_comm(mbox->comm_fifo, SIMIX_COMM_RECEIVE, match_fun, data, this_synchro);
+ smx_synchro_t other_synchro =
+ _find_matching_comm(mbox->comm_queue, SIMIX_COMM_RECEIVE, match_fun, data, this_synchro, /*remove_matching*/true);
if (!other_synchro) {
other_synchro = this_synchro;
other_synchro->state = SIMIX_READY;
other_synchro->comm.dst_proc=mbox->permanent_receiver;
other_synchro->comm.refcount++;
- xbt_fifo_push(mbox->done_comm_fifo,other_synchro);
+ mbox->done_comm_queue->push_back(other_synchro);
other_synchro->comm.mbox=mbox;
XBT_DEBUG("pushing a message into the permanent receive fifo %p, comm %p", mbox, &(other_synchro->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_fifo);
+ XBT_DEBUG("recv from %p %p", mbox, mbox->comm_queue);
smx_synchro_t this_synchro = SIMIX_comm_new(SIMIX_COMM_RECEIVE);
smx_synchro_t other_synchro;
//communication already done, get it inside the fifo of completed comms
- if (mbox->permanent_receiver && xbt_fifo_size(mbox->done_comm_fifo)!=0) {
+ if (mbox->permanent_receiver && ! 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 = SIMIX_fifo_get_comm(mbox->done_comm_fifo, SIMIX_COMM_SEND, match_fun, data, this_synchro);
+ 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 = SIMIX_fifo_get_comm(mbox->comm_fifo, SIMIX_COMM_SEND, match_fun, data, this_synchro);
+ 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 %d", xbt_fifo_size(mbox->comm_fifo));
+ 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_fifo);
+ XBT_DEBUG("iprobe from %p %p", mbox, mbox->comm_queue);
smx_synchro_t this_synchro;
int smx_type;
if(type == 1){
smx_type = SIMIX_COMM_SEND;
}
smx_synchro_t other_synchro=NULL;
- if(mbox->permanent_receiver && xbt_fifo_size(mbox->done_comm_fifo)!=0){
- //find a match in the already received fifo
- XBT_DEBUG("first try in the perm recv mailbox");
-
- other_synchro = SIMIX_fifo_probe_comm(
- mbox->done_comm_fifo, (e_smx_comm_type_t) smx_type,
- match_fun, data, this_synchro);
+ if(mbox->permanent_receiver && ! 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_synchro,/*remove_matching*/false);
}
- // }else{
- if(!other_synchro){
- XBT_DEBUG("try in the normal mailbox");
- other_synchro = SIMIX_fifo_probe_comm(
- mbox->comm_fifo, (e_smx_comm_type_t) smx_type,
- match_fun, data, this_synchro);
- }
-// }
- if(other_synchro)other_synchro->comm.refcount--;
+ 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_synchro,/*remove_matching*/false);
+ }
+ if(other_synchro)
+ other_synchro->comm.refcount--;
SIMIX_comm_destroy(this_synchro);
return other_synchro;
*/
double SIMIX_comm_get_remains(smx_synchro_t synchro)
{
- double remains;
-
- if(!synchro){
+ if(!synchro)
return 0;
- }
+ double remains;
switch (synchro->state) {
case SIMIX_RUNNING:
void SIMIX_comm_copy_pointer_callback(smx_synchro_t comm, void* buff, size_t buff_size)
{
- xbt_assert((buff_size == sizeof(void *)),
- "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
+ xbt_assert((buff_size == sizeof(void *)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
*(void **) (comm->comm.dst_buff) = buff;
}