Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use RAII for sthread_enable/disable. dev
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 29 Jun 2022 08:28:04 +0000 (10:28 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 27 Nov 2023 08:45:06 +0000 (09:45 +0100)
src/kernel/context/Context.cpp
src/kernel/context/ContextSwapped.cpp
src/sthread/sthread.h
src/sthread/sthread_impl.cpp

index 5736b26..a7fa14a 100644 (file)
@@ -83,7 +83,6 @@ Context::~Context()
 void Context::stop()
 {
   this->actor_->cleanup_from_self();
-  sthread_disable();
   throw ForcefulKillException(); // clean RAII variables with the dedicated exception
 }
 AttachContext::~AttachContext() = default;
index aff10af..89f1b49 100644 (file)
@@ -38,15 +38,12 @@ 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();
+    const SThreadGuard sthread_guard;
     (*context)();
-    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;
   }
@@ -174,6 +171,7 @@ void SwappedContext::swap_into(SwappedContext* to)
 /** Maestro wants to run all ready actors */
 void SwappedContextFactory::run_all(std::vector<actor::ActorImpl*> const& actors_list)
 {
+  const SThreadGuard sthread_guard;
   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
@@ -222,7 +220,6 @@ 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);
@@ -264,12 +261,10 @@ 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_;
     }
   }
index cf96500..cf2b477 100644 (file)
@@ -99,4 +99,15 @@ void sthread_access_end(void* objaddr, const char* objname, const char* file, in
 }
 #endif
 
+#if defined(__cplusplus)
+// Helper class using RAII to enable/disable sthread
+class SThreadGuard {
+public:
+  SThreadGuard() { sthread_enable(); }
+  SThreadGuard(const SThreadGuard&) = delete;
+  SThreadGuard& operator=(const SThreadGuard&) = delete;
+  ~SThreadGuard() { sthread_disable(); }
+};
+#endif
+
 #endif
index 4a92967..91c5c55 100644 (file)
@@ -80,11 +80,10 @@ int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**
   zone->seal();
 
   /* Launch the user's main() on an actor */
-  sthread_enable();
+  const SThreadGuard sthread_guard;
   sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
 
   sg4::Engine::get_instance()->run();
-  sthread_disable();
   XBT_INFO("All threads exited. Terminating the simulation.");
 
   return 0;
@@ -115,9 +114,8 @@ int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr
         if (SMPI_is_inited())
           SMPI_thread_create();
 #endif
-        sthread_enable();
+        const SThreadGuard sthread_guard;
         user_function(param);
-        sthread_disable();
       },
       start_routine, arg);