Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ContextBoost: define helper function for jump_fcontext.
[simgrid.git] / src / kernel / context / ContextBoost.cpp
index 3028445..d36722c 100644 (file)
@@ -127,13 +127,22 @@ void BoostContextFactory::run_all()
 
 // BoostContext
 
-static void smx_ctx_boost_wrapper(std::intptr_t arg)
+void BoostContext::smx_ctx_boost_wrapper(std::intptr_t arg)
 {
-  BoostContext* context = (BoostContext*)(arg);
+  BoostContext* context = reinterpret_cast<BoostContext*>(arg);
   (*context)();
   context->stop();
 }
 
+inline void BoostContext::smx_ctx_boost_jump_fcontext(BoostContext* from, BoostContext* to)
+{
+#if BOOST_VERSION < 105600
+  boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
+#else
+  boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
+#endif
+}
+
 BoostContext::BoostContext(std::function<void()> code,
     void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
   : Context(std::move(code), cleanup_func, process)
@@ -154,7 +163,7 @@ BoostContext::BoostContext(std::function<void()> code,
                       smx_context_usable_stack_size,
                       smx_ctx_boost_wrapper);
   } else {
-#if HAVE_BOOST_CONTEXTS == 1
+#if BOOST_VERSION < 105600
     this->fc_ = new boost::context::fcontext_t();
 #endif
     if (BoostContext::maestro_context_ == nullptr)
@@ -164,7 +173,7 @@ BoostContext::BoostContext(std::function<void()> code,
 
 BoostContext::~BoostContext()
 {
-#if HAVE_BOOST_CONTEXTS == 1
+#if BOOST_VERSION < 105600
   if (not this->stack_)
     delete this->fc_;
 #endif
@@ -178,11 +187,7 @@ BoostContext::~BoostContext()
 void BoostContext::resume()
 {
   SIMIX_context_set_current(this);
-#if HAVE_BOOST_CONTEXTS == 1
-  boost::context::jump_fcontext(maestro_context_->fc_, this->fc_, (intptr_t) this);
-#else
-  boost::context::jump_fcontext(&maestro_context_->fc_, this->fc_, (intptr_t) this);
-#endif
+  smx_ctx_boost_jump_fcontext(maestro_context_, this);
 }
 
 void BoostSerialContext::suspend()
@@ -201,12 +206,8 @@ void BoostSerialContext::suspend()
     XBT_DEBUG("No more process to run");
     next_context = static_cast<BoostSerialContext*>(maestro_context_);
   }
-  SIMIX_context_set_current((smx_context_t) next_context);
-#if HAVE_BOOST_CONTEXTS == 1
-  boost::context::jump_fcontext(this->fc_, next_context->fc_, (intptr_t) next_context);
-#else
-  boost::context::jump_fcontext(&this->fc_, next_context->fc_, (intptr_t) next_context);
-#endif
+  SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
+  smx_ctx_boost_jump_fcontext(this, next_context);
 }
 
 void BoostSerialContext::stop()
@@ -228,16 +229,12 @@ void BoostParallelContext::suspend()
     next_context = static_cast<BoostParallelContext*>(next_work.get()->context);
   } else {
     XBT_DEBUG("No more processes to run");
-    uintptr_t worker_id = (uintptr_t)xbt_os_thread_get_specific(worker_id_key_);
+    uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
     next_context = static_cast<BoostParallelContext*>(workers_context_[worker_id]);
   }
 
-  SIMIX_context_set_current(static_cast<smx_context_t> (next_context));
-#if HAVE_BOOST_CONTEXTS == 1
-  boost::context::jump_fcontext(this->fc_, next_context->fc_, (intptr_t)(next_context));
-#else
-  boost::context::jump_fcontext(&this->fc_, next_context->fc_, (intptr_t)(next_context));
-#endif
+  SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
+  smx_ctx_boost_jump_fcontext(this, next_context);
 }
 
 void BoostParallelContext::stop()
@@ -249,17 +246,13 @@ void BoostParallelContext::stop()
 void BoostParallelContext::resume()
 {
   uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
-  xbt_os_thread_set_specific(worker_id_key_, (void*) worker_id);
+  xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
 
   BoostParallelContext* worker_context = static_cast<BoostParallelContext*>(SIMIX_context_self());
   workers_context_[worker_id] = worker_context;
 
   SIMIX_context_set_current(this);
-#if HAVE_BOOST_CONTEXTS == 1
-  boost::context::jump_fcontext(worker_context->fc_, this->fc_, (intptr_t) this);
-#else
-  boost::context::jump_fcontext(&worker_context->fc_, this->fc_, (intptr_t) this);
-#endif
+  smx_ctx_boost_jump_fcontext(worker_context, this);
 }
 
 #endif