Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use C++ to implement dynamic dispatch
authorMartin Quinson <martin.quinson@loria.fr>
Tue, 10 May 2016 10:12:19 +0000 (12:12 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Tue, 10 May 2016 10:12:22 +0000 (12:12 +0200)
Have a look at the code of SIMIX_simcall_exit() to understand what
this change is about.

21 files changed:
src/simix/Synchro.h
src/simix/SynchroComm.cpp
src/simix/SynchroComm.hpp
src/simix/SynchroExec.cpp
src/simix/SynchroExec.hpp
src/simix/SynchroIo.cpp
src/simix/SynchroIo.hpp
src/simix/SynchroRaw.cpp
src/simix/SynchroRaw.hpp
src/simix/SynchroSleep.cpp
src/simix/SynchroSleep.hpp
src/simix/popping.cpp
src/simix/smx_host.cpp
src/simix/smx_host_private.h
src/simix/smx_io.cpp
src/simix/smx_network.cpp
src/simix/smx_network_private.h
src/simix/smx_process.cpp
src/simix/smx_process_private.h
src/simix/smx_synchro.cpp
src/simix/smx_synchro_private.h

index dc5a949..05b28a1 100644 (file)
@@ -25,6 +25,7 @@ namespace simix {
 
     virtual void suspend()=0;
     virtual void resume()=0;
+    virtual void post() =0; // What to do when a simcall terminates
 
     void ref();
     void unref();
index b074baa..4342cb1 100644 (file)
@@ -109,3 +109,30 @@ void simgrid::simix::Comm::cleanupSurf()
     dst_timeout = NULL;
   }
 }
+
+void simgrid::simix::Comm::post()
+{
+  /* Update synchro state */
+  if (src_timeout &&  src_timeout->getState() == simgrid::surf::Action::State::done)
+    state = SIMIX_SRC_TIMEOUT;
+  else if (dst_timeout && dst_timeout->getState() == simgrid::surf::Action::State::done)
+    state = SIMIX_DST_TIMEOUT;
+  else if (src_timeout && src_timeout->getState() == simgrid::surf::Action::State::failed)
+    state = SIMIX_SRC_HOST_FAILURE;
+  else if (dst_timeout && dst_timeout->getState() == simgrid::surf::Action::State::failed)
+    state = SIMIX_DST_HOST_FAILURE;
+  else if (surf_comm && surf_comm->getState() == simgrid::surf::Action::State::failed) {
+    state = SIMIX_LINK_FAILURE;
+  } else
+    state = SIMIX_DONE;
+
+  XBT_DEBUG("SIMIX_post_comm: comm %p, state %d, src_proc %p, dst_proc %p, detached: %d",
+            this, (int)state, src_proc, dst_proc, detached);
+
+  /* destroy the surf actions associated with the Simix communication */
+  cleanupSurf();
+
+  /* if there are simcalls associated with the synchro, then answer them */
+  if (xbt_fifo_size(simcalls))
+    SIMIX_comm_finish(this);
+}
index 6d4d7b7..b58c83c 100644 (file)
@@ -25,6 +25,7 @@ namespace simix {
     Comm(e_smx_comm_type_t type);
     void suspend();
     void resume();
+    void post() override;
     void cancel();
     double remains();
     void cleanupSurf(); // FIXME: make me protected
index e206b73..3b0f2c8 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "src/simix/SynchroExec.hpp"
 #include "src/surf/surf_interface.hpp"
+#include "src/simix/smx_host_private.h"
 
 simgrid::simix::Exec::~Exec()
 {
@@ -30,3 +31,26 @@ double simgrid::simix::Exec::remains()
 
   return 0;
 }
+
+void simgrid::simix::Exec::post()
+{
+  if (host && host->isOff()) {/* FIMXE: handle resource failure for parallel tasks too */
+    /* If the host running the synchro failed, notice it. This way, the asking
+     * process can be killed if it runs on that host itself */
+    state = SIMIX_FAILED;
+  } else if (surf_exec->getState() == simgrid::surf::Action::State::failed) {
+    /* If the host running the synchro didn't fail, then the synchro was canceled */
+    state = SIMIX_CANCELED;
+  } else {
+    state = SIMIX_DONE;
+  }
+
+  if (surf_exec) {
+    surf_exec->unref();
+    surf_exec = NULL;
+  }
+
+  /* If there are simcalls associated with the synchro, then answer them */
+  if (xbt_fifo_size(simcalls))
+    SIMIX_execution_finish(this);
+}
index 7904cfe..d0abb29 100644 (file)
@@ -17,6 +17,7 @@ namespace simix {
   public:
     void suspend();
     void resume();
+    void post() override;
     double remains();
 
     sg_host_t host;                /* The host where the execution takes place */
index e27e5f5..dd2f2e2 100644 (file)
@@ -5,6 +5,8 @@
 
 #include "src/simix/SynchroIo.hpp"
 #include "src/surf/surf_interface.hpp"
+#include "src/simix/popping_private.h"
+#include "src/simix/smx_private.h"
 
 void simgrid::simix::Io::suspend()
 {
@@ -17,3 +19,51 @@ void simgrid::simix::Io::resume()
   if (surf_io)
     surf_io->resume();
 }
+
+void simgrid::simix::Io::post()
+{
+  xbt_fifo_item_t i;
+  smx_simcall_t simcall;
+
+  xbt_fifo_foreach(simcalls,i,simcall,smx_simcall_t) {
+    switch (simcall->call) {
+    case SIMCALL_FILE_OPEN: {
+      smx_file_t tmp = xbt_new(s_smx_file_t,1);
+      tmp->surf_file = surf_storage_action_get_file(surf_io);
+      simcall_file_open__set__result(simcall, tmp);
+      break;
+    }
+    case SIMCALL_FILE_CLOSE:
+      xbt_free(simcall_file_close__get__fd(simcall));
+      simcall_file_close__set__result(simcall, 0);
+      break;
+    case SIMCALL_FILE_WRITE:
+      simcall_file_write__set__result(simcall, surf_io->getCost());
+      break;
+
+    case SIMCALL_FILE_READ:
+      simcall_file_read__set__result(simcall, surf_io->getCost());
+      break;
+
+    default:
+      break;
+    }
+  }
+
+  switch (surf_io->getState()) {
+
+    case simgrid::surf::Action::State::failed:
+      state = SIMIX_FAILED;
+      break;
+
+    case simgrid::surf::Action::State::done:
+      state = SIMIX_DONE;
+      break;
+
+    default:
+      THROW_IMPOSSIBLE;
+      break;
+  }
+
+  SIMIX_io_finish(this);
+}
index 3d0aa2b..afe6041 100644 (file)
@@ -16,6 +16,7 @@ namespace simix {
   public:
     void suspend();
     void resume();
+    void post() override;
 
     sg_host_t host;
     surf_action_t surf_io;
index e73fe3d..872cf47 100644 (file)
@@ -5,12 +5,28 @@
 
 #include "src/simix/SynchroRaw.hpp"
 #include "src/surf/surf_interface.hpp"
+#include "src/simix/smx_synchro_private.h"
 
-void simgrid::simix::Raw::suspend() {
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_synchro);
+
+void simgrid::simix::Raw::suspend()
+{
   /* The suspension of raw synchros is delayed to when the process is rescheduled. */
 }
 
-void simgrid::simix::Raw::resume() {
+void simgrid::simix::Raw::resume()
+{
   /* I cannot resume raw synchros directly. This is delayed to when the process is rescheduled at
    * the end of the synchro. */
 }
+void simgrid::simix::Raw::post()
+{
+  XBT_IN("(%p)",this);
+  if (sleep->getState() == simgrid::surf::Action::State::failed)
+    state = SIMIX_FAILED;
+  else if(sleep->getState() == simgrid::surf::Action::State::done)
+    state = SIMIX_SRC_TIMEOUT;
+
+  SIMIX_synchro_finish(this);
+  XBT_OUT();
+}
index c0a3444..25d4c2d 100644 (file)
@@ -17,6 +17,7 @@ namespace simix {
   public:
     void suspend();
     void resume();
+    void post() override;
 
     surf_action_t sleep;
   };
index 2395950..853dd31 100644 (file)
@@ -5,11 +5,56 @@
 
 #include "src/simix/SynchroSleep.hpp"
 #include "src/surf/surf_interface.hpp"
+#include "src/simix/popping_private.h"
+#include "src/simix/smx_process_private.h"
 
-void simgrid::simix::Sleep::suspend() {
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
+
+void simgrid::simix::Sleep::suspend()
+{
   surf_sleep->suspend();
 }
 
-void simgrid::simix::Sleep::resume() {
+void simgrid::simix::Sleep::resume()
+{
   surf_sleep->resume();
 }
+
+void simgrid::simix::Sleep::post()
+{
+  smx_simcall_t simcall;
+  e_smx_state_t state;
+
+  while ((simcall = (smx_simcall_t) xbt_fifo_shift(simcalls))) {
+
+    switch (surf_sleep->getState()){
+      case simgrid::surf::Action::State::failed:
+        simcall->issuer->context->iwannadie = 1;
+        //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
+        state = SIMIX_SRC_HOST_FAILURE;
+        break;
+
+      case simgrid::surf::Action::State::done:
+        state = SIMIX_DONE;
+        break;
+
+      default:
+        THROW_IMPOSSIBLE;
+        break;
+    }
+    if (simcall->issuer->host->isOff()) {
+      simcall->issuer->context->iwannadie = 1;
+    }
+    simcall_process_sleep__set__result(simcall, state);
+    simcall->issuer->waiting_synchro = NULL;
+    if (simcall->issuer->suspended) {
+      XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
+      simcall->issuer->suspended = 0;
+      simcall_HANDLER_process_suspend(simcall, simcall->issuer);
+    } else {
+      SIMIX_simcall_answer(simcall);
+    }
+  }
+
+  SIMIX_process_sleep_destroy(this);
+}
index 5a87136..b999526 100644 (file)
@@ -16,6 +16,7 @@ namespace simix {
   public:
     void suspend();
     void resume();
+    void post() override;
 
     sg_host_t host;                /* The host that is sleeping */
     surf_action_t surf_sleep;       /* The Surf sleeping action encapsulated */
index e8c8e5f..631bc80 100644 (file)
@@ -37,35 +37,7 @@ void SIMIX_simcall_answer(smx_simcall_t simcall)
 
 void SIMIX_simcall_exit(smx_synchro_t synchro)
 {
-  simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(synchro);
-  if (exec != nullptr) {
-    SIMIX_post_host_execute(exec);
-    return;
-  }
-
-  simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(synchro);
-  if (comm != nullptr) {
-    SIMIX_post_comm(synchro);
-    return;
-  }
-
-  simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(synchro);
-  if (sleep != nullptr) {
-    SIMIX_post_process_sleep(synchro);
-    return;
-  }
-
-  simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(synchro);
-  if (raw != nullptr) {
-    SIMIX_post_synchro(synchro);
-    return;
-  }
-
-  simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(synchro);
-  if (io != nullptr) {
-    SIMIX_post_io(synchro);
-    return;
-  }
+  synchro->post();
 }
 
 void SIMIX_run_kernel(void* code)
index 79630cc..2fc7eb4 100644 (file)
@@ -16,8 +16,6 @@
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix, "SIMIX hosts");
 
-static void SIMIX_execution_finish(simgrid::simix::Exec *exec);
-
 /**
  * \brief Internal function to create a SIMIX host.
  * \param name name of the host to create
@@ -438,32 +436,6 @@ void SIMIX_execution_finish(simgrid::simix::Exec *exec)
   exec->unref();
 }
 
-
-void SIMIX_post_host_execute(simgrid::simix::Exec *exec)
-{
-  if (exec != nullptr && exec->host && /* FIMXE: handle resource failure for parallel tasks too */
-      exec->host->isOff()) {
-    /* If the host running the synchro failed, notice it. This way, the asking
-     * process can be killed if it runs on that host itself */
-    exec->state = SIMIX_FAILED;
-  } else if (exec->surf_exec->getState() == simgrid::surf::Action::State::failed) {
-    /* If the host running the synchro didn't fail, then the synchro was canceled */
-    exec->state = SIMIX_CANCELED;
-  } else {
-    exec->state = SIMIX_DONE;
-  }
-
-  if (exec != nullptr && exec->surf_exec) {
-    exec->surf_exec->unref();
-    exec->surf_exec = NULL;
-  }
-
-  /* If there are simcalls associated with the synchro, then answer them */
-  if (xbt_fifo_size(exec->simcalls))
-    SIMIX_execution_finish(exec);
-}
-
-
 void SIMIX_set_category(smx_synchro_t synchro, const char *category)
 {
   if (synchro->state != SIMIX_RUNNING) return;
index 2981202..a96e5de 100644 (file)
@@ -52,8 +52,8 @@ XBT_PRIVATE void SIMIX_execution_set_affinity(smx_synchro_t synchro, sg_host_t h
 
 XBT_PRIVATE void SIMIX_execution_suspend(smx_synchro_t synchro);
 XBT_PRIVATE void SIMIX_execution_resume(smx_synchro_t synchro);
+XBT_PRIVATE void SIMIX_execution_finish(simgrid::simix::Exec *exec);
 
-XBT_PRIVATE void SIMIX_post_host_execute(simgrid::simix::Exec *exec);
 XBT_PRIVATE void SIMIX_set_category(smx_synchro_t synchro, const char *category);
 
 /* vm related stuff */
index 755f531..0834120 100644 (file)
@@ -255,52 +255,6 @@ const char* SIMIX_storage_get_host(smx_storage_t storage){
 
 void SIMIX_post_io(smx_synchro_t synchro)
 {
-  xbt_fifo_item_t i;
-  smx_simcall_t simcall;
-
-  simgrid::simix::Io *io = static_cast<simgrid::simix::Io*>(synchro);
-
-  xbt_fifo_foreach(synchro->simcalls,i,simcall,smx_simcall_t) {
-    switch (simcall->call) {
-    case SIMCALL_FILE_OPEN: {
-      smx_file_t tmp = xbt_new(s_smx_file_t,1);
-      tmp->surf_file = surf_storage_action_get_file(io->surf_io);
-      simcall_file_open__set__result(simcall, tmp);
-      break;
-    }
-    case SIMCALL_FILE_CLOSE:
-      xbt_free(simcall_file_close__get__fd(simcall));
-      simcall_file_close__set__result(simcall, 0);
-      break;
-    case SIMCALL_FILE_WRITE:
-      simcall_file_write__set__result(simcall, io->surf_io->getCost());
-      break;
-
-    case SIMCALL_FILE_READ:
-      simcall_file_read__set__result(simcall, io->surf_io->getCost());
-      break;
-
-    default:
-      break;
-    }
-  }
-
-  switch (io->surf_io->getState()) {
-
-    case simgrid::surf::Action::State::failed:
-      synchro->state = SIMIX_FAILED;
-      break;
-
-    case simgrid::surf::Action::State::done:
-      synchro->state = SIMIX_DONE;
-      break;
-
-    default:
-      THROW_IMPOSSIBLE;
-      break;
-  }
-
-  SIMIX_io_finish(synchro);
 }
 
 void SIMIX_io_destroy(smx_synchro_t synchro)
index bec9527..51a5ab7 100644 (file)
@@ -693,44 +693,6 @@ void SIMIX_comm_finish(smx_synchro_t synchro)
     static_cast<simgrid::simix::Comm*>(synchro)->unref();
 }
 
-/**
- * \brief This function is called when a Surf communication synchro is finished.
- * \param synchro the corresponding Simix communication
- */
-void SIMIX_post_comm(smx_synchro_t synchro)
-{
-  simgrid::simix::Comm *comm = static_cast<simgrid::simix::Comm*>(synchro);
-
-  /* Update synchro state */
-  if (comm->src_timeout &&
-      comm->src_timeout->getState() == simgrid::surf::Action::State::done)
-    synchro->state = SIMIX_SRC_TIMEOUT;
-  else if (comm->dst_timeout &&
-    comm->dst_timeout->getState() == simgrid::surf::Action::State::done)
-    synchro->state = SIMIX_DST_TIMEOUT;
-  else if (comm->src_timeout &&
-    comm->src_timeout->getState() == simgrid::surf::Action::State::failed)
-    synchro->state = SIMIX_SRC_HOST_FAILURE;
-  else if (comm->dst_timeout &&
-      comm->dst_timeout->getState() == simgrid::surf::Action::State::failed)
-    synchro->state = SIMIX_DST_HOST_FAILURE;
-  else if (comm->surf_comm &&
-    comm->surf_comm->getState() == simgrid::surf::Action::State::failed) {
-    synchro->state = SIMIX_LINK_FAILURE;
-  } else
-    synchro->state = SIMIX_DONE;
-
-  XBT_DEBUG("SIMIX_post_comm: comm %p, state %d, src_proc %p, dst_proc %p, detached: %d",
-            comm, (int)comm->state, comm->src_proc, comm->dst_proc, comm->detached);
-
-  /* destroy the surf actions associated with the Simix communication */
-  comm->cleanupSurf();
-
-  /* if there are simcalls associated with the synchro, then answer them */
-  if (xbt_fifo_size(synchro->simcalls))
-    SIMIX_comm_finish(comm);
-}
-
 /******************************************************************************/
 /*                    SIMIX_comm_copy_data callbacks                       */
 /******************************************************************************/
index 1451a8e..55fc699 100644 (file)
@@ -35,7 +35,6 @@ XBT_PRIVATE smx_synchro_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_mailbox_t
                               void *data, double rate);
 XBT_PRIVATE smx_synchro_t SIMIX_comm_iprobe(smx_process_t dst_proc, smx_mailbox_t mbox, int type, int src,
                               int tag, int (*match_fun)(void *, void *, smx_synchro_t), void *data);
-XBT_PRIVATE void SIMIX_post_comm(smx_synchro_t synchro);
 
 #endif
 
index 0d46b87..ef55883 100644 (file)
@@ -859,46 +859,6 @@ smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
   return synchro;
 }
 
-void SIMIX_post_process_sleep(smx_synchro_t synchro)
-{
-  smx_simcall_t simcall;
-  e_smx_state_t state;
-  simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
-
-  while ((simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls))) {
-
-    switch (sleep->surf_sleep->getState()){
-      case simgrid::surf::Action::State::failed:
-        simcall->issuer->context->iwannadie = 1;
-        //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
-        state = SIMIX_SRC_HOST_FAILURE;
-        break;
-
-      case simgrid::surf::Action::State::done:
-        state = SIMIX_DONE;
-        break;
-
-      default:
-        THROW_IMPOSSIBLE;
-        break;
-    }
-    if (simcall->issuer->host->isOff()) {
-      simcall->issuer->context->iwannadie = 1;
-    }
-    simcall_process_sleep__set__result(simcall, state);
-    simcall->issuer->waiting_synchro = NULL;
-    if (simcall->issuer->suspended) {
-      XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
-      simcall->issuer->suspended = 0;
-      simcall_HANDLER_process_suspend(simcall, simcall->issuer);
-    } else {
-      SIMIX_simcall_answer(simcall);
-    }
-  }
-
-  SIMIX_process_sleep_destroy(synchro);
-}
-
 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
 {
   XBT_DEBUG("Destroy synchro %p", synchro);
index c721913..89a28c3 100644 (file)
@@ -101,7 +101,6 @@ XBT_PRIVATE int SIMIX_process_is_suspended(smx_process_t process);
 XBT_PRIVATE xbt_dict_t SIMIX_process_get_properties(smx_process_t process);
 XBT_PRIVATE smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout);
 XBT_PRIVATE smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration);
-XBT_PRIVATE void SIMIX_post_process_sleep(smx_synchro_t synchro);
 
 XBT_PRIVATE void SIMIX_process_sleep_destroy(smx_synchro_t synchro);
 XBT_PRIVATE void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart);
index afb5783..442695e 100644 (file)
@@ -14,7 +14,6 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
                                 "SIMIX Synchronization (mutex, semaphores and conditions)");
 
 static smx_synchro_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout);
-static void SIMIX_synchro_finish(smx_synchro_t synchro);
 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
                              smx_process_t issuer, smx_simcall_t simcall);
 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
@@ -74,20 +73,7 @@ void SIMIX_synchro_destroy(smx_synchro_t synchro)
   delete raw;
 }
 
-void SIMIX_post_synchro(smx_synchro_t synchro)
-{
-  XBT_IN("(%p)",synchro);
-  simgrid::simix::Raw *raw = static_cast<simgrid::simix::Raw*>(synchro);
-  if (raw->sleep->getState() == simgrid::surf::Action::State::failed)
-    raw->state = SIMIX_FAILED;
-  else if(raw->sleep->getState() == simgrid::surf::Action::State::done)
-    raw->state = SIMIX_SRC_TIMEOUT;
-
-  SIMIX_synchro_finish(raw);
-  XBT_OUT();
-}
-
-static void SIMIX_synchro_finish(smx_synchro_t synchro)
+void SIMIX_synchro_finish(smx_synchro_t synchro)
 {
   XBT_IN("(%p)",synchro);
   smx_simcall_t simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls);
index 9cf2592..c8ea1f6 100644 (file)
@@ -10,6 +10,7 @@
 #include "xbt/base.h"
 #include "xbt/swag.h"
 #include "xbt/xbt_os_thread.h"
+#include "src/simix/popping_private.h"
 
 typedef struct s_smx_mutex {
   unsigned int locked;
@@ -30,6 +31,7 @@ typedef struct s_smx_sem {
 XBT_PRIVATE void SIMIX_post_synchro(smx_synchro_t synchro);
 XBT_PRIVATE void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t simcall);
 XBT_PRIVATE void SIMIX_synchro_destroy(smx_synchro_t synchro);
+XBT_PRIVATE void SIMIX_synchro_finish(smx_synchro_t synchro);
 
 XBT_PRIVATE smx_mutex_t SIMIX_mutex_init(void);
 XBT_PRIVATE int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer);