Logo AND Algorithmique Numérique Distribuée

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