From: Arnaud Giersch Date: Wed, 29 Jun 2022 08:28:04 +0000 (+0200) Subject: Use RAII for sthread_enable/disable. X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/refs/heads/dev Use RAII for sthread_enable/disable. --- diff --git a/src/kernel/context/Context.cpp b/src/kernel/context/Context.cpp index 5736b265cd..a7fa14ac5b 100644 --- a/src/kernel/context/Context.cpp +++ b/src/kernel/context/Context.cpp @@ -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; diff --git a/src/kernel/context/ContextSwapped.cpp b/src/kernel/context/ContextSwapped.cpp index aff10af720..89f1b49d34 100644 --- a/src/kernel/context/ContextSwapped.cpp +++ b/src/kernel/context/ContextSwapped.cpp @@ -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 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(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_; } } diff --git a/src/sthread/sthread.h b/src/sthread/sthread.h index cf96500d2b..cf2b4777c5 100644 --- a/src/sthread/sthread.h +++ b/src/sthread/sthread.h @@ -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 diff --git a/src/sthread/sthread_impl.cpp b/src/sthread/sthread_impl.cpp index 4a92967507..91c5c55f2a 100644 --- a/src/sthread/sthread_impl.cpp +++ b/src/sthread/sthread_impl.cpp @@ -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);