From d17f206ea75d3cccc5f1b83b67f58d4fe87781d1 Mon Sep 17 00:00:00 2001 From: Gabriel Corona Date: Fri, 20 May 2016 15:37:26 +0200 Subject: [PATCH] [simix] Wrap (xbt_main_func_t, argc, argv) in a std::function everywhere SIMIX_process_get_code() is a casuality of this refactoring: it does not make sens in this design. --- include/simgrid/simix.h | 1 - include/simgrid/simix.hpp | 6 ++-- src/msg/msg_private.h | 10 +++--- src/msg/msg_process.cpp | 23 +++++++------ src/simix/smx_host.cpp | 22 ++++-------- src/simix/smx_host_private.h | 5 +-- src/simix/smx_process.cpp | 59 ++++++++++++--------------------- src/simix/smx_process_private.h | 10 +++--- src/surf/sg_platf.cpp | 27 ++++++--------- 9 files changed, 65 insertions(+), 98 deletions(-) diff --git a/include/simgrid/simix.h b/include/simgrid/simix.h index f0de3ed8fd..bc07ffe3ee 100644 --- a/include/simgrid/simix.h +++ b/include/simgrid/simix.h @@ -223,7 +223,6 @@ XBT_PUBLIC(void) SIMIX_process_set_context(smx_process_t p,smx_context_t c); XBT_PUBLIC(int) SIMIX_process_has_pending_comms(smx_process_t process); XBT_PUBLIC(void) SIMIX_process_on_exit_runall(smx_process_t process); XBT_PUBLIC(void) SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data); -XBT_PUBLIC(xbt_main_func_t) SIMIX_process_get_code(void); /****************************** Communication *********************************/ XBT_PUBLIC(void) SIMIX_comm_set_copy_data_callback(void (*callback) (smx_synchro_t, void*, size_t)); diff --git a/include/simgrid/simix.hpp b/include/simgrid/simix.hpp index cf51596aed..cc08c5ea68 100644 --- a/include/simgrid/simix.hpp +++ b/include/simgrid/simix.hpp @@ -296,11 +296,10 @@ XBT_PUBLIC(void) create_maestro(std::function code); */ typedef smx_process_t (*smx_creation_func_t) ( /* name */ const char*, - /* code */ xbt_main_func_t, + std::function code, /* userdata */ void*, /* hostname */ const char*, /* kill_time */ double, - simgrid::simix::args args, /* props */ xbt_dict_t, /* auto_restart */ int, /* parent_process */ smx_process_t); @@ -309,11 +308,10 @@ extern "C" XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function); XBT_PUBLIC(smx_process_t) simcall_process_create(const char *name, - xbt_main_func_t code, + std::function code, void *data, const char *hostname, double kill_time, - simgrid::simix::args args, xbt_dict_t properties, int auto_restart); diff --git a/src/msg/msg_private.h b/src/msg/msg_private.h index 0d004f739b..bb771f3ed1 100644 --- a/src/msg/msg_private.h +++ b/src/msg/msg_private.h @@ -7,6 +7,8 @@ #ifndef METASIMGRID_PRIVATE_H #define METASIMGRID_PRIVATE_H +#include + #include "simgrid/msg.h" #include "simgrid/simix.h" #include "src/include/surf/surf.h" @@ -139,9 +141,8 @@ XBT_PRIVATE void __MSG_file_destroy(msg_file_priv_t host); 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, + std::function code, void *data, const char *hostname, double kill_time, - 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); @@ -191,8 +192,7 @@ XBT_PRIVATE void TRACE_msg_vm_end(msg_vm_t vm); SG_END_DECL() XBT_PUBLIC(msg_process_t) MSG_process_create_with_environment( - const char *name, xbt_main_func_t code, void *data, - msg_host_t host, simgrid::simix::args args, - xbt_dict_t properties); + const char *name, std::function code, void *data, + msg_host_t host, xbt_dict_t properties); #endif diff --git a/src/msg/msg_process.cpp b/src/msg/msg_process.cpp index 6ca8e3a127..41a526372e 100644 --- a/src/msg/msg_process.cpp +++ b/src/msg/msg_process.cpp @@ -4,6 +4,8 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ +#include + #include "msg_private.h" #include "xbt/sysdep.h" #include "xbt/log.h" @@ -52,13 +54,14 @@ 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, simgrid::simix::args args, xbt_dict_t properties, - int auto_restart, smx_process_t parent_process) +smx_process_t MSG_process_create_from_SIMIX( + const char *name, std::function code, void *data, const char *hostname, + double kill_time, 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, args.argc(), args.to_argv(), properties); + name, std::move(code), data, host, properties); if (p) { MSG_process_set_kill_time(p,kill_time); MSG_process_auto_restart_set(p,auto_restart); @@ -128,8 +131,9 @@ msg_process_t MSG_process_create_with_arguments(const char *name, xbt_main_func_ msg_process_t MSG_process_create_with_environment(const char *name, xbt_main_func_t code, void *data, msg_host_t host, int argc, char **argv, xbt_dict_t properties) { - msg_process_t res = MSG_process_create_with_environment(name, code, data, host, - simgrid::simix::args(argc, argv), properties); + msg_process_t res = MSG_process_create_with_environment(name, + simgrid::simix::wrap_main(code, argc, argv), data, host, + properties); for (int i = 0; i != argc; ++i) xbt_free(argv[i]); xbt_free(argv); @@ -137,9 +141,8 @@ msg_process_t MSG_process_create_with_environment(const char *name, xbt_main_fun } msg_process_t MSG_process_create_with_environment( - const char *name, xbt_main_func_t code, void *data, - msg_host_t host, simgrid::simix::args args, - xbt_dict_t properties) + const char *name, std::function code, void *data, + msg_host_t host, xbt_dict_t properties) { xbt_assert(code != NULL && host != NULL, "Invalid parameters: host and code params must not be NULL"); simdata_process_t simdata = xbt_new0(s_simdata_process_t, 1); @@ -155,7 +158,7 @@ msg_process_t MSG_process_create_with_environment( /* Let's create the process: SIMIX may decide to start it right now, * even before returning the flow control to us */ process = simcall_process_create( - name, code, simdata, sg_host_get_name(host), -1, std::move(args), properties, 0); + name, std::move(code), simdata, sg_host_get_name(host), -1, properties, 0); if (!process) { /* Undo everything we have just changed */ diff --git a/src/simix/smx_host.cpp b/src/simix/smx_host.cpp index d4684dd72f..cf092ea486 100644 --- a/src/simix/smx_host.cpp +++ b/src/simix/smx_host.cpp @@ -57,7 +57,6 @@ void SIMIX_host_on(sg_host_t h) NULL, arg->hostname, arg->kill_time, - arg->args, arg->properties, arg->auto_restart, NULL); @@ -67,7 +66,6 @@ void SIMIX_host_on(sg_host_t h) NULL, arg->hostname, arg->kill_time, - arg->args, arg->properties, arg->auto_restart); } @@ -167,26 +165,20 @@ void _SIMIX_host_free_process_arg(void *data) * The processes will only be restarted once, meaning that you will have to register the process * again to restart the process again. */ -void SIMIX_host_add_auto_restart_process(sg_host_t host, - const char *name, - xbt_main_func_t code, - void *data, - const char *hostname, - double kill_time, - int argc, char **argv, - xbt_dict_t properties, - int auto_restart) +void SIMIX_host_add_auto_restart_process( + sg_host_t host, const char *name, std::function code, + void* data, const char *hostname, double kill_time, + xbt_dict_t properties, int auto_restart) { if (!sg_host_simix(host)->auto_restart_processes) { sg_host_simix(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg); } smx_process_arg_t arg = new simgrid::simix::ProcessArg(); arg->name = name; - arg->code = code; + arg->code = std::move(code); arg->data = data; arg->hostname = hostname; arg->kill_time = kill_time; - arg->args.assign(argc, argv); arg->properties = properties; arg->auto_restart = auto_restart; @@ -216,17 +208,15 @@ void SIMIX_host_autorestart(sg_host_t host) NULL, arg->hostname, arg->kill_time, - arg->args, arg->properties, arg->auto_restart, NULL); } else { simcall_process_create(arg->name.c_str(), - (xbt_main_func_t) arg->code, + arg->code, NULL, arg->hostname, arg->kill_time, - arg->args, arg->properties, arg->auto_restart); } diff --git a/src/simix/smx_host_private.h b/src/simix/smx_host_private.h index eb8b7e64f0..bac4f9f3c4 100644 --- a/src/simix/smx_host_private.h +++ b/src/simix/smx_host_private.h @@ -7,6 +7,8 @@ #ifndef _SIMIX_HOST_PRIVATE_H #define _SIMIX_HOST_PRIVATE_H +#include + #include #include "simgrid/simix.h" @@ -29,11 +31,10 @@ XBT_PRIVATE void SIMIX_host_destroy(void *host); XBT_PRIVATE void SIMIX_host_add_auto_restart_process(sg_host_t host, const char *name, - xbt_main_func_t code, + std::function code, void *data, const char *hostname, double kill_time, - int argc, char **argv, xbt_dict_t properties, int auto_restart); diff --git a/src/simix/smx_process.cpp b/src/simix/smx_process.cpp index f77724d82e..72651d24e5 100644 --- a/src/simix/smx_process.cpp +++ b/src/simix/smx_process.cpp @@ -194,7 +194,7 @@ void SIMIX_process_stop(smx_process_t arg) { arg->code, arg->data, sg_host_get_name(arg->host), SIMIX_timer_get_date(arg->kill_timer), - arg->args.argc(), arg->args.argv(), arg->properties, + arg->properties, arg->auto_restart); } XBT_DEBUG("Process %s (%s) is dead", @@ -202,20 +202,15 @@ void SIMIX_process_stop(smx_process_t arg) { arg->context->stop(); } -void* simcall_HANDLER_process_create(smx_simcall_t simcall, - const char *name, - xbt_main_func_t code, - void *data, - const char *hostname, - double kill_time, - int argc, char **argv, - xbt_dict_t properties, - int auto_restart) +void* simcall_HANDLER_process_create( + smx_simcall_t simcall, const char *name, xbt_main_func_t code, + void *data, const char *hostname, double kill_time, + int argc, char **argv, xbt_dict_t properties, + 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); + void* res = SIMIX_process_create( + name, simgrid::simix::wrap_main(code, argc, argv), data, hostname, + kill_time, properties, auto_restart, simcall->issuer); for (int i = 0; i != argc; ++i) xbt_free(argv[i]); xbt_free(argv); @@ -238,11 +233,10 @@ static void kill_process(void* process) */ smx_process_t SIMIX_process_create( const char *name, - xbt_main_func_t code, + std::function code, void *data, const char *hostname, double kill_time, - simgrid::simix::args args, xbt_dict_t properties, int auto_restart, smx_process_t parent_process) @@ -260,7 +254,7 @@ smx_process_t SIMIX_process_create( else { process = new simgrid::simix::Process(); - xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters"); + xbt_assert(code && host != NULL, "Invalid parameters"); /* Process data */ process->pid = simix_process_maxpid++; process->name = std::string(name); @@ -291,11 +285,10 @@ smx_process_t SIMIX_process_create( /* Process data for auto-restart */ process->auto_restart = auto_restart; process->code = code; - process->args = args; XBT_VERB("Create context %s", process->name.c_str()); process->context = SIMIX_context_new( - simgrid::simix::wrap_main(code, std::move(args)), + std::move(code), simix_global->cleanup_process_function, process); process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t)); @@ -728,10 +721,6 @@ sg_host_t SIMIX_process_get_host(smx_process_t process) return process->host; } -xbt_main_func_t SIMIX_process_get_code(void){ - return SIMIX_process_self()->code; -} - /* needs to be public and without simcall because it is called by exceptions and logging events */ const char* SIMIX_process_self_get_name(void) { @@ -1002,7 +991,6 @@ smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) arg.data = process->data; arg.properties = NULL; arg.auto_restart = process->auto_restart; - arg.args = process->args; //kill the old process SIMIX_process_kill(process, issuer); @@ -1010,16 +998,14 @@ smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) //start the new process if (simix_global->create_process_function) return simix_global->create_process_function( - arg.name.c_str(), arg.code, arg.data, + arg.name.c_str(), std::move(arg.code), arg.data, arg.hostname, arg.kill_time, - arg.args, arg.properties, arg.auto_restart, nullptr); else return simcall_process_create( - arg.name.c_str(), arg.code, arg.data, + arg.name.c_str(), std::move(arg.code), arg.data, arg.hostname, arg.kill_time, - arg.args, arg.properties, arg.auto_restart); } @@ -1027,21 +1013,18 @@ void SIMIX_segment_index_set(smx_process_t proc, int index){ proc->segment_index = index; } -smx_process_t simcall_process_create(const char *name, - xbt_main_func_t code, - void *data, - const char *hostname, - double kill_time, - simgrid::simix::args args, - xbt_dict_t properties, - int auto_restart) +smx_process_t simcall_process_create( + const char *name, std::function code, void *data, + const char *hostname, double kill_time, + xbt_dict_t properties, int auto_restart) { if (name == nullptr) name = ""; smx_process_t self = SIMIX_process_self(); return simgrid::simix::kernel([&] { - return SIMIX_process_create(name, code, data, hostname, - kill_time, std::move(args), properties, auto_restart, + return SIMIX_process_create(name, + std::move(code), data, hostname, + kill_time, properties, auto_restart, self); }); } \ No newline at end of file diff --git a/src/simix/smx_process_private.h b/src/simix/smx_process_private.h index 3a3db8d9ef..22af111eca 100644 --- a/src/simix/smx_process_private.h +++ b/src/simix/smx_process_private.h @@ -7,6 +7,7 @@ #ifndef _SIMIX_PROCESS_PRIVATE_H #define _SIMIX_PROCESS_PRIVATE_H +#include #include #include @@ -26,8 +27,7 @@ namespace simix { class ProcessArg { public: std::string name; - xbt_main_func_t code = nullptr; - simgrid::simix::args args; + std::function code; void *data = nullptr; const char *hostname = nullptr; double kill_time = 0.0; @@ -65,8 +65,7 @@ public: void *data = nullptr; /* kept for compatibility, it should be replaced with moddata */ xbt_dynar_t on_exit = nullptr; /* list of functions executed when the process dies */ - xbt_main_func_t code = nullptr; - simgrid::simix::args args; + std::function code; smx_timer_t kill_timer = nullptr; int segment_index = 0; /*Reference to an SMPI process' data segment. Default value is -1 if not in SMPI context*/ }; @@ -82,11 +81,10 @@ SG_BEGIN_DECL() XBT_PRIVATE smx_process_t SIMIX_process_create( const char *name, - xbt_main_func_t code, + std::function code, void *data, const char *hostname, double kill_time, - 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 35cd58ed2c..5f7f50d686 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -572,15 +572,16 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) double kill_time = process->kill_time; int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1; + std::function code = simgrid::simix::wrap_main(parse_code, process->argc, process->argv); + smx_process_arg_t arg = NULL; smx_process_t process_created = NULL; arg = new simgrid::simix::ProcessArg(); arg->name = std::string(process->argv[0]); - arg->code = parse_code; + arg->code = code; arg->data = NULL; arg->hostname = sg_host_get_name(host); - arg->args.assign(process->argc, process->argv); arg->kill_time = kill_time; arg->properties = current_property_set; if (!sg_host_simix(host)->boot_processes) @@ -589,12 +590,12 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg); if (start_time > SIMIX_get_clock()) { + arg = new simgrid::simix::ProcessArg(); arg->name = std::string(process->argv[0]); - arg->code = parse_code; + arg->code = std::move(code); arg->data = NULL; arg->hostname = sg_host_get_name(host); - arg->args.assign(process->argc, process->argv); arg->kill_time = kill_time; arg->properties = current_property_set; @@ -604,11 +605,10 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) smx_process_arg_t arg = static_cast(p); simix_global->create_process_function( arg->name.c_str(), - arg->code, + std::move(arg->code), arg->data, arg->hostname, arg->kill_time, - std::move(arg->args), arg->properties, arg->auto_restart, NULL); @@ -620,18 +620,13 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) if (simix_global->create_process_function) process_created = simix_global->create_process_function( - arg->name.c_str(), - parse_code, - NULL, - sg_host_get_name(host), - kill_time, - arg->args, - current_property_set, - auto_restart, NULL); + arg->name.c_str(), std::move(code), NULL, + sg_host_get_name(host), kill_time, + current_property_set, auto_restart, NULL); else process_created = simcall_process_create( - arg->name.c_str(), parse_code, NULL, sg_host_get_name(host), kill_time, - arg->args, current_property_set,auto_restart); + arg->name.c_str(), std::move(code), NULL, sg_host_get_name(host), kill_time, + current_property_set,auto_restart); /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */ if (!process_created) { -- 2.20.1