Logo AND Algorithmique Numérique Distribuée

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