Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill src/include
[simgrid.git] / src / kernel / context / ContextSwapped.cpp
index a88a5bc..13e7e9a 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009-2021. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2009-2023. 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. */
@@ -8,25 +8,15 @@
 #include "src/internal_config.h"
 #include "src/kernel/EngineImpl.hpp"
 #include "src/kernel/actor/ActorImpl.hpp"
-#include "xbt/parmap.hpp"
+#include "src/sthread/sthread.h"
+#include "src/xbt/parmap.hpp"
 
 #include "src/kernel/context/ContextSwapped.hpp"
 
 #include <boost/core/demangle.hpp>
 #include <memory>
-#include <typeinfo>
-
-#ifdef _WIN32
-#include <malloc.h>
-#include <windows.h>
-#else
 #include <sys/mman.h>
-#endif
-
-#ifdef __MINGW32__
-#define _aligned_malloc __mingw_aligned_malloc
-#define _aligned_free __mingw_aligned_free
-#endif /*MINGW*/
+#include <typeinfo>
 
 #if HAVE_VALGRIND_H
 #include <valgrind/valgrind.h>
@@ -48,11 +38,15 @@ void smx_ctx_wrapper(simgrid::kernel::context::SwappedContext* context)
   __sanitizer_finish_switch_fiber(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
 #endif
   try {
+    sthread_enable();
     (*context)();
-    context->Context::stop();
+    sthread_disable();
+    context->stop();
   } catch (simgrid::ForcefulKillException const&) {
+    sthread_disable();
     XBT_DEBUG("Caught a ForcefulKillException");
   } catch (simgrid::Exception const& e) {
+    sthread_disable();
     XBT_INFO("Actor killed by an uncaught exception %s", boost::core::demangle(typeid(e).name()).c_str());
     throw;
   }
@@ -63,15 +57,13 @@ void smx_ctx_wrapper(simgrid::kernel::context::SwappedContext* context)
   THROW_IMPOSSIBLE;
 }
 
-namespace simgrid {
-namespace kernel {
-namespace context {
+namespace simgrid::kernel::context {
 
 /* thread-specific storage for the worker's context */
 thread_local SwappedContext* SwappedContext::worker_context_ = nullptr;
 
-SwappedContext::SwappedContext(std::function<void()>&& code, smx_actor_t actor, SwappedContextFactory* factory)
-    : Context(std::move(code), actor), factory_(*factory)
+SwappedContext::SwappedContext(std::function<void()>&& code, actor::ActorImpl* actor, SwappedContextFactory* factory)
+    : Context(std::move(code), actor, not code /* maestro if no code */), factory_(*factory)
 {
   // Save maestro (=first created context) in preparation for run_all
   if (not is_parallel() && factory_.maestro_context_ == nullptr)
@@ -96,15 +88,12 @@ SwappedContext::SwappedContext(std::function<void()>&& code, smx_actor_t actor,
       auto* alloc          = static_cast<unsigned char*>(xbt_malloc0(size + xbt_pagesize));
       stack_               = alloc - (reinterpret_cast<uintptr_t>(alloc) & (xbt_pagesize - 1)) + xbt_pagesize;
       reinterpret_cast<unsigned char**>(stack_)[-1] = alloc;
-#elif !defined(_WIN32)
+#else
       void* alloc;
       xbt_assert(posix_memalign(&alloc, xbt_pagesize, size) == 0, "Failed to allocate stack.");
       this->stack_ = static_cast<unsigned char*>(alloc);
-#else
-      this->stack_ = static_cast<unsigned char*>(_aligned_malloc(size, xbt_pagesize));
 #endif
 
-#ifndef _WIN32
       /* This is fatal. We are going to fail at some point when we try reusing this. */
       xbt_assert(
           mprotect(this->stack_, guard_size, PROT_NONE) != -1,
@@ -114,7 +103,7 @@ SwappedContext::SwappedContext(std::function<void()>&& code, smx_actor_t actor,
           "Please see https://simgrid.org/doc/latest/Configuring_SimGrid.html#configuring-the-user-code-virtualization "
           "for more information.",
           strerror(errno));
-#endif
+
       this->stack_ = this->stack_ + guard_size;
     } else {
       this->stack_ = static_cast<unsigned char*>(xbt_malloc0(actor->get_stacksize()));
@@ -151,7 +140,6 @@ SwappedContext::~SwappedContext()
     VALGRIND_STACK_DEREGISTER(valgrind_stack_id_);
 #endif
 
-#ifndef _WIN32
   if (guard_size > 0 && not MC_is_active()) {
     stack_ = stack_ - guard_size;
     if (mprotect(stack_, guard_size, PROT_READ | PROT_WRITE) == -1) {
@@ -163,7 +151,6 @@ SwappedContext::~SwappedContext()
     stack_ = reinterpret_cast<unsigned char**>(stack_)[-1];
 #endif
   }
-#endif /* not windows */
 
   xbt_free(stack_);
 }
@@ -178,13 +165,6 @@ unsigned char* SwappedContext::get_stack_bottom() const
 #endif
 }
 
-void SwappedContext::stop()
-{
-  Context::stop();
-  /* We must cut the actor execution using an exception to properly free the C++ RAII variables */
-  throw ForcefulKillException();
-}
-
 void SwappedContext::swap_into(SwappedContext* to)
 {
 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
@@ -204,17 +184,18 @@ void SwappedContext::swap_into(SwappedContext* to)
 }
 
 /** Maestro wants to run all ready actors */
-void SwappedContextFactory::run_all()
+void SwappedContextFactory::run_all(std::vector<actor::ActorImpl*> const& actors_list)
 {
   const auto* engine = EngineImpl::get_instance();
   /* This function is called by maestro at the beginning of a scheduling round to get all working threads executing some
    * 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 (is_parallel()) {
+  if (Context::is_parallel()) {
     // We lazily create the parmap so that all options are actually processed when doing so.
     if (parmap_ == nullptr)
-      parmap_ = std::make_unique<simgrid::xbt::Parmap<smx_actor_t>>(get_nthreads(), get_parallel_mode());
+      parmap_ =
+          std::make_unique<simgrid::xbt::Parmap<actor::ActorImpl*>>(Context::get_nthreads(), Context::parallel_mode);
 
     // Usually, Parmap::apply() executes the provided function on all elements of the array.
     // Here, the executed function does not return the control to the parmap before all the array is processed:
@@ -227,9 +208,9 @@ void SwappedContextFactory::run_all()
           auto* context = static_cast<SwappedContext*>(actor->context_.get());
           context->resume();
         },
-        engine->get_actors_to_run());
+        actors_list);
   } else { // sequential execution
-    if (not engine->has_actors_to_run())
+    if (actors_list.empty())
       return;
 
     /* maestro is already saved in the first slot of workers_context_ */
@@ -253,6 +234,7 @@ void SwappedContext::resume()
     // Save my current soul (either maestro, or one of the minions) in a thread-specific area
     worker_context_ = old;
   }
+  sthread_enable();
   // Switch my soul and the actor's one
   Context::set_current(this);
   old->swap_into(this);
@@ -273,7 +255,7 @@ void SwappedContext::suspend()
   SwappedContext* next_context;
   if (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();
+    boost::optional<actor::ActorImpl*> next_work = factory_.parmap_->next();
     if (next_work) {
       // There is a next soul to embody (ie, another executable actor)
       XBT_DEBUG("Run next process");
@@ -294,10 +276,12 @@ void SwappedContext::suspend()
     if (i < engine->get_actor_to_run_count()) {
       /* Actually swap into the next actor directly without transiting to maestro */
       XBT_DEBUG("Run next actor");
+      sthread_enable();
       next_context = static_cast<SwappedContext*>(engine->get_actor_to_run_at(i)->context_.get());
     } else {
       /* all processes were run, actually return to maestro */
       XBT_DEBUG("No more actors to run");
+      sthread_disable();
       next_context = factory_.maestro_context_;
     }
   }
@@ -305,6 +289,4 @@ void SwappedContext::suspend()
   this->swap_into(next_context);
 }
 
-} // namespace context
-} // namespace kernel
-} // namespace simgrid
+} // namespace simgrid::kernel::context