Fix issues of dependencies between extensions in destruction.
#include <xbt/dict.h>
#include <xbt/swag.h>
#include <xbt/string.hpp>
+#include <xbt/signal.hpp>
#include <xbt/Extendable.hpp>
#include <simgrid/datatypes.h>
static Host* by_name_or_null(const char* name);
static Host* by_name_or_create(const char* name);
+
+ /*** Called on each newly created object */
+ static simgrid::xbt::signal<void(Host&)> onCreation;
+ /*** Called just before destructing an object */
+ static simgrid::xbt::signal<void(Host&)> onDestruction;
+ /*** Called when the machine is turned on or off */
+ static simgrid::xbt::signal<void(Host&)> onStateChange;
};
}
SIMIX_function_register_process_cleanup(MSG_process_cleanup_from_SIMIX);
sg_platf_postparse_add_cb(MSG_post_create_environment);
- surf_on_host_created(MSG_host_create_);
+ simgrid::Host::onCreation.connect([](simgrid::Host& host) {
+ MSG_host_create_(&host);
+ });
}
if(MC_is_active()){
namespace simgrid {
+simgrid::xbt::signal<void(Host&)> Host::onCreation;
+simgrid::xbt::signal<void(Host&)> Host::onDestruction;
+simgrid::xbt::signal<void(Host&)> Host::onStateChange;
+
Host::Host(std::string const& id)
: name_(id)
{
/* register a function to be called by SURF after the environment creation */
sg_platf_init();
sg_platf_postparse_add_cb(SIMIX_post_create_environment);
- surf_on_host_created(SIMIX_host_create);
+ simgrid::Host::onCreation.connect([](simgrid::Host& host) {
+ SIMIX_host_create(&host);
+ });
surf_on_storage_created(SIMIX_storage_create_);
}
#include "src/surf/surf_interface.hpp"
#include "src/surf/host_interface.hpp"
-void surf_on_host_created(void (*callback)(sg_host_t))
-{
- simgrid::surf::Host::onCreation.connect([callback](simgrid::surf::Host* host) {
- callback(host->p_host);
- });
-}
-
void surf_on_storage_created(void (*callback)(sg_storage_t))
{
simgrid::surf::storageCreatedCallbacks.connect([callback](simgrid::surf::Storage* storage) {
/************
* Resource *
************/
-simgrid::xbt::signal<void(simgrid::surf::Host*)> Host::onCreation;
-simgrid::xbt::signal<void(simgrid::surf::Host*)> Host::onDestruction;
-simgrid::xbt::signal<void(simgrid::surf::Host*)> Host::onStateChange;
+
void Host::classInit()
{
if (!EXTENSION_ID.valid()) {
- EXTENSION_ID = simgrid::Host::extension_create<simgrid::surf::Host>([](void *h) {
- static_cast<simgrid::surf::Host*>(h)->destroy();
- });
+ EXTENSION_ID = simgrid::Host::extension_create<simgrid::surf::Host>();
}
}
/** @brief use destroy() instead of this destructor */
Host::~Host()
{
- xbt_assert(currentlyDestroying_, "Don't delete Hosts directly. Call destroy() instead.");
- delete p_cpu;
- // FIXME: VM plays strange games, leading to segfaults if I do the expected thing of next line
- // delete p_netElm;
- // delete p_storage;
-}
-/** @brief Fire the require callbacks and destroy the object
- *
- * Don't delete directly an Host, call h->destroy() instead.
- */
-void Host::destroy()
-{
- if (!currentlyDestroying_) {
- currentlyDestroying_ = true;
- onDestruction(this);
- delete this;
- }
}
void Host::attach(simgrid::Host* host)
xbt_die("Already attached to host %s", host->getName().c_str());
host->extension_set(this);
p_host = host;
- onCreation(this);
}
bool Host::isOn() {
void Host::turnOn(){
if (isOff()) {
p_cpu->turnOn();
- onStateChange(this);
+ simgrid::Host::onStateChange(*this->p_host);
}
}
void Host::turnOff(){
if (isOn()) {
p_cpu->turnOff();
- onStateChange(this);
+ simgrid::Host::onStateChange(*this->p_host);
}
}
/*********
* Tools *
*********/
+
XBT_PUBLIC_DATA(simgrid::surf::HostModel*) surf_host_model;
XBT_PUBLIC(void) host_add_traces();
public:
static simgrid::xbt::Extension<simgrid::Host, Host> EXTENSION_ID;
- /* callbacks */
- static simgrid::xbt::signal<void(Host*)> onCreation; /** Called on each newly created object */
- static simgrid::xbt::signal<void(Host*)> onDestruction; /** Called just before destructing an object */
- static simgrid::xbt::signal<void(Host*)> onStateChange; /** Called when the machine is turned on or off */
-
public:
static void classInit(); // must be called before the first use of that class
/**
/* Host destruction logic */
/**************************/
-protected:
~Host();
-public:
- void destroy(); // Must be called instead of the destructor
-private:
- bool currentlyDestroying_ = false;
-
public:
HostModel *getModel()
routing_model_create(NULL);
- simgrid::surf::Host::onCreation.connect([](simgrid::surf::Host*) {
+ simgrid::Host::onCreation.connect([](simgrid::Host&) {
host_number_int++;
});
sg_platf_link_add_cb(netcste_parse_nolink);
XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
-static void IB_create_host_callback(simgrid::surf::Host* host){
+static void IB_create_host_callback(simgrid::Host& host){
using namespace simgrid::surf;
static int id=0;
id++;
xbt_dict_set(((NetworkIBModel*)surf_network_model)->active_nodes,
- host->getName(), act, NULL);
-
+ host.getName().c_str(), act, NULL);
+
}
static void IB_action_state_changed_callback(
xbt_dynar_push(all_existing_models, &surf_network_model);
networkActionStateChangedCallbacks.connect(IB_action_state_changed_callback);
networkCommunicateCallbacks.connect(IB_action_init_callback);
- simgrid::surf::Host::onCreation.connect(IB_create_host_callback);
+ simgrid::Host::onCreation.connect(IB_create_host_callback);
xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
}
}
/* **************************** events callback *************************** */
-static void onCreation(simgrid::surf::Host *host) {
- if (dynamic_cast<simgrid::surf::VirtualMachine*>(host)) // Ignore virtual machines
+static void onCreation(simgrid::Host& host) {
+ simgrid::surf::Host* surf_host = host.extension<simgrid::surf::Host>();
+ if (dynamic_cast<simgrid::surf::VirtualMachine*>(surf_host)) // Ignore virtual machines
return;
- host->p_host->extension_set(new HostEnergy(host->p_host));
+ host.extension_set(new HostEnergy(&host));
}
static void onActionStateChange(simgrid::surf::CpuAction *action,
host_energy->update();
}
-static void onHostStateChange(simgrid::surf::Host *host) {
- if (dynamic_cast<simgrid::surf::VirtualMachine*>(host)) // Ignore virtual machines
+static void onHostStateChange(simgrid::Host &host) {
+ simgrid::surf::Host* surf_host = host.extension<simgrid::surf::Host>();
+ if (dynamic_cast<simgrid::surf::VirtualMachine*>(surf_host)) // Ignore virtual machines
return;
- HostEnergy *host_energy = host->p_host->extension<HostEnergy>();
+ HostEnergy *host_energy = host.extension<HostEnergy>();
if(host_energy->last_updated < surf_get_clock())
host_energy->update();
}
-static void onHostDestruction(simgrid::surf::Host *host) {
+static void onHostDestruction(simgrid::Host& host) {
// Ignore virtual machines
- if (dynamic_cast<simgrid::surf::VirtualMachine*>(host))
+ simgrid::surf::Host* surf_host = host.extension<simgrid::surf::Host>();
+ if (dynamic_cast<simgrid::surf::VirtualMachine*>(surf_host))
return;
- HostEnergy *host_energy = host->p_host->extension<HostEnergy>();
+ HostEnergy *host_energy = host.extension<HostEnergy>();
host_energy->update();
- XBT_INFO("Total energy of host %s: %f Joules", host->getName(), host_energy->getConsumedEnergy());
+ XBT_INFO("Total energy of host %s: %f Joules",
+ host.getName().c_str(), host_energy->getConsumedEnergy());
}
/* **************************** Public interface *************************** */
HostEnergy::EXTENSION_ID = simgrid::Host::extension_create<HostEnergy>();
- simgrid::surf::Host::onCreation.connect(&onCreation);
+ simgrid::Host::onCreation.connect(&onCreation);
+ simgrid::Host::onStateChange.connect(&onHostStateChange);
+ simgrid::Host::onDestruction.connect(&onHostDestruction);
simgrid::surf::CpuAction::onStateChange.connect(&onActionStateChange);
- simgrid::surf::Host::onStateChange.connect(&onHostStateChange);
- simgrid::surf::Host::onDestruction.connect(&onHostDestruction);
}
/** @brief Returns the total energy consumed by the host so far (in Joules)
host->initiallyOn,
host->state_trace);
surf_host_model->createHost(host->id, net, cpu, host->properties)->attach(h);
+ simgrid::Host::onCreation(*h);
+
if (TRACE_is_enabled() && TRACE_needs_platform())
sg_instr_new_host(host);
}
{
XBT_DEBUG("Create all Libs");
host_list = xbt_dict_new_homogeneous([](void*p) {
- delete static_cast<simgrid::Host*>(p);
+ simgrid::Host* host = static_cast<simgrid::Host*>(p);
+ simgrid::Host::onDestruction(*host);
+ delete host;
});
as_router_lib = xbt_lib_new();
storage_lib = xbt_lib_new();