From: Martin Quinson Date: Mon, 21 Dec 2015 21:40:07 +0000 (+0100) Subject: code simplification around Host::onDestruction X-Git-Tag: v3_13~1416 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/736f038a333b21482b9e73ed2860a7f096e29ac8 code simplification around Host::onDestruction --- diff --git a/src/surf/cpu_cas01.cpp b/src/surf/cpu_cas01.cpp index 9f1a3a2c42..1535c15e67 100644 --- a/src/surf/cpu_cas01.cpp +++ b/src/surf/cpu_cas01.cpp @@ -183,7 +183,6 @@ CpuCas01::CpuCas01(CpuCas01Model *model, const char *name, xbt_dynar_t speedPeak CpuCas01::~CpuCas01() { - this->die(); if (getModel() == surf_cpu_model_pm) xbt_dynar_free(&p_speedPeakList); } diff --git a/src/surf/cpu_interface.cpp b/src/surf/cpu_interface.cpp index 772e86ae3f..ea7f63e37a 100644 --- a/src/surf/cpu_interface.cpp +++ b/src/surf/cpu_interface.cpp @@ -189,14 +189,8 @@ Cpu::Cpu(Model *model, const char *name, : Cpu(model, name, core, speedPeak, speedScale, SURF_RESOURCE_ON) {} -void Cpu::onDie() -{ - Resource::onDie(); -} - Cpu::~Cpu() { - this->die(); if (p_constraintCoreId){ for (int i = 0; i < m_core; i++) { xbt_free(p_constraintCoreId[i]); diff --git a/src/surf/cpu_interface.hpp b/src/surf/cpu_interface.hpp index c2faaf0ac7..bdbbe642ac 100644 --- a/src/surf/cpu_interface.hpp +++ b/src/surf/cpu_interface.hpp @@ -159,9 +159,6 @@ public: 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 */ diff --git a/src/surf/cpu_ti.cpp b/src/surf/cpu_ti.cpp index 3a23bf89a5..90447fb0e3 100644 --- a/src/surf/cpu_ti.cpp +++ b/src/surf/cpu_ti.cpp @@ -586,7 +586,6 @@ CpuTi::CpuTi(CpuTiModel *model, const char *name, xbt_dynar_t speedPeak, CpuTi::~CpuTi() { - this->die(); modified(false); delete p_availTrace; delete p_actionSet; diff --git a/src/surf/host_clm03.cpp b/src/surf/host_clm03.cpp index f49434ed37..4584295a1c 100644 --- a/src/surf/host_clm03.cpp +++ b/src/surf/host_clm03.cpp @@ -128,7 +128,6 @@ HostCLM03::HostCLM03(HostModel *model, const char* name, xbt_dict_t properties, HostCLM03::~HostCLM03() { - this->die(); } bool HostCLM03::isUsed(){ diff --git a/src/surf/host_interface.cpp b/src/surf/host_interface.cpp index 3d08ac4e44..85dc8b73fc 100644 --- a/src/surf/host_interface.cpp +++ b/src/surf/host_interface.cpp @@ -79,10 +79,14 @@ simgrid::surf::signal Host::onCreation; simgrid::surf::signal Host::onDestruction; simgrid::surf::signal Host::onStateChange; -void Host::init() +static void host_destroy(void *h){ + static_cast(h)->destroy(); +} + +void Host::classInit() { if (!EXTENSION_ID.valid()) { - EXTENSION_ID = simgrid::Host::extension_create(); + EXTENSION_ID = simgrid::Host::extension_create(host_destroy); 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; } -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) diff --git a/src/surf/host_interface.hpp b/src/surf/host_interface.hpp index 986bff7db4..d0ebc3ecd3 100644 --- a/src/surf/host_interface.hpp +++ b/src/surf/host_interface.hpp @@ -70,7 +70,7 @@ public: ************/ /** @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 { @@ -83,7 +83,7 @@ public: static simgrid::surf::signal onStateChange; public: - static void init(); + static void classInit(); /** * @brief Host constructor * @@ -112,9 +112,17 @@ public: lmm_constraint_t constraint, xbt_dynar_t storage, RoutingEdge *netElm, Cpu *cpu); - /** @brief Host destructor */ + /* Host destruction logic */ + /**************************/ +protected: ~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); @@ -149,7 +157,6 @@ public: * @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); @@ -244,9 +251,6 @@ public: */ virtual int fileMove(surf_file_t fd, const char* fullpath); -protected: - void onDie() override; - public: xbt_dynar_t p_storage; RoutingEdge *p_netElm; diff --git a/src/surf/host_ptask_L07.cpp b/src/surf/host_ptask_L07.cpp index 7d4b2122ba..af348850d4 100644 --- a/src/surf/host_ptask_L07.cpp +++ b/src/surf/host_ptask_L07.cpp @@ -407,7 +407,6 @@ HostL07::HostL07(HostModel *model, const char* name, xbt_dict_t props, RoutingEd HostL07::~HostL07() { - this->die(); } CpuL07::CpuL07(CpuL07Model *model, const char* name, @@ -429,7 +428,6 @@ CpuL07::CpuL07(CpuL07Model *model, const char* name, CpuL07::~CpuL07() { - this->die(); } LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props, diff --git a/src/surf/surf_interface.cpp b/src/surf/surf_interface.cpp index 3935c9606c..695653f5d7 100644 --- a/src/surf/surf_interface.cpp +++ b/src/surf/surf_interface.cpp @@ -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"); - simgrid::surf::Host::init(); + simgrid::surf::Host::classInit(); 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) {} -/** 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() { - this->die(); xbt_free((void*)p_name); } diff --git a/src/surf/surf_interface.hpp b/src/surf/surf_interface.hpp index 695fe11cc7..8599ee8ae4 100644 --- a/src/surf/surf_interface.hpp +++ b/src/surf/surf_interface.hpp @@ -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); -protected: - void die(); - virtual void onDie(); - private: - bool alive_ = true; const char *p_name; Model *p_model; bool m_running;