X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/149c63f36e15b8500b1e826bda5138318ff7ba2b..b592b5aa6acbd39d705426f2b275ee8736548d0d:/include/simgrid/kernel/future.hpp diff --git a/include/simgrid/kernel/future.hpp b/include/simgrid/kernel/future.hpp index 7a1a1d3293..e3a24687ac 100644 --- a/include/simgrid/kernel/future.hpp +++ b/include/simgrid/kernel/future.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2016-2020. The SimGrid Team. +/* Copyright (c) 2016-2021. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -17,7 +17,7 @@ #include #include -#include +#include namespace simgrid { namespace kernel { @@ -48,7 +48,7 @@ public: FutureStateBase(FutureStateBase const&) = delete; FutureStateBase& operator=(FutureStateBase const&) = delete; - XBT_PUBLIC void schedule(simgrid::xbt::Task&& job); + XBT_PUBLIC void schedule(simgrid::xbt::Task&& job) const; void set_exception(std::exception_ptr exception) { @@ -116,8 +116,7 @@ protected: **/ void resolve() { - if (status_ != FutureStatus::ready) - xbt_die("Deadlock: this future is not ready"); + xbt_assert(status_ == FutureStatus::ready, "Deadlock: this future is not ready"); status_ = FutureStatus::done; if (exception_) { std::exception_ptr exception = std::move(exception_); @@ -279,17 +278,12 @@ class Future { public: Future() = default; explicit Future(std::shared_ptr> state) : state_(std::move(state)) {} - ~Future() = default; // Move type: Future(Future&) = delete; - Future& operator=(Future&) = delete; - Future(Future&& that) : state_(std::move(that.state_)) {} - Future& operator=(Future&& that) - { - state_ = std::move(that.state_); - return *this; - } + Future& operator=(const Future&) = delete; + Future(Future&&) noexcept = default; + Future& operator=(Future&&) noexcept = default; /** Whether the future is valid:. * @@ -333,7 +327,7 @@ public: */ template auto then_no_unwrap(F continuation) -> Future { - typedef decltype(continuation(std::move(*this))) R; + using R = decltype(continuation(std::move(*this))); if (state_ == nullptr) throw std::future_error(std::future_errc::no_state); auto state = std::move(state_); @@ -362,19 +356,16 @@ public: * @exception std::future_error no state is associated with the future */ template - auto then(F continuation) -> typename std::enable_if::value, - Future>::type + auto then(F continuation) -> typename std::enable_if_t::value, + Future> { return this->then_no_unwrap(std::move(continuation)); } /** Attach a continuation to this future (future chaining) */ - template - auto then(F continuation) - -> typename std::enable_if< - is_future::value, - decltype(continuation(std::move(*this))) - >::type + template + auto then(F continuation) -> typename std::enable_if_t::value, + decltype(continuation(std::move(*this)))> { return unwrap_future(this->then_no_unwrap(std::move(continuation))); } @@ -451,9 +442,9 @@ public: // Move type Promise(Promise const&) = delete; Promise& operator=(Promise const&) = delete; - Promise(Promise&& that) : state_(std::move(that.state_)) { std::swap(future_get_, that.future_get_); } + Promise(Promise&& that) noexcept : state_(std::move(that.state_)) { std::swap(future_get_, that.future_get_); } - Promise& operator=(Promise&& that) + Promise& operator=(Promise&& that) noexcept { this->state_ = std::move(that.state_); this->future_get_ = that.future_get_; @@ -489,7 +480,7 @@ public: } private: - std::shared_ptr> state_{new FutureState()}; + std::shared_ptr> state_ = std::make_shared>(); bool future_get_ = false; }; @@ -508,8 +499,8 @@ public: // Move type Promise(Promise const&) = delete; Promise& operator=(Promise const&) = delete; - Promise(Promise&& that) : state_(std::move(that.state_)) { std::swap(future_get_, that.future_get_); } - Promise& operator=(Promise&& that) + Promise(Promise&& that) noexcept : state_(std::move(that.state_)) { std::swap(future_get_, that.future_get_); } + Promise& operator=(Promise&& that) noexcept { this->state_ = std::move(that.state_); this->future_get_ = that.future_get_; @@ -526,13 +517,13 @@ public: future_get_ = true; return Future(state_); } - void set_value() + void set_value() const { if (state_ == nullptr) throw std::future_error(std::future_errc::no_state); state_->set_value(); } - void set_exception(std::exception_ptr exception) + void set_exception(std::exception_ptr exception) const { if (state_ == nullptr) throw std::future_error(std::future_errc::no_state); @@ -540,7 +531,7 @@ public: } private: - std::shared_ptr> state_{new FutureState()}; + std::shared_ptr> state_ = std::make_shared>(); bool future_get_ = false; };