Logo AND Algorithmique Numérique Distribuée

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