Logo AND Algorithmique Numérique Distribuée

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