Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] simcall to run code in kernel mode
[simgrid.git] / include / simgrid / simix.hpp
index 99f476f..5fbc4d5 100644 (file)
@@ -7,15 +7,39 @@
 #ifndef SIMGRID_SIMIX_HPP
 #define SIMGRID_SIMIX_HPP
 
+#include <cstddef>
+
+#include <string>
 #include <utility>
 #include <memory>
+#include <functional>
+#include <future>
+#include <type_traits>
 
 #include <xbt/function_types.h>
 #include <simgrid/simix.h>
 
+XBT_PUBLIC(void) simcall_run_kernel(std::function<void()> const& code);
+
 namespace simgrid {
 namespace simix {
 
+template<class F>
+typename std::result_of<F()>::type kernel(F&& code)
+{
+  typedef typename std::result_of<F()>::type R;
+  std::promise<R> promise;
+  simcall_run_kernel([&]{
+    try {
+      promise.set_value(code());
+    }
+    catch(...) {
+      promise.set_exception(std::current_exception());
+    }
+  });
+  return promise.get_future().get();
+}
+
 class Context;
 class ContextFactory;
 
@@ -26,10 +50,8 @@ public:
 
   ContextFactory(std::string name) : name_(std::move(name)) {}
   virtual ~ContextFactory();
-  virtual Context* create_context(
-    xbt_main_func_t, int, char **, void_pfn_smxprocess_t,
-    smx_process_t process
-    ) = 0;
+  virtual Context* create_context(std::function<void()> code,
+    void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
   virtual void run_all() = 0;
   virtual Context* self();
   std::string const& name() const
@@ -49,23 +71,23 @@ protected:
 };
 
 class Context {
-protected:
-  xbt_main_func_t code_ = nullptr;
-  int argc_ = 0;
-  char **argv_ = nullptr;
 private:
+  std::function<void()> code_;
   void_pfn_smxprocess_t cleanup_func_ = nullptr;
   smx_process_t process_ = nullptr;
 public:
   bool iwannadie;
 public:
-  Context(xbt_main_func_t code,
-          int argc, char **argv,
+  Context(std::function<void()> code,
           void_pfn_smxprocess_t cleanup_func,
           smx_process_t process);
-  int operator()()
+  void operator()()
+  {
+    code_();
+  }
+  bool has_code() const
   {
-    return code_(argc_, argv_);
+    return (bool) code_;
   }
   smx_process_t process()
   {