From f740c70bdd98735830f48bdd757135d3eb4825d0 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Mon, 22 May 2023 17:02:54 +0200 Subject: [PATCH] Add instance signals for all Actor signals --- ChangeLog | 4 +++ docs/source/Plugins.rst | 6 +++++ docs/source/app_s4u.rst | 7 ++++++ include/simgrid/s4u/Actor.hpp | 45 +++++++++++++++++++++++++++------- src/kernel/actor/ActorImpl.cpp | 5 +++- src/s4u/s4u_Actor.cpp | 6 +++++ 6 files changed, 63 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3474305c2a..20dfd23841 100644 --- a/ChangeLog +++ b/ChangeLog @@ -20,6 +20,10 @@ S4U: possible. - Allow to set a concurrency limit on disks and hosts, as it was already the case for links. - Rename Link::get_usage() to Link::get_load() for consistency with Host:: + - Every signal now come with a static version that is invoked for every object of that class, + and an instance version that is invoked for this specific object only. For example, + s4u::Actor::on_suspend_cb() adds a callback that is invoked for the suspend of any actor while + s4u::Actor::on_this_suspend_cb() adds a callback for this specific actor only. New S4U plugins: - Operation: They are designed to represent workflows, i.e, graphs of repeatable Activities. diff --git a/docs/source/Plugins.rst b/docs/source/Plugins.rst index 2fae39481b..a173215837 100644 --- a/docs/source/Plugins.rst +++ b/docs/source/Plugins.rst @@ -71,11 +71,17 @@ Partial list of existing signals in s4u: - :cpp:func:`Actor::on_creation ` :cpp:func:`Actor::on_suspend ` + :cpp:func:`Actor::on_this_suspend ` :cpp:func:`Actor::on_resume ` + :cpp:func:`Actor::on_this_resume ` :cpp:func:`Actor::on_sleep ` + :cpp:func:`Actor::on_this_sleep ` :cpp:func:`Actor::on_wake_up ` + :cpp:func:`Actor::on_this_wake_up ` :cpp:func:`Actor::on_host_change ` + :cpp:func:`Actor::on_this_host_change ` :cpp:func:`Actor::on_termination ` + :cpp:func:`Actor::on_this_termination ` :cpp:func:`Actor::on_destruction ` - :cpp:func:`Comm::on_send ` :cpp:func:`Comm::on_recv ` diff --git a/docs/source/app_s4u.rst b/docs/source/app_s4u.rst index 7d3d6b0767..b768946bc5 100644 --- a/docs/source/app_s4u.rst +++ b/docs/source/app_s4u.rst @@ -612,12 +612,19 @@ Signals .. doxygenfunction:: simgrid::s4u::Actor::on_creation_cb .. doxygenfunction:: simgrid::s4u::Actor::on_suspend_cb + .. doxygenfunction:: simgrid::s4u::Actor::on_this_suspend_cb .. doxygenfunction:: simgrid::s4u::Actor::on_host_change_cb + .. doxygenfunction:: simgrid::s4u::Actor::on_this_host_change_cb .. doxygenfunction:: simgrid::s4u::Actor::on_resume_cb + .. doxygenfunction:: simgrid::s4u::Actor::on_this_resume_cb .. doxygenfunction:: simgrid::s4u::Actor::on_sleep_cb + .. doxygenfunction:: simgrid::s4u::Actor::on_this_sleep_cb .. doxygenfunction:: simgrid::s4u::Actor::on_wake_up_cb + .. doxygenfunction:: simgrid::s4u::Actor::on_this_wake_up_cb .. doxygenfunction:: simgrid::s4u::Actor::on_termination_cb + .. doxygenfunction:: simgrid::s4u::Actor::on_this_termination_cb .. doxygenfunction:: simgrid::s4u::Actor::on_destruction_cb + .. doxygenfunction:: simgrid::s4u::Actor::on_this_destruction_cb .. _API_s4u_this_actor: diff --git a/include/simgrid/s4u/Actor.hpp b/include/simgrid/s4u/Actor.hpp index acdcafe1c8..da3f82ef38 100644 --- a/include/simgrid/s4u/Actor.hpp +++ b/include/simgrid/s4u/Actor.hpp @@ -215,31 +215,51 @@ public: private: static xbt::signal on_creation; static xbt::signal on_suspend; + xbt::signal on_this_suspend; static xbt::signal on_resume; + xbt::signal on_this_resume; static xbt::signal on_sleep; + xbt::signal on_this_sleep; static xbt::signal on_wake_up; + xbt::signal on_this_wake_up; static xbt::signal on_host_change; + xbt::signal on_this_host_change; static xbt::signal on_termination; + xbt::signal on_this_termination; static xbt::signal on_destruction; + xbt::signal on_this_destruction; public: /** Add a callback fired when a new actor has been created **/ static void on_creation_cb(const std::function& cb) { on_creation.connect(cb); } - /** Add a callback fired when an actor has been suspended**/ + /** Add a callback fired when any actor is suspended**/ static void on_suspend_cb(const std::function& cb) { on_suspend.connect(cb); } - /** Add a callback fired when an actor has been resumed **/ + /** Add a callback fired when this specific actor is suspended**/ + void on_this_suspend_cb(const std::function& cb) { on_this_suspend.connect(cb); } + /** Add a callback fired when any actor is resumed **/ static void on_resume_cb(const std::function& cb) { on_resume.connect(cb); } - /** Add a callback fired when an actor starts sleeping **/ + /** Add a callback fired when any actor is resumed **/ + void on_this_resume_cb(const std::function& cb) { on_this_resume.connect(cb); } + /** Add a callback fired when any actor starts sleeping **/ static void on_sleep_cb(const std::function& cb) { on_sleep.connect(cb); } - /** Add a callback fired when an actor wakes up from a sleep **/ + /** Add a callback fired when this specific actor starts sleeping **/ + void on_this_sleep_cb(const std::function& cb) { on_this_sleep.connect(cb); } + /** Add a callback fired when any actor wakes up from a sleep **/ static void on_wake_up_cb(const std::function& cb) { on_wake_up.connect(cb); } - /** Add a callback fired when an actor is has been migrated to another host **/ + /** Add a callback fired when this specific actor wakes up from a sleep **/ + void on_this_wake_up_cb(const std::function& cb) { on_this_wake_up.connect(cb); } + /** Add a callback fired when any actor is has been migrated to another host **/ static void on_host_change_cb(const std::function& cb) { on_host_change.connect(cb); } + /** Add a callback fired when this specific actor is has been migrated to another host **/ + void on_this_host_change_cb(const std::function& cb) + { + on_this_host_change.connect(cb); + } - /** Add a callback fired when an actor terminates its code. + /** Add a callback fired when any actor terminates its code. * @beginrst * The actor may continue to exist if it is still referenced in the simulation, but it's not active anymore. * If you want to free extra data when the actor's destructor is called, use :cpp:func:`Actor::on_destruction_cb`. @@ -247,11 +267,18 @@ public: * @endrst */ static void on_termination_cb(const std::function& cb) { on_termination.connect(cb); } + /** Add a callback fired when this specific actor terminates its code. + * @beginrst + * The actor may continue to exist if it is still referenced in the simulation, but it's not active anymore. + * If you want to free extra data when the actor's destructor is called, use :cpp:func:`Actor::on_this_destruction_cb`. + * @endrst + */ + void on_this_termination_cb(const std::function& cb) { on_this_termination.connect(cb); } /** Add a callback fired when an actor is about to disappear (its destructor was called). - * This signal is fired for any destructed actor, which is mostly useful when designing plugins and extensions. - * If you want to react to the end of the actor's code, use Actor::on_termination instead. - * If you want to register to the termination of a given actor, use this_actor::on_exit() instead.*/ + * This signal is fired for any destructed actor, which is mostly useful when designing plugins and extensions. */ static void on_destruction_cb(const std::function& cb) { on_destruction.connect(cb); } + /** Add a callback fired when this specific actor is about to disappear (its destructor was called). */ + void on_this_destruction_cb(const std::function& cb) { on_this_destruction.connect(cb); } /** Create an actor from a @c std::function. * If the actor is restarted, it gets a fresh copy of the function. diff --git a/src/kernel/actor/ActorImpl.cpp b/src/kernel/actor/ActorImpl.cpp index c2e2b489c2..4f5fb245c7 100644 --- a/src/kernel/actor/ActorImpl.cpp +++ b/src/kernel/actor/ActorImpl.cpp @@ -44,8 +44,10 @@ ActorImpl::ActorImpl(const std::string& name, s4u::Host* host, aid_t ppid) ActorImpl::~ActorImpl() { - if (EngineImpl::has_instance() && not EngineImpl::get_instance()->is_maestro(this)) + if (EngineImpl::has_instance() && not EngineImpl::get_instance()->is_maestro(this)) { s4u::Actor::on_destruction(*get_ciface()); + get_ciface()->on_this_destruction(*get_ciface()); + } } /* Become an actor in the simulation @@ -129,6 +131,7 @@ void ActorImpl::cleanup_from_kernel() undaemonize(); s4u::Actor::on_termination(*get_ciface()); + get_ciface()->on_this_termination(*get_ciface()); while (not mailboxes_.empty()) mailboxes_.back()->set_receiver(nullptr); diff --git a/src/s4u/s4u_Actor.cpp b/src/s4u/s4u_Actor.cpp index 3e84903f38..e60f73d5fa 100644 --- a/src/s4u/s4u_Actor.cpp +++ b/src/s4u/s4u_Actor.cpp @@ -164,6 +164,7 @@ void Actor::set_host(Host* new_host) }); s4u::Actor::on_host_change(*this, *previous_location); + s4u::Actor::on_this_host_change(*this, *previous_location); } s4u::Host* Actor::get_host() const @@ -213,6 +214,7 @@ void Actor::suspend() kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self(); kernel::actor::ActorImpl* target = pimpl_; s4u::Actor::on_suspend(*this); + s4u::Actor::on_this_suspend(*this); kernel::actor::simcall_blocking([issuer, target]() { target->suspend(); if (target != issuer) { @@ -226,6 +228,7 @@ void Actor::resume() { kernel::actor::simcall_answered([this] { pimpl_->resume(); }); s4u::Actor::on_resume(*this); + s4u::Actor::on_this_resume(*this); } bool Actor::is_suspended() const @@ -326,6 +329,7 @@ void sleep_for(double duration) kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self(); Actor::on_sleep(*issuer->get_ciface()); + issuer->get_ciface()->on_this_sleep(*issuer->get_ciface()); kernel::actor::simcall_blocking([issuer, duration]() { if (MC_is_active() || MC_record_replay_is_active()) { @@ -338,6 +342,7 @@ void sleep_for(double duration) }); Actor::on_wake_up(*issuer->get_ciface()); + issuer->get_ciface()->on_this_wake_up(*issuer->get_ciface()); } void yield() @@ -442,6 +447,7 @@ void suspend() { kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self(); s4u::Actor::on_suspend(*self->get_ciface()); + self->get_ciface()->on_this_suspend(*self->get_ciface()); kernel::actor::simcall_blocking([self] { self->suspend(); }); } -- 2.20.1