Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] .wait() and .is_ready() on simix::Future
authorGabriel Corona <gabriel.corona@loria.fr>
Mon, 20 Jun 2016 13:25:53 +0000 (15:25 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Mon, 20 Jun 2016 13:48:18 +0000 (15:48 +0200)
include/simgrid/kernel/future.hpp
include/simgrid/simix/blocking_simcall.hpp
teshsuite/simix/generic_simcalls/generic_simcalls.cpp
teshsuite/simix/generic_simcalls/generic_simcalls.tesh

index a11c8ec..ffd1a33 100644 (file)
@@ -261,7 +261,8 @@ public:
   Future(Future&& that) : state_(std::move(that.state_)) {}
   Future& operator=(Future&& that)
   {
   Future(Future&& that) : state_(std::move(that.state_)) {}
   Future& operator=(Future&& that)
   {
-    state_ = std::move(that.stat_);
+    state_ = std::move(that.state_);
+    return *this;
   }
 
   /** Whether the future is valid:.
   }
 
   /** Whether the future is valid:.
index ee12510..c89b273 100644 (file)
@@ -100,7 +100,33 @@ public:
     });
     return result.get();
   }
     });
     return result.get();
   }
-  // TODO, wait()
+  bool is_ready() const
+  {
+    if (!valid())
+      throw std::future_error(std::future_errc::no_state);
+    return future_.is_ready();
+  }
+  void wait()
+  {
+    if (!valid())
+      throw std::future_error(std::future_errc::no_state);
+    std::exception_ptr exception;
+    smx_process_t self = SIMIX_process_self();
+    simcall_run_blocking([this, &exception, self]{
+      try {
+        // When the kernel future is ready...
+        this->future_.then([this, self](simgrid::kernel::Future<T> value) {
+          // ...store it the simix kernel and wake up.
+          this->future_ = std::move(value);
+          simgrid::simix::unblock(self);
+        });
+      }
+      catch (...) {
+        exception = std::current_exception();
+        simgrid::simix::unblock(self);
+      }
+    });
+  }
   // TODO, wait_for()
   // TODO, wait_until()
 private:
   // TODO, wait_for()
   // TODO, wait_until()
 private:
index b2525f5..1156e9b 100644 (file)
@@ -85,6 +85,19 @@ static int master(int argc, char *argv[])
   res = future.get();
   XBT_INFO("kernelAsync with value returned with %i", res);
 
   res = future.get();
   XBT_INFO("kernelAsync with value returned with %i", res);
 
+  // Synchronize on a successul Future<int> and get the value:
+  future = simgrid::simix::kernelAsync([&] {
+    return kernel_defer(60, [] {
+      XBT_INFO("kernelAsync with value");
+      return 43;
+    });
+  });
+  XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
+  future.wait();
+  XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
+  res = future.get();
+  XBT_INFO("kernelAsync with value returned with %i", res);
+
   return 0;
 }
 
   return 0;
 }
 
index 25c22ef..0bb08af 100644 (file)
@@ -9,3 +9,7 @@ $ ${bindir:=.}/generic_simcalls --cfg=contexts/stack-size:96 ${srcdir:=.}/exampl
 > [Tremblay:master:(0) 30.000000] [test/INFO] kernelSync with value returned with 42
 > [50.000000] [test/INFO] kernelAsync with value
 > [Tremblay:master:(0) 50.000000] [test/INFO] kernelAsync with value returned with 43
 > [Tremblay:master:(0) 30.000000] [test/INFO] kernelSync with value returned with 42
 > [50.000000] [test/INFO] kernelAsync with value
 > [Tremblay:master:(0) 50.000000] [test/INFO] kernelAsync with value returned with 43
+> [Tremblay:master:(0) 50.000000] [test/INFO] The future is not ready
+> [60.000000] [test/INFO] kernelAsync with value
+> [Tremblay:master:(0) 60.000000] [test/INFO] The future is ready
+> [Tremblay:master:(0) 60.000000] [test/INFO] kernelAsync with value returned with 43
\ No newline at end of file