Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move global variables to class Context.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 16 Oct 2020 06:52:20 +0000 (08:52 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 16 Oct 2020 07:30:10 +0000 (09:30 +0200)
src/kernel/context/Context.cpp
src/kernel/context/Context.hpp
src/kernel/context/ContextThread.cpp
src/simix/smx_global.cpp

index b53a2a0..a869ad5 100644 (file)
@@ -11,8 +11,9 @@
 #include "src/simix/smx_private.hpp"
 #include "src/surf/surf_interface.hpp"
 
-XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
+#include <array>
 
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
 
 namespace simgrid {
 namespace kernel {
@@ -22,14 +23,28 @@ ContextFactoryInitializer factory_initializer = nullptr;
 
 ContextFactory::~ContextFactory() = default;
 
-static thread_local Context* smx_current_context = nullptr;
+thread_local Context* Context::current_context_ = nullptr;
+
+#ifndef WIN32
+/* Install or disable alternate signal stack, for SIGSEGV handler. */
+int Context::install_sigsegv_stack(stack_t* old_stack, bool enable)
+{
+  static std::array<unsigned char, SIGSTKSZ> sigsegv_stack;
+  stack_t stack;
+  stack.ss_sp    = sigsegv_stack.data();
+  stack.ss_size  = sigsegv_stack.size();
+  stack.ss_flags = enable ? 0 : SS_DISABLE;
+  return sigaltstack(&stack, old_stack);
+}
+#endif
+
 Context* Context::self()
 {
-  return smx_current_context;
+  return current_context_;
 }
 void Context::set_current(Context* self)
 {
-  smx_current_context = self;
+  current_context_ = self;
 }
 
 void Context::declare_context(std::size_t size)
index f1aa12b..5edadf8 100644 (file)
@@ -9,7 +9,6 @@
 #include "simgrid/forward.h"
 #include "src/kernel/activity/ActivityImpl.hpp"
 
-#include <array>
 #include <csignal>
 #include <functional>
 
@@ -44,12 +43,16 @@ protected:
 class XBT_PUBLIC Context {
   friend ContextFactory;
 
+  static thread_local Context* current_context_;
+
   std::function<void()> code_;
   actor::ActorImpl* actor_ = nullptr;
   bool iwannadie_          = false;
   void declare_context(std::size_t size);
 
 public:
+  static int install_sigsegv_stack(stack_t* old_stack, bool enable);
+
   Context(std::function<void()>&& code, actor::ActorImpl* actor);
   Context(const Context&) = delete;
   Context& operator=(const Context&) = delete;
@@ -104,8 +107,4 @@ XBT_PRIVATE ContextFactory* boost_factory();
 
 XBT_PRIVATE void SIMIX_context_mod_init();
 XBT_PRIVATE void SIMIX_context_mod_exit();
-
-#ifndef WIN32
-XBT_PUBLIC_DATA std::array<unsigned char, SIGSTKSZ> sigsegv_stack;
-#endif
 #endif
index dc6e2ad..6be2b09 100644 (file)
@@ -87,12 +87,7 @@ void ThreadContext::wrapper(ThreadContext* context)
   Context::set_current(context);
 
 #ifndef WIN32
-  /* Install alternate signal stack, for SIGSEGV handler. */
-  stack_t stack;
-  stack.ss_sp    = sigsegv_stack.data();
-  stack.ss_size  = sigsegv_stack.size();
-  stack.ss_flags = 0;
-  sigaltstack(&stack, nullptr);
+  install_sigsegv_stack(nullptr, true);
 #endif
   // Tell the caller (normally the maestro) we are starting, and wait for its green light
   context->end_.release();
@@ -115,8 +110,7 @@ void ThreadContext::wrapper(ThreadContext* context)
   context->yield();
 
 #ifndef WIN32
-  stack.ss_flags = SS_DISABLE;
-  sigaltstack(&stack, nullptr);
+  install_sigsegv_stack(nullptr, false);
 #endif
   XBT_DEBUG("Terminating");
   Context::set_current(nullptr);
index 1349b13..034e5be 100644 (file)
@@ -93,13 +93,9 @@ std::array<unsigned char, SIGSTKSZ> sigsegv_stack; /* alternate stack for SIGSEG
  */
 static void install_segvhandler()
 {
-  stack_t stack;
   stack_t old_stack;
-  stack.ss_sp    = sigsegv_stack.data();
-  stack.ss_size  = sigsegv_stack.size();
-  stack.ss_flags = 0;
 
-  if (sigaltstack(&stack, &old_stack) == -1) {
+  if (simgrid::kernel::context::Context::install_sigsegv_stack(&old_stack, true) == -1) {
     XBT_WARN("Failed to register alternate signal stack: %s", strerror(errno));
     return;
   }