Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve context termination.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 21 Sep 2017 21:28:31 +0000 (23:28 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 22 Sep 2017 08:49:45 +0000 (10:49 +0200)
Throw an exception to terminate the context at upper level, and have
resources freed by stack unwinding.

Closes #204, since pthread_exit is not called anymore.

src/kernel/context/Context.hpp
src/kernel/context/ContextBoost.cpp
src/kernel/context/ContextRaw.cpp
src/kernel/context/ContextThread.cpp
src/kernel/context/ContextThread.hpp
src/kernel/context/ContextUnix.cpp

index 8102fb1..41e1f8f 100644 (file)
@@ -80,6 +80,8 @@ namespace context {
     void_pfn_smxprocess_t cleanup_func_ = nullptr;
     smx_actor_t process_ = nullptr;
   public:
+    class StopRequest {
+    };
     bool iwannadie;
 
     Context(std::function<void()> code,
index f39f1d5..a4c78b3 100644 (file)
@@ -135,8 +135,13 @@ void BoostContext::smx_ctx_boost_wrapper(BoostContext::ctx_arg_type arg)
   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
   BoostContext* context                         = static_cast<BoostContext**>(arg.data)[1];
 #endif
-  (*context)();
-  context->stop();
+  try {
+    (*context)();
+    context->stop();
+  } catch (StopRequest) {
+    XBT_DEBUG("Caught a StopRequest");
+  }
+  context->suspend();
 }
 
 inline void BoostContext::smx_ctx_boost_jump_fcontext(BoostContext* from, BoostContext* to)
@@ -223,7 +228,7 @@ void BoostSerialContext::suspend()
 void BoostSerialContext::stop()
 {
   BoostContext::stop();
-  this->suspend();
+  throw StopRequest();
 }
 
 // BoostParallelContext
@@ -250,7 +255,7 @@ void BoostParallelContext::suspend()
 void BoostParallelContext::stop()
 {
   BoostContext::stop();
-  this->suspend();
+  throw StopRequest();
 }
 
 void BoostParallelContext::resume()
index 53726aa..00c6008 100644 (file)
@@ -291,8 +291,13 @@ RawContext* RawContextFactory::create_context(std::function<void()> code,
 void RawContext::wrapper(void* arg)
 {
   RawContext* context = static_cast<RawContext*>(arg);
-  (*context)();
-  context->stop();
+  try {
+    (*context)();
+    context->stop();
+  } catch (StopRequest) {
+    XBT_DEBUG("Caught a StopRequest");
+  }
+  context->suspend();
 }
 
 RawContext::RawContext(std::function<void()> code,
@@ -323,7 +328,7 @@ RawContext::~RawContext()
 void RawContext::stop()
 {
   Context::stop();
-  this->suspend();
+  throw StopRequest();
 }
 
 void RawContextFactory::run_all()
index 3cbb7d5..322d0e1 100644 (file)
@@ -151,9 +151,17 @@ void *ThreadContext::wrapper(void *param)
   if (smx_ctx_thread_sem)       /* parallel run */
     xbt_os_sem_acquire(smx_ctx_thread_sem);
 
-  (*context)();
-  context->stop();
+  try {
+    (*context)();
+    context->stop();
+  } catch (StopRequest) {
+    XBT_DEBUG("Caught a StopRequest");
+  }
 
+#ifndef WIN32
+  stack.ss_flags = SS_DISABLE;
+  sigaltstack(&stack, nullptr);
+#endif
   return nullptr;
 }
 
@@ -202,14 +210,7 @@ void ThreadContext::stop()
   // Signal to the maestro that it has finished:
   xbt_os_sem_release(this->end_);
 
-#ifndef WIN32
-  stack_t stack;
-  stack.ss_sp    = nullptr;
-  stack.ss_size  = 0;
-  stack.ss_flags = SS_DISABLE;
-  sigaltstack(&stack, nullptr);
-#endif
-  xbt_os_thread_exit(nullptr);
+  throw StopRequest();
 }
 
 void ThreadContext::suspend()
index 096a01c..3449f88 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009-2015. The SimGrid Team.
+/* Copyright (c) 2009-2017. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
index ec26e43..5a7148d 100644 (file)
@@ -247,8 +247,13 @@ static void smx_ctx_sysv_wrapper(int first, ...)
   }
   memcpy(&context, ctx_addr, sizeof(simgrid::kernel::context::UContext*));
 
-  (*context)();
-  context->stop();
+  try {
+    (*context)();
+    context->stop();
+  } catch (simgrid::kernel::context::Context::StopRequest) {
+    XBT_DEBUG("Caught a StopRequest");
+  }
+  context->suspend();
 }
 
 namespace simgrid {
@@ -258,7 +263,7 @@ namespace context {
 void SerialUContext::stop()
 {
   Context::stop();
-  this->suspend();
+  throw StopRequest();
 }
 
 void SerialUContext::suspend()
@@ -291,7 +296,7 @@ void SerialUContext::resume()
 void ParallelUContext::stop()
 {
   UContext::stop();
-  this->suspend();
+  throw StopRequest();
 }
 
 /** Run one particular simulated process on the current thread. */