Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move ContextFactory::self() into Context::self() and tidy it up
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 5 Jan 2019 14:33:22 +0000 (15:33 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 5 Jan 2019 14:33:22 +0000 (15:33 +0100)
src/bindings/java/JavaContext.cpp
src/include/xbt/parmap.hpp
src/kernel/context/Context.cpp
src/kernel/context/Context.hpp
src/kernel/context/ContextBoost.cpp
src/kernel/context/ContextRaw.cpp
src/kernel/context/ContextThread.cpp
src/kernel/context/ContextUnix.cpp

index 937f707..f42bcde 100644 (file)
@@ -53,7 +53,7 @@ JavaContext::JavaContext(std::function<void()> code, void_pfn_smxprocess_t clean
 
 void JavaContext::start_hook()
 {
-  SIMIX_context_set_current(this); // We need to attach it also for maestro, in contrary to our ancestor
+  Context::set_current(this); // We need to attach it also for maestro, in contrary to our ancestor
 
   //Attach the thread to the JVM
   JNIEnv *env;
index c5c79a6..c589364 100644 (file)
@@ -281,7 +281,7 @@ template <typename T> void* Parmap<T>::worker_main(void* arg)
   Parmap<T>& parmap     = data->parmap;
   unsigned round        = 0;
   smx_context_t context = SIMIX_context_new(std::function<void()>(), nullptr, nullptr);
-  SIMIX_context_set_current(context);
+  kernel::context::Context::set_current(context);
 
   XBT_CDEBUG(xbt_parmap, "New worker thread created");
 
index 0aead0c..8a72534 100644 (file)
@@ -29,16 +29,20 @@ smx_context_t SIMIX_context_new(
 namespace simgrid {
 namespace kernel {
 namespace context {
-static thread_local smx_context_t smx_current_context;
 
 ContextFactoryInitializer factory_initializer = nullptr;
 
 ContextFactory::~ContextFactory() = default;
 
-Context* ContextFactory::self()
+static thread_local smx_context_t smx_current_context;
+Context* Context::self()
 {
   return smx_current_context;
 }
+void Context::set_current(Context* self)
+{
+  smx_current_context = self;
+}
 
 void Context::declare_context(std::size_t size)
 {
@@ -70,7 +74,7 @@ Context::Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
   if (has_code())
     this->cleanup_func_ = cleanup_func;
   else
-    SIMIX_context_set_current(this);
+    set_current(this);
 }
 
 Context::~Context() = default;
@@ -100,16 +104,7 @@ void SIMIX_context_runall()
 smx_context_t SIMIX_context_self()
 {
   if (simix_global && simix_global->context_factory)
-    return simix_global->context_factory->self();
+    return simgrid::kernel::context::Context::self();
   else
     return nullptr;
 }
-
-/**
- * @brief Sets the current context of this thread.
- * @param context the context to set
- */
-void SIMIX_context_set_current(smx_context_t context)
-{
-  simgrid::kernel::context::smx_current_context = context;
-}
index 0f4a2b0..5219608 100644 (file)
@@ -34,8 +34,6 @@ public:
   virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
 
   virtual void run_all() = 0;
-  /** @brief Returns the current context of this thread. */
-  Context* self();
   std::string const& name() const { return name_; }
 
 protected:
@@ -57,34 +55,41 @@ private:
   void declare_context(std::size_t size);
 
 public:
-  class StopRequest {
-    /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
-     *
-     * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
-     * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
-     */
-  public:
-    StopRequest() = default;
-    explicit StopRequest(std::string msg) : msg_(msg) {}
-
-  private:
-    std::string msg_;
-  };
   bool iwannadie = false;
 
   Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
   Context(const Context&) = delete;
   Context& operator=(const Context&) = delete;
+  virtual ~Context();
 
   void operator()() { code_(); }
   bool has_code() const { return static_cast<bool>(code_); }
   smx_actor_t process() { return this->actor_; }
   void set_cleanup(void_pfn_smxprocess_t cleanup) { cleanup_func_ = cleanup; }
 
-  // Virtual methods
-  virtual ~Context();
+  // Scheduling methods
   virtual void stop();
   virtual void suspend() = 0;
+
+  // Retrieving the self() context
+  /** @brief Retrives the current context of this thread */
+  static Context* self();
+  /** @brief Sets the current context of this thread */
+  static void set_current(Context* self);
+
+  class StopRequest {
+    /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
+     *
+     * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
+     * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
+     */
+  public:
+    StopRequest() = default;
+    explicit StopRequest(std::string msg) : msg_(msg) {}
+
+  private:
+    std::string msg_;
+  };
 };
 
 class XBT_PUBLIC AttachContext : public Context {
@@ -146,8 +151,6 @@ XBT_PUBLIC smx_context_t SIMIX_context_self(); // public because it's used in si
 XBT_PRIVATE void *SIMIX_context_stack_new();
 XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
 
-XBT_PUBLIC void SIMIX_context_set_current(smx_context_t context);
-
 XBT_PUBLIC int SIMIX_process_get_maxpid();
 
 XBT_PRIVATE void SIMIX_post_create_environment();
index e1fff93..c745dff 100644 (file)
@@ -157,13 +157,13 @@ void SerialBoostContext::suspend()
     XBT_DEBUG("No more process to run");
     next_context = static_cast<SerialBoostContext*>(BoostContext::get_maestro());
   }
-  SIMIX_context_set_current(next_context);
+  Context::set_current(next_context);
   BoostContext::swap(this, next_context);
 }
 
 void SerialBoostContext::resume()
 {
-  SIMIX_context_set_current(this);
+  Context::set_current(this);
   BoostContext::swap(BoostContext::get_maestro(), this);
 }
 
@@ -223,7 +223,7 @@ void ParallelBoostContext::suspend()
     next_context = workers_context_[worker_id_];
   }
 
-  SIMIX_context_set_current(next_context);
+  Context::set_current(next_context);
   BoostContext::swap(this, next_context);
 }
 
@@ -234,7 +234,7 @@ void ParallelBoostContext::resume()
   ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(SIMIX_context_self());
   workers_context_[worker_id_]         = worker_context;
 
-  SIMIX_context_set_current(this);
+  Context::set_current(this);
   BoostContext::swap(worker_context, this);
 }
 
index 3029274..d98470b 100644 (file)
@@ -297,13 +297,13 @@ void SerialRawContext::suspend()
     XBT_DEBUG("No more process to run");
     next_context = static_cast<SerialRawContext*>(RawContext::get_maestro());
   }
-  SIMIX_context_set_current(next_context);
+  Context::set_current(next_context);
   RawContext::swap(this, next_context);
 }
 
 void SerialRawContext::resume()
 {
-  SIMIX_context_set_current(this);
+  Context::set_current(this);
   RawContext::swap(RawContext::get_maestro(), this);
 }
 
@@ -367,7 +367,7 @@ void ParallelRawContext::suspend()
     XBT_DEBUG("Restoring worker stack %zu (working threads = %zu)", worker_id_, threads_working_.load());
   }
 
-  SIMIX_context_set_current(next_context);
+  Context::set_current(next_context);
   RawContext::swap(this, next_context);
 }
 
@@ -377,7 +377,7 @@ void ParallelRawContext::resume()
   ParallelRawContext* worker_context = static_cast<ParallelRawContext*>(SIMIX_context_self());
   workers_context_[worker_id_]       = worker_context;
   XBT_DEBUG("Saving worker stack %zu", worker_id_);
-  SIMIX_context_set_current(this);
+  Context::set_current(this);
   RawContext::swap(worker_context, this);
 }
 
index 01c084e..33df5e7 100644 (file)
@@ -76,7 +76,7 @@ ThreadContext::ThreadContext(std::function<void()> code, void_pfn_smxprocess_t c
 
   /* Otherwise, we attach to the current thread */
   else {
-    SIMIX_context_set_current(this);
+    Context::set_current(this);
   }
 }
 
@@ -89,7 +89,7 @@ ThreadContext::~ThreadContext()
 void *ThreadContext::wrapper(void *param)
 {
   ThreadContext* context = static_cast<ThreadContext*>(param);
-  SIMIX_context_set_current(context);
+  Context::set_current(context);
 
 #ifndef WIN32
   /* Install alternate signal stack, for SIGSEGV handler. */
@@ -176,7 +176,7 @@ void ThreadContext::attach_stop()
   ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
   maestro->end_.acquire();
 
-  SIMIX_context_set_current(nullptr);
+  Context::set_current(nullptr);
 }
 
 // SerialThreadContext
index e31ba6a..5dc49b5 100644 (file)
@@ -170,13 +170,13 @@ void SerialUContext::suspend()
     XBT_DEBUG("No more process to run");
     next_context = static_cast<SerialUContext*>(UContext::get_maestro());
   }
-  SIMIX_context_set_current(next_context);
+  Context::set_current(next_context);
   UContext::swap(this, next_context);
 }
 
 void SerialUContext::resume()
 {
-  SIMIX_context_set_current(this);
+  Context::set_current(this);
   UContext::swap(UContext::get_maestro(), this);
 }
 
@@ -255,7 +255,7 @@ void ParallelUContext::suspend()
     // When given that soul, the body will wait for the next scheduling round
   }
 
-  SIMIX_context_set_current(next_context);
+  Context::set_current(next_context);
   // Get the next soul to run, either simulated or initial minion's one:
   UContext::swap(this, next_context);
 }
@@ -270,7 +270,7 @@ void ParallelUContext::resume()
   // Write down that this soul is hosted in that body (for now)
   workers_context_[worker_id_] = worker_context;
   // Write in simix that I switched my soul
-  SIMIX_context_set_current(this);
+  Context::set_current(this);
   // Actually do that using the relevant library call:
   UContext::swap(worker_context, this);
   // No body runs that soul anymore at this point.  Instead the current body took the soul of simulated process The