From 4542c9b5320d863afc041a1d9eada0f27e133086 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Thu, 22 Apr 2021 00:02:17 +0200 Subject: [PATCH] Factor Activity::wait() and wait_for() through CRTP. --- include/simgrid/s4u/Activity.hpp | 7 +++++-- include/simgrid/s4u/Comm.hpp | 1 - include/simgrid/s4u/Exec.hpp | 3 +-- include/simgrid/s4u/Io.hpp | 2 -- src/s4u/s4u_Activity.cpp | 12 ++++++++++++ src/s4u/s4u_Comm.cpp | 6 ------ src/s4u/s4u_Exec.cpp | 16 ---------------- src/s4u/s4u_Io.cpp | 16 ---------------- 8 files changed, 18 insertions(+), 45 deletions(-) diff --git a/include/simgrid/s4u/Activity.hpp b/include/simgrid/s4u/Activity.hpp index a1c196844f..3e31a941ff 100644 --- a/include/simgrid/s4u/Activity.hpp +++ b/include/simgrid/s4u/Activity.hpp @@ -108,10 +108,10 @@ public: */ virtual Activity* start() = 0; /** Blocks the current actor until the activity is terminated */ - virtual Activity* wait() = 0; + Activity* wait() { return wait_for(-1.0); } /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n * Raises: timeout exception.*/ - virtual Activity* wait_for(double timeout) = 0; + Activity* wait_for(double timeout); /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n * Raises: timeout exception. */ void wait_until(double time_limit); @@ -221,6 +221,9 @@ public: } AnyActivity* cancel() { return static_cast(Activity::cancel()); } + AnyActivity* wait() { return wait_for(-1.0); } + virtual AnyActivity* wait_for(double timeout) { return static_cast(Activity::wait_for(timeout)); } + #ifndef DOXYGEN /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and diff --git a/include/simgrid/s4u/Comm.hpp b/include/simgrid/s4u/Comm.hpp index 5291c7c75c..9fb2288747 100644 --- a/include/simgrid/s4u/Comm.hpp +++ b/include/simgrid/s4u/Comm.hpp @@ -79,7 +79,6 @@ public: static int test_any(const std::vector* comms); Comm* start() override; - Comm* wait() override; Comm* wait_for(double timeout) override; bool test() override; diff --git a/include/simgrid/s4u/Exec.hpp b/include/simgrid/s4u/Exec.hpp index d4149aabf8..ac5b5f6c53 100644 --- a/include/simgrid/s4u/Exec.hpp +++ b/include/simgrid/s4u/Exec.hpp @@ -50,8 +50,7 @@ public: static ExecPtr init(); Exec* start() override; - Exec* wait() override; - Exec* wait_for(double timeout) override; + /*! take a vector of s4u::ExecPtr and return when one of them is finished. * The return value is the rank of the first finished ExecPtr. */ static int wait_any(std::vector* execs) { return wait_any_for(execs, -1); } diff --git a/include/simgrid/s4u/Io.hpp b/include/simgrid/s4u/Io.hpp index 5fe8f69ff9..5752373847 100644 --- a/include/simgrid/s4u/Io.hpp +++ b/include/simgrid/s4u/Io.hpp @@ -35,8 +35,6 @@ public: static IoPtr init(); Io* start() override; - Io* wait() override; - Io* wait_for(double timeout) override; double get_remaining() const override; sg_size_t get_performed_ioops() const; diff --git a/src/s4u/s4u_Activity.cpp b/src/s4u/s4u_Activity.cpp index 00b3ff49a1..7b32938217 100644 --- a/src/s4u/s4u_Activity.cpp +++ b/src/s4u/s4u_Activity.cpp @@ -8,6 +8,7 @@ #include "simgrid/s4u/Activity.hpp" #include "simgrid/s4u/Engine.hpp" #include "src/kernel/activity/ActivityImpl.hpp" +#include "src/kernel/actor/ActorImpl.hpp" XBT_LOG_EXTERNAL_CATEGORY(s4u); XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_activity, s4u, "S4U activities"); @@ -22,6 +23,17 @@ void Activity::wait_until(double time_limit) wait_for(time_limit - now); } +Activity* Activity::wait_for(double timeout) +{ + if (state_ == State::INITED) + vetoable_start(); + + kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self(); + kernel::actor::simcall_blocking([this, issuer, timeout] { this->get_impl()->wait_for(issuer, timeout); }); + complete(State::FINISHED); + return this; +} + bool Activity::test() { xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING || diff --git a/src/s4u/s4u_Comm.cpp b/src/s4u/s4u_Comm.cpp index 8d747ed1a6..b0c22d8fcd 100644 --- a/src/s4u/s4u_Comm.cpp +++ b/src/s4u/s4u_Comm.cpp @@ -182,12 +182,6 @@ Comm* Comm::start() return this; } -/** @brief Block the calling actor until the communication is finished */ -Comm* Comm::wait() -{ - return this->wait_for(-1); -} - /** @brief Block the calling actor until the communication is finished, or until timeout * * On timeout, an exception is thrown and the communication is invalidated. diff --git a/src/s4u/s4u_Exec.cpp b/src/s4u/s4u_Exec.cpp index 0fa076c358..2ae3ad2f2d 100644 --- a/src/s4u/s4u_Exec.cpp +++ b/src/s4u/s4u_Exec.cpp @@ -54,22 +54,6 @@ Exec* Exec::start() return this; } -Exec* Exec::wait() -{ - return this->wait_for(-1); -} - -Exec* Exec::wait_for(double timeout) -{ - if (state_ == State::INITED) - vetoable_start(); - - kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self(); - kernel::actor::simcall_blocking([this, issuer, timeout] { this->get_impl()->wait_for(issuer, timeout); }); - complete(State::FINISHED); - return this; -} - int Exec::wait_any_for(std::vector* execs, double timeout) { std::vector rexecs(execs->size()); diff --git a/src/s4u/s4u_Io.cpp b/src/s4u/s4u_Io.cpp index 548513b19e..733b481658 100644 --- a/src/s4u/s4u_Io.cpp +++ b/src/s4u/s4u_Io.cpp @@ -45,22 +45,6 @@ Io* Io::start() return this; } -Io* Io::wait() -{ - return this->wait_for(-1); -} - -Io* Io::wait_for(double timeout) -{ - if (state_ == State::INITED) - vetoable_start(); - - kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self(); - kernel::actor::simcall_blocking([this, issuer, timeout] { this->get_impl()->wait_for(issuer, timeout); }); - complete(state_ = State::FINISHED); - return this; -} - IoPtr Io::set_disk(const_sg_disk_t disk) { xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set disk once the Io is started"); -- 2.20.1