Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make ActorImpl::context_ a std::unique_ptr.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 8 Mar 2019 22:06:26 +0000 (23:06 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Sun, 10 Mar 2019 13:24:47 +0000 (14:24 +0100)
src/bindings/java/jmsg_process.cpp
src/kernel/context/ContextSwapped.cpp
src/kernel/context/ContextThread.cpp
src/msg/msg_process.cpp
src/simix/ActorImpl.cpp
src/simix/ActorImpl.hpp
src/simix/smx_global.cpp

index 0cfc805..26906a4 100644 (file)
@@ -26,7 +26,7 @@ jfieldID jprocess_field_Process_ppid;
 jobject jprocess_from_native(msg_process_t process)
 {
   simgrid::kernel::context::JavaContext* context =
-      (simgrid::kernel::context::JavaContext*)process->get_impl()->context_;
+      static_cast<simgrid::kernel::context::JavaContext*>(process->get_impl()->context_.get());
   return context->jprocess_;
 }
 
index 0b59cd8..ccc0420 100644 (file)
@@ -171,7 +171,7 @@ void SwappedContextFactory::run_all()
     //   - So, resume() is only launched from the parmap for the first job of each minion.
     parmap_->apply(
         [](smx_actor_t process) {
-          SwappedContext* context = static_cast<SwappedContext*>(process->context_);
+          SwappedContext* context = static_cast<SwappedContext*>(process->context_.get());
           context->resume();
         },
         simix_global->actors_to_run);
@@ -183,7 +183,7 @@ void SwappedContextFactory::run_all()
     smx_actor_t first_actor = simix_global->actors_to_run.front();
     process_index_          = 1;
     /* execute the first actor; it will chain to the others when using suspend() */
-    static_cast<SwappedContext*>(first_actor->context_)->resume();
+    static_cast<SwappedContext*>(first_actor->context_.get())->resume();
   }
 }
 
@@ -230,7 +230,7 @@ void SwappedContext::suspend()
     if (next_work) {
       // There is a next soul to embody (ie, another executable actor)
       XBT_DEBUG("Run next process");
-      next_context = static_cast<SwappedContext*>(next_work.get()->context_);
+      next_context = static_cast<SwappedContext*>(next_work.get()->context_.get());
     } else {
       // All actors were run, go back to the parmap context
       XBT_DEBUG("No more actors to run");
@@ -252,7 +252,7 @@ void SwappedContext::suspend()
     if (i < simix_global->actors_to_run.size()) {
       /* Actually swap into the next actor directly without transiting to maestro */
       XBT_DEBUG("Run next actor");
-      next_context = static_cast<SwappedContext*>(simix_global->actors_to_run[i]->context_);
+      next_context = static_cast<SwappedContext*>(simix_global->actors_to_run[i]->context_.get());
     } else {
       /* all processes were run, actually return to maestro */
       XBT_DEBUG("No more actors to run");
index 7914f29..3e2b066 100644 (file)
@@ -158,7 +158,7 @@ void ThreadContext::suspend()
 void ThreadContext::attach_start()
 {
   // We're breaking the layers here by depending on the upper layer:
-  ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
+  ThreadContext* maestro = static_cast<ThreadContext*>(simix_global->maestro_process->context_.get());
   maestro->begin_.release();
   xbt_assert(not this->is_maestro());
   this->start();
@@ -169,7 +169,7 @@ void ThreadContext::attach_stop()
   xbt_assert(not this->is_maestro());
   this->yield();
 
-  ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
+  ThreadContext* maestro = static_cast<ThreadContext*>(simix_global->maestro_process->context_.get());
   maestro->end_.acquire();
 
   Context::set_current(nullptr);
@@ -181,7 +181,7 @@ void SerialThreadContext::run_all()
 {
   for (smx_actor_t const& actor : simix_global->actors_to_run) {
     XBT_DEBUG("Handling %p", actor);
-    ThreadContext* context = static_cast<ThreadContext*>(actor->context_);
+    ThreadContext* context = static_cast<ThreadContext*>(actor->context_.get());
     context->release();
     context->wait();
   }
@@ -205,9 +205,9 @@ void ParallelThreadContext::finalize()
 void ParallelThreadContext::run_all()
 {
   for (smx_actor_t const& actor : simix_global->actors_to_run)
-    static_cast<ThreadContext*>(actor->context_)->release();
+    static_cast<ThreadContext*>(actor->context_.get())->release();
   for (smx_actor_t const& actor : simix_global->actors_to_run)
-    static_cast<ThreadContext*>(actor->context_)->wait();
+    static_cast<ThreadContext*>(actor->context_.get())->wait();
 }
 
 void ParallelThreadContext::start_hook()
index e4acc92..9cdb605 100644 (file)
@@ -181,7 +181,7 @@ msg_process_t MSG_process_self()
 }
 
 smx_context_t MSG_process_get_smx_ctx(msg_process_t process) { // deprecated -- smx_context_t should die afterward
-  return process->get_impl()->context_;
+  return process->get_impl()->context_.get();
 }
 /** @brief Add a function to the list of "on_exit" functions for the current process.
  *  The on_exit functions are the functions executed when your process is killed.
index 17c34d2..984e0ec 100644 (file)
@@ -58,10 +58,8 @@ ActorImpl::ActorImpl(simgrid::xbt::string name, s4u::Host* host) : host_(host),
   simcall.issuer = this;
 }
 
-ActorImpl::~ActorImpl()
-{
-  delete this->context_;
-}
+ActorImpl::~ActorImpl() = default;
+
 /* Become an actor in the simulation
  *
  * Currently this can only be called by the main thread (once) and only work with some thread factories
@@ -90,7 +88,7 @@ ActorImplPtr ActorImpl::attach(std::string name, void* data, s4u::Host* host,
 
   XBT_VERB("Create context %s", actor->get_cname());
   xbt_assert(simix_global != nullptr, "simix is not initialized, please call MSG_init first");
-  actor->context_ = simix_global->context_factory->attach(actor);
+  actor->context_.reset(simix_global->context_factory->attach(actor));
 
   /* Add properties */
   if (properties != nullptr)
@@ -106,7 +104,7 @@ ActorImplPtr ActorImpl::attach(std::string name, void* data, s4u::Host* host,
   simix_global->actors_to_run.push_back(actor);
   intrusive_ptr_add_ref(actor);
 
-  auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(actor->context_);
+  auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(actor->context_.get());
   xbt_assert(nullptr != context, "Not a suitable context");
   context->attach_start();
 
@@ -469,7 +467,7 @@ ActorImpl* ActorImpl::start(const simix::ActorCode& code)
 
   this->code = code;
   XBT_VERB("Create context %s", get_cname());
-  context_ = simix_global->context_factory->create_context(simix::ActorCode(code), this);
+  context_.reset(simix_global->context_factory->create_context(simix::ActorCode(code), this));
 
   XBT_DEBUG("Start context '%s'", get_cname());
 
@@ -514,9 +512,9 @@ void create_maestro(const std::function<void()>& code)
   ActorImpl* maestro = new ActorImpl(xbt::string(""), /*host*/ nullptr);
 
   if (not code) {
-    maestro->context_ = simix_global->context_factory->create_context(simix::ActorCode(), maestro);
+    maestro->context_.reset(simix_global->context_factory->create_context(simix::ActorCode(), maestro));
   } else {
-    maestro->context_ = simix_global->context_factory->create_maestro(simix::ActorCode(code), maestro);
+    maestro->context_.reset(simix_global->context_factory->create_maestro(simix::ActorCode(code), maestro));
   }
 
   maestro->simcall.issuer       = maestro;
index 18ee938..785e25b 100644 (file)
@@ -13,6 +13,7 @@
 #include <functional>
 #include <list>
 #include <map>
+#include <memory>
 
 struct s_smx_process_exit_fun_t {
   std::function<void(bool, void*)> fun;
@@ -59,7 +60,7 @@ public:
   bool has_to_auto_restart() { return auto_restart_; }
   void set_auto_restart(bool autorestart) { auto_restart_ = autorestart; }
 
-  context::Context* context_ = nullptr; /* the context (uctx/raw/thread) that executes the user function */
+  std::unique_ptr<context::Context> context_; /* the context (uctx/raw/thread) that executes the user function */
 
   std::exception_ptr exception_;
   bool finished_  = false;
index 9fe7ef1..2485979 100644 (file)
@@ -288,8 +288,6 @@ void SIMIX_clean()
 #endif
 
   /* Let's free maestro now */
-  delete simix_global->maestro_process->context_;
-  simix_global->maestro_process->context_ = nullptr;
   delete simix_global->maestro_process;
   simix_global->maestro_process = nullptr;