Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Give the comm to {Send,Recv}Transition, as CommDet needs it
[simgrid.git] / src / kernel / activity / CommImpl.cpp
1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <simgrid/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/modelchecker.h>
9 #include <simgrid/s4u/Host.hpp>
10
11 #include "src/kernel/activity/CommImpl.hpp"
12 #include "src/kernel/activity/MailboxImpl.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
14 #include "src/kernel/context/Context.hpp"
15 #include "src/kernel/resource/CpuImpl.hpp"
16 #include "src/kernel/resource/LinkImpl.hpp"
17 #include "src/kernel/resource/StandardLinkImpl.hpp"
18 #include "src/mc/mc_replay.hpp"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_network, kernel, "Kernel network-related synchronization");
21
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)
27 {
28   simgrid::kernel::actor::CommIsendSimcall observer(src, mbox, task_size, rate, src_buff, src_buff_size, match_fun,
29                                                     nullptr, copy_data_fun, data, false);
30   simgrid::kernel::activity::ActivityImplPtr comm = simgrid::kernel::activity::CommImpl::isend(&observer);
31   simcall->mc_value_ = 0;
32   comm->wait_for(simcall->issuer_, timeout);
33 }
34
35 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr simcall_HANDLER_comm_isend(
36     smx_simcall_t /*simcall*/, smx_actor_t src_proc, smx_mailbox_t mbox, double task_size, double rate,
37     unsigned char* src_buff, size_t src_buff_size,
38     bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
39     void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
40     void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), // used to copy data if not default one
41     void* data, bool detached)
42 {
43   simgrid::kernel::actor::CommIsendSimcall observer(src_proc, mbox, task_size, rate, src_buff, src_buff_size, match_fun,
44                                                     clean_fun, copy_data_fun, data, detached);
45   return simgrid::kernel::activity::CommImpl::isend(&observer);
46 }
47
48 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
49                                            unsigned char* dst_buff, size_t* dst_buff_size,
50                                            bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
51                                            void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
52                                            void* data, double timeout, double rate)
53 {
54   simgrid::kernel::actor::CommIrecvSimcall observer(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun,
55                                                     data, rate);
56   simgrid::kernel::activity::ActivityImplPtr comm = simgrid::kernel::activity::CommImpl::irecv(&observer);
57   simcall->mc_value_ = 0;
58   comm->wait_for(simcall->issuer_, timeout);
59 }
60
61 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr
62 simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver, smx_mailbox_t mbox, unsigned char* dst_buff,
63                            size_t* dst_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
64                            void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
65                            double rate)
66 {
67   simgrid::kernel::actor::CommIrecvSimcall observer(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun,
68                                                     data, rate);
69   return simgrid::kernel::activity::CommImpl::irecv(&observer);
70 }
71
72 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm, double timeout)
73 {
74   comm->wait_for(simcall->issuer_, timeout);
75 }
76
77 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count,
78                                   double timeout)
79 {
80   std::vector<simgrid::kernel::activity::CommImpl*> comms_vec(comms, comms + count);
81   simgrid::kernel::activity::CommImpl::wait_any_for(simcall->issuer_, comms_vec, timeout);
82 }
83
84 /******************************************************************************/
85 /*                    SIMIX_comm_copy_data callbacks                       */
86 /******************************************************************************/
87 // XBT_ATTRIB_DEPRECATED_v333
88 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
89 {
90   simgrid::kernel::activity::CommImpl::set_copy_data_callback(callback);
91 }
92
93 // XBT_ATTRIB_DEPRECATED_v333
94 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
95 {
96   simgrid::s4u::Comm::copy_buffer_callback(comm, buff, buff_size);
97 }
98
99 // XBT_ATTRIB_DEPRECATED_v333
100 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
101 {
102   simgrid::s4u::Comm::copy_pointer_callback(comm, buff, buff_size);
103 }
104
105 namespace simgrid {
106 namespace kernel {
107 namespace activity {
108 xbt::signal<void(CommImpl const&)> CommImpl::on_start;
109 xbt::signal<void(CommImpl const&)> CommImpl::on_completion;
110
111 void (*CommImpl::copy_data_callback_)(CommImpl*, void*, size_t) = &s4u::Comm::copy_pointer_callback;
112
113 void CommImpl::set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t))
114 {
115   copy_data_callback_ = callback;
116 }
117
118 CommImpl& CommImpl::set_size(double size)
119 {
120   size_ = size;
121   return *this;
122 }
123
124 CommImpl& CommImpl::set_rate(double rate)
125 {
126   rate_ = rate;
127   return *this;
128 }
129 CommImpl& CommImpl::set_mailbox(MailboxImpl* mbox)
130 {
131   if (mbox != nullptr)
132     mbox_id_ = mbox->get_id();
133   mbox_ = mbox;
134   return *this;
135 }
136
137 CommImpl& CommImpl::set_src_buff(unsigned char* buff, size_t size)
138 {
139   src_buff_      = buff;
140   src_buff_size_ = size;
141   return *this;
142 }
143
144 CommImpl& CommImpl::set_dst_buff(unsigned char* buff, size_t* size)
145 {
146   dst_buff_      = buff;
147   dst_buff_size_ = size;
148   return *this;
149 }
150
151 CommImpl& CommImpl::detach()
152 {
153   detached_ = true;
154   return *this;
155 }
156
157 CommImpl::CommImpl(s4u::Host* from, s4u::Host* to, double bytes) : size_(bytes), detached_(true), from_(from), to_(to)
158 {
159   set_state(State::READY);
160 }
161
162 CommImpl::~CommImpl()
163 {
164   XBT_DEBUG("Really free communication %p in state %s (detached = %d)", this, get_state_str(), detached_);
165
166   cleanup_surf();
167
168   if (detached_ && get_state() != State::DONE) {
169     /* the communication has failed and was detached:
170      * we have to free the buffer */
171     if (clean_fun)
172       clean_fun(src_buff_);
173     src_buff_ = nullptr;
174   } else if (mbox_) {
175     mbox_->remove(this);
176   }
177 }
178
179 /**  @brief Starts the simulation of a communication synchro. */
180 CommImpl* CommImpl::start()
181 {
182   /* If both the sender and the receiver are already there, start the communication */
183   if (get_state() == State::READY) {
184     from_ = from_ != nullptr ? from_ : src_actor_->get_host();
185     to_   = to_ != nullptr ? to_ : dst_actor_->get_host();
186     /* Getting the network_model from the origin host
187      * Valid while we have a single network model, otherwise we would need to change this function to first get the
188      * routes and later create the respective surf actions */
189     auto net_model = from_->get_netpoint()->get_englobing_zone()->get_network_model();
190
191     surf_action_ = net_model->communicate(from_, to_, size_, rate_);
192     surf_action_->set_activity(this);
193     surf_action_->set_category(get_tracing_category());
194     set_start_time(surf_action_->get_start_time());
195     set_state(State::RUNNING);
196     on_start(*this);
197
198     XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p; state: %s)", this, from_->get_cname(),
199               to_->get_cname(), surf_action_, get_state_str());
200
201     /* If a link is failed, detect it immediately */
202     if (surf_action_->get_state() == resource::Action::State::FAILED) {
203       XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", from_->get_cname(),
204                 to_->get_cname());
205       set_state(State::LINK_FAILURE);
206       post();
207
208     } else if ((src_actor_ != nullptr && src_actor_->is_suspended()) ||
209                (dst_actor_ != nullptr && dst_actor_->is_suspended())) {
210       /* If any of the actor is suspended, create the synchro but stop its execution,
211          it will be restarted when the sender actor resume */
212       if (src_actor_->is_suspended())
213         XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
214                   "communication",
215                   src_actor_->get_cname(), src_actor_->get_host()->get_cname());
216       else
217         XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
218                   "communication",
219                   dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
220
221       surf_action_->suspend();
222     }
223   }
224
225   return this;
226 }
227
228 std::vector<s4u::Link*> CommImpl::get_traversed_links() const
229 {
230   xbt_assert(get_state() != State::WAITING, "You cannot use %s() if your communication is not ready (%s)", __FUNCTION__,
231              get_state_str());
232   std::vector<s4u::Link*> vlinks;
233   XBT_ATTRIB_UNUSED double res = 0;
234   from_->route_to(to_, vlinks, &res);
235   return vlinks;
236 }
237
238 /** @brief Copy the communication data from the sender's buffer to the receiver's one  */
239 void CommImpl::copy_data()
240 {
241   size_t buff_size = src_buff_size_;
242   /* If there is no data to copy then return */
243   if (not src_buff_ || not dst_buff_ || copied_)
244     return;
245
246   XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
247             src_actor_ ? src_actor_->get_host()->get_cname() : "a finished actor", src_buff_,
248             dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished actor", dst_buff_, buff_size);
249
250   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
251   if (dst_buff_size_) {
252     buff_size = std::min(buff_size, *dst_buff_size_);
253
254     /* Update the receiver's buffer size to the copied amount */
255     *dst_buff_size_ = buff_size;
256   }
257
258   if (buff_size > 0) {
259     if (copy_data_fun)
260       copy_data_fun(this, src_buff_, buff_size);
261     else
262       copy_data_callback_(this, src_buff_, buff_size);
263   }
264
265   /* Set the copied flag so we copy data only once */
266   /* (this function might be called from both communication ends) */
267   copied_ = true;
268 }
269
270 ActivityImplPtr CommImpl::isend(actor::CommIsendSimcall* observer)
271 {
272   auto* mbox = observer->get_mailbox();
273   XBT_DEBUG("send from mailbox %p", mbox);
274
275   /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
276   CommImplPtr this_comm(new CommImpl(CommImpl::Type::SEND));
277
278   /* Look for communication synchro matching our needs. We also provide a description of
279    * ourself so that the other side also gets a chance of choosing if it wants to match with us.
280    *
281    * If it is not found then push our communication into the rendez-vous point */
282   CommImplPtr other_comm =
283       mbox->find_matching_comm(CommImpl::Type::RECEIVE, observer->get_match_fun(), observer->get_payload(), this_comm,
284                                /*done*/ false, /*remove_matching*/ true);
285
286   if (not other_comm) {
287     other_comm = std::move(this_comm);
288
289     if (mbox->is_permanent()) {
290       // this mailbox is for small messages, which have to be sent right now
291       other_comm->set_state(State::READY);
292       other_comm->dst_actor_ = mbox->get_permanent_receiver().get();
293       mbox->push_done(other_comm);
294       XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
295
296     } else {
297       mbox->push(other_comm);
298     }
299   } else {
300     XBT_DEBUG("Receive already pushed");
301
302     other_comm->set_state(State::READY);
303   }
304   observer->set_comm(other_comm.get());
305
306   if (observer->is_detached()) {
307     other_comm->detach();
308     other_comm->clean_fun = observer->get_clean_fun();
309   } else {
310     other_comm->clean_fun = nullptr;
311     observer->get_issuer()->activities_.emplace_back(other_comm);
312   }
313
314   /* Setup the communication synchro */
315   other_comm->src_actor_ = observer->get_issuer();
316   other_comm->src_data_  = observer->get_payload();
317   (*other_comm)
318       .set_src_buff(observer->get_src_buff(), observer->get_src_buff_size())
319       .set_size(observer->get_payload_size())
320       .set_rate(observer->get_rate());
321
322   other_comm->match_fun     = observer->get_match_fun();
323   other_comm->copy_data_fun = observer->get_copy_data_fun();
324
325   if (MC_is_active() || MC_record_replay_is_active())
326     other_comm->set_state(simgrid::kernel::activity::State::RUNNING);
327   else
328     other_comm->start();
329
330   return (observer->is_detached() ? nullptr : other_comm);
331 }
332
333 ActivityImplPtr CommImpl::irecv(actor::CommIrecvSimcall* observer)
334 {
335   CommImplPtr this_synchro(new CommImpl(CommImpl::Type::RECEIVE));
336   auto* mbox = observer->get_mailbox();
337   XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
338
339   CommImplPtr other_comm;
340   // communication already done, get it inside the list of completed comms
341   if (mbox->is_permanent() && mbox->has_some_done_comm()) {
342     XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
343     // find a match in the list of already received comms
344     other_comm = mbox->find_matching_comm(CommImpl::Type::SEND, observer->get_match_fun(), observer->get_payload(),
345                                           this_synchro, /*done*/ true, /*remove_matching*/ true);
346     // if not found, assume the receiver came first, register it to the mailbox in the classical way
347     if (not other_comm) {
348       XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
349                 "into list");
350       other_comm = std::move(this_synchro);
351       mbox->push(other_comm);
352     } else {
353       if (other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
354         XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
355         other_comm->set_state(State::DONE);
356         other_comm->set_mailbox(nullptr);
357       }
358     }
359   } else {
360     /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
361
362     /* Look for communication activity matching our needs. We also provide a description of
363      * ourself so that the other side also gets a chance of choosing if it wants to match with us.
364      *
365      * If it is not found then push our communication into the rendez-vous point */
366     other_comm = mbox->find_matching_comm(CommImpl::Type::SEND, observer->get_match_fun(), observer->get_payload(),
367                                           this_synchro, /*done*/ false, /*remove_matching*/ true);
368
369     if (other_comm == nullptr) {
370       XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->size());
371       other_comm = std::move(this_synchro);
372       mbox->push(other_comm);
373     } else {
374       XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
375
376       other_comm->set_state(simgrid::kernel::activity::State::READY);
377     }
378     observer->get_issuer()->activities_.emplace_back(other_comm);
379   }
380   observer->set_comm(other_comm.get());
381
382   /* Setup communication synchro */
383   other_comm->dst_actor_ = observer->get_issuer();
384   other_comm->dst_data_  = observer->get_payload();
385   other_comm->set_dst_buff(observer->get_dst_buff(), observer->get_dst_buff_size());
386
387   if (observer->get_rate() > -1.0 && (other_comm->get_rate() < 0.0 || observer->get_rate() < other_comm->get_rate()))
388     other_comm->set_rate(observer->get_rate());
389
390   other_comm->match_fun     = observer->get_match_fun();
391   other_comm->copy_data_fun = observer->get_copy_data_fun();
392
393   if (MC_is_active() || MC_record_replay_is_active()) {
394     other_comm->set_state(State::RUNNING);
395     return other_comm;
396   }
397   other_comm->start();
398
399   return other_comm;
400 }
401
402 bool CommImpl::test(actor::ActorImpl* issuer)
403 {
404   if ((MC_is_active() || MC_record_replay_is_active()) && src_actor_ && dst_actor_)
405     set_state(State::DONE);
406   return ActivityImpl::test(issuer);
407 }
408
409 void CommImpl::wait_for(actor::ActorImpl* issuer, double timeout)
410 {
411   XBT_DEBUG("CommImpl::wait_for(%g), %p, state %s", timeout, this, get_state_str());
412
413   /* Associate this simcall to the wait synchro */
414   register_simcall(&issuer->simcall_);
415   if (MC_is_active() || MC_record_replay_is_active()) {
416     int idx = issuer->simcall_.mc_value_;
417     if (idx == 0) {
418       set_state(State::DONE);
419     } else {
420       /* If we reached this point, the wait simcall must have a timeout */
421       /* Otherwise it shouldn't be enabled and executed by the MC */
422       xbt_assert(timeout >= 0.0,
423                  "The checker asked me to raise a timeout on a communication that is not expecting any timeout");
424       set_state(issuer == src_actor_ ? State::SRC_TIMEOUT : State::DST_TIMEOUT);
425     }
426     finish();
427     return;
428   }
429   ActivityImpl::wait_for(issuer, timeout);
430 }
431
432 void CommImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout)
433 {
434   std::vector<ActivityImpl*> activities(comms.begin(), comms.end());
435   ActivityImpl::wait_any_for(issuer, activities, timeout);
436 }
437
438 void CommImpl::suspend()
439 {
440   /* FIXME: shall we suspend also the timeout synchro? */
441   if (surf_action_)
442     surf_action_->suspend();
443   /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
444 }
445
446 void CommImpl::resume()
447 {
448   /*FIXME: check what happen with the timeouts */
449   if (surf_action_)
450     surf_action_->resume();
451   /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
452 }
453
454 void CommImpl::cancel()
455 {
456   /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
457   if (get_state() == State::WAITING) {
458     if (not detached_) {
459       mbox_->remove(this);
460       set_state(State::CANCELED);
461     }
462   } else if (not MC_is_active() /* when running the MC there are no surf actions */
463              && not MC_record_replay_is_active() && (get_state() == State::READY || get_state() == State::RUNNING)) {
464     surf_action_->cancel();
465   }
466 }
467
468 /** @brief This is part of the cleanup process, probably an internal command */
469 void CommImpl::cleanup_surf()
470 {
471   clean_action();
472
473   if (src_timeout_) {
474     src_timeout_->unref();
475     src_timeout_ = nullptr;
476   }
477
478   if (dst_timeout_) {
479     dst_timeout_->unref();
480     dst_timeout_ = nullptr;
481   }
482 }
483
484 void CommImpl::post()
485 {
486   on_completion(*this);
487
488   /* Update synchro state */
489   if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
490     set_state(State::SRC_TIMEOUT);
491   else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
492     set_state(State::DST_TIMEOUT);
493   else if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED)
494     set_state(State::SRC_HOST_FAILURE);
495   else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED)
496     set_state(State::DST_HOST_FAILURE);
497   else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
498     set_state(State::LINK_FAILURE);
499   } else
500     set_state(State::DONE);
501
502   XBT_DEBUG("CommImpl::post(): comm %p, state %s, src_proc %p, dst_proc %p, detached: %d", this, get_state_str(),
503             src_actor_.get(), dst_actor_.get(), detached_);
504
505   /* destroy the surf actions associated with the Simix communication */
506   cleanup_surf();
507
508   /* Answer all simcalls associated with the synchro */
509   finish();
510 }
511 void CommImpl::set_exception(actor::ActorImpl* issuer)
512 {
513   switch (get_state()) {
514     case State::FAILED:
515       issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
516       break;
517     case State::SRC_TIMEOUT:
518       issuer->exception_ =
519           std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the sender"));
520       break;
521
522     case State::DST_TIMEOUT:
523       issuer->exception_ =
524           std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
525       break;
526
527     case State::SRC_HOST_FAILURE:
528       if (issuer == src_actor_)
529         issuer->context_->set_wannadie();
530       else {
531         set_state(State::FAILED);
532         issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
533       }
534       break;
535
536     case State::DST_HOST_FAILURE:
537       if (issuer == dst_actor_)
538         issuer->context_->set_wannadie();
539       else {
540         set_state(State::FAILED);
541         issuer->exception_ = std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
542       }
543       break;
544
545     case State::LINK_FAILURE:
546       XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
547                 "detached:%d",
548                 this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
549                 dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, issuer->get_cname(), issuer, detached_);
550       if (src_actor_ == issuer) {
551         XBT_DEBUG("I'm source");
552       } else if (dst_actor_ == issuer) {
553         XBT_DEBUG("I'm dest");
554       } else {
555         XBT_DEBUG("I'm neither source nor dest");
556       }
557       set_state(State::FAILED);
558       issuer->throw_exception(std::make_exception_ptr(NetworkFailureException(XBT_THROW_POINT, "Link failure")));
559       break;
560
561     case State::CANCELED:
562       if (issuer == dst_actor_)
563         issuer->exception_ =
564             std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
565       else
566         issuer->exception_ =
567             std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
568       break;
569
570     default:
571       xbt_assert(get_state() == State::DONE, "Internal error in CommImpl::finish(): unexpected synchro state %s",
572                  get_state_str());
573   }
574 }
575
576 void CommImpl::finish()
577 {
578   XBT_DEBUG("CommImpl::finish() in state %s", get_state_str());
579   /* If the synchro is still in a rendez-vous point then remove from it */
580   if (mbox_)
581     mbox_->remove(this);
582
583   if (get_state() == State::DONE)
584     copy_data();
585
586   while (not simcalls_.empty()) {
587     smx_simcall_t simcall = simcalls_.front();
588     simcalls_.pop_front();
589
590     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
591      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
592      * simcall */
593
594     if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
595       continue;                                 // if actor handling comm is killed
596
597     handle_activity_waitany(simcall);
598
599     /* Check out for errors */
600
601     if (not simcall->issuer_->get_host()->is_on()) {
602       simcall->issuer_->context_->set_wannadie();
603     } else {
604       // Do not answer to dying actors
605       if (not simcall->issuer_->context_->wannadie()) {
606         set_exception(simcall->issuer_);
607         simcall->issuer_->simcall_answer();
608       }
609     }
610
611     simcall->issuer_->waiting_synchro_ = nullptr;
612     simcall->issuer_->activities_.remove(this);
613     if (detached_) {
614       if (simcall->issuer_ != dst_actor_ && dst_actor_ != nullptr)
615         dst_actor_->activities_.remove(this);
616       if (simcall->issuer_ != src_actor_ && src_actor_ != nullptr)
617         src_actor_->activities_.remove(this);
618     }
619   }
620 }
621
622 } // namespace activity
623 } // namespace kernel
624 } // namespace simgrid