From 571ed94e23c9aefbd5f81f1a800eb5f1dddd8ae9 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Tue, 1 Mar 2016 16:52:57 +0100 Subject: [PATCH 1/1] create a singleton for s4u::Engine --- include/simgrid/s4u/engine.hpp | 4 ++++ src/s4u/s4u_engine.cpp | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/simgrid/s4u/engine.hpp b/include/simgrid/s4u/engine.hpp index b81a57f591..2c9fa3cd13 100644 --- a/include/simgrid/s4u/engine.hpp +++ b/include/simgrid/s4u/engine.hpp @@ -47,6 +47,10 @@ public: /** @brief Retrieve the simulation time */ static double getClock(); + /** @brief Retrieve the engine singleton */ + static s4u::Engine *instance(); +private: + static s4u::Engine *instance_; }; }} // namespace simgrid::sgo diff --git a/src/s4u/s4u_engine.cpp b/src/s4u/s4u_engine.cpp index ca3415a2fc..ce3cc47f76 100644 --- a/src/s4u/s4u_engine.cpp +++ b/src/s4u/s4u_engine.cpp @@ -12,14 +12,26 @@ XBT_LOG_NEW_CATEGORY(s4u,"Log channels of the S4U (Simgrid for you) interface"); using namespace simgrid; -double s4u::Engine::getClock() { - return SIMIX_get_clock(); -} +s4u::Engine *s4u::Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */ + s4u::Engine::Engine(int *argc, char **argv) { + xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine"); + s4u::Engine::instance_ = this; + SIMIX_global_init(argc, argv); } +s4u::Engine *s4u::Engine::instance() { + if (s4u::Engine::instance_ == nullptr) + new Engine(0,nullptr); + return s4u::Engine::instance_; +} + +double s4u::Engine::getClock() { + return SIMIX_get_clock(); +} + void s4u::Engine::loadPlatform(const char *platf) { SIMIX_create_environment(platf); } -- 2.20.1