From: Frederic Suter Date: Thu, 30 Jan 2020 10:20:23 +0000 (+0100) Subject: use ActivityPtr X-Git-Tag: v3.25~33^2^2~6 X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/4b4bf86d97cde4f9814db6ed79bf99b797204e80?hp=e87e7a6959f53286092e5c160dd865579601ba0e use ActivityPtr --- diff --git a/examples/s4u/exec-dependent/s4u-exec-dependent.cpp b/examples/s4u/exec-dependent/s4u-exec-dependent.cpp index 67b32b986f..7eb71369b6 100644 --- a/examples/s4u/exec-dependent/s4u-exec-dependent.cpp +++ b/examples/s4u/exec-dependent/s4u-exec-dependent.cpp @@ -30,8 +30,8 @@ static void worker() child->set_name("child"); // Create the dependencies by declaring 'child' as a successor of first_parent and second_parent - first_parent->add_successor(child.get()); - second_parent->add_successor(child.get()); + first_parent->add_successor(child); + second_parent->add_successor(child); // Start the activities. first_parent->start(); diff --git a/examples/s4u/io-dependent/s4u-io-dependent.cpp b/examples/s4u/io-dependent/s4u-io-dependent.cpp index ba182261b4..908dd44404 100644 --- a/examples/s4u/io-dependent/s4u-io-dependent.cpp +++ b/examples/s4u/io-dependent/s4u-io-dependent.cpp @@ -28,9 +28,9 @@ static void test() // 'bob_write' is a successor of 'bob_compute' // 'carl_read' is a successor of 'bob_write' // 'carl_compute' is a successor of 'carl_read' - bob_compute->add_successor(bob_write.get()); - bob_write->add_successor(carl_read.get()); - carl_read->add_successor(carl_compute.get()); + bob_compute->add_successor(bob_write); + bob_write->add_successor(carl_read); + carl_read->add_successor(carl_compute); // Start the activities. bob_compute->start(); diff --git a/include/simgrid/s4u/Activity.hpp b/include/simgrid/s4u/Activity.hpp index 67fe59beeb..1c87d16e4d 100644 --- a/include/simgrid/s4u/Activity.hpp +++ b/include/simgrid/s4u/Activity.hpp @@ -63,10 +63,11 @@ public: void set_state(Activity::State state) { state_ = state; } /** Tests whether the given activity is terminated yet. This is a pure function. */ virtual bool test() = 0; + virtual const char* get_cname() = 0; + virtual const std::string& get_name() = 0; /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */ virtual double get_remaining(); - /** Set the [remaining] amount of work that this Activity will entail * * It is forbidden to change the amount of work once the Activity is started */ @@ -75,6 +76,41 @@ public: /** Returns the internal implementation of this Activity */ kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); } + void add_successor(ActivityPtr a) + { + successors_.push_back(a); + a->add_dependency_on(this); + } + void remove_successor() { successors_.pop_back(); } + ActivityPtr get_successor() { return successors_.back(); } + bool has_successors() { return not successors_.empty(); } + + void add_dependency_on(ActivityPtr a) { dependencies_.insert({a}); } + void remove_dependency_on(ActivityPtr a) { dependencies_.erase(a); } + bool has_dependencies() { return not dependencies_.empty(); } + void release_dependencies() + { + while (has_successors()) { + ActivityPtr b = get_successor(); + XBT_CDEBUG(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname()); + b->remove_dependency_on(this); + if (not b->has_dependencies()) { + b->vetoable_start(); + } + remove_successor(); + } + } + + void vetoable_start() + { + state_ = State::STARTING; + if (not has_dependencies()) { + state_ = State::STARTED; + XBT_CDEBUG(s4u_activity, "All dependencies are solved, let's start '%s'", get_cname()); + start(); + } + } + #ifndef DOXYGEN friend void intrusive_ptr_release(Activity* a) { @@ -89,57 +125,17 @@ private: kernel::activity::ActivityImplPtr pimpl_ = nullptr; Activity::State state_ = Activity::State::INITED; double remains_ = 0; + std::vector successors_; + std::set dependencies_; std::atomic_int_fast32_t refcount_{0}; }; template class Activity_T : public Activity { -private: std::string name_ = "unnamed"; std::string tracing_category_ = ""; void* user_data_ = nullptr; - std::vector successors_; - std::set dependencies_; public: - - void add_successor(Activity* a) - { - successors_.push_back(a); - static_cast(a)->add_dependency_on(static_cast(this)); - } - void remove_successor() { successors_.pop_back(); } - Activity* get_successor() { return successors_.back(); } - bool has_successors() { return not successors_.empty(); } - - void add_dependency_on(Activity* a) { dependencies_.insert({a}); } - void remove_dependency_on(Activity* a) { dependencies_.erase(a); } - bool has_dependencies() { return not dependencies_.empty(); } - void release_dependencies() - { - while (has_successors()) { - AnyActivity* b = static_cast(get_successor()); - XBT_CDEBUG(s4u_activity, "Remove a dependency from '%s' on '%s'", static_cast(this)->get_cname(), - b->get_cname()); - b->remove_dependency_on(static_cast(this)); - if (not b->has_dependencies()) { - b->vetoable_start(); - } - remove_successor(); - } - } - - AnyActivity* vetoable_start() - { - set_state(State::STARTING); - if (has_dependencies()) - return static_cast(this); - set_state(State::STARTED); - XBT_CDEBUG(s4u_activity, "All dependencies are solved, let's start '%s'", - static_cast(this)->get_cname()); - static_cast(this)->start(); - return static_cast(this); - } - AnyActivity* set_name(const std::string& name) { xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");