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.
{
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()
{
} // 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)