Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / src / kernel / activity / CommImpl.cpp
index 769c8e6..9f4b218 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -20,19 +20,20 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related syn
 
 XBT_PRIVATE void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_actor_t src, smx_mailbox_t mbox, double task_size,
                                            double rate, unsigned char* src_buff, size_t src_buff_size,
-                                           int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
+                                           bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
                                            void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
                                            void* data, double timeout)
 {
-  smx_activity_t comm = simcall_HANDLER_comm_isend(simcall, src, mbox, task_size, rate, src_buff, src_buff_size,
-                                                   match_fun, nullptr, copy_data_fun, data, 0);
+  simgrid::kernel::activity::ActivityImplPtr comm = simcall_HANDLER_comm_isend(
+      simcall, src, mbox, task_size, rate, src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, false);
   SIMCALL_SET_MC_VALUE(*simcall, 0);
   simcall_HANDLER_comm_wait(simcall, static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
 }
 
-XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_isend(
+XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr simcall_HANDLER_comm_isend(
     smx_simcall_t /*simcall*/, smx_actor_t src_proc, smx_mailbox_t mbox, double task_size, double rate,
-    unsigned char* src_buff, size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
+    unsigned char* src_buff, size_t src_buff_size,
+    bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
     void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
     void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), // used to copy data if not default one
     void* data, bool detached)
@@ -40,8 +41,7 @@ XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_isend(
   XBT_DEBUG("send from mailbox %p", mbox);
 
   /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
-  simgrid::kernel::activity::CommImplPtr this_comm =
-      simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl());
+  simgrid::kernel::activity::CommImplPtr this_comm(new simgrid::kernel::activity::CommImpl());
   this_comm->set_type(simgrid::kernel::activity::CommImpl::Type::SEND);
 
   /* Look for communication synchro matching our needs. We also provide a description of
@@ -77,7 +77,7 @@ XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_isend(
     other_comm->clean_fun = clean_fun;
   } else {
     other_comm->clean_fun = nullptr;
-    src_proc->comms.push_back(other_comm);
+    src_proc->activities_.emplace_back(other_comm);
   }
 
   /* Setup the communication synchro */
@@ -98,30 +98,29 @@ XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_isend(
 
 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
                                            unsigned char* dst_buff, size_t* dst_buff_size,
-                                           int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
+                                           bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
                                            void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
                                            void* data, double timeout, double rate)
 {
-  smx_activity_t comm = simcall_HANDLER_comm_irecv(simcall, receiver, mbox, dst_buff, dst_buff_size, match_fun,
-                                                   copy_data_fun, data, rate);
+  simgrid::kernel::activity::ActivityImplPtr comm = simcall_HANDLER_comm_irecv(
+      simcall, receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
   SIMCALL_SET_MC_VALUE(*simcall, 0);
   simcall_HANDLER_comm_wait(simcall, static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
 }
 
-XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_irecv(
-    smx_simcall_t /*simcall*/, smx_actor_t receiver, smx_mailbox_t mbox, unsigned char* dst_buff, size_t* dst_buff_size,
-    simix_match_func_t match_fun, void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
-    void* data, double rate)
+XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr
+simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver, smx_mailbox_t mbox, unsigned char* dst_buff,
+                           size_t* dst_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
+                           void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
+                           double rate)
 {
-  simgrid::kernel::activity::CommImplPtr this_synchro =
-      simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl());
+  simgrid::kernel::activity::CommImplPtr this_synchro(new simgrid::kernel::activity::CommImpl());
   this_synchro->set_type(simgrid::kernel::activity::CommImpl::Type::RECEIVE);
   XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
 
   simgrid::kernel::activity::CommImplPtr other_comm;
   // communication already done, get it inside the list of completed comms
   if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
-
     XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
     // find a match in the list of already received comms
     other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
@@ -161,7 +160,7 @@ XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_irecv(
       other_comm->state_ = simgrid::kernel::activity::State::READY;
       other_comm->set_type(simgrid::kernel::activity::CommImpl::Type::READY);
     }
-    receiver->comms.push_back(other_comm);
+    receiver->activities_.emplace_back(other_comm);
   }
 
   /* Setup communication synchro */
@@ -310,9 +309,10 @@ void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, simgrid::kernel::activi
   }
 
   if (timeout < 0.0) {
-    simcall->timeout_cb_ = NULL;
+    simcall->timeout_cb_ = nullptr;
   } else {
     simcall->timeout_cb_ = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [simcall]() {
+      simcall->timeout_cb_ = nullptr;
       SIMIX_waitany_remove_simcall_from_actions(simcall);
       simcall_comm_waitany__set__result(simcall, -1);
       simcall->issuer_->simcall_answer();
@@ -430,7 +430,6 @@ CommImpl* CommImpl::start()
 {
   /* If both the sender and the receiver are already there, start the communication */
   if (state_ == State::READY) {
-
     s4u::Host* sender   = src_actor_->get_host();
     s4u::Host* receiver = dst_actor_->get_host();
 
@@ -481,12 +480,12 @@ void CommImpl::copy_data()
             dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished process", dst_buff_, buff_size);
 
   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
-  if (dst_buff_size_)
+  if (dst_buff_size_) {
     buff_size = std::min(buff_size, *(dst_buff_size_));
 
-  /* Update the receiver's buffer size to the copied amount */
-  if (dst_buff_size_)
+    /* Update the receiver's buffer size to the copied amount */
     *dst_buff_size_ = buff_size;
+  }
 
   if (buff_size > 0) {
     if (copy_data_fun)
@@ -582,9 +581,9 @@ void CommImpl::finish()
      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
      * simcall */
 
-    if (simcall->call_ == SIMCALL_NONE) // FIXME: maybe a better way to handle this case
-      continue;                         // if actor handling comm is killed
-    if (simcall->call_ == SIMCALL_COMM_WAITANY) {
+    if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
+      continue;                                 // if actor handling comm is killed
+    if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
       SIMIX_waitany_remove_simcall_from_actions(simcall);
       if (simcall->timeout_cb_) {
         simcall->timeout_cb_->remove();
@@ -608,10 +607,9 @@ void CommImpl::finish()
     /* Check out for errors */
 
     if (not simcall->issuer_->get_host()->is_on()) {
-      simcall->issuer_->context_->iwannadie = true;
+      simcall->issuer_->context_->set_wannadie();
     } else {
       switch (state_) {
-
         case State::DONE:
           XBT_DEBUG("Communication %p complete!", this);
           copy_data();
@@ -629,7 +627,7 @@ void CommImpl::finish()
 
         case State::SRC_HOST_FAILURE:
           if (simcall->issuer_ == src_actor_)
-            simcall->issuer_->context_->iwannadie = true;
+            simcall->issuer_->context_->set_wannadie();
           else
             simcall->issuer_->exception_ =
                 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
@@ -637,7 +635,7 @@ void CommImpl::finish()
 
         case State::DST_HOST_FAILURE:
           if (simcall->issuer_ == dst_actor_)
-            simcall->issuer_->context_->iwannadie = true;
+            simcall->issuer_->context_->set_wannadie();
           else
             simcall->issuer_->exception_ =
                 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
@@ -676,15 +674,15 @@ void CommImpl::finish()
     }
     /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
     if (simcall->issuer_->exception_ &&
-        (simcall->call_ == SIMCALL_COMM_WAITANY || simcall->call_ == SIMCALL_COMM_TESTANY)) {
+        (simcall->call_ == simix::Simcall::COMM_WAITANY || simcall->call_ == simix::Simcall::COMM_TESTANY)) {
       // First retrieve the rank of our failing synchro
       CommImpl** comms;
       size_t count;
-      if (simcall->call_ == SIMCALL_COMM_WAITANY) {
+      if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
         comms = simcall_comm_waitany__get__comms(simcall);
         count = simcall_comm_waitany__get__count(simcall);
       } else {
-        /* simcall->call_ == SIMCALL_COMM_TESTANY */
+        /* simcall->call_ == simix::Simcall::COMM_TESTANY */
         comms = simcall_comm_testany__get__comms(simcall);
         count = simcall_comm_testany__get__count(simcall);
       }
@@ -699,18 +697,18 @@ void CommImpl::finish()
       }
     }
 
-    simcall->issuer_->waiting_synchro = nullptr;
-    simcall->issuer_->comms.remove(this);
+    simcall->issuer_->waiting_synchro_ = nullptr;
+    simcall->issuer_->activities_.remove(this);
     if (detached_) {
       if (simcall->issuer_ == src_actor_) {
         if (dst_actor_)
-          dst_actor_->comms.remove(this);
+          dst_actor_->activities_.remove(this);
       } else if (simcall->issuer_ == dst_actor_) {
         if (src_actor_)
-          src_actor_->comms.remove(this);
+          src_actor_->activities_.remove(this);
       } else {
-        dst_actor_->comms.remove(this);
-        src_actor_->comms.remove(this);
+        dst_actor_->activities_.remove(this);
+        src_actor_->activities_.remove(this);
       }
     }
   }