From 5aaf4dee2a063da0b9e1dc52672872a43a0a8e10 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Mon, 9 May 2016 21:17:11 +0200 Subject: [PATCH] SIMIX_comm_destroy() -> simix::Comm::unref() --- src/simix/SynchroComm.cpp | 28 +++++++++++++++ src/simix/SynchroComm.hpp | 1 + src/simix/smx_network.cpp | 62 +++++++-------------------------- src/simix/smx_network_private.h | 1 - src/simix/smx_process.cpp | 6 ++-- 5 files changed, 44 insertions(+), 54 deletions(-) diff --git a/src/simix/SynchroComm.cpp b/src/simix/SynchroComm.cpp index e066252f22..5a5ccb5a52 100644 --- a/src/simix/SynchroComm.cpp +++ b/src/simix/SynchroComm.cpp @@ -73,6 +73,34 @@ double simgrid::simix::Comm::remains() } } +void simgrid::simix::Comm::unref() +{ + XBT_DEBUG("Destroy synchro %p (refcount: %d), state: %d", this, refcount, (int)state); + + xbt_assert(refcount > 0, + "This comm has a negative refcount! You must not call test() or wait() more than once on a given communication."); + + refcount--; + if (refcount > 0) + return; + XBT_DEBUG("Really free communication %p; refcount is now %d", this, refcount); + + cleanupSurf(); + + if (detached && state != SIMIX_DONE) { + /* the communication has failed and was detached: + * we have to free the buffer */ + if (clean_fun) + clean_fun(src_buff); + src_buff = NULL; + } + + if(mbox) + SIMIX_mbox_remove(mbox, this); + + delete this; +} + /** @brief This is part of the cleanup process, probably an internal command */ void simgrid::simix::Comm::cleanupSurf() { diff --git a/src/simix/SynchroComm.hpp b/src/simix/SynchroComm.hpp index 70359b2785..0f85b832cf 100644 --- a/src/simix/SynchroComm.hpp +++ b/src/simix/SynchroComm.hpp @@ -26,6 +26,7 @@ namespace simix { void resume(); void cancel(); double remains(); + void unref(); void cleanupSurf(); // FIXME: make me protected e_smx_comm_type_t type; /* Type of the communication (SIMIX_COMM_SEND or SIMIX_COMM_RECEIVE) */ diff --git a/src/simix/smx_network.cpp b/src/simix/smx_network.cpp index 60e790058c..40144d40ff 100644 --- a/src/simix/smx_network.cpp +++ b/src/simix/smx_network.cpp @@ -155,43 +155,6 @@ static smx_synchro_t _find_matching_comm(std::deque *deque, e_smx /******************************************************************************/ /* Communication synchros */ /******************************************************************************/ - -/** - * \brief Destroy a communicate synchro - * \param synchro The communicate synchro to be destroyed - */ -void SIMIX_comm_destroy(smx_synchro_t synchro) -{ - simgrid::simix::Comm *comm = static_cast(synchro); - - XBT_DEBUG("Destroy synchro %p (refcount: %d), state: %d", comm, comm->refcount, (int)comm->state); - - if (comm->refcount <= 0) { - xbt_backtrace_display_current(); - xbt_die("This comm has a negative refcount! You must not call test() or wait() more than once on a given communication."); - } - comm->refcount--; - if (comm->refcount > 0) - return; - XBT_DEBUG("Really free communication %p; refcount is now %d", comm, comm->refcount); - - comm->cleanupSurf(); - - if (comm->detached && comm->state != SIMIX_DONE) { - /* the communication has failed and was detached: - * we have to free the buffer */ - if (comm->clean_fun) { - comm->clean_fun(comm->src_buff); - } - comm->src_buff = NULL; - } - - if(comm->mbox) - SIMIX_mbox_remove(comm->mbox, comm); - - delete comm; -} - void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_process_t src, smx_mailbox_t mbox, double task_size, double rate, void *src_buff, size_t src_buff_size, @@ -215,7 +178,7 @@ smx_synchro_t simcall_HANDLER_comm_isend(smx_simcall_t simcall, smx_process_t sr XBT_DEBUG("send from %p", mbox); /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */ - smx_synchro_t this_synchro = new simgrid::simix::Comm(SIMIX_COMM_SEND); + simgrid::simix::Comm* this_synchro = new simgrid::simix::Comm(SIMIX_COMM_SEND); /* Look for communication synchro matching our needs. We also provide a description of * ourself so that the other side also gets a chance of choosing if it wants to match with us. @@ -244,8 +207,7 @@ smx_synchro_t simcall_HANDLER_comm_isend(smx_simcall_t simcall, smx_process_t sr } } else { XBT_DEBUG("Receive already pushed"); - - SIMIX_comm_destroy(this_synchro); + this_synchro->unref(); other_comm->state = SIMIX_READY; other_comm->type = SIMIX_COMM_READY; @@ -310,7 +272,7 @@ smx_synchro_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_mailbox_t mbox, void void *data, double rate) { XBT_DEBUG("recv from %p %p", mbox, mbox->comm_queue); - smx_synchro_t this_synchro = new simgrid::simix::Comm(SIMIX_COMM_RECEIVE); + 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 @@ -334,7 +296,7 @@ smx_synchro_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_mailbox_t mbox, void other_comm->mbox = NULL; } other_comm->refcount--; - SIMIX_comm_destroy(this_synchro); + static_cast(this_synchro)->unref(); } } else { /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */ @@ -350,7 +312,7 @@ smx_synchro_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_mailbox_t mbox, void other_synchro = this_synchro; SIMIX_mbox_push(mbox, this_synchro); } else { - SIMIX_comm_destroy(this_synchro); + this_synchro->unref(); simgrid::simix::Comm *other_comm = static_cast(other_synchro); other_comm->state = SIMIX_READY; @@ -392,24 +354,24 @@ smx_synchro_t SIMIX_comm_iprobe(smx_process_t dst_proc, smx_mailbox_t mbox, int int tag, int (*match_fun)(void *, void *, smx_synchro_t), void *data) { XBT_DEBUG("iprobe from %p %p", mbox, mbox->comm_queue); - smx_synchro_t this_synchro; + simgrid::simix::Comm* this_comm; int smx_type; if(type == 1){ - this_synchro = new simgrid::simix::Comm(SIMIX_COMM_SEND); + this_comm = new simgrid::simix::Comm(SIMIX_COMM_SEND); smx_type = SIMIX_COMM_RECEIVE; } else{ - this_synchro = new simgrid::simix::Comm(SIMIX_COMM_RECEIVE); + this_comm = new simgrid::simix::Comm(SIMIX_COMM_RECEIVE); smx_type = SIMIX_COMM_SEND; } smx_synchro_t other_synchro=NULL; 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); + _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_synchro,/*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) { @@ -417,7 +379,7 @@ smx_synchro_t SIMIX_comm_iprobe(smx_process_t dst_proc, smx_mailbox_t mbox, int other_comm->refcount--; } - SIMIX_comm_destroy(this_synchro); + this_comm->unref(); return other_synchro; } @@ -733,7 +695,7 @@ void SIMIX_comm_finish(smx_synchro_t synchro) } while (destroy_count-- > 0) - SIMIX_comm_destroy(synchro); + static_cast(synchro)->unref(); } /** diff --git a/src/simix/smx_network_private.h b/src/simix/smx_network_private.h index 8a60c4c427..1451a8e1cf 100644 --- a/src/simix/smx_network_private.h +++ b/src/simix/smx_network_private.h @@ -33,7 +33,6 @@ XBT_PRIVATE smx_synchro_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_mailbox_t int (*)(void *, void *, smx_synchro_t), void (*copy_data_fun)(smx_synchro_t, void*, size_t), void *data, double rate); -XBT_PRIVATE void SIMIX_comm_destroy(smx_synchro_t synchro); XBT_PRIVATE 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_PRIVATE void SIMIX_post_comm(smx_synchro_t synchro); diff --git a/src/simix/smx_process.cpp b/src/simix/smx_process.cpp index 1bd0a3b6a1..95e411c706 100644 --- a/src/simix/smx_process.cpp +++ b/src/simix/smx_process.cpp @@ -83,7 +83,7 @@ void SIMIX_process_cleanup(smx_process_t process) if (comm->detached) XBT_DEBUG("Don't destroy it since it's a detached comm and I'm the sender"); else - SIMIX_comm_destroy(comm); + comm->unref(); } else if (comm->dst_proc == process){ @@ -95,7 +95,7 @@ void SIMIX_process_cleanup(smx_process_t process) /* the comm will be freed right now, remove it from the sender */ xbt_fifo_remove(comm->src_proc->comms, comm); } - SIMIX_comm_destroy(comm); + comm->unref(); } else { xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro); } @@ -508,7 +508,7 @@ void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) { xbt_fifo_remove(process->comms, process->waiting_synchro); comm->cancel(); xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall); - SIMIX_comm_destroy(process->waiting_synchro); + comm->unref(); } else if (sleep != nullptr) { SIMIX_process_sleep_destroy(process->waiting_synchro); -- 2.20.1