X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/c217e2c9296581e5d2ab70fee58236fc66f90681..594b10a112b66208df1de4b26d65d93d601a128e:/src/s4u/s4u_Comm.cpp diff --git a/src/s4u/s4u_Comm.cpp b/src/s4u/s4u_Comm.cpp index b39648d097..c770e2ca8d 100644 --- a/src/s4u/s4u_Comm.cpp +++ b/src/s4u/s4u_Comm.cpp @@ -6,6 +6,7 @@ #include "src/msg/msg_private.hpp" #include "xbt/log.h" +#include "simgrid/Exception.hpp" #include "simgrid/s4u/Comm.hpp" #include "simgrid/s4u/Mailbox.hpp" @@ -29,21 +30,14 @@ Comm::~Comm() } } -int Comm::wait_any_for(std::vector* comms_in, double timeout) +int Comm::wait_any_for(std::vector* comms, double timeout) { - // Map to dynar: - xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), nullptr); - for (auto const& comm : *comms_in) { - if (comm->state_ == Activity::State::INITED) - comm->start(); - xbt_assert(comm->state_ == Activity::State::STARTED); - simgrid::kernel::activity::ActivityImpl* ptr = comm->pimpl_.get(); - xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, ptr); - } - // Call the underlying simcall: - int idx = simcall_comm_waitany(comms, timeout); - xbt_dynar_free(&comms); - return idx; + std::unique_ptr rcomms( + new simgrid::kernel::activity::CommImpl*[comms->size()]); + std::transform(begin(*comms), end(*comms), rcomms.get(), [](const CommPtr& comm) { + return static_cast(comm->pimpl_.get()); + }); + return simcall_comm_waitany(rcomms.get(), comms->size(), timeout); } void Comm::wait_all(std::vector* comms) @@ -54,7 +48,7 @@ void Comm::wait_all(std::vector* comms) comm->wait(); } -Comm* Comm::set_rate(double rate) +CommPtr Comm::set_rate(double rate) { xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)", __FUNCTION__); @@ -62,7 +56,7 @@ Comm* Comm::set_rate(double rate) return this; } -Comm* Comm::set_src_data(void* buff) +CommPtr Comm::set_src_data(void* buff) { xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)", __FUNCTION__); @@ -70,14 +64,16 @@ Comm* Comm::set_src_data(void* buff) src_buff_ = buff; return this; } -Comm* Comm::set_src_data_size(size_t size) + +CommPtr Comm::set_src_data_size(size_t size) { xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)", __FUNCTION__); src_buff_size_ = size; return this; } -Comm* Comm::set_src_data(void* buff, size_t size) + +CommPtr Comm::set_src_data(void* buff, size_t size) { xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)", __FUNCTION__); @@ -87,7 +83,7 @@ Comm* Comm::set_src_data(void* buff, size_t size) src_buff_size_ = size; return this; } -Comm* Comm::set_dst_data(void** buff) +CommPtr Comm::set_dst_data(void** buff) { xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)", __FUNCTION__); @@ -95,12 +91,13 @@ Comm* Comm::set_dst_data(void** buff) dst_buff_ = buff; return this; } + size_t Comm::get_dst_data_size() { xbt_assert(state_ == State::FINISHED, "You cannot use %s before your communication terminated", __FUNCTION__); return dst_buff_size_; } -Comm* Comm::set_dst_data(void** buff, size_t size) +CommPtr Comm::set_dst_data(void** buff, size_t size) { xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)", __FUNCTION__); @@ -171,6 +168,9 @@ Comm* Comm::wait_for(double timeout) state_ = State::FINISHED; return this; + case State::CANCELED: + throw CancelException(XBT_THROW_POINT, "Communication canceled"); + default: THROW_IMPOSSIBLE; } @@ -178,13 +178,12 @@ Comm* Comm::wait_for(double timeout) } int Comm::test_any(std::vector* comms) { - smx_activity_t* array = new smx_activity_t[comms->size()]; - for (unsigned int i = 0; i < comms->size(); i++) { - array[i] = comms->at(i)->pimpl_; - } - int res = simcall_comm_testany(array, comms->size()); - delete[] array; - return res; + std::unique_ptr rcomms( + new simgrid::kernel::activity::CommImpl*[comms->size()]); + std::transform(begin(*comms), end(*comms), rcomms.get(), [](const CommPtr& comm) { + return static_cast(comm->pimpl_.get()); + }); + return simcall_comm_testany(rcomms.get(), comms->size()); } Comm* Comm::detach() @@ -198,7 +197,10 @@ Comm* Comm::detach() Comm* Comm::cancel() { - simgrid::simix::simcall([this] { static_cast(pimpl_.get())->cancel(); }); + simgrid::simix::simcall([this] { + if (pimpl_) + boost::static_pointer_cast(pimpl_)->cancel(); + }); state_ = State::CANCELED; return this; }