Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Noreturn annotations in contexts.
[simgrid.git] / src / kernel / context / ContextSwapped.cpp
index fbecf2b..292cb59 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2009-2020. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -38,22 +38,16 @@ namespace context {
 /* thread-specific storage for the worker's context */
 thread_local SwappedContext* SwappedContext::worker_context_ = nullptr;
 
-SwappedContextFactory::SwappedContextFactory() : ContextFactory(), parallel_(SIMIX_context_is_parallel())
-{
-  parmap_ = nullptr; // will be created lazily with the right parameters if needed (ie, in parallel)
-}
-
 SwappedContext::SwappedContext(std::function<void()>&& code, smx_actor_t actor, SwappedContextFactory* factory)
     : Context(std::move(code), actor), factory_(factory)
 {
   // Save maestro (=context created first) in preparation for run_all
-  if (not factory->parallel_ && factory->maestro_context_ == nullptr)
+  if (not SIMIX_context_is_parallel() && factory->maestro_context_ == nullptr)
     factory->maestro_context_ = this;
 
   if (has_code()) {
     xbt_assert((smx_context_stack_size & 0xf) == 0, "smx_context_stack_size should be multiple of 16");
     if (smx_context_guard_size > 0 && not MC_is_active()) {
-
 #if !defined(PTH_STACKGROWTH) || (PTH_STACKGROWTH != -1)
       xbt_die(
           "Stack overflow protection is known to be broken on your system: you stacks grow upwards (or detection is "
@@ -155,7 +149,7 @@ void SwappedContextFactory::run_all()
    * stuff It is much easier to understand what happens if you see the working threads as bodies that swap their soul
    * for the ones of the simulated processes that must run.
    */
-  if (parallel_) {
+  if (SIMIX_context_is_parallel()) {
     // We lazily create the parmap so that all options are actually processed when doing so.
     if (parmap_ == nullptr)
       parmap_.reset(
@@ -168,7 +162,7 @@ void SwappedContextFactory::run_all()
     //     It only yields back to worker_context when the work array is exhausted.
     //   - So, resume() is only launched from the parmap for the first job of each minion.
     parmap_->apply(
-        [](smx_actor_t process) {
+        [](const actor::ActorImpl* process) {
           SwappedContext* context = static_cast<SwappedContext*>(process->context_.get());
           context->resume();
         },
@@ -178,7 +172,7 @@ void SwappedContextFactory::run_all()
       return;
 
     /* maestro is already saved in the first slot of workers_context_ */
-    smx_actor_t first_actor = simix_global->actors_to_run.front();
+    const actor::ActorImpl* 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_.get())->resume();
@@ -193,35 +187,32 @@ void SwappedContextFactory::run_all()
  */
 void SwappedContext::resume()
 {
-  if (factory_->parallel_) {
+  SwappedContext* old = static_cast<SwappedContext*>(self());
+  if (SIMIX_context_is_parallel()) {
     // Save my current soul (either maestro, or one of the minions) in a thread-specific area
-    worker_context_ = static_cast<SwappedContext*>(self());
-    // Switch my soul and the actor's one
-    Context::set_current(this);
-    worker_context_->swap_into(this);
-    // No body runs that soul anymore at this point, but it is stored in a safe place.
-    // When the executed actor will do a blocking action, ActorImpl::yield() will call suspend(), below.
-  } else { // sequential execution
-    SwappedContext* old = static_cast<SwappedContext*>(self());
-    Context::set_current(this);
-    old->swap_into(this);
+    worker_context_ = old;
   }
+  // Switch my soul and the actor's one
+  Context::set_current(this);
+  old->swap_into(this);
+  // No body runs that soul anymore at this point, but it is stored in a safe place.
+  // When the executed actor will do a blocking action, ActorImpl::yield() will call suspend(), below.
 }
 
 /** The actor wants to yield back to maestro, because it is blocked in a simcall (i.e., in ActorImpl::yield())
  *
  * Actually, it does not really yield back to maestro, but directly into the next executable actor.
  *
- * This makes the parmap::apply awkward (see ParallelUContext::run_all()) because it only apply regularly
+ * This makes the parmap::apply awkward (see SwappedContextFactory::run_all()) because it only apply regularly
  * on the few first elements of the array, but it saves a lot of context switches back to maestro,
  * and directly forth to the next executable actor.
  */
 void SwappedContext::suspend()
 {
-  if (factory_->parallel_) {
+  SwappedContext* next_context;
+  if (SIMIX_context_is_parallel()) {
     // Get some more work to directly swap into the next executable actor instead of yielding back to the parmap
     boost::optional<smx_actor_t> next_work = factory_->parmap_->next();
-    SwappedContext* next_context;
     if (next_work) {
       // There is a next soul to embody (ie, another executable actor)
       XBT_DEBUG("Run next process");
@@ -233,14 +224,8 @@ void SwappedContext::suspend()
       next_context = worker_context_;
       // When given that soul, the body will wait for the next scheduling round
     }
-
-    // Get the next soul to run, either from another actor or the initial minion's one
-    Context::set_current(next_context);
-    this->swap_into(next_context);
-
   } else { // sequential execution
     /* determine the next context */
-    SwappedContext* next_context;
     unsigned long int i = factory_->process_index_;
     factory_->process_index_++;
 
@@ -253,9 +238,9 @@ void SwappedContext::suspend()
       XBT_DEBUG("No more actors to run");
       next_context = factory_->maestro_context_;
     }
-    Context::set_current(next_context);
-    this->swap_into(next_context);
   }
+  Context::set_current(next_context);
+  this->swap_into(next_context);
 }
 
 } // namespace context