Logo AND Algorithmique Numérique Distribuée

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