Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / src / kernel / activity / CommImpl.cpp
1 /* Copyright (c) 2007-2020. 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 "src/kernel/activity/CommImpl.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/kernel/resource/Action.hpp"
9 #include "simgrid/modelchecker.h"
10 #include "simgrid/s4u/Host.hpp"
11 #include "src/kernel/activity/MailboxImpl.hpp"
12 #include "src/kernel/context/Context.hpp"
13 #include "src/mc/mc_replay.hpp"
14 #include "src/surf/cpu_interface.hpp"
15 #include "src/surf/network_interface.hpp"
16 #include "src/surf/surf_interface.hpp"
17
18 #include <boost/range/algorithm.hpp>
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX 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   smx_activity_t comm = simcall_HANDLER_comm_isend(simcall, src, mbox, task_size, rate, src_buff, src_buff_size,
28                                                    match_fun, nullptr, copy_data_fun, data, 0);
29   SIMCALL_SET_MC_VALUE(*simcall, 0);
30   simcall_HANDLER_comm_wait(simcall, static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
31 }
32
33 XBT_PRIVATE smx_activity_t 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   XBT_DEBUG("send from mailbox %p", mbox);
42
43   /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
44   simgrid::kernel::activity::CommImplPtr this_comm =
45       simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl());
46   this_comm->set_type(simgrid::kernel::activity::CommImpl::Type::SEND);
47
48   /* Look for communication synchro matching our needs. We also provide a description of
49    * ourself so that the other side also gets a chance of choosing if it wants to match with us.
50    *
51    * If it is not found then push our communication into the rendez-vous point */
52   simgrid::kernel::activity::CommImplPtr other_comm =
53       mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::RECEIVE, match_fun, data, this_comm,
54                                /*done*/ false, /*remove_matching*/ true);
55
56   if (not other_comm) {
57     other_comm = std::move(this_comm);
58
59     if (mbox->permanent_receiver_ != nullptr) {
60       // this mailbox is for small messages, which have to be sent right now
61       other_comm->state_     = simgrid::kernel::activity::State::READY;
62       other_comm->dst_actor_ = mbox->permanent_receiver_.get();
63       mbox->done_comm_queue_.push_back(other_comm);
64       XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
65
66     } else {
67       mbox->push(other_comm);
68     }
69   } else {
70     XBT_DEBUG("Receive already pushed");
71
72     other_comm->state_ = simgrid::kernel::activity::State::READY;
73     other_comm->set_type(simgrid::kernel::activity::CommImpl::Type::READY);
74   }
75
76   if (detached) {
77     other_comm->detach();
78     other_comm->clean_fun = clean_fun;
79   } else {
80     other_comm->clean_fun = nullptr;
81     src_proc->comms.push_back(other_comm);
82   }
83
84   /* Setup the communication synchro */
85   other_comm->src_actor_     = src_proc;
86   other_comm->src_data_      = data;
87   (*other_comm).set_src_buff(src_buff, src_buff_size).set_size(task_size).set_rate(rate);
88
89   other_comm->match_fun     = match_fun;
90   other_comm->copy_data_fun = copy_data_fun;
91
92   if (MC_is_active() || MC_record_replay_is_active())
93     other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
94   else
95     other_comm->start();
96
97   return (detached ? nullptr : other_comm);
98 }
99
100 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
101                                            unsigned char* dst_buff, size_t* dst_buff_size,
102                                            bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
103                                            void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
104                                            void* data, double timeout, double rate)
105 {
106   smx_activity_t comm = simcall_HANDLER_comm_irecv(simcall, receiver, mbox, dst_buff, dst_buff_size, match_fun,
107                                                    copy_data_fun, data, rate);
108   SIMCALL_SET_MC_VALUE(*simcall, 0);
109   simcall_HANDLER_comm_wait(simcall, static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
110 }
111
112 XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_irecv(
113     smx_simcall_t /*simcall*/, smx_actor_t receiver, smx_mailbox_t mbox, unsigned char* dst_buff, size_t* dst_buff_size,
114     bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
115     void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data, double rate)
116 {
117   simgrid::kernel::activity::CommImplPtr this_synchro =
118       simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl());
119   this_synchro->set_type(simgrid::kernel::activity::CommImpl::Type::RECEIVE);
120   XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
121
122   simgrid::kernel::activity::CommImplPtr other_comm;
123   // communication already done, get it inside the list of completed comms
124   if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
125     XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
126     // find a match in the list of already received comms
127     other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
128                                           this_synchro, /*done*/ true,
129                                           /*remove_matching*/ true);
130     // if not found, assume the receiver came first, register it to the mailbox in the classical way
131     if (not other_comm) {
132       XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
133                 "into list");
134       other_comm = std::move(this_synchro);
135       mbox->push(other_comm);
136     } else {
137       if (other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
138         XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
139         other_comm->state_ = simgrid::kernel::activity::State::DONE;
140         other_comm->set_type(simgrid::kernel::activity::CommImpl::Type::DONE).set_mailbox(nullptr);
141       }
142     }
143   } else {
144     /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
145
146     /* Look for communication activity matching our needs. We also provide a description of
147      * ourself so that the other side also gets a chance of choosing if it wants to match with us.
148      *
149      * If it is not found then push our communication into the rendez-vous point */
150     other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
151                                           this_synchro, /*done*/ false,
152                                           /*remove_matching*/ true);
153
154     if (other_comm == nullptr) {
155       XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->comm_queue_.size());
156       other_comm = std::move(this_synchro);
157       mbox->push(other_comm);
158     } else {
159       XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
160
161       other_comm->state_ = simgrid::kernel::activity::State::READY;
162       other_comm->set_type(simgrid::kernel::activity::CommImpl::Type::READY);
163     }
164     receiver->comms.push_back(other_comm);
165   }
166
167   /* Setup communication synchro */
168   other_comm->dst_actor_     = receiver;
169   other_comm->dst_data_      = data;
170   other_comm->set_dst_buff(dst_buff, dst_buff_size);
171
172   if (rate > -1.0 && (other_comm->get_rate() < 0.0 || rate < other_comm->get_rate()))
173     other_comm->set_rate(rate);
174
175   other_comm->match_fun     = match_fun;
176   other_comm->copy_data_fun = copy_data_fun;
177
178   if (MC_is_active() || MC_record_replay_is_active()) {
179     other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
180     return other_comm;
181   }
182   other_comm->start();
183   return other_comm;
184 }
185
186 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm, double timeout)
187 {
188   /* Associate this simcall to the wait synchro */
189   XBT_DEBUG("simcall_HANDLER_comm_wait, %p", comm);
190
191   comm->register_simcall(simcall);
192
193   if (MC_is_active() || MC_record_replay_is_active()) {
194     int idx = SIMCALL_GET_MC_VALUE(*simcall);
195     if (idx == 0) {
196       comm->state_ = simgrid::kernel::activity::State::DONE;
197     } else {
198       /* If we reached this point, the wait simcall must have a timeout */
199       /* Otherwise it shouldn't be enabled and executed by the MC */
200       if (timeout < 0.0)
201         THROW_IMPOSSIBLE;
202
203       if (comm->src_actor_ == simcall->issuer_)
204         comm->state_ = simgrid::kernel::activity::State::SRC_TIMEOUT;
205       else
206         comm->state_ = simgrid::kernel::activity::State::DST_TIMEOUT;
207     }
208
209     comm->finish();
210     return;
211   }
212
213   /* If the synchro has already finish perform the error handling, */
214   /* otherwise set up a waiting timeout on the right side          */
215   if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
216       comm->state_ != simgrid::kernel::activity::State::RUNNING) {
217     comm->finish();
218   } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
219     simgrid::kernel::resource::Action* sleep = simcall->issuer_->get_host()->pimpl_cpu->sleep(timeout);
220     sleep->set_activity(comm);
221
222     if (simcall->issuer_ == comm->src_actor_)
223       comm->src_timeout_ = sleep;
224     else
225       comm->dst_timeout_ = sleep;
226   }
227 }
228
229 void simcall_HANDLER_comm_test(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm)
230 {
231   bool res;
232
233   if (MC_is_active() || MC_record_replay_is_active()) {
234     res = comm->src_actor_ && comm->dst_actor_;
235     if (res)
236       comm->state_ = simgrid::kernel::activity::State::DONE;
237   } else {
238     res = comm->state_ != simgrid::kernel::activity::State::WAITING &&
239           comm->state_ != simgrid::kernel::activity::State::RUNNING;
240   }
241
242   simcall_comm_test__set__result(simcall, res);
243   if (res) {
244     comm->simcalls_.push_back(simcall);
245     comm->finish();
246   } else {
247     simcall->issuer_->simcall_answer();
248   }
249 }
250
251 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count)
252 {
253   // The default result is -1 -- this means, "nothing is ready".
254   // It can be changed below, but only if something matches.
255   simcall_comm_testany__set__result(simcall, -1);
256
257   if (MC_is_active() || MC_record_replay_is_active()) {
258     int idx = SIMCALL_GET_MC_VALUE(*simcall);
259     if (idx == -1) {
260       simcall->issuer_->simcall_answer();
261     } else {
262       simgrid::kernel::activity::CommImpl* comm = comms[idx];
263       simcall_comm_testany__set__result(simcall, idx);
264       comm->simcalls_.push_back(simcall);
265       comm->state_ = simgrid::kernel::activity::State::DONE;
266       comm->finish();
267     }
268     return;
269   }
270
271   for (std::size_t i = 0; i != count; ++i) {
272     simgrid::kernel::activity::CommImpl* comm = comms[i];
273     if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
274         comm->state_ != simgrid::kernel::activity::State::RUNNING) {
275       simcall_comm_testany__set__result(simcall, i);
276       comm->simcalls_.push_back(simcall);
277       comm->finish();
278       return;
279     }
280   }
281   simcall->issuer_->simcall_answer();
282 }
283
284 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
285 {
286   simgrid::kernel::activity::CommImpl** comms = simcall_comm_waitany__get__comms(simcall);
287   size_t count                                = simcall_comm_waitany__get__count(simcall);
288
289   for (size_t i = 0; i < count; i++) {
290     // Remove the first occurrence of simcall:
291     auto* comm = comms[i];
292     auto j     = boost::range::find(comm->simcalls_, simcall);
293     if (j != comm->simcalls_.end())
294       comm->simcalls_.erase(j);
295   }
296 }
297 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count,
298                                   double timeout)
299 {
300   if (MC_is_active() || MC_record_replay_is_active()) {
301     if (timeout > 0.0)
302       xbt_die("Timeout not implemented for waitany in the model-checker");
303     int idx                 = SIMCALL_GET_MC_VALUE(*simcall);
304     auto* comm              = comms[idx];
305     comm->simcalls_.push_back(simcall);
306     simcall_comm_waitany__set__result(simcall, idx);
307     comm->state_ = simgrid::kernel::activity::State::DONE;
308     comm->finish();
309     return;
310   }
311
312   if (timeout < 0.0) {
313     simcall->timeout_cb_ = NULL;
314   } else {
315     simcall->timeout_cb_ = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [simcall]() {
316       SIMIX_waitany_remove_simcall_from_actions(simcall);
317       simcall_comm_waitany__set__result(simcall, -1);
318       simcall->issuer_->simcall_answer();
319     });
320   }
321
322   for (size_t i = 0; i < count; i++) {
323     /* associate this simcall to the the synchro */
324     auto* comm = comms[i];
325     comm->simcalls_.push_back(simcall);
326
327     /* see if the synchro is already finished */
328     if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
329         comm->state_ != simgrid::kernel::activity::State::RUNNING) {
330       comm->finish();
331       break;
332     }
333   }
334 }
335
336 /******************************************************************************/
337 /*                    SIMIX_comm_copy_data callbacks                       */
338 /******************************************************************************/
339 static void (*SIMIX_comm_copy_data_callback)(simgrid::kernel::activity::CommImpl*, void*,
340                                              size_t) = &SIMIX_comm_copy_pointer_callback;
341
342 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
343 {
344   XBT_DEBUG("Copy the data over");
345   memcpy(comm->dst_buff_, buff, buff_size);
346   if (comm->detached()) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
347                           // original buffer available to the application ASAP
348     xbt_free(buff);
349     comm->src_buff_ = nullptr;
350   }
351 }
352
353 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
354 {
355   SIMIX_comm_copy_data_callback = callback;
356 }
357
358 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
359 {
360   xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
361   *(void**)(comm->dst_buff_) = buff;
362 }
363
364 namespace simgrid {
365 namespace kernel {
366 namespace activity {
367
368 CommImpl& CommImpl::set_type(CommImpl::Type type)
369 {
370   type_ = type;
371   return *this;
372 }
373
374 CommImpl& CommImpl::set_size(double size)
375 {
376   size_ = size;
377   return *this;
378 }
379
380 CommImpl& CommImpl::set_rate(double rate)
381 {
382   rate_ = rate;
383   return *this;
384 }
385 CommImpl& CommImpl::set_mailbox(MailboxImpl* mbox)
386 {
387   mbox_ = mbox;
388   return *this;
389 }
390
391 CommImpl& CommImpl::set_src_buff(unsigned char* buff, size_t size)
392 {
393   src_buff_      = buff;
394   src_buff_size_ = size;
395   return *this;
396 }
397
398 CommImpl& CommImpl::set_dst_buff(unsigned char* buff, size_t* size)
399 {
400   dst_buff_      = buff;
401   dst_buff_size_ = size;
402   return *this;
403 }
404
405 CommImpl& CommImpl::detach()
406 {
407   detached_ = true;
408   return *this;
409 }
410
411 CommImpl::~CommImpl()
412 {
413   XBT_DEBUG("Really free communication %p in state %d (detached = %d)", this, static_cast<int>(state_), detached_);
414
415   cleanupSurf();
416
417   if (detached_ && state_ != State::DONE) {
418     /* the communication has failed and was detached:
419      * we have to free the buffer */
420     if (clean_fun)
421       clean_fun(src_buff_);
422     src_buff_ = nullptr;
423   } else if (mbox_) {
424     mbox_->remove(this);
425   }
426 }
427
428 /**  @brief Starts the simulation of a communication synchro. */
429 CommImpl* CommImpl::start()
430 {
431   /* If both the sender and the receiver are already there, start the communication */
432   if (state_ == State::READY) {
433     s4u::Host* sender   = src_actor_->get_host();
434     s4u::Host* receiver = dst_actor_->get_host();
435
436     surf_action_ = surf_network_model->communicate(sender, receiver, size_, rate_);
437     surf_action_->set_activity(this);
438     surf_action_->set_category(get_tracing_category());
439     state_ = State::RUNNING;
440
441     XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p)", this, sender->get_cname(),
442               receiver->get_cname(), surf_action_);
443
444     /* If a link is failed, detect it immediately */
445     if (surf_action_->get_state() == resource::Action::State::FAILED) {
446       XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", sender->get_cname(),
447                 receiver->get_cname());
448       state_ = State::LINK_FAILURE;
449       post();
450
451     } else if (src_actor_->is_suspended() || dst_actor_->is_suspended()) {
452       /* If any of the process is suspended, create the synchro but stop its execution,
453          it will be restarted when the sender process resume */
454       if (src_actor_->is_suspended())
455         XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
456                   "communication",
457                   src_actor_->get_cname(), src_actor_->get_host()->get_cname());
458       else
459         XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
460                   "communication",
461                   dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
462
463       surf_action_->suspend();
464     }
465   }
466
467   return this;
468 }
469
470 /** @brief Copy the communication data from the sender's buffer to the receiver's one  */
471 void CommImpl::copy_data()
472 {
473   size_t buff_size = src_buff_size_;
474   /* If there is no data to copy then return */
475   if (not src_buff_ || not dst_buff_ || copied_)
476     return;
477
478   XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
479             src_actor_ ? src_actor_->get_host()->get_cname() : "a finished process", src_buff_,
480             dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished process", dst_buff_, buff_size);
481
482   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
483   if (dst_buff_size_)
484     buff_size = std::min(buff_size, *(dst_buff_size_));
485
486   /* Update the receiver's buffer size to the copied amount */
487   if (dst_buff_size_)
488     *dst_buff_size_ = buff_size;
489
490   if (buff_size > 0) {
491     if (copy_data_fun)
492       copy_data_fun(this, src_buff_, buff_size);
493     else
494       SIMIX_comm_copy_data_callback(this, src_buff_, buff_size);
495   }
496
497   /* Set the copied flag so we copy data only once */
498   /* (this function might be called from both communication ends) */
499   copied_ = true;
500 }
501
502 void CommImpl::suspend()
503 {
504   /* FIXME: shall we suspend also the timeout synchro? */
505   if (surf_action_)
506     surf_action_->suspend();
507   /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
508 }
509
510 void CommImpl::resume()
511 {
512   /*FIXME: check what happen with the timeouts */
513   if (surf_action_)
514     surf_action_->resume();
515   /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
516 }
517
518 void CommImpl::cancel()
519 {
520   /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
521   if (state_ == State::WAITING) {
522     if (not detached_) {
523       mbox_->remove(this);
524       state_ = State::CANCELED;
525     }
526   } else if (not MC_is_active() /* when running the MC there are no surf actions */
527              && not MC_record_replay_is_active() && (state_ == State::READY || state_ == State::RUNNING)) {
528     surf_action_->cancel();
529   }
530 }
531
532 /** @brief This is part of the cleanup process, probably an internal command */
533 void CommImpl::cleanupSurf()
534 {
535   clean_action();
536
537   if (src_timeout_) {
538     src_timeout_->unref();
539     src_timeout_ = nullptr;
540   }
541
542   if (dst_timeout_) {
543     dst_timeout_->unref();
544     dst_timeout_ = nullptr;
545   }
546 }
547
548 void CommImpl::post()
549 {
550   /* Update synchro state */
551   if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
552     state_ = State::SRC_TIMEOUT;
553   else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
554     state_ = State::DST_TIMEOUT;
555   else if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED)
556     state_ = State::SRC_HOST_FAILURE;
557   else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED)
558     state_ = State::DST_HOST_FAILURE;
559   else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
560     state_ = State::LINK_FAILURE;
561   } else
562     state_ = State::DONE;
563
564   XBT_DEBUG("SIMIX_post_comm: comm %p, state %d, src_proc %p, dst_proc %p, detached: %d", this, (int)state_,
565             src_actor_.get(), dst_actor_.get(), detached_);
566
567   /* destroy the surf actions associated with the Simix communication */
568   cleanupSurf();
569
570   /* Answer all simcalls associated with the synchro */
571   finish();
572 }
573
574 void CommImpl::finish()
575 {
576   while (not simcalls_.empty()) {
577     smx_simcall_t simcall = simcalls_.front();
578     simcalls_.pop_front();
579
580     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
581      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
582      * simcall */
583
584     if (simcall->call_ == SIMCALL_NONE) // FIXME: maybe a better way to handle this case
585       continue;                         // if actor handling comm is killed
586     if (simcall->call_ == SIMCALL_COMM_WAITANY) {
587       SIMIX_waitany_remove_simcall_from_actions(simcall);
588       if (simcall->timeout_cb_) {
589         simcall->timeout_cb_->remove();
590         simcall->timeout_cb_ = nullptr;
591       }
592       if (not MC_is_active() && not MC_record_replay_is_active()) {
593         CommImpl** comms   = simcall_comm_waitany__get__comms(simcall);
594         size_t count       = simcall_comm_waitany__get__count(simcall);
595         CommImpl** element = std::find(comms, comms + count, this);
596         int rank           = (element != comms + count) ? element - comms : -1;
597         simcall_comm_waitany__set__result(simcall, rank);
598       }
599     }
600
601     /* If the synchro is still in a rendez-vous point then remove from it */
602     if (mbox_)
603       mbox_->remove(this);
604
605     XBT_DEBUG("CommImpl::finish(): synchro state = %d", static_cast<int>(state_));
606
607     /* Check out for errors */
608
609     if (not simcall->issuer_->get_host()->is_on()) {
610       simcall->issuer_->context_->iwannadie = true;
611     } else {
612       switch (state_) {
613         case State::DONE:
614           XBT_DEBUG("Communication %p complete!", this);
615           copy_data();
616           break;
617
618         case State::SRC_TIMEOUT:
619           simcall->issuer_->exception_ = std::make_exception_ptr(
620               simgrid::TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the sender"));
621           break;
622
623         case State::DST_TIMEOUT:
624           simcall->issuer_->exception_ = std::make_exception_ptr(
625               simgrid::TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
626           break;
627
628         case State::SRC_HOST_FAILURE:
629           if (simcall->issuer_ == src_actor_)
630             simcall->issuer_->context_->iwannadie = true;
631           else
632             simcall->issuer_->exception_ =
633                 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
634           break;
635
636         case State::DST_HOST_FAILURE:
637           if (simcall->issuer_ == dst_actor_)
638             simcall->issuer_->context_->iwannadie = true;
639           else
640             simcall->issuer_->exception_ =
641                 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
642           break;
643
644         case State::LINK_FAILURE:
645           XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
646                     "detached:%d",
647                     this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
648                     dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, simcall->issuer_->get_cname(),
649                     simcall->issuer_, detached_);
650           if (src_actor_ == simcall->issuer_) {
651             XBT_DEBUG("I'm source");
652           } else if (dst_actor_ == simcall->issuer_) {
653             XBT_DEBUG("I'm dest");
654           } else {
655             XBT_DEBUG("I'm neither source nor dest");
656           }
657           simcall->issuer_->throw_exception(
658               std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Link failure")));
659           break;
660
661         case State::CANCELED:
662           if (simcall->issuer_ == dst_actor_)
663             simcall->issuer_->exception_ = std::make_exception_ptr(
664                 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
665           else
666             simcall->issuer_->exception_ = std::make_exception_ptr(
667                 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
668           break;
669
670         default:
671           xbt_die("Unexpected synchro state in CommImpl::finish: %d", static_cast<int>(state_));
672       }
673       simcall->issuer_->simcall_answer();
674     }
675     /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
676     if (simcall->issuer_->exception_ &&
677         (simcall->call_ == SIMCALL_COMM_WAITANY || simcall->call_ == SIMCALL_COMM_TESTANY)) {
678       // First retrieve the rank of our failing synchro
679       CommImpl** comms;
680       size_t count;
681       if (simcall->call_ == SIMCALL_COMM_WAITANY) {
682         comms = simcall_comm_waitany__get__comms(simcall);
683         count = simcall_comm_waitany__get__count(simcall);
684       } else {
685         /* simcall->call_ == SIMCALL_COMM_TESTANY */
686         comms = simcall_comm_testany__get__comms(simcall);
687         count = simcall_comm_testany__get__count(simcall);
688       }
689       CommImpl** element = std::find(comms, comms + count, this);
690       int rank           = (element != comms + count) ? element - comms : -1;
691
692       // In order to modify the exception we have to rethrow it:
693       try {
694         std::rethrow_exception(simcall->issuer_->exception_);
695       } catch (simgrid::Exception& e) {
696         e.value = rank;
697       }
698     }
699
700     simcall->issuer_->waiting_synchro = nullptr;
701     simcall->issuer_->comms.remove(this);
702     if (detached_) {
703       if (simcall->issuer_ == src_actor_) {
704         if (dst_actor_)
705           dst_actor_->comms.remove(this);
706       } else if (simcall->issuer_ == dst_actor_) {
707         if (src_actor_)
708           src_actor_->comms.remove(this);
709       } else {
710         dst_actor_->comms.remove(this);
711         src_actor_->comms.remove(this);
712       }
713     }
714   }
715 }
716
717 } // namespace activity
718 } // namespace kernel
719 } // namespace simgrid