From 22968d21e6cec9997f587ca50c376b816a938ec9 Mon Sep 17 00:00:00 2001 From: Gabriel Corona Date: Mon, 30 Nov 2015 12:06:58 +0100 Subject: [PATCH] [simix] Fix signatures of callbacks (timer and kill_process_function) * Conversion between pointer-to-pointer and pointer to data is undefined behaviour. * Moreover, this hides error. The kill_process_function was called in the timer with only one argument whreas it expects two arguments (the killed process and the killer process). * Simpligy the signature of SIMIX_function_register_process_kill() to only take a single argument. The second one is never used. --- include/simgrid/simix.h | 6 ++---- src/simix/libsmx.c | 7 ++++++- src/simix/smx_global.c | 20 ++++++++++++++++---- src/simix/smx_private.h | 11 +---------- src/surf/sg_platf.cpp | 4 +++- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/include/simgrid/simix.h b/include/simgrid/simix.h index edaa5660af..2d4da0f277 100644 --- a/include/simgrid/simix.h +++ b/include/simgrid/simix.h @@ -130,8 +130,6 @@ typedef struct s_smx_context_factory *smx_context_factory_t; /* Process creation/destruction callbacks */ typedef void (*void_pfn_smxprocess_t) (smx_process_t); -/* Process kill */ -typedef void (*void_pfn_smxprocess_t_smxprocess_t) (smx_process_t, smx_process_t); /* for auto-restart function */ typedef void (*void_pfn_sghost_t) (sg_host_t); @@ -225,7 +223,7 @@ XBT_PUBLIC(void) SIMIX_global_init(int *argc, char **argv); 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_smxprocess_t function); +XBT_PUBLIC(void) SIMIX_function_register_process_kill(void_pfn_smxprocess_t function); /* Simulation execution */ XBT_PUBLIC(void) SIMIX_run(void); @@ -234,7 +232,7 @@ XBT_PUBLIC(double) SIMIX_get_clock(void); /* Timer functions FIXME: should these be public? */ typedef struct s_smx_timer* smx_timer_t; -XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, void *function, void *arg); +XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, void (*function)(void*), void *arg); XBT_PUBLIC(void) SIMIX_timer_remove(smx_timer_t timer); XBT_PUBLIC(double) SIMIX_timer_next(void); XBT_PUBLIC(double) SIMIX_timer_get_date(smx_timer_t timer); diff --git a/src/simix/libsmx.c b/src/simix/libsmx.c index cdddd84d40..4db1dab4a0 100644 --- a/src/simix/libsmx.c +++ b/src/simix/libsmx.c @@ -658,6 +658,11 @@ void simcall_process_set_data(smx_process_t process, void *data) } } +static void kill_process(void* arg) +{ + simix_global->kill_process_function((smx_process_t) arg); +} + /** * \ingroup simix_process_management * \brief Set the kill time of a process. @@ -669,7 +674,7 @@ void simcall_process_set_kill_time(smx_process_t process, double kill_time) if (simix_global->kill_process_function) { XBT_DEBUG("Set kill time %f for process %s(%s)",kill_time, process->name, sg_host_get_name(process->host)); - process->kill_timer = SIMIX_timer_set(kill_time, simix_global->kill_process_function, process); + process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process); } } } diff --git a/src/simix/smx_global.c b/src/simix/smx_global.c index 677f9d52a2..dba302930f 100644 --- a/src/simix/smx_global.c +++ b/src/simix/smx_global.c @@ -45,6 +45,13 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix, smx_global_t simix_global = NULL; static xbt_heap_t simix_timers = NULL; +/** @brief Timer datatype */ +typedef struct s_smx_timer { + double date; + void(* func)(void*); + void* args; +} s_smx_timer_t; + static void* SIMIX_synchro_mallocator_new_f(void); static void SIMIX_synchro_mallocator_free_f(void* synchro); static void SIMIX_synchro_mallocator_reset_f(void* synchro); @@ -145,6 +152,11 @@ XBT_INLINE double SIMIX_timer_next(void) return xbt_heap_size(simix_timers) > 0 ? xbt_heap_maxkey(simix_timers) : -1.0; } +static void kill_process(smx_process_t process) +{ + SIMIX_process_kill(process, NULL); +} + /** * \ingroup SIMIX_API * \brief Initialize SIMIX internal data. @@ -179,7 +191,7 @@ void SIMIX_global_init(int *argc, char **argv) simix_global->registered_functions = xbt_dict_new_homogeneous(NULL); simix_global->create_process_function = SIMIX_process_create; - simix_global->kill_process_function = SIMIX_process_kill; + simix_global->kill_process_function = kill_process; simix_global->cleanup_process_function = SIMIX_process_cleanup; simix_global->synchro_mallocator = xbt_mallocator_new(65536, SIMIX_synchro_mallocator_new_f, SIMIX_synchro_mallocator_free_f, @@ -466,7 +478,7 @@ void SIMIX_run(void) // (i.e. provide dispatchers that read and expand the args) timer = xbt_heap_pop(simix_timers); if (timer->func) - ((void (*)(void*))timer->func)(timer->args); + timer->func(timer->args); xbt_free(timer); } @@ -524,7 +536,7 @@ void SIMIX_run(void) * \param arg Parameters of the function * */ -XBT_INLINE smx_timer_t SIMIX_timer_set(double date, void *function, void *arg) +XBT_INLINE smx_timer_t SIMIX_timer_set(double date, void (*function)(void*), void *arg) { smx_timer_t timer = xbt_new0(s_smx_timer_t, 1); @@ -566,7 +578,7 @@ XBT_INLINE void SIMIX_function_register_process_create(smx_creation_func_t * * \param function Kill process function */ -XBT_INLINE void SIMIX_function_register_process_kill(void_pfn_smxprocess_t_smxprocess_t +XBT_INLINE void SIMIX_function_register_process_kill(void_pfn_smxprocess_t function) { simix_global->kill_process_function = function; diff --git a/src/simix/smx_private.h b/src/simix/smx_private.h index 572d474b0a..6aa57755ef 100644 --- a/src/simix/smx_private.h +++ b/src/simix/smx_private.h @@ -47,7 +47,7 @@ typedef struct s_smx_global { smx_process_t maestro_process; xbt_dict_t registered_functions; smx_creation_func_t create_process_function; - void_pfn_smxprocess_t_smxprocess_t kill_process_function; + void_pfn_smxprocess_t kill_process_function; /** Callback used when killing a SMX_process */ void_pfn_smxprocess_t cleanup_process_function; xbt_mallocator_t synchro_mallocator; @@ -83,15 +83,6 @@ typedef struct s_smx_file { void* data; /**< @brief user data */ } s_smx_file_t; -/*********************************** Time ************************************/ - -/** @brief Timer datatype */ -typedef struct s_smx_timer { - double date; - void* func; - void* args; -} s_smx_timer_t; - /********************************* synchro *************************************/ typedef enum { diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index 9a72f08740..7a7868b653 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -329,7 +329,9 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name, arg->hostname, start_time); - SIMIX_timer_set(start_time, (void*) SIMIX_process_create_from_wrapper, arg); + SIMIX_timer_set(start_time, [](void* arg) { + SIMIX_process_create_from_wrapper((smx_process_arg_t) arg); + }, arg); } else { // start_time <= SIMIX_get_clock() XBT_DEBUG("Starting Process %s(%s) right now", process->argv[0], sg_host_get_name(host)); -- 2.20.1