Logo AND Algorithmique Numérique Distribuée

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