Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
deprecate SIMIX_process_{a,de}tach
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Fri, 22 Feb 2019 13:14:07 +0000 (14:14 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Fri, 22 Feb 2019 13:16:42 +0000 (14:16 +0100)
* replaced by ActorImpl::{a,de}tach
* add sg_actor_{a,de}tach to the C API of actors
* MSG_process_{a,de}tach go to msg_legacy

include/simgrid/actor.h
include/simgrid/simix.h
src/msg/msg_legacy.cpp
src/msg/msg_process.cpp
src/s4u/s4u_Actor.cpp
src/simix/ActorImpl.cpp
src/simix/ActorImpl.hpp

index 01e8e07..4dddd46 100644 (file)
@@ -40,6 +40,8 @@ XBT_PUBLIC void sg_actor_kill_all();
 XBT_PUBLIC void sg_actor_set_kill_time(sg_actor_t actor, double kill_time);
 XBT_PUBLIC void sg_actor_yield();
 XBT_PUBLIC void sg_actor_sleep_for(double duration);
+XBT_PUBLIC sg_actor_t sg_actor_attach(const char* name, void* data, sg_host_t host, xbt_dict_t properties);
+XBT_PUBLIC void sg_actor_detach();
 SG_END_DECL()
 
 #endif /* INCLUDE_SIMGRID_ACTOR_H_ */
index fb1aa18..311090b 100644 (file)
@@ -129,12 +129,13 @@ XBT_PUBLIC void SIMIX_launch_application(const std::string& file);
  */
 
 #ifdef __cplusplus
-XBT_PUBLIC smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
-                                            std::unordered_map<std::string, std::string>* properties,
-                                            smx_actor_t parent_process);
+XBT_ATTRIB_DEPRECATED_v325("Please use ActorImpl::attach() or sg_actor_attach()") XBT_PUBLIC smx_actor_t
+    SIMIX_process_attach(const char* name, void* data, const char* hostname,
+                         std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process);
 #endif
 SG_BEGIN_DECL()
-XBT_PUBLIC void SIMIX_process_detach();
+XBT_ATTRIB_DEPRECATED_v325("Please use ActorImpl::detach() or sg_actor_detach()") XBT_PUBLIC
+    void SIMIX_process_detach();
 SG_END_DECL()
 
 /********************************* Process ************************************/
index 578f862..fd4562c 100644 (file)
@@ -134,6 +134,17 @@ msg_error_t MSG_process_sleep(double duration)
     return MSG_HOST_FAILURE;
   }
 }
+
+msg_process_t MSG_process_attach(const char* name, void* data, msg_host_t host, xbt_dict_t properties)
+{
+  return sg_actor_attach(name, data, host, properties);
+}
+
+void MSG_process_detach()
+{
+  sg_actor_detach();
+}
+
 /* ************************** NetZones *************************** */
 sg_netzone_t MSG_zone_get_root()
 {
index da46838..83fbcdb 100644 (file)
@@ -98,43 +98,6 @@ msg_process_t MSG_process_create_with_environment(const char *name, xbt_main_fun
   return process->ciface();
 }
 
-/* Become a process in the simulation
- *
- * Currently this can only be called by the main thread (once) and only work with some thread factories
- * (currently ThreadContextFactory).
- *
- * In the future, it might be extended in order to attach other threads created by a third party library.
- */
-msg_process_t MSG_process_attach(const char *name, void *data, msg_host_t host, xbt_dict_t properties)
-{
-  xbt_assert(host != nullptr, "Invalid parameters: host and code params must not be nullptr");
-  std::unordered_map<std::string, std::string> props;
-  xbt_dict_cursor_t cursor = nullptr;
-  char* key;
-  char* value;
-  xbt_dict_foreach (properties, cursor, key, value)
-    props[key] = value;
-  xbt_dict_free(&properties);
-
-  /* Let's create the process: SIMIX may decide to start it right now, even before returning the flow control to us */
-  smx_actor_t process = SIMIX_process_attach(name, data, host->get_cname(), &props, nullptr);
-  if (not process)
-    xbt_die("Could not attach");
-  MSG_process_yield();
-  return process->ciface();
-}
-
-/** @brief Detach a process attached with `MSG_process_attach()`
- *
- *  This is called when the current process has finished its job.
- *  Used in the main thread, it waits for the simulation to finish before  returning. When it returns, the other
- *  simulated processes and the maestro are destroyed.
- */
-void MSG_process_detach()
-{
-  SIMIX_process_detach();
-}
-
 /** @brief Returns the user data of a process.
  *
  * This function checks whether @a process is a valid pointer and returns the user data associated to this process.
index 7619d44..a98818a 100644 (file)
@@ -649,3 +649,27 @@ void sg_actor_sleep_for(double duration)
 {
   simgrid::s4u::this_actor::sleep_for(duration);
 }
+
+sg_actor_t sg_actor_attach(const char* name, void* data, sg_host_t host, xbt_dict_t properties)
+{
+  xbt_assert(host != nullptr, "Invalid parameters: host and code params must not be nullptr");
+  std::unordered_map<std::string, std::string> props;
+  xbt_dict_cursor_t cursor = nullptr;
+  char* key;
+  char* value;
+  xbt_dict_foreach (properties, cursor, key, value)
+    props[key] = value;
+  xbt_dict_free(&properties);
+
+  /* Let's create the process: SIMIX may decide to start it right now, even before returning the flow control to us */
+  smx_actor_t actor = simgrid::kernel::actor::ActorImpl::attach(name, data, host, &props).get();
+  if (not actor)
+    xbt_die("Could not attach");
+  actor->yield();
+  return actor->ciface();
+}
+
+void sg_actor_detach()
+{
+  simgrid::kernel::actor::ActorImpl::detach();
+}
index e31c9c5..e0a2043 100644 (file)
@@ -62,6 +62,74 @@ ActorImpl::~ActorImpl()
 {
   delete this->context_;
 }
+/* Become an actor in the simulation
+ *
+ * Currently this can only be called by the main thread (once) and only work with some thread factories
+ * (currently ThreadContextFactory).
+ *
+ * In the future, it might be extended in order to attach other threads created by a third party library.
+ */
+
+ActorImplPtr ActorImpl::attach(std::string name, void* data, s4u::Host* host,
+                               std::unordered_map<std::string, std::string>* properties)
+{
+  // This is mostly a copy/paste from create(), it'd be nice to share some code between those two functions.
+
+  XBT_DEBUG("Attach process %s on host '%s'", name.c_str(), host->get_cname());
+
+  if (not host->is_on()) {
+    XBT_WARN("Cannot launch process '%s' on failed host '%s'", name.c_str(), host->get_cname());
+    return nullptr;
+  }
+
+  ActorImpl* actor = new ActorImpl(xbt::string(name), host);
+  /* Actor data */
+  actor->set_user_data(data);
+  actor->code = nullptr;
+
+  XBT_VERB("Create context %s", actor->get_cname());
+  xbt_assert(simix_global != nullptr, "simix is not initialized, please call MSG_init first");
+  actor->context_ = simix_global->context_factory->attach(actor);
+
+  /* Add properties */
+  if (properties != nullptr)
+    for (auto const& kv : *properties)
+      actor->set_property(kv.first, kv.second);
+
+  /* Add the process to it's host process list */
+  host->pimpl_->process_list_.push_back(*actor);
+
+  /* Now insert it in the global process list and in the process to run list */
+  simix_global->process_list[actor->get_pid()] = actor;
+  XBT_DEBUG("Inserting [%p] %s(%s) in the to_run list", actor, actor->get_cname(), host->get_cname());
+  simix_global->actors_to_run.push_back(actor);
+  intrusive_ptr_add_ref(actor);
+
+  auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(actor->context_);
+  xbt_assert(nullptr != context, "Not a suitable context");
+  context->attach_start();
+
+  /* The on_creation() signal must be delayed until there, where the pid and everything is set */
+  simgrid::s4u::ActorPtr tmp = actor->iface(); // Passing this directly to on_creation will lead to crashes
+  simgrid::s4u::Actor::on_creation(tmp);
+
+  return ActorImplPtr(actor);
+}
+/** @brief Detach an actor attached with `attach()`
+ *
+ *  This is called when the current actor has finished its job.
+ *  Used in the main thread, it waits for the simulation to finish before returning. When it returns, the other
+ *  simulated actors and the maestro are destroyed.
+ */
+void ActorImpl::detach()
+{
+  auto* context = dynamic_cast<context::AttachContext*>(context::Context::self());
+  if (context == nullptr)
+    xbt_die("Not a suitable context");
+
+  context->get_actor()->cleanup();
+  context->attach_stop();
+}
 
 void ActorImpl::cleanup()
 {
@@ -439,68 +507,18 @@ void create_maestro(simix::ActorCode code)
 } // namespace kernel
 }
 
-smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
-                                 std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
+void SIMIX_process_detach()
 {
-  // This is mostly a copy/paste from SIMIX_process_new(),
-  // it'd be nice to share some code between those two functions.
-
-  sg_host_t host = sg_host_by_name(hostname);
-  XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
-
-  if (not host->is_on()) {
-    XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
-    return nullptr;
-  }
-
-  smx_actor_t actor = new simgrid::kernel::actor::ActorImpl(simgrid::xbt::string(name), host);
-  /* Actor data */
-  actor->set_user_data(data);
-  actor->code = nullptr;
-
-  if (parent_process != nullptr)
-    actor->set_ppid(parent_process->get_pid());
-
-  XBT_VERB("Create context %s", actor->get_cname());
-  xbt_assert(simix_global != nullptr, "simix is not initialized, please call MSG_init first");
-  actor->context_ = simix_global->context_factory->attach(actor);
-
-  /* Add properties */
-  if (properties != nullptr)
-    for (auto const& kv : *properties)
-      actor->set_property(kv.first, kv.second);
-
-  /* Add the process to it's host process list */
-  host->pimpl_->process_list_.push_back(*actor);
-
-  /* Now insert it in the global process list and in the process to run list */
-  simix_global->process_list[actor->get_pid()] = actor;
-  XBT_DEBUG("Inserting [%p] %s(%s) in the to_run list", actor, actor->get_cname(), host->get_cname());
-  simix_global->actors_to_run.push_back(actor);
-  intrusive_ptr_add_ref(actor);
-
-  auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(actor->context_);
-  xbt_assert(nullptr != context, "Not a suitable context");
-  context->attach_start();
-
-  /* The on_creation() signal must be delayed until there, where the pid and everything is set */
-  simgrid::s4u::ActorPtr tmp = actor->iface(); // Passing this directly to on_creation will lead to crashes
-  simgrid::s4u::Actor::on_creation(tmp);
-
-  return actor;
+  simgrid::kernel::actor::ActorImpl::detach();
 }
 
-void SIMIX_process_detach()
+smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
+                                 std::unordered_map<std::string, std::string>* properties,
+                                 smx_actor_t /*parent_process*/)
 {
-  auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(simgrid::kernel::context::Context::self());
-  if (context == nullptr)
-    xbt_die("Not a suitable context");
-
-  context->get_actor()->cleanup();
-  context->attach_stop();
+  return simgrid::kernel::actor::ActorImpl::attach(name, data, sg_host_by_name(hostname), properties).get();
 }
 
-
 /** @deprecated When this function gets removed, also remove the xbt_ex class, that is only there to help users to
  * transition */
 void SIMIX_process_throw(smx_actor_t actor, xbt_errcat_t cat, int value, const char* msg)
index 2ca5dc3..0d7ea73 100644 (file)
@@ -105,6 +105,9 @@ public:
 
   static ActorImplPtr create(std::string name, simix::ActorCode code, void* data, s4u::Host* host,
                              std::unordered_map<std::string, std::string>* properties, ActorImpl* parent_actor);
+  static ActorImplPtr attach(std::string name, void* data, s4u::Host* host,
+                             std::unordered_map<std::string, std::string>* properties);
+  static void detach();
   void cleanup();
   void exit();
   void kill(ActorImpl* actor);