Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
code simplification around Host::onDestruction
authorMartin Quinson <martin.quinson@loria.fr>
Mon, 21 Dec 2015 21:40:07 +0000 (22:40 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Mon, 21 Dec 2015 21:40:07 +0000 (22:40 +0100)
src/surf/cpu_cas01.cpp
src/surf/cpu_interface.cpp
src/surf/cpu_interface.hpp
src/surf/cpu_ti.cpp
src/surf/host_clm03.cpp
src/surf/host_interface.cpp
src/surf/host_interface.hpp
src/surf/host_ptask_L07.cpp
src/surf/surf_interface.cpp
src/surf/surf_interface.hpp

index 9f1a3a2..1535c15 100644 (file)
@@ -183,7 +183,6 @@ CpuCas01::CpuCas01(CpuCas01Model *model, const char *name, xbt_dynar_t speedPeak
 
 CpuCas01::~CpuCas01()
 {
 
 CpuCas01::~CpuCas01()
 {
-  this->die();
   if (getModel() == surf_cpu_model_pm)
     xbt_dynar_free(&p_speedPeakList);
 }
   if (getModel() == surf_cpu_model_pm)
     xbt_dynar_free(&p_speedPeakList);
 }
index 772e86a..ea7f63e 100644 (file)
@@ -189,14 +189,8 @@ Cpu::Cpu(Model *model, const char *name,
 : Cpu(model, name, core, speedPeak, speedScale, SURF_RESOURCE_ON)
 {}
 
 : Cpu(model, name, core, speedPeak, speedScale, SURF_RESOURCE_ON)
 {}
 
-void Cpu::onDie()
-{
-  Resource::onDie();
-}
-
 Cpu::~Cpu()
 {
 Cpu::~Cpu()
 {
-  this->die();
   if (p_constraintCoreId){
     for (int i = 0; i < m_core; i++) {
          xbt_free(p_constraintCoreId[i]);
   if (p_constraintCoreId){
     for (int i = 0; i < m_core; i++) {
          xbt_free(p_constraintCoreId[i]);
index c2faaf0..bdbbe64 100644 (file)
@@ -159,9 +159,6 @@ public:
   void addTraces(void);
   simgrid::Host* getHost() { return m_host; }
 
   void addTraces(void);
   simgrid::Host* getHost() { return m_host; }
 
-protected:
-  virtual void onDie() override;
-
 public:
   int m_core = 1;                /* Amount of cores */
   double m_speedPeak;            /*< CPU speed peak, ie max value */
 public:
   int m_core = 1;                /* Amount of cores */
   double m_speedPeak;            /*< CPU speed peak, ie max value */
index 3a23bf8..90447fb 100644 (file)
@@ -586,7 +586,6 @@ CpuTi::CpuTi(CpuTiModel *model, const char *name, xbt_dynar_t speedPeak,
 
 CpuTi::~CpuTi()
 {
 
 CpuTi::~CpuTi()
 {
-  this->die();
   modified(false);
   delete p_availTrace;
   delete p_actionSet;
   modified(false);
   delete p_availTrace;
   delete p_actionSet;
index f49434e..4584295 100644 (file)
@@ -128,7 +128,6 @@ HostCLM03::HostCLM03(HostModel *model, const char* name, xbt_dict_t properties,
 
 HostCLM03::~HostCLM03()
 {
 
 HostCLM03::~HostCLM03()
 {
-  this->die();
 }
 
 bool HostCLM03::isUsed(){
 }
 
 bool HostCLM03::isUsed(){
index 3d08ac4..85dc8b7 100644 (file)
@@ -79,10 +79,14 @@ simgrid::surf::signal<void(simgrid::surf::Host*)> Host::onCreation;
 simgrid::surf::signal<void(simgrid::surf::Host*)> Host::onDestruction;
 simgrid::surf::signal<void(simgrid::surf::Host*, e_surf_resource_state_t, e_surf_resource_state_t)> Host::onStateChange;
 
 simgrid::surf::signal<void(simgrid::surf::Host*)> Host::onDestruction;
 simgrid::surf::signal<void(simgrid::surf::Host*, e_surf_resource_state_t, e_surf_resource_state_t)> Host::onStateChange;
 
-void Host::init()
+static void host_destroy(void *h){
+       static_cast<simgrid::surf::Host*>(h)->destroy();
+}
+
+void Host::classInit()
 {
   if (!EXTENSION_ID.valid()) {
 {
   if (!EXTENSION_ID.valid()) {
-    EXTENSION_ID = simgrid::Host::extension_create<simgrid::surf::Host>();
+    EXTENSION_ID = simgrid::Host::extension_create<simgrid::surf::Host>(host_destroy);
     SURF_HOST_LEVEL = EXTENSION_ID.id(); // FIXME: KILLME
   }
 }
     SURF_HOST_LEVEL = EXTENSION_ID.id(); // FIXME: KILLME
   }
 }
@@ -105,15 +109,22 @@ Host::Host(simgrid::surf::Model *model, const char *name, xbt_dict_t props, lmm_
   p_params.ramsize = 0;
 }
 
   p_params.ramsize = 0;
 }
 
-void Host::onDie()
+/** @brief use destroy() instead of this destructor */
+Host::~Host()
 {
 {
-  onDestruction(this);
-  Resource::onDie();
+       xbt_assert(currentlyDestroying_, "Don't delete Hosts directly. Call destroy() instead.");
 }
 }
-
-Host::~Host()
+/** @brief Fire the require callbacks and destroy the object
+ *
+ * Don't delete directly an Host, call h->destroy() instead.
+ */
+void Host::destroy()
 {
 {
-  this->die();
+       if (!currentlyDestroying_) {
+               currentlyDestroying_ = true;
+               onDestruction(this);
+               delete this;
+       }
 }
 
 void Host::attach(simgrid::Host* host)
 }
 
 void Host::attach(simgrid::Host* host)
index 986bff7..d0ebc3e 100644 (file)
@@ -70,7 +70,7 @@ public:
  ************/
 /** @ingroup SURF_host_interface
  * @brief SURF Host interface class
  ************/
 /** @ingroup SURF_host_interface
  * @brief SURF Host interface class
- * @details An host represents a machine with a aggregation of a Cpu, a Link and a Storage
+ * @details An host represents a machine with a aggregation of a Cpu, a RoutingEdge and a Storage
  */
 class Host : public simgrid::surf::Resource,
                 public simgrid::surf::PropertyHolder {
  */
 class Host : public simgrid::surf::Resource,
                 public simgrid::surf::PropertyHolder {
@@ -83,7 +83,7 @@ public:
   static simgrid::surf::signal<void(simgrid::surf::Host*, e_surf_resource_state_t, e_surf_resource_state_t)> onStateChange;
 
 public:
   static simgrid::surf::signal<void(simgrid::surf::Host*, e_surf_resource_state_t, e_surf_resource_state_t)> onStateChange;
 
 public:
-  static void init();
+  static void classInit();
   /**
    * @brief Host constructor
    *
   /**
    * @brief Host constructor
    *
@@ -112,9 +112,17 @@ public:
       lmm_constraint_t constraint, xbt_dynar_t storage, RoutingEdge *netElm,
       Cpu *cpu);
 
       lmm_constraint_t constraint, xbt_dynar_t storage, RoutingEdge *netElm,
       Cpu *cpu);
 
-  /** @brief Host destructor */
+  /* Host destruction logic */
+  /**************************/
+protected:
   ~Host();
   ~Host();
+public:
+       void destroy(); // Must be called instead of the destructor
+private:
+       bool currentlyDestroying_ = false;
+
 
 
+public:
   void attach(simgrid::Host* host);
   void setState(e_surf_resource_state_t state);
 
   void attach(simgrid::Host* host);
   void setState(e_surf_resource_state_t state);
 
@@ -149,7 +157,6 @@ public:
    * @brief Open a file
    *
    * @param fullpath The full path to the file
    * @brief Open a file
    *
    * @param fullpath The full path to the file
-   *
    * @return The StorageAction corresponding to the opening
    */
   virtual Action *open(const char* fullpath);
    * @return The StorageAction corresponding to the opening
    */
   virtual Action *open(const char* fullpath);
@@ -244,9 +251,6 @@ public:
    */
   virtual int fileMove(surf_file_t fd, const char* fullpath);
 
    */
   virtual int fileMove(surf_file_t fd, const char* fullpath);
 
-protected:
-  void onDie() override;
-
 public:
   xbt_dynar_t p_storage;
   RoutingEdge *p_netElm;
 public:
   xbt_dynar_t p_storage;
   RoutingEdge *p_netElm;
index 7d4b212..af34885 100644 (file)
@@ -407,7 +407,6 @@ HostL07::HostL07(HostModel *model, const char* name, xbt_dict_t props, RoutingEd
 
 HostL07::~HostL07()
 {
 
 HostL07::~HostL07()
 {
-  this->die();
 }
 
 CpuL07::CpuL07(CpuL07Model *model, const char* name,
 }
 
 CpuL07::CpuL07(CpuL07Model *model, const char* name,
@@ -429,7 +428,6 @@ CpuL07::CpuL07(CpuL07Model *model, const char* name,
 
 CpuL07::~CpuL07()
 {
 
 CpuL07::~CpuL07()
 {
-  this->die();
 }
 
 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
 }
 
 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
index 3935c96..695653f 100644 (file)
@@ -316,7 +316,7 @@ void surf_init(int *argc, char **argv)
   ROUTING_PROP_ASR_LEVEL = xbt_lib_add_level(as_router_lib,routing_asr_prop_free);
 
   XBT_DEBUG("Add SURF levels");
   ROUTING_PROP_ASR_LEVEL = xbt_lib_add_level(as_router_lib,routing_asr_prop_free);
 
   XBT_DEBUG("Add SURF levels");
-  simgrid::surf::Host::init();
+  simgrid::surf::Host::classInit();
   SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib,surf_storage_free);
 
   xbt_init(argc, argv);
   SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib,surf_storage_free);
 
   xbt_init(argc, argv);
@@ -614,34 +614,8 @@ Resource::Resource(Model *model, const char *name, e_surf_resource_state_t state
   , m_running(true), m_stateCurrent(stateInit)
 {}
 
   , m_running(true), m_stateCurrent(stateInit)
 {}
 
-/** Cleanup code of the full object.
- *
- *  The destructed callbacks might need to have a fully
- *  created `Cpu` instance so they cannot be called `~Cpu()`
- *  (at this point the fields of the parents have been destroyed
- *  and the virtual methods are the ones of `Cpu`). If a `Cpu`
- *  subclass overrides any virtual method, it should call this
- *  method at the beginning of the destructor in order to trigger
- *  the callbacks in the real state of the Cpu.
- *
- *  Once the method has been called once, it becomes a noop ensuring
- *  that the callbacks are not for each class of the hierarchy.
- *
- *  A better solution would be to call a `Cpu::destroy()` method
- *  before calling the destructor and trigger the callbacks here.
- */
-void Resource::die()
-{
-  if (alive_) {
-    onDie();
-    alive_ = false;
-  }
-}
-
-void Resource::onDie() {}
 
 Resource::~Resource() {
 
 Resource::~Resource() {
-  this->die();
   xbt_free((void*)p_name);
 }
 
   xbt_free((void*)p_name);
 }
 
index 695fe11..8599ee8 100644 (file)
@@ -478,12 +478,7 @@ public:
   /** @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource */
   virtual void setState(e_surf_resource_state_t state);
 
   /** @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource */
   virtual void setState(e_surf_resource_state_t state);
 
-protected:
-  void die();
-  virtual void onDie();
-
 private:
 private:
-  bool alive_ = true;
   const char *p_name;
   Model *p_model;
   bool m_running;
   const char *p_name;
   Model *p_model;
   bool m_running;