Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
In C++, classes don't need a name because they have a class
[simgrid.git] / src / kernel / context / ContextThread.cpp
index 01c084e..9949353 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2009-2019. 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. */
@@ -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,35 +59,31 @@ 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();
   }
 
   /* Otherwise, we attach to the current thread */
   else {
-    SIMIX_context_set_current(this);
+    Context::set_current(this);
   }
 }
 
 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);
+    thread_->join();
+  delete thread_;
 }
 
 void *ThreadContext::wrapper(void *param)
 {
   ThreadContext* context = static_cast<ThreadContext*>(param);
-  SIMIX_context_set_current(context);
+  Context::set_current(context);
 
 #ifndef WIN32
   /* Install alternate signal stack, for SIGSEGV handler. */
@@ -176,16 +170,16 @@ void ThreadContext::attach_stop()
   ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
   maestro->end_.acquire();
 
-  SIMIX_context_set_current(nullptr);
+  Context::set_current(nullptr);
 }
 
 // SerialThreadContext
 
 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 +202,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()