Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Constify pointer and reference local variables in src/s4u/.
[simgrid.git] / src / s4u / s4u_Actor.cpp
index 8099c33..df7dc2d 100644 (file)
@@ -18,7 +18,7 @@
 #include <algorithm>
 #include <sstream>
 
-XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_actor, s4u, "S4U actors");
 
 namespace simgrid {
 namespace s4u {
@@ -28,8 +28,9 @@ xbt::signal<void(Actor const&)> s4u::Actor::on_suspend;
 xbt::signal<void(Actor const&)> s4u::Actor::on_resume;
 xbt::signal<void(Actor const&)> s4u::Actor::on_sleep;
 xbt::signal<void(Actor const&)> s4u::Actor::on_wake_up;
-xbt::signal<void(Actor const&)> s4u::Actor::on_migration_start;
-xbt::signal<void(Actor const&)> s4u::Actor::on_migration_end;
+xbt::signal<void(Actor const&)> s4u::Actor::on_migration_start; // deprecated
+xbt::signal<void(Actor const&)> s4u::Actor::on_migration_end;   // deprecated
+xbt::signal<void(Actor const&, Host const& previous_location)> s4u::Actor::on_host_change;
 xbt::signal<void(Actor const&)> s4u::Actor::on_termination;
 xbt::signal<void(Actor const&)> s4u::Actor::on_destruction;
 
@@ -45,7 +46,7 @@ Actor* Actor::self()
 
 ActorPtr Actor::init(const std::string& name, s4u::Host* host)
 {
-  kernel::actor::ActorImpl* self = SIMIX_process_self();
+  kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
   kernel::actor::ActorImpl* actor =
       kernel::actor::simcall([self, &name, host] { return self->init(name, host).get(); });
   return actor->iface();
@@ -59,7 +60,7 @@ ActorPtr Actor::start(const std::function<void()>& code)
 
 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::function<void()>& code)
 {
-  kernel::actor::ActorImpl* self = SIMIX_process_self();
+  kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
   kernel::actor::ActorImpl* actor =
       kernel::actor::simcall([self, &name, host, &code] { return self->init(name, host)->start(code); });
 
@@ -69,7 +70,7 @@ ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::func
 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::string& function,
                        std::vector<std::string> args)
 {
-  simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
+  const simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
   return create(name, host, factory(std::move(args)));
 }
 
@@ -95,7 +96,7 @@ void Actor::join()
 
 void Actor::join(double timeout)
 {
-  kernel::actor::ActorImpl* issuer = SIMIX_process_self();
+  kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
   kernel::actor::ActorImpl* target = pimpl_;
   kernel::actor::simcall_blocking<void>([issuer, target, timeout] {
     if (target->finished_) {
@@ -122,12 +123,22 @@ void Actor::set_auto_restart(bool autorestart)
 
 void Actor::on_exit(const std::function<void(bool /*failed*/)>& fun) const
 {
-  kernel::actor::simcall([this, &fun] { SIMIX_process_on_exit(pimpl_, fun); });
+  kernel::actor::simcall([this, &fun] { pimpl_->on_exit->emplace_back(fun); });
 }
 
-void Actor::migrate(Host* new_host)
+void Actor::set_host(Host* new_host)
 {
-  s4u::Actor::on_migration_start(*this);
+  if (s4u::Actor::on_migration_start.get_slot_count() > 0) { // XBT_ATTRIB_DEPRECATED_v329
+    static bool already_warned = false;
+    if (not already_warned) {
+      XBT_INFO("Please use s4u::Actor::on_host_change instead of s4u::Actor::on_migration_start. This will be removed "
+               "in v3.29");
+      already_warned = true;
+    }
+    s4u::Actor::on_migration_start(*this);
+  }
+
+  const s4u::Host* previous_location = get_host();
 
   kernel::actor::simcall([this, new_host]() {
     if (pimpl_->waiting_synchro != nullptr) {
@@ -141,7 +152,17 @@ void Actor::migrate(Host* new_host)
     this->pimpl_->set_host(new_host);
   });
 
-  s4u::Actor::on_migration_end(*this);
+  if (s4u::Actor::on_migration_end.get_slot_count() > 0) { // XBT_ATTRIB_DEPRECATED_v329
+    static bool already_warned = false;
+    if (not already_warned) {
+      XBT_INFO("Please use s4u::Actor::on_host_change instead of s4u::Actor::on_migration_end. This will be removed in "
+               "v3.29");
+      already_warned = true;
+    }
+    s4u::Actor::on_migration_end(*this);
+  }
+
+  s4u::Actor::on_host_change(*this, *previous_location);
 }
 
 s4u::Host* Actor::get_host() const
@@ -181,7 +202,7 @@ aid_t Actor::get_ppid() const
 
 void Actor::suspend()
 {
-  kernel::actor::ActorImpl* issuer = SIMIX_process_self();
+  kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
   kernel::actor::ActorImpl* target = pimpl_;
   s4u::Actor::on_suspend(*this);
   kernel::actor::simcall_blocking<void>([issuer, target]() {
@@ -217,7 +238,7 @@ double Actor::get_kill_time()
 
 void Actor::kill()
 {
-  kernel::actor::ActorImpl* self = SIMIX_process_self();
+  kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
   kernel::actor::simcall([this, self] { self->kill(pimpl_); });
 }
 
@@ -234,7 +255,7 @@ ActorPtr Actor::by_pid(aid_t pid)
 
 void Actor::kill_all()
 {
-  kernel::actor::ActorImpl* self = SIMIX_process_self();
+  kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
   kernel::actor::simcall([self] { self->kill_all(); });
 }
 
@@ -282,7 +303,7 @@ void sleep_for(double duration)
   xbt_assert(std::isfinite(duration), "duration is not finite!");
 
   if (duration > 0) {
-    kernel::actor::ActorImpl* issuer = SIMIX_process_self();
+    kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
     Actor::on_sleep(*issuer->ciface());
 
     kernel::actor::simcall_blocking<void>([issuer, duration]() {
@@ -324,11 +345,22 @@ void execute(double flops, double priority)
 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
                       const std::vector<double>& bytes_amounts)
 {
-  parallel_execute(hosts, flops_amounts, bytes_amounts, -1);
+  exec_init(hosts, flops_amounts, bytes_amounts)->wait();
 }
 
 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
-                      const std::vector<double>& bytes_amounts, double timeout)
+                      const std::vector<double>& bytes_amounts, double timeout) // XBT_ATTRIB_DEPRECATED_v329
+{
+  exec_init(hosts, flops_amounts, bytes_amounts)->wait_for(timeout);
+}
+
+ExecPtr exec_init(double flops_amount)
+{
+  return ExecPtr(new ExecSeq(get_host(), flops_amount));
+}
+
+ExecPtr exec_init(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
+                  const std::vector<double>& bytes_amounts)
 {
   xbt_assert(hosts.size() > 0, "Your parallel executions must span over at least one host.");
   xbt_assert(hosts.size() == flops_amounts.size() || flops_amounts.empty(),
@@ -350,17 +382,6 @@ void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<do
   xbt_assert(std::all_of(bytes_amounts.begin(), bytes_amounts.end(), [](double elm) { return std::isfinite(elm); }),
              "flops_amounts comprises infinite values!");
 
-  exec_init(hosts, flops_amounts, bytes_amounts)->set_timeout(timeout)->wait();
-}
-
-ExecPtr exec_init(double flops_amount)
-{
-  return ExecPtr(new ExecSeq(get_host(), flops_amount));
-}
-
-ExecPtr exec_init(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
-                  const std::vector<double>& bytes_amounts)
-{
   return ExecPtr(new ExecPar(hosts, flops_amounts, bytes_amounts));
 }
 
@@ -373,61 +394,65 @@ ExecPtr exec_async(double flops)
 
 aid_t get_pid()
 {
-  return SIMIX_process_self()->get_pid();
+  return simgrid::kernel::actor::ActorImpl::self()->get_pid();
 }
 
 aid_t get_ppid()
 {
-  return SIMIX_process_self()->get_ppid();
+  return simgrid::kernel::actor::ActorImpl::self()->get_ppid();
 }
 
 std::string get_name()
 {
-  return SIMIX_process_self()->get_name();
+  return simgrid::kernel::actor::ActorImpl::self()->get_name();
 }
 
 const char* get_cname()
 {
-  return SIMIX_process_self()->get_cname();
+  return simgrid::kernel::actor::ActorImpl::self()->get_cname();
 }
 
 Host* get_host()
 {
-  return SIMIX_process_self()->get_host();
+  return simgrid::kernel::actor::ActorImpl::self()->get_host();
 }
 
 void suspend()
 {
-  kernel::actor::ActorImpl* self = SIMIX_process_self();
+  kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self();
   s4u::Actor::on_suspend(*self->ciface());
   kernel::actor::simcall_blocking<void>([self] { self->suspend(); });
 }
 
 void resume()
 {
-  kernel::actor::ActorImpl* self = SIMIX_process_self();
+  kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self();
   kernel::actor::simcall([self] { self->resume(); });
   Actor::on_resume(*self->ciface());
 }
 
 void exit()
 {
-  kernel::actor::ActorImpl* self = SIMIX_process_self();
+  kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self();
   simgrid::kernel::actor::simcall([self] { self->exit(); });
 }
 
 void on_exit(const std::function<void(bool)>& fun)
 {
-  SIMIX_process_self()->iface()->on_exit(fun);
+  simgrid::kernel::actor::ActorImpl::self()->iface()->on_exit(fun);
 }
 
 /** @brief Moves the current actor to another host
  *
  * @see simgrid::s4u::Actor::migrate() for more information
  */
-void migrate(Host* new_host)
+void set_host(Host* new_host)
 {
-  SIMIX_process_self()->iface()->migrate(new_host);
+  simgrid::kernel::actor::ActorImpl::self()->iface()->set_host(new_host);
+}
+void migrate(Host* new_host) // deprecated
+{
+  set_host(new_host);
 }
 
 } // namespace this_actor
@@ -594,9 +619,13 @@ void sg_actor_daemonize(sg_actor_t actor)
  *
  * This function changes the value of the #sg_host_t on  which @a actor is running.
  */
-void sg_actor_migrate(sg_actor_t process, sg_host_t host)
+void sg_actor_set_host(sg_actor_t actor, sg_host_t host)
+{
+  actor->set_host(host);
+}
+void sg_actor_migrate(sg_actor_t process, sg_host_t host) // deprecated
 {
-  process->migrate(host);
+  process->set_host(host);
 }
 
 /** @ingroup m_actor_management