Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] SIMIX futures
authorGabriel Corona <gabriel.corona@loria.fr>
Fri, 17 Jun 2016 11:01:25 +0000 (13:01 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Fri, 17 Jun 2016 11:02:15 +0000 (13:02 +0200)
include/simgrid/simix/blocking_simcall.hpp
teshsuite/simix/generic_simcalls/generic_simcalls.cpp
teshsuite/simix/generic_simcalls/generic_simcalls.tesh

index aa85620..59f688c 100644 (file)
@@ -18,6 +18,7 @@
 #include <xbt/future.hpp>
 #include <simgrid/kernel/future.hpp>
 #include <simgrid/simix.h>
+#include <simgrid/simix.hpp>
 
 XBT_PUBLIC(void) simcall_run_blocking(std::function<void()> const& code);
 
@@ -68,6 +69,63 @@ auto blocking_simcall(F code) -> decltype(code().get())
   return result.get();
 }
 
+/** A blocking (`wait()`-based) future for SIMIX processes */
+template <class T>
+class Future {
+public:
+  Future() {}
+  Future(simgrid::kernel::Future<T> future) : future_(std::move(future)) {}
+
+  bool valid() const { return future_.valid(); }
+  T get()
+  {
+    if (!valid())
+      throw std::future_error(std::future_errc::no_state);
+    smx_process_t self = SIMIX_process_self();
+    simgrid::xbt::Result<T> result;
+    simcall_run_blocking([this, &result, self]{
+      try {
+        // When the kernel future is ready...
+        this->future_.then([this, &result, self](simgrid::kernel::Future<T> value) {
+          // ... wake up the process with the result of the kernel future.
+          simgrid::xbt::setPromise(result, value);
+          simgrid::simix::unblock(self);
+        });
+      }
+      catch (...) {
+        result.set_exception(std::current_exception());
+        simgrid::simix::unblock(self);
+      }
+    });
+    return result.get();
+  }
+  // TODO, wait()
+  // TODO, wait_for()
+  // TODO, wait_until()
+private:
+  // We wrap an event-based kernel future:
+  simgrid::kernel::Future<T> future_;
+};
+
+/** Start some asynchronous work
+ *
+ *  @param code SimGrid kernel code which returns a simgrid::kernel::Future
+ *  @return     User future
+ */
+template<class F>
+auto asynchronous_simcall(F code)
+  -> Future<decltype(code().get())>
+{
+  typedef decltype(code().get()) T;
+
+  // Execute the code in the kernel and get the kernel simcall:
+  simgrid::kernel::Future<T> future =
+    simgrid::simix::kernel(std::move(code));
+
+  // Wrap tyhe kernel simcall in a user simcall:
+  return simgrid::simix::Future<T>(std::move(future));
+}
+
 }
 }
 
index f1743b5..3160657 100644 (file)
@@ -75,6 +75,16 @@ static int master(int argc, char *argv[])
   });
   XBT_INFO("blocking_simcall with value returned with %i", res);
 
+  // Synchronize on a successul Future<int> and get the value:
+  simgrid::simix::Future<int> future = simgrid::simix::asynchronous_simcall([&] {
+    return kernel_defer(50, [] {
+      XBT_INFO("asynchronous_simcall with value");
+      return 43;
+    });
+  });
+  res = future.get();
+  XBT_INFO("asynchronous_simcall with value returned with %i", res);
+
   return 0;
 }
 
index 1c10ecb..bd2dfd5 100644 (file)
@@ -7,3 +7,5 @@ $ ${bindir:=.}/generic_simcalls --cfg=contexts/stack-size:96 ${srcdir:=.}/exampl
 > [Tremblay:master:(0) 20.000000] [test/INFO] Exception caught: Exception throwed from kernel_defer
 > [30.000000] [test/INFO] blocking_simcall with value
 > [Tremblay:master:(0) 30.000000] [test/INFO] blocking_simcall with value returned with 42
+> [50.000000] [test/INFO] asynchronous_simcall with value
+> [Tremblay:master:(0) 50.000000] [test/INFO] asynchronous_simcall with value returned with 43