Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add sg_actor_exit and sg_actor_on_exit
[simgrid.git] / src / s4u / s4u_Actor.cpp
index a23ed083b65007d6dc6c7fc4dd371365585088a2..ac4af83d9958fe90dd48f7d62a2200eea2f4c9c0 100644 (file)
@@ -11,6 +11,7 @@
 #include "simgrid/s4u/Host.hpp"
 #include "simgrid/s4u/VirtualMachine.hpp"
 #include "src/include/mc/mc.h"
+#include "src/kernel/EngineImpl.hpp"
 #include "src/kernel/activity/ExecImpl.hpp"
 #include "src/mc/mc_replay.hpp"
 #include "src/surf/HostImpl.hpp"
@@ -21,6 +22,9 @@
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_actor, s4u, "S4U actors");
 
 namespace simgrid {
+
+template class xbt::Extendable<s4u::Actor>;
+
 namespace s4u {
 
 xbt::signal<void(Actor&)> s4u::Actor::on_creation;
@@ -70,7 +74,8 @@ 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)
 {
-  const simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
+  const simgrid::kernel::actor::ActorCodeFactory& factory =
+      simgrid::kernel::EngineImpl::get_instance()->get_function(function);
   return create(name, host, factory(std::move(args)));
 }
 
@@ -103,7 +108,7 @@ void Actor::join(double timeout)
       // The joined process is already finished, just wake up the issuer right away
       issuer->simcall_answer();
     } else {
-      smx_activity_t sync = issuer->join(target, timeout);
+      kernel::activity::ActivityImplPtr sync = issuer->join(target, timeout);
       sync->register_simcall(&issuer->simcall);
     }
   });
@@ -312,7 +317,7 @@ void sleep_for(double duration)
         issuer->simcall_answer();
         return;
       }
-      smx_activity_t sync = issuer->sleep(duration);
+      kernel::activity::ActivityImplPtr sync = issuer->sleep(duration);
       sync->register_simcall(&issuer->simcall);
     });
 
@@ -424,13 +429,6 @@ void suspend()
   kernel::actor::simcall_blocking<void>([self] { self->suspend(); });
 }
 
-void resume()
-{
-  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 = simgrid::kernel::actor::ActorImpl::self();
@@ -466,19 +464,25 @@ sg_actor_t sg_actor_init(const char* name, sg_host_t host)
   return simgrid::s4u::Actor::init(name, host).get();
 }
 
-void sg_actor_start(sg_actor_t actor, xbt_main_func_t code, int argc, char** argv)
+void sg_actor_start(sg_actor_t actor, xbt_main_func_t code, int argc, const char* const* argv)
 {
-  simgrid::simix::ActorCode function;
+  simgrid::kernel::actor::ActorCode function;
   if (code)
-    function = simgrid::xbt::wrap_main(code, argc, static_cast<const char* const*>(argv));
+    function = simgrid::xbt::wrap_main(code, argc, argv);
   actor->start(std::move(function));
 }
 
+void sg_actor_exit()
+{
+  simgrid::s4u::this_actor::exit();
+}
+
 /** @ingroup m_actor_management
  * @brief Returns the process ID of @a actor.
  *
  * This function checks whether @a actor is a valid pointer and return its PID (or 0 in case of problem).
  */
+
 aid_t sg_actor_get_PID(const_sg_actor_t actor)
 {
   /* Do not raise an exception here: this function is called by the logs
@@ -755,3 +759,11 @@ void sg_actor_data_set(sg_actor_t actor, void* userdata)
 {
   actor->set_data(userdata);
 }
+/** @brief Add a function to the list of "on_exit" functions for the current process.
+ *  The on_exit functions are the functions executed when your process is killed.
+ *  You should use them to free the data used by your process.
+ */
+void sg_actor_on_exit(int_f_int_pvoid_t fun, void* data)
+{
+  simgrid::s4u::this_actor::on_exit([fun, data](bool failed) { fun(failed ? 1 /*FAILURE*/ : 0 /*SUCCESS*/, data); });
+}