1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/kernel/activity/CommImpl.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/kernel/resource/Action.hpp"
9 #include "simgrid/kernel/routing/NetPoint.hpp"
10 #include "simgrid/modelchecker.h"
11 #include "simgrid/s4u/Host.hpp"
12 #include "src/kernel/activity/MailboxImpl.hpp"
13 #include "src/kernel/context/Context.hpp"
14 #include "src/mc/mc_replay.hpp"
15 #include "src/surf/cpu_interface.hpp"
16 #include "src/surf/network_interface.hpp"
17 #include "src/surf/surf_interface.hpp"
19 #include <boost/range/algorithm.hpp>
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related synchronization");
22 XBT_PRIVATE void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_actor_t src, smx_mailbox_t mbox, double task_size,
23 double rate, unsigned char* src_buff, size_t src_buff_size,
24 bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
25 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
26 void* data, double timeout)
28 simgrid::kernel::activity::ActivityImplPtr comm = simcall_HANDLER_comm_isend(
29 simcall, src, mbox, task_size, rate, src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, false);
30 simcall->mc_value_ = 0;
31 simcall_HANDLER_comm_wait(simcall, static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
34 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr simcall_HANDLER_comm_isend(
35 smx_simcall_t /*simcall*/, smx_actor_t src_proc, smx_mailbox_t mbox, double task_size, double rate,
36 unsigned char* src_buff, size_t src_buff_size,
37 bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
38 void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
39 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), // used to copy data if not default one
40 void* data, bool detached)
42 XBT_DEBUG("send from mailbox %p", mbox);
44 /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
45 simgrid::kernel::activity::CommImplPtr this_comm(
46 new simgrid::kernel::activity::CommImpl(simgrid::kernel::activity::CommImpl::Type::SEND));
48 /* Look for communication synchro matching our needs. We also provide a description of
49 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
51 * If it is not found then push our communication into the rendez-vous point */
52 simgrid::kernel::activity::CommImplPtr other_comm =
53 mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::RECEIVE, match_fun, data, this_comm,
54 /*done*/ false, /*remove_matching*/ true);
57 other_comm = std::move(this_comm);
59 if (mbox->permanent_receiver_ != nullptr) {
60 // this mailbox is for small messages, which have to be sent right now
61 other_comm->state_ = simgrid::kernel::activity::State::READY;
62 other_comm->dst_actor_ = mbox->permanent_receiver_.get();
63 mbox->done_comm_queue_.push_back(other_comm);
64 XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
67 mbox->push(other_comm);
70 XBT_DEBUG("Receive already pushed");
72 other_comm->state_ = simgrid::kernel::activity::State::READY;
77 other_comm->clean_fun = clean_fun;
79 other_comm->clean_fun = nullptr;
80 src_proc->activities_.emplace_back(other_comm);
83 /* Setup the communication synchro */
84 other_comm->src_actor_ = src_proc;
85 other_comm->src_data_ = data;
86 (*other_comm).set_src_buff(src_buff, src_buff_size).set_size(task_size).set_rate(rate);
88 other_comm->match_fun = match_fun;
89 other_comm->copy_data_fun = copy_data_fun;
91 if (MC_is_active() || MC_record_replay_is_active())
92 other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
96 return (detached ? nullptr : other_comm);
99 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
100 unsigned char* dst_buff, size_t* dst_buff_size,
101 bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
102 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
103 void* data, double timeout, double rate)
105 simgrid::kernel::activity::ActivityImplPtr comm = simcall_HANDLER_comm_irecv(
106 simcall, receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
107 simcall->mc_value_ = 0;
108 simcall_HANDLER_comm_wait(simcall, static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
111 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr
112 simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver, smx_mailbox_t mbox, unsigned char* dst_buff,
113 size_t* dst_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
114 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
117 simgrid::kernel::activity::CommImplPtr this_synchro(
118 new simgrid::kernel::activity::CommImpl(simgrid::kernel::activity::CommImpl::Type::RECEIVE));
119 XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
121 simgrid::kernel::activity::CommImplPtr other_comm;
122 // communication already done, get it inside the list of completed comms
123 if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
124 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
125 // find a match in the list of already received comms
126 other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
127 this_synchro, /*done*/ true,
128 /*remove_matching*/ true);
129 // if not found, assume the receiver came first, register it to the mailbox in the classical way
130 if (not other_comm) {
131 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
133 other_comm = std::move(this_synchro);
134 mbox->push(other_comm);
136 if (other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
137 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
138 other_comm->state_ = simgrid::kernel::activity::State::DONE;
139 other_comm->set_mailbox(nullptr);
143 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
145 /* Look for communication activity matching our needs. We also provide a description of
146 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
148 * If it is not found then push our communication into the rendez-vous point */
149 other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
150 this_synchro, /*done*/ false,
151 /*remove_matching*/ true);
153 if (other_comm == nullptr) {
154 XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->comm_queue_.size());
155 other_comm = std::move(this_synchro);
156 mbox->push(other_comm);
158 XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
160 other_comm->state_ = simgrid::kernel::activity::State::READY;
162 receiver->activities_.emplace_back(other_comm);
165 /* Setup communication synchro */
166 other_comm->dst_actor_ = receiver;
167 other_comm->dst_data_ = data;
168 other_comm->set_dst_buff(dst_buff, dst_buff_size);
170 if (rate > -1.0 && (other_comm->get_rate() < 0.0 || rate < other_comm->get_rate()))
171 other_comm->set_rate(rate);
173 other_comm->match_fun = match_fun;
174 other_comm->copy_data_fun = copy_data_fun;
176 if (MC_is_active() || MC_record_replay_is_active()) {
177 other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
184 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm, double timeout)
186 /* Associate this simcall to the wait synchro */
187 XBT_DEBUG("simcall_HANDLER_comm_wait, %p", comm);
189 comm->register_simcall(simcall);
191 if (MC_is_active() || MC_record_replay_is_active()) {
192 int idx = simcall->mc_value_;
194 comm->state_ = simgrid::kernel::activity::State::DONE;
196 /* If we reached this point, the wait simcall must have a timeout */
197 /* Otherwise it shouldn't be enabled and executed by the MC */
201 if (comm->src_actor_ == simcall->issuer_)
202 comm->state_ = simgrid::kernel::activity::State::SRC_TIMEOUT;
204 comm->state_ = simgrid::kernel::activity::State::DST_TIMEOUT;
211 /* If the synchro has already finish perform the error handling, */
212 /* otherwise set up a waiting timeout on the right side */
213 if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
214 comm->state_ != simgrid::kernel::activity::State::RUNNING) {
216 } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
217 simgrid::kernel::resource::Action* sleep = simcall->issuer_->get_host()->pimpl_cpu->sleep(timeout);
218 sleep->set_activity(comm);
220 if (simcall->issuer_ == comm->src_actor_)
221 comm->src_timeout_ = sleep;
223 comm->dst_timeout_ = sleep;
227 void simcall_HANDLER_comm_test(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm)
231 if (MC_is_active() || MC_record_replay_is_active()) {
232 res = comm->src_actor_ && comm->dst_actor_;
234 comm->state_ = simgrid::kernel::activity::State::DONE;
236 res = comm->state_ != simgrid::kernel::activity::State::WAITING &&
237 comm->state_ != simgrid::kernel::activity::State::RUNNING;
240 simcall_comm_test__set__result(simcall, res);
242 comm->simcalls_.push_back(simcall);
245 simcall->issuer_->simcall_answer();
249 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count)
251 // The default result is -1 -- this means, "nothing is ready".
252 // It can be changed below, but only if something matches.
253 simcall_comm_testany__set__result(simcall, -1);
255 if (MC_is_active() || MC_record_replay_is_active()) {
256 int idx = simcall->mc_value_;
258 simcall->issuer_->simcall_answer();
260 simgrid::kernel::activity::CommImpl* comm = comms[idx];
261 simcall_comm_testany__set__result(simcall, idx);
262 comm->simcalls_.push_back(simcall);
263 comm->state_ = simgrid::kernel::activity::State::DONE;
269 for (std::size_t i = 0; i != count; ++i) {
270 simgrid::kernel::activity::CommImpl* comm = comms[i];
271 if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
272 comm->state_ != simgrid::kernel::activity::State::RUNNING) {
273 simcall_comm_testany__set__result(simcall, i);
274 comm->simcalls_.push_back(simcall);
279 simcall->issuer_->simcall_answer();
282 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
284 simgrid::kernel::activity::CommImpl** comms = simcall_comm_waitany__get__comms(simcall);
285 size_t count = simcall_comm_waitany__get__count(simcall);
287 for (size_t i = 0; i < count; i++) {
288 // Remove the first occurrence of simcall:
289 auto* comm = comms[i];
290 auto j = boost::range::find(comm->simcalls_, simcall);
291 if (j != comm->simcalls_.end())
292 comm->simcalls_.erase(j);
295 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count,
298 if (MC_is_active() || MC_record_replay_is_active()) {
300 xbt_die("Timeout not implemented for waitany in the model-checker");
301 int idx = simcall->mc_value_;
302 auto* comm = comms[idx];
303 comm->simcalls_.push_back(simcall);
304 simcall_comm_waitany__set__result(simcall, idx);
305 comm->state_ = simgrid::kernel::activity::State::DONE;
311 simcall->timeout_cb_ = nullptr;
313 simcall->timeout_cb_ = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [simcall]() {
314 simcall->timeout_cb_ = nullptr;
315 SIMIX_waitany_remove_simcall_from_actions(simcall);
316 simcall_comm_waitany__set__result(simcall, -1);
317 simcall->issuer_->simcall_answer();
321 for (size_t i = 0; i < count; i++) {
322 /* associate this simcall to the the synchro */
323 auto* comm = comms[i];
324 comm->simcalls_.push_back(simcall);
326 /* see if the synchro is already finished */
327 if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
328 comm->state_ != simgrid::kernel::activity::State::RUNNING) {
335 /******************************************************************************/
336 /* SIMIX_comm_copy_data callbacks */
337 /******************************************************************************/
338 static void (*SIMIX_comm_copy_data_callback)(simgrid::kernel::activity::CommImpl*, void*,
339 size_t) = &SIMIX_comm_copy_pointer_callback;
341 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
343 XBT_DEBUG("Copy the data over");
344 memcpy(comm->dst_buff_, buff, buff_size);
345 if (comm->detached()) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
346 // original buffer available to the application ASAP
348 comm->src_buff_ = nullptr;
352 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
354 SIMIX_comm_copy_data_callback = callback;
357 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
359 xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
360 *(void**)(comm->dst_buff_) = buff;
367 CommImpl& CommImpl::set_size(double size)
373 CommImpl& CommImpl::set_rate(double rate)
378 CommImpl& CommImpl::set_mailbox(MailboxImpl* mbox)
384 CommImpl& CommImpl::set_src_buff(unsigned char* buff, size_t size)
387 src_buff_size_ = size;
391 CommImpl& CommImpl::set_dst_buff(unsigned char* buff, size_t* size)
394 dst_buff_size_ = size;
398 CommImpl& CommImpl::detach()
404 CommImpl::CommImpl(s4u::Host* from, s4u::Host* to, double bytes) : size_(bytes), detached_(true), from_(from), to_(to)
406 state_ = State::READY;
409 CommImpl::~CommImpl()
411 XBT_DEBUG("Really free communication %p in state %s (detached = %d)", this, get_state_str(), detached_);
415 if (detached_ && state_ != State::DONE) {
416 /* the communication has failed and was detached:
417 * we have to free the buffer */
419 clean_fun(src_buff_);
426 /** @brief Starts the simulation of a communication synchro. */
427 CommImpl* CommImpl::start()
429 /* If both the sender and the receiver are already there, start the communication */
430 if (state_ == State::READY) {
431 from_ = from_ != nullptr ? from_ : src_actor_->get_host();
432 to_ = to_ != nullptr ? to_ : dst_actor_->get_host();
434 /* FIXME[donassolo]: getting the network_model from the origin host
435 * Soon we need to change this function to first get the routes and later
436 * create the respective surf actions */
437 auto net_model = from_->get_netpoint()->get_englobing_zone()->get_network_model();
439 surf_action_ = net_model->communicate(from_, to_, size_, rate_);
440 surf_action_->set_activity(this);
441 surf_action_->set_category(get_tracing_category());
442 state_ = State::RUNNING;
444 XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p; state: %s)", this, from_->get_cname(),
445 to_->get_cname(), surf_action_, get_state_str());
447 /* If a link is failed, detect it immediately */
448 if (surf_action_->get_state() == resource::Action::State::FAILED) {
449 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", from_->get_cname(),
451 state_ = State::LINK_FAILURE;
454 } else if ((src_actor_ != nullptr && src_actor_->is_suspended()) ||
455 (dst_actor_ != nullptr && dst_actor_->is_suspended())) {
456 /* If any of the process is suspended, create the synchro but stop its execution,
457 it will be restarted when the sender process resume */
458 if (src_actor_->is_suspended())
459 XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
461 src_actor_->get_cname(), src_actor_->get_host()->get_cname());
463 XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
465 dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
467 surf_action_->suspend();
474 /** @brief Copy the communication data from the sender's buffer to the receiver's one */
475 void CommImpl::copy_data()
477 size_t buff_size = src_buff_size_;
478 /* If there is no data to copy then return */
479 if (not src_buff_ || not dst_buff_ || copied_)
482 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
483 src_actor_ ? src_actor_->get_host()->get_cname() : "a finished process", src_buff_,
484 dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished process", dst_buff_, buff_size);
486 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
487 if (dst_buff_size_) {
488 buff_size = std::min(buff_size, *(dst_buff_size_));
490 /* Update the receiver's buffer size to the copied amount */
491 *dst_buff_size_ = buff_size;
496 copy_data_fun(this, src_buff_, buff_size);
498 SIMIX_comm_copy_data_callback(this, src_buff_, buff_size);
501 /* Set the copied flag so we copy data only once */
502 /* (this function might be called from both communication ends) */
506 void CommImpl::suspend()
508 /* FIXME: shall we suspend also the timeout synchro? */
510 surf_action_->suspend();
511 /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
514 void CommImpl::resume()
516 /*FIXME: check what happen with the timeouts */
518 surf_action_->resume();
519 /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
522 void CommImpl::cancel()
524 /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
525 if (state_ == State::WAITING) {
528 state_ = State::CANCELED;
530 } else if (not MC_is_active() /* when running the MC there are no surf actions */
531 && not MC_record_replay_is_active() && (state_ == State::READY || state_ == State::RUNNING)) {
532 surf_action_->cancel();
536 /** @brief This is part of the cleanup process, probably an internal command */
537 void CommImpl::cleanup_surf()
542 src_timeout_->unref();
543 src_timeout_ = nullptr;
547 dst_timeout_->unref();
548 dst_timeout_ = nullptr;
552 void CommImpl::post()
554 /* Update synchro state */
555 if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
556 state_ = State::SRC_TIMEOUT;
557 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
558 state_ = State::DST_TIMEOUT;
559 else if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED)
560 state_ = State::SRC_HOST_FAILURE;
561 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED)
562 state_ = State::DST_HOST_FAILURE;
563 else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
564 state_ = State::LINK_FAILURE;
566 state_ = State::DONE;
568 XBT_DEBUG("CommImpl::post(): comm %p, state %s, src_proc %p, dst_proc %p, detached: %d", this, get_state_str(),
569 src_actor_.get(), dst_actor_.get(), detached_);
571 /* destroy the surf actions associated with the Simix communication */
574 /* Answer all simcalls associated with the synchro */
578 void CommImpl::finish()
580 while (not simcalls_.empty()) {
581 smx_simcall_t simcall = simcalls_.front();
582 simcalls_.pop_front();
584 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
585 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
588 if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
589 continue; // if actor handling comm is killed
590 if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
591 SIMIX_waitany_remove_simcall_from_actions(simcall);
592 if (simcall->timeout_cb_) {
593 simcall->timeout_cb_->remove();
594 simcall->timeout_cb_ = nullptr;
596 if (not MC_is_active() && not MC_record_replay_is_active()) {
597 CommImpl** comms = simcall_comm_waitany__get__comms(simcall);
598 size_t count = simcall_comm_waitany__get__count(simcall);
599 CommImpl** element = std::find(comms, comms + count, this);
600 int rank = (element != comms + count) ? element - comms : -1;
601 simcall_comm_waitany__set__result(simcall, rank);
605 /* If the synchro is still in a rendez-vous point then remove from it */
609 XBT_DEBUG("CommImpl::finish(): synchro state = %d", static_cast<int>(state_));
611 /* Check out for errors */
613 if (not simcall->issuer_->get_host()->is_on()) {
614 simcall->issuer_->context_->set_wannadie();
618 XBT_DEBUG("Communication %p complete!", this);
622 case State::SRC_TIMEOUT:
623 simcall->issuer_->exception_ = std::make_exception_ptr(
624 simgrid::TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the sender"));
627 case State::DST_TIMEOUT:
628 simcall->issuer_->exception_ = std::make_exception_ptr(
629 simgrid::TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
632 case State::SRC_HOST_FAILURE:
633 if (simcall->issuer_ == src_actor_)
634 simcall->issuer_->context_->set_wannadie();
636 simcall->issuer_->exception_ =
637 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
640 case State::DST_HOST_FAILURE:
641 if (simcall->issuer_ == dst_actor_)
642 simcall->issuer_->context_->set_wannadie();
644 simcall->issuer_->exception_ =
645 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
648 case State::LINK_FAILURE:
649 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
651 this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
652 dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, simcall->issuer_->get_cname(),
653 simcall->issuer_, detached_);
654 if (src_actor_ == simcall->issuer_) {
655 XBT_DEBUG("I'm source");
656 } else if (dst_actor_ == simcall->issuer_) {
657 XBT_DEBUG("I'm dest");
659 XBT_DEBUG("I'm neither source nor dest");
661 simcall->issuer_->throw_exception(
662 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Link failure")));
665 case State::CANCELED:
666 if (simcall->issuer_ == dst_actor_)
667 simcall->issuer_->exception_ = std::make_exception_ptr(
668 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
670 simcall->issuer_->exception_ = std::make_exception_ptr(
671 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
675 xbt_die("Unexpected synchro state in CommImpl::finish: %d", static_cast<int>(state_));
677 simcall->issuer_->simcall_answer();
679 /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
680 if (simcall->issuer_->exception_ &&
681 (simcall->call_ == simix::Simcall::COMM_WAITANY || simcall->call_ == simix::Simcall::COMM_TESTANY)) {
682 // First retrieve the rank of our failing synchro
685 if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
686 comms = simcall_comm_waitany__get__comms(simcall);
687 count = simcall_comm_waitany__get__count(simcall);
689 /* simcall->call_ == simix::Simcall::COMM_TESTANY */
690 comms = simcall_comm_testany__get__comms(simcall);
691 count = simcall_comm_testany__get__count(simcall);
693 CommImpl** element = std::find(comms, comms + count, this);
694 int rank = (element != comms + count) ? element - comms : -1;
695 // In order to modify the exception we have to rethrow it:
697 std::rethrow_exception(simcall->issuer_->exception_);
698 } catch (simgrid::Exception& e) {
703 simcall->issuer_->waiting_synchro_ = nullptr;
704 simcall->issuer_->activities_.remove(this);
706 if (simcall->issuer_ != dst_actor_ && dst_actor_ != nullptr)
707 dst_actor_->activities_.remove(this);
708 if (simcall->issuer_ != src_actor_ && src_actor_ != nullptr)
709 src_actor_->activities_.remove(this);
714 } // namespace activity
715 } // namespace kernel
716 } // namespace simgrid