Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
using ucontext as a type name for ucontext_t is not portable
[simgrid.git] / src / simix / UContext.cpp
index a5e8e8f..af244c1 100644 (file)
@@ -8,6 +8,8 @@
 
 #include <stdarg.h>
 
+#include <functional>
+
 #include "xbt/parmap.h"
 #include "smx_private.h"
 #include "smx_private.hpp"
@@ -35,7 +37,7 @@
  * variable is decomposed into a serie of integers and
  * each integer is passed as argument to makecontext. */
 XBT_PRIVATE
-void simgrid_makecontext(ucontext* ucp, void (*func)(int first, ...), void* arg)
+void simgrid_makecontext(ucontext_t* ucp, void (*func)(int first, ...), void* arg)
 {
   int ctx_addr[CTX_ADDR_LEN];
   memcpy(ctx_addr, &arg, sizeof(void*));
@@ -83,10 +85,8 @@ protected:
   char *stack_ = nullptr; /* the thread stack */
 public:
   friend UContextFactory;
-  UContext(xbt_main_func_t code,
-          int argc, char **argv,
-          void_pfn_smxprocess_t cleanup_func,
-          smx_process_t process);
+  UContext(std::function<void()>  code,
+    void_pfn_smxprocess_t cleanup_func, smx_process_t process);
   ~UContext();
 protected:
   static void wrapper(int first, ...);
@@ -94,11 +94,9 @@ protected:
 
 class SerialUContext : public UContext {
 public:
-  SerialUContext(xbt_main_func_t code,
-          int argc, char **argv,
-          void_pfn_smxprocess_t cleanup_func,
-          smx_process_t process)
-    : UContext(code, argc, argv, cleanup_func, process)
+  SerialUContext(std::function<void()>  code,
+      void_pfn_smxprocess_t cleanup_func, smx_process_t process)
+    : UContext(std::move(code), cleanup_func, process)
   {}
   void stop() override;
   void suspend() override;
@@ -107,11 +105,9 @@ public:
 
 class ParallelUContext : public UContext {
 public:
-  ParallelUContext(xbt_main_func_t code,
-          int argc, char **argv,
-          void_pfn_smxprocess_t cleanup_func,
-          smx_process_t process)
-    : UContext(code, argc, argv, cleanup_func, process)
+  ParallelUContext(std::function<void()>  code,
+      void_pfn_smxprocess_t cleanup_func, smx_process_t process)
+    : UContext(std::move(code), cleanup_func, process)
   {}
   void stop() override;
   void suspend() override;
@@ -126,10 +122,8 @@ public:
 
   UContextFactory();
   virtual ~UContextFactory();
-  virtual Context* create_context(
-    xbt_main_func_t, int, char **, void_pfn_smxprocess_t,
-    smx_process_t process
-    ) override;
+  virtual Context* create_context(std::function<void()> code,
+    void_pfn_smxprocess_t, smx_process_t process) override;
   void run_all() override;
 };
 
@@ -207,27 +201,22 @@ void UContextFactory::run_all()
   }
 }
 
-Context* UContextFactory::create_context(
-  xbt_main_func_t code, int argc, char ** argv,
-  void_pfn_smxprocess_t cleanup,
-  smx_process_t process
-  )
+Context* UContextFactory::create_context(std::function<void()> code,
+  void_pfn_smxprocess_t cleanup, smx_process_t process)
 {
   if (sysv_parallel)
-    return new_context<ParallelUContext>(code, argc, argv, cleanup, process);
+    return new_context<ParallelUContext>(std::move(code), cleanup, process);
   else
-    return new_context<SerialUContext>(code, argc, argv, cleanup, process);
+    return new_context<SerialUContext>(std::move(code), cleanup, process);
 }
 
-UContext::UContext(xbt_main_func_t code,
-        int argc, char **argv,
-        void_pfn_smxprocess_t cleanup_func,
-        smx_process_t process)
-  : Context(code, argc, argv, cleanup_func, process)
+UContext::UContext(std::function<void()> code,
+    void_pfn_smxprocess_t cleanup_func, smx_process_t process)
+  : Context(std::move(code), cleanup_func, process)
 {
   /* if the user provided a function for the process then use it,
      otherwise it is the context for maestro */
-  if (code) {
+  if (has_code()) {
     this->stack_ = (char*) SIMIX_context_stack_new();
     getcontext(&this->uc_);
     this->uc_.uc_link = nullptr;
@@ -243,7 +232,7 @@ UContext::UContext(xbt_main_func_t code,
 
 #ifdef HAVE_MC
   if (MC_is_active() && code) {
-    MC_register_stack_area(this->stack_, context->process,
+    MC_register_stack_area(this->stack_, process,
                       &(this->uc_), smx_context_usable_stack_size);
   }
 #endif