From 8a0edc1a3b6e911b8e6b8db2cb2c94b751fae29a Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Fri, 22 Feb 2019 14:14:07 +0100 Subject: [PATCH] deprecate SIMIX_process_{a,de}tach * 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 | 2 + include/simgrid/simix.h | 9 +-- src/msg/msg_legacy.cpp | 11 ++++ src/msg/msg_process.cpp | 37 ------------ src/s4u/s4u_Actor.cpp | 24 ++++++++ src/simix/ActorImpl.cpp | 130 +++++++++++++++++++++++----------------- src/simix/ActorImpl.hpp | 3 + 7 files changed, 119 insertions(+), 97 deletions(-) diff --git a/include/simgrid/actor.h b/include/simgrid/actor.h index 01e8e07eea..4dddd4620c 100644 --- a/include/simgrid/actor.h +++ b/include/simgrid/actor.h @@ -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_ */ diff --git a/include/simgrid/simix.h b/include/simgrid/simix.h index fb1aa18073..311090b55c 100644 --- a/include/simgrid/simix.h +++ b/include/simgrid/simix.h @@ -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* 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* 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 ************************************/ diff --git a/src/msg/msg_legacy.cpp b/src/msg/msg_legacy.cpp index 578f862cf2..fd4562c206 100644 --- a/src/msg/msg_legacy.cpp +++ b/src/msg/msg_legacy.cpp @@ -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() { diff --git a/src/msg/msg_process.cpp b/src/msg/msg_process.cpp index da46838777..83fbcdbb21 100644 --- a/src/msg/msg_process.cpp +++ b/src/msg/msg_process.cpp @@ -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 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. diff --git a/src/s4u/s4u_Actor.cpp b/src/s4u/s4u_Actor.cpp index 7619d44bc6..a98818afe4 100644 --- a/src/s4u/s4u_Actor.cpp +++ b/src/s4u/s4u_Actor.cpp @@ -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 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(); +} diff --git a/src/simix/ActorImpl.cpp b/src/simix/ActorImpl.cpp index e31c9c5e07..e0a2043b1d 100644 --- a/src/simix/ActorImpl.cpp +++ b/src/simix/ActorImpl.cpp @@ -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* 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(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::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* 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(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* properties, + smx_actor_t /*parent_process*/) { - auto* context = dynamic_cast(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) diff --git a/src/simix/ActorImpl.hpp b/src/simix/ActorImpl.hpp index 2ca5dc3065..0d7ea73fcd 100644 --- a/src/simix/ActorImpl.hpp +++ b/src/simix/ActorImpl.hpp @@ -105,6 +105,9 @@ public: static ActorImplPtr create(std::string name, simix::ActorCode code, void* data, s4u::Host* host, std::unordered_map* properties, ActorImpl* parent_actor); + static ActorImplPtr attach(std::string name, void* data, s4u::Host* host, + std::unordered_map* properties); + static void detach(); void cleanup(); void exit(); void kill(ActorImpl* actor); -- 2.20.1