From: Gabriel Corona Date: Fri, 20 May 2016 11:39:33 +0000 (+0200) Subject: [simix] Use simgrid::simix::args in MSG X-Git-Tag: v3_14~1185 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/5c037db836667c8408d94daf61b893dd1efbabca [simix] Use simgrid::simix::args in MSG The main missing bit is the simcall. --- diff --git a/include/simgrid/simix.h b/include/simgrid/simix.h index 8f31a42b4b..f0de3ed8fd 100644 --- a/include/simgrid/simix.h +++ b/include/simgrid/simix.h @@ -111,31 +111,6 @@ typedef enum { } smx_process_exit_status_t; /** @} */ - -/* - * Type of function that creates a process. - * The function must accept the following parameters: - * void* process: the process created will be stored there - * const char *name: a name for the object. It is for user-level information and can be NULL - * xbt_main_func_t code: is a function describing the behavior of the process - * void *data: data a pointer to any data one may want to attach to the new object. - * sg_host_t host: the location where the new process is executed - * int argc, char **argv: parameters passed to code - * xbt_dict_t pros: properties - */ -typedef smx_process_t (*smx_creation_func_t) ( - /* name */ const char*, - /* code */ xbt_main_func_t, - /* userdata */ void*, - /* hostname */ const char*, - /* kill_time */ double, - /* argc */ int, - /* argv */ char**, - /* props */ xbt_dict_t, - /* auto_restart */ int, - /* parent_process */ smx_process_t); - - /******************************* Networking ***********************************/ /** * \ingroup simix_mbox_management @@ -178,7 +153,6 @@ XBT_PUBLIC(void) SIMIX_global_init(int *argc, char **argv); XBT_PUBLIC(void) SIMIX_set_maestro(void (*code)(void*), void* data); XBT_PUBLIC(void) SIMIX_function_register_process_cleanup(void_pfn_smxprocess_t function); -XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function); XBT_PUBLIC(void) SIMIX_function_register_process_kill(void_pfn_smxprocess_t function); /* Simulation execution */ diff --git a/include/simgrid/simix.hpp b/include/simgrid/simix.hpp index c28bf8b12a..9f73275352 100644 --- a/include/simgrid/simix.hpp +++ b/include/simgrid/simix.hpp @@ -157,19 +157,32 @@ public: char* operator[](std::size_t i) { return argv_[i]; } }; -inline -std::function wrap_main(xbt_main_func_t code, int argc, const char*const* argv) +inline std::function wrap_main( + xbt_main_func_t code, std::shared_ptr args) { if (code) { - auto arg = std::make_shared(argc, argv); return [=]() { - code(arg->argc(), arg->argv()); + code(args->argc(), args->argv()); }; } - // TODO, we should free argv else return std::function(); } +inline +std::function wrap_main(xbt_main_func_t code, simgrid::simix::args args) +{ + if (code) + return wrap_main(code, std::unique_ptr( + new simgrid::simix::args(std::move(args)))); + else return std::function(); +} + +inline +std::function wrap_main(xbt_main_func_t code, int argc, const char*const* argv) +{ + return wrap_main(code, simgrid::simix::args(argc, argv)); +} + class Context; class ContextFactory; @@ -270,4 +283,29 @@ XBT_PUBLIC(void) create_maestro(std::function code); } } +/* + * Type of function that creates a process. + * The function must accept the following parameters: + * void* process: the process created will be stored there + * const char *name: a name for the object. It is for user-level information and can be NULL + * xbt_main_func_t code: is a function describing the behavior of the process + * void *data: data a pointer to any data one may want to attach to the new object. + * sg_host_t host: the location where the new process is executed + * int argc, char **argv: parameters passed to code + * xbt_dict_t pros: properties + */ +typedef smx_process_t (*smx_creation_func_t) ( + /* name */ const char*, + /* code */ xbt_main_func_t, + /* userdata */ void*, + /* hostname */ const char*, + /* kill_time */ double, + simgrid::simix::args args, + /* props */ xbt_dict_t, + /* auto_restart */ int, + /* parent_process */ smx_process_t); + +extern "C" +XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function); + #endif diff --git a/src/msg/msg_private.h b/src/msg/msg_private.h index f39cdd0d62..0e976d82ad 100644 --- a/src/msg/msg_private.h +++ b/src/msg/msg_private.h @@ -79,8 +79,6 @@ typedef struct simdata_process { msg_host_t put_host; /* used for debugging purposes */ smx_synchro_t waiting_action; msg_task_t waiting_task; - char **argv; /* arguments table if any */ - int argc; /* arguments number if any */ msg_error_t last_errno; /* the last value returned by a MSG_function */ void* data; /* user data */ @@ -143,7 +141,7 @@ XBT_PRIVATE void MSG_process_cleanup_from_SIMIX(smx_process_t smx_proc); XBT_PRIVATE smx_process_t MSG_process_create_from_SIMIX(const char *name, xbt_main_func_t code, void *data, const char *hostname, double kill_time, - int argc, char **argv, + simgrid::simix::args args, xbt_dict_t properties, int auto_restart, smx_process_t parent_process); XBT_PRIVATE void MSG_comm_copy_data_from_SIMIX(smx_synchro_t comm, void* buff, size_t buff_size); diff --git a/src/msg/msg_process.cpp b/src/msg/msg_process.cpp index 1b4599d78f..e871197f87 100644 --- a/src/msg/msg_process.cpp +++ b/src/msg/msg_process.cpp @@ -53,11 +53,12 @@ void MSG_process_cleanup_from_SIMIX(smx_process_t smx_proc) /* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */ smx_process_t MSG_process_create_from_SIMIX(const char *name, xbt_main_func_t code, void *data, const char *hostname, - double kill_time, int argc, char **argv, xbt_dict_t properties, + double kill_time, simgrid::simix::args args, xbt_dict_t properties, int auto_restart, smx_process_t parent_process) { msg_host_t host = MSG_host_by_name(hostname); - msg_process_t p = MSG_process_create_with_environment(name, code, data, host, argc, argv, properties); + msg_process_t p = MSG_process_create_with_environment( + name, code, data, host, args.argc(), args.to_argv(), properties); if (p) { MSG_process_set_kill_time(p,kill_time); MSG_process_auto_restart_set(p,auto_restart); @@ -135,8 +136,6 @@ msg_process_t MSG_process_create_with_environment(const char *name, xbt_main_fun simdata->waiting_action = NULL; simdata->waiting_task = NULL; simdata->m_host = host; - simdata->argc = argc; - simdata->argv = argv; simdata->data = data; simdata->last_errno = MSG_OK; @@ -178,8 +177,6 @@ msg_process_t MSG_process_attach(const char *name, void *data, msg_host_t host, simdata->waiting_action = NULL; simdata->waiting_task = NULL; simdata->m_host = host; - simdata->argc = 0; - simdata->argv = NULL; simdata->data = data; simdata->last_errno = MSG_OK; diff --git a/src/simix/smx_global.cpp b/src/simix/smx_global.cpp index 9771e894a2..4e753fea5e 100644 --- a/src/simix/smx_global.cpp +++ b/src/simix/smx_global.cpp @@ -549,8 +549,7 @@ double SIMIX_timer_get_date(smx_timer_t timer) { * to call SIMIX_process_create(). * \param function create process function */ -void SIMIX_function_register_process_create(smx_creation_func_t - function) +void SIMIX_function_register_process_create(smx_creation_func_t function) { simix_global->create_process_function = function; } diff --git a/src/simix/smx_host.cpp b/src/simix/smx_host.cpp index 1fdde2763f..32a9a3f188 100644 --- a/src/simix/smx_host.cpp +++ b/src/simix/smx_host.cpp @@ -57,7 +57,7 @@ void SIMIX_host_on(sg_host_t h) NULL, arg->hostname, arg->kill_time, - arg->args.argc(), arg->args.to_argv(), + arg->args, arg->properties, arg->auto_restart, NULL); @@ -216,7 +216,7 @@ void SIMIX_host_autorestart(sg_host_t host) NULL, arg->hostname, arg->kill_time, - arg->args.argc(), arg->args.to_argv(), + arg->args, arg->properties, arg->auto_restart, NULL); diff --git a/src/simix/smx_process.cpp b/src/simix/smx_process.cpp index 76ec52f9f8..b6c816f9d7 100644 --- a/src/simix/smx_process.cpp +++ b/src/simix/smx_process.cpp @@ -210,10 +210,16 @@ void* simcall_HANDLER_process_create(smx_simcall_t simcall, double kill_time, int argc, char **argv, xbt_dict_t properties, - int auto_restart){ - return (void*)SIMIX_process_create(name, code, data, hostname, - kill_time, argc, argv, properties, auto_restart, + int auto_restart) +{ + simgrid::simix::args args(argc, argv); + void* res = SIMIX_process_create(name, code, data, hostname, + kill_time, std::move(args), properties, auto_restart, simcall->issuer); + for (int i = 0; i != argc; ++i) + xbt_free(argv[i]); + xbt_free(argv); + return res; } static void kill_process(void* process) @@ -236,7 +242,7 @@ smx_process_t SIMIX_process_create( void *data, const char *hostname, double kill_time, - int argc, char **argv, + simgrid::simix::args args, xbt_dict_t properties, int auto_restart, smx_process_t parent_process) @@ -247,12 +253,9 @@ smx_process_t SIMIX_process_create( XBT_DEBUG("Start process %s on host '%s'", name, hostname); if (host->isOff()) { - int i; XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname); - for (i = 0; i < argc; i++) - xbt_free(argv[i]); - xbt_free(argv); + return nullptr; } else { process = new simgrid::simix::Process(); @@ -288,17 +291,13 @@ smx_process_t SIMIX_process_create( /* Process data for auto-restart */ process->auto_restart = auto_restart; process->code = code; - process->args.assign(argc, argv); + process->args = args; XBT_VERB("Create context %s", process->name.c_str()); process->context = SIMIX_context_new( - simgrid::simix::wrap_main(code, argc, argv), + simgrid::simix::wrap_main(code, std::move(args)), simix_global->cleanup_process_function, process); - for (int i = 0; i < argc; i++) - free(argv[i]); - free(argv); - process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t)); XBT_RUNNING_CTX_INITIALIZE(process->running_ctx); @@ -1013,7 +1012,7 @@ smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) return simix_global->create_process_function( arg.name.c_str(), arg.code, arg.data, arg.hostname, arg.kill_time, - arg.args.argc(), arg.args.to_argv(), + arg.args, arg.properties, arg.auto_restart, nullptr); else diff --git a/src/simix/smx_process_private.h b/src/simix/smx_process_private.h index 204eb3dc45..3a3db8d9ef 100644 --- a/src/simix/smx_process_private.h +++ b/src/simix/smx_process_private.h @@ -86,7 +86,7 @@ XBT_PRIVATE smx_process_t SIMIX_process_create( void *data, const char *hostname, double kill_time, - int argc, char **argv, + simgrid::simix::args args, xbt_dict_t properties, int auto_restart, smx_process_t parent_process); diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index 8ccd61c856..e1259d7a24 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -608,7 +608,7 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) arg->data, arg->hostname, arg->kill_time, - arg->args.argc(), arg->args.to_argv(), + std::move(arg->args), arg->properties, arg->auto_restart, NULL); @@ -625,7 +625,7 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) NULL, sg_host_get_name(host), kill_time, - arg->args.argc(), arg->args.to_argv(), + arg->args, current_property_set, auto_restart, NULL); else