X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/b6dde5ff6f208b83545a4b4e9e81712d0d8617a1..0abc9e8c70d2f3c5c98ed3429d4b630683f77c22:/src/s4u/s4u_Comm.cpp diff --git a/src/s4u/s4u_Comm.cpp b/src/s4u/s4u_Comm.cpp index fdaf3bcbda..7e9d64857b 100644 --- a/src/s4u/s4u_Comm.cpp +++ b/src/s4u/s4u_Comm.cpp @@ -35,10 +35,10 @@ Comm::~Comm() int Comm::wait_any_for(const std::vector* comms, double timeout) { - std::unique_ptr rcomms(new kernel::activity::CommImpl*[comms->size()]); - std::transform(begin(*comms), end(*comms), rcomms.get(), + std::vector rcomms(comms->size()); + std::transform(begin(*comms), end(*comms), begin(rcomms), [](const CommPtr& comm) { return static_cast(comm->pimpl_.get()); }); - int changed_pos = simcall_comm_waitany(rcomms.get(), comms->size(), timeout); + int changed_pos = simcall_comm_waitany(rcomms.data(), rcomms.size(), timeout); if (changed_pos != -1) comms->at(changed_pos)->release_dependencies(); return changed_pos; @@ -97,7 +97,7 @@ CommPtr Comm::set_dst_data(void** buff) return this; } -size_t Comm::get_dst_data_size() +size_t Comm::get_dst_data_size() const { xbt_assert(state_ == State::FINISHED, "You cannot use %s before your communication terminated", __FUNCTION__); return dst_buff_size_; @@ -138,6 +138,10 @@ Comm* Comm::start() } else { xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver"); } + + if (suspended_) + pimpl_->suspend(); + state_ = State::STARTED; return this; } @@ -194,10 +198,10 @@ Comm* Comm::wait_for(double timeout) int Comm::test_any(const std::vector* comms) { - std::unique_ptr rcomms(new kernel::activity::CommImpl*[comms->size()]); - std::transform(begin(*comms), end(*comms), rcomms.get(), + std::vector rcomms(comms->size()); + std::transform(begin(*comms), end(*comms), begin(rcomms), [](const CommPtr& comm) { return static_cast(comm->pimpl_.get()); }); - int changed_pos = simcall_comm_testany(rcomms.get(), comms->size()); + int changed_pos = simcall_comm_testany(rcomms.data(), rcomms.size()); if (changed_pos != -1) comms->at(changed_pos)->release_dependencies(); return changed_pos; @@ -225,7 +229,8 @@ Comm* Comm::cancel() bool Comm::test() { - xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED); + xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING || + state_ == State::FINISHED); if (state_ == State::FINISHED) return true; @@ -241,31 +246,97 @@ bool Comm::test() return false; } -Mailbox* Comm::get_mailbox() +Mailbox* Comm::get_mailbox() const { return mailbox_; } -Actor* Comm::get_sender() +Actor* Comm::get_sender() const { - return sender_ ? sender_->ciface() : nullptr; + kernel::actor::ActorImplPtr sender = nullptr; + if (pimpl_) + sender = boost::static_pointer_cast(pimpl_)->src_actor_; + return sender ? sender->get_ciface() : nullptr; } } // namespace s4u } // namespace simgrid /* **************************** Public C interface *************************** */ +void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*)) +{ + comm->detach(clean_function); + comm->unref(); +} +void sg_comm_unref(sg_comm_t comm) +{ + comm->unref(); +} +int sg_comm_test(sg_comm_t comm) +{ + bool finished = comm->test(); + if (finished) + comm->unref(); + return finished; +} + +sg_error_t sg_comm_wait(sg_comm_t comm) +{ + sg_error_t status = SG_OK; + + simgrid::s4u::CommPtr s4u_comm(comm, false); + try { + s4u_comm->wait_for(-1); + } catch (const simgrid::TimeoutException&) { + status = SG_ERROR_TIMEOUT; + } catch (const simgrid::CancelException&) { + status = SG_ERROR_CANCELED; + } catch (const simgrid::NetworkFailureException&) { + status = SG_ERROR_NETWORK; + } + return status; +} + +sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout) +{ + sg_error_t status = SG_OK; + + simgrid::s4u::CommPtr s4u_comm(comm, false); + try { + s4u_comm->wait_for(timeout); + } catch (const simgrid::TimeoutException&) { + status = SG_ERROR_TIMEOUT; + } catch (const simgrid::CancelException&) { + status = SG_ERROR_CANCELED; + } catch (const simgrid::NetworkFailureException&) { + status = SG_ERROR_NETWORK; + } + return status; +} + +void sg_comm_wait_all(sg_comm_t* comms, size_t count) +{ + std::vector s4u_comms; + for (unsigned int i = 0; i < count; i++) + s4u_comms.emplace_back(comms[i], false); + + simgrid::s4u::Comm::wait_all(&s4u_comms); +} + int sg_comm_wait_any(sg_comm_t* comms, size_t count) { return sg_comm_wait_any_for(comms, count, -1); } + int sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout) { std::vector s4u_comms; - for (unsigned int i = 0; i < count; i++) { - s4u_comms.emplace_back(comms[i]); - } + for (unsigned int i = 0; i < count; i++) + s4u_comms.emplace_back(comms[i], false); + int pos = simgrid::s4u::Comm::wait_any_for(&s4u_comms, timeout); - if (pos != -1) - s4u_comms[pos]->unref(); + for (unsigned i = 0; i < count; i++) { + if (pos != -1 && static_cast(pos) != i) + s4u_comms[i]->add_ref(); + } return pos; }