-/* Copyright (c) 2016-2019. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2016-2021. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#include <xbt/sysdep.h>
-#include <xbt/future.hpp>
#include <simgrid/kernel/future.hpp>
#include <simgrid/simix.h>
#include <simgrid/simix.hpp>
+#include <xbt/promise.hpp>
namespace simgrid {
namespace simix {
*/
template <class F> auto kernel_sync(F code) -> decltype(code().get())
{
- typedef decltype(code().get()) T;
+ using T = decltype(code().get());
if (SIMIX_is_maestro())
xbt_die("Can't execute blocking call in kernel mode");
[&result, self, &code] {
try {
auto future = code();
- future.then_([&result, self](std::shared_ptr<simgrid::kernel::FutureState<T>>&& value) {
- simgrid::xbt::set_promise(result, simgrid::kernel::Future<T>(value));
+ future.then_([&result, self](std::shared_ptr<simgrid::kernel::FutureState<T>> value) {
+ simgrid::xbt::set_promise(result, simgrid::kernel::Future<T>(std::move(value)));
simgrid::simix::unblock(self);
});
} catch (...) {
}
/** A blocking (`wait()`-based) future for SIMIX processes */
-// TODO, .wait_for
-// TODO, .wait_until
-// TODO, SharedFuture
-// TODO, simgrid::simix::when_all - wait for all future to be ready (this one is simple!)
-// TODO, simgrid::simix::when_any - wait for any future to be ready
+// TODO:
+// - .wait_for
+// - .wait_until
+// - SharedFuture
+// - simgrid::simix::when_all - wait for all future to be ready (this one is simple!)
+// - simgrid::simix::when_any - wait for any future to be ready
template <class T>
class Future {
public:
- Future() { /* Nothing to do*/}
+ Future() = default;
explicit Future(simgrid::kernel::Future<T> future) : future_(std::move(future)) {}
+ Future(Future&&) noexcept = default;
+ Future& operator=(Future&&) noexcept = default;
bool valid() const { return future_.valid(); }
T get()
[this, &result, self] {
try {
// When the kernel future is ready...
- this->future_.then_([&result, self](std::shared_ptr<simgrid::kernel::FutureState<T>>&& value) {
+ this->future_.then_([&result, self](std::shared_ptr<simgrid::kernel::FutureState<T>> value) {
// ... wake up the process with the result of the kernel future.
- simgrid::xbt::set_promise(result, simgrid::kernel::Future<T>(value));
+ simgrid::xbt::set_promise(result, simgrid::kernel::Future<T>(std::move(value)));
simgrid::simix::unblock(self);
});
} catch (...) {
[this, &exception, self] {
try {
// When the kernel future is ready...
- this->future_.then_([this, self](std::shared_ptr<simgrid::kernel::FutureState<T>>&& value) {
+ this->future_.then_([this, self](std::shared_ptr<simgrid::kernel::FutureState<T>> value) {
// ...store it the simix kernel and wake up.
- this->future_ = std::move(simgrid::kernel::Future<T>(value));
+ this->future_ = simgrid::kernel::Future<T>(std::move(value));
simgrid::simix::unblock(self);
});
} catch (...) {
},
nullptr);
}
+
private:
// We wrap an event-based kernel future:
simgrid::kernel::Future<T> future_;
*/
template <class F> auto kernel_async(F code) -> Future<decltype(code().get())>
{
- typedef decltype(code().get()) T;
+ using T = decltype(code().get());
// Execute the code in the kernel and get the kernel future:
simgrid::kernel::Future<T> future = simgrid::kernel::actor::simcall(std::move(code));
- // Wrap the kernel future in a actor future:
+ // Wrap the kernel future in an actor future:
return simgrid::simix::Future<T>(std::move(future));
}
}