Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sanitize the OOP of kernel::profile
[simgrid.git] / src / kernel / context / ContextThread.cpp
index 8b4acdf..171e32e 100644 (file)
@@ -10,7 +10,6 @@
 #include "src/simix/smx_private.hpp"
 #include "src/xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */
 #include "xbt/function_types.h"
-#include "xbt/xbt_os_thread.h"
 
 #include <functional>
 #include <utility>
@@ -23,8 +22,7 @@ namespace context {
 
 // ThreadContextFactory
 
-ThreadContextFactory::ThreadContextFactory()
-    : ContextFactory("ThreadContextFactory"), parallel_(SIMIX_context_is_parallel())
+ThreadContextFactory::ThreadContextFactory() : ContextFactory(), parallel_(SIMIX_context_is_parallel())
 {
   if (parallel_)
     ParallelThreadContext::initialize();
@@ -37,12 +35,12 @@ ThreadContextFactory::~ThreadContextFactory()
 }
 
 ThreadContext* ThreadContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup,
-                                                    smx_actor_t process, bool maestro)
+                                                    smx_actor_t actor, bool maestro)
 {
   if (parallel_)
-    return this->new_context<ParallelThreadContext>(std::move(code), cleanup, process, maestro);
+    return this->new_context<ParallelThreadContext>(std::move(code), cleanup, actor, maestro);
   else
-    return this->new_context<SerialThreadContext>(std::move(code), cleanup, process, maestro);
+    return this->new_context<SerialThreadContext>(std::move(code), cleanup, actor, maestro);
 }
 
 void ThreadContextFactory::run_all()
@@ -61,16 +59,11 @@ void ThreadContextFactory::run_all()
 ThreadContext::ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t actor, bool maestro)
     : AttachContext(std::move(code), cleanup, actor), is_maestro_(maestro)
 {
-  /* If the user provided a function for the process then use it */
+  /* If the user provided a function for the actor then use it */
   if (has_code()) {
-    if (smx_context_stack_size_was_set)
-      xbt_os_thread_setstacksize(smx_context_stack_size);
-    if (smx_context_guard_size_was_set)
-      xbt_os_thread_setguardsize(smx_context_guard_size);
-
-    /* create and start the process */
-    this->thread_ = xbt_os_thread_create(ThreadContext::wrapper, this);
-    /* wait the starting of the newly created process */
+    /* create and start the actor */
+    this->thread_ = new std::thread(ThreadContext::wrapper, this);
+    /* wait the starting of the newly created actor */
     this->end_.acquire();
   }
 
@@ -82,8 +75,10 @@ ThreadContext::ThreadContext(std::function<void()> code, void_pfn_smxprocess_t c
 
 ThreadContext::~ThreadContext()
 {
-  if (this->thread_) /* If there is a thread (maestro don't have any), wait for its termination */
-    xbt_os_thread_join(this->thread_, nullptr);
+  if (this->thread_) { /* Maestro don't have any thread */
+    thread_->join();
+    delete thread_;
+  }
 }
 
 void *ThreadContext::wrapper(void *param)
@@ -105,16 +100,17 @@ void *ThreadContext::wrapper(void *param)
 
   try {
     (*context)();
+    if (not context->is_maestro()) { // Just in case somebody detached maestro
+      context->Context::stop();
+      context->stop_hook();
+    }
   } catch (StopRequest const&) {
-    XBT_DEBUG("Caught a StopRequest");
+    XBT_DEBUG("Caught a StopRequest in Thread::wrapper");
     xbt_assert(not context->is_maestro(), "Maestro shall not receive StopRequests, even when detached.");
   } catch (simgrid::Exception const& e) {
     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
     throw;
   }
-  if (not context->is_maestro()) // Just in case somebody detached maestro
-    context->Context::stop();
-
   // Signal to the caller (normally the maestro) that we have finished:
   context->yield();
 
@@ -122,6 +118,8 @@ void *ThreadContext::wrapper(void *param)
   stack.ss_flags = SS_DISABLE;
   sigaltstack(&stack, nullptr);
 #endif
+  XBT_DEBUG("Terminating");
+  Context::set_current(nullptr);
   return nullptr;
 }
 
@@ -150,6 +148,7 @@ void ThreadContext::yield()
 void ThreadContext::stop()
 {
   Context::stop();
+  stop_hook();
   throw StopRequest();
 }
 
@@ -183,9 +182,9 @@ void ThreadContext::attach_stop()
 
 void SerialThreadContext::run_all()
 {
-  for (smx_actor_t const& process : simix_global->process_to_run) {
-    XBT_DEBUG("Handling %p", process);
-    ThreadContext* context = static_cast<ThreadContext*>(process->context_);
+  for (smx_actor_t const& actor : simix_global->process_to_run) {
+    XBT_DEBUG("Handling %p", actor);
+    ThreadContext* context = static_cast<ThreadContext*>(actor->context_);
     context->release();
     context->wait();
   }
@@ -208,10 +207,10 @@ void ParallelThreadContext::finalize()
 
 void ParallelThreadContext::run_all()
 {
-  for (smx_actor_t const& process : simix_global->process_to_run)
-    static_cast<ThreadContext*>(process->context_)->release();
-  for (smx_actor_t const& process : simix_global->process_to_run)
-    static_cast<ThreadContext*>(process->context_)->wait();
+  for (smx_actor_t const& actor : simix_global->process_to_run)
+    static_cast<ThreadContext*>(actor->context_)->release();
+  for (smx_actor_t const& actor : simix_global->process_to_run)
+    static_cast<ThreadContext*>(actor->context_)->wait();
 }
 
 void ParallelThreadContext::start_hook()