X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/ccf671a80a47f0489c33fb1dc2a8aadfc28b5b88..3580b0137eab12ca216d9847823c86918b10dd53:/src/kernel/context/ContextThread.cpp diff --git a/src/kernel/context/ContextThread.cpp b/src/kernel/context/ContextThread.cpp index f329df45b0..f5a1f7a5e6 100644 --- a/src/kernel/context/ContextThread.cpp +++ b/src/kernel/context/ContextThread.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2009-2021. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2009-2022. 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. */ @@ -7,15 +7,16 @@ #include "simgrid/Exception.hpp" #include "src/internal_config.h" /* loads context system definitions */ -#include "src/simix/smx_private.hpp" -#include "src/xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */ +#include "src/kernel/EngineImpl.hpp" #include "xbt/function_types.h" +#include "xbt/xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */ #include #include +#include #include -XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context); +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_context); namespace simgrid { namespace kernel { @@ -25,21 +26,21 @@ namespace context { ThreadContextFactory::ThreadContextFactory() : ContextFactory() { - if (smx_context_stack_size != 8 * 1024 * 1024) + if (stack_size != 8 * 1024 * 1024) XBT_INFO("Stack size modifications are ignored by thread factory."); - if (SIMIX_context_is_parallel()) + if (is_parallel()) ParallelThreadContext::initialize(); } ThreadContextFactory::~ThreadContextFactory() { - if (SIMIX_context_is_parallel()) + if (is_parallel()) ParallelThreadContext::finalize(); } ThreadContext* ThreadContextFactory::create_context(std::function&& code, actor::ActorImpl* actor, bool maestro) { - if (SIMIX_context_is_parallel()) + if (is_parallel()) return this->new_context(std::move(code), actor, maestro); else return this->new_context(std::move(code), actor, maestro); @@ -47,7 +48,7 @@ ThreadContext* ThreadContextFactory::create_context(std::function&& code void ThreadContextFactory::run_all() { - if (SIMIX_context_is_parallel()) { + if (is_parallel()) { // Parallel execution ParallelThreadContext::run_all(); } else { @@ -59,13 +60,13 @@ void ThreadContextFactory::run_all() // ThreadContext ThreadContext::ThreadContext(std::function&& code, actor::ActorImpl* actor, bool maestro) - : AttachContext(std::move(code), actor), is_maestro_(maestro) + : AttachContext(std::move(code), actor, maestro) { /* If the user provided a function for the actor then use it */ if (has_code()) { /* create and start the actor */ this->thread_ = new std::thread(ThreadContext::wrapper, this); - /* wait the starting of the newly created actor */ + /* wait the start of the newly created actor */ this->end_.acquire(); } @@ -96,10 +97,8 @@ void ThreadContext::wrapper(ThreadContext* context) try { (*context)(); - if (not context->is_maestro()) { // Just in case somebody detached maestro - context->Context::stop(); - context->stop_hook(); - } + if (not context->is_maestro()) // Just in case somebody detached maestro + context->stop(); } catch (ForcefulKillException const&) { XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper"); xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached."); @@ -139,13 +138,6 @@ void ThreadContext::yield() this->end_.release(); } -void ThreadContext::stop() -{ - Context::stop(); - stop_hook(); - throw ForcefulKillException(); -} - void ThreadContext::suspend() { this->yield(); @@ -155,7 +147,7 @@ void ThreadContext::suspend() void ThreadContext::attach_start() { // We're breaking the layers here by depending on the upper layer: - auto* maestro = static_cast(simix_global->maestro_->context_.get()); + auto* maestro = static_cast(EngineImpl::get_instance()->get_maestro()->context_.get()); maestro->begin_.release(); xbt_assert(not this->is_maestro()); this->start(); @@ -166,7 +158,7 @@ void ThreadContext::attach_stop() xbt_assert(not this->is_maestro()); this->yield(); - auto* maestro = static_cast(simix_global->maestro_->context_.get()); + auto* maestro = static_cast(EngineImpl::get_instance()->get_maestro()->context_.get()); maestro->end_.acquire(); Context::set_current(nullptr); @@ -176,7 +168,8 @@ void ThreadContext::attach_stop() void SerialThreadContext::run_all() { - for (smx_actor_t const& actor : simix_global->actors_to_run) { + const auto& to_run = EngineImpl::get_instance()->get_actors_to_run(); + for (smx_actor_t const& actor : to_run) { XBT_DEBUG("Handling %p", actor); auto* context = static_cast(actor->context_.get()); context->release(); @@ -190,7 +183,7 @@ xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr; void ParallelThreadContext::initialize() { - thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads()); + thread_sem_ = new xbt::OsSemaphore(get_nthreads()); } void ParallelThreadContext::finalize() @@ -201,9 +194,11 @@ void ParallelThreadContext::finalize() void ParallelThreadContext::run_all() { - for (smx_actor_t const& actor : simix_global->actors_to_run) + const auto& to_release = EngineImpl::get_instance()->get_actors_to_run(); + for (smx_actor_t const& actor : to_release) static_cast(actor->context_.get())->release(); - for (smx_actor_t const& actor : simix_global->actors_to_run) + const auto& to_wait = EngineImpl::get_instance()->get_actors_to_run(); + for (smx_actor_t const& actor : to_wait) static_cast(actor->context_.get())->wait(); }