Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::string for Synchro instead of char*
[simgrid.git] / src / simix / smx_global.cpp
index 9b31320..9ba653b 100644 (file)
@@ -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 <functional>
+
 #include <signal.h> /* Signal handling */
 #include <stdlib.h>
 #include "src/internal_config.h"
@@ -12,7 +14,6 @@
 #include "src/surf/storage_interface.hpp"
 #include "src/surf/xml/platf.hpp"
 #include "smx_private.h"
-#include "smx_private.hpp"
 #include "xbt/str.h"
 #include "xbt/ex.h"             /* ex_backtrace_display */
 #include "mc/mc.h"
@@ -48,9 +49,12 @@ static xbt_heap_t simix_timers = NULL;
 
 /** @brief Timer datatype */
 typedef struct s_smx_timer {
-  double date;
-  void(* func)(void*);
-  void* args;
+  double date = 0.0;
+  std::function<void()> callback;
+
+  s_smx_timer() {}
+  s_smx_timer(double date, std::function<void()> callback)
+    : date(date), callback(std::move(callback)) {}
 } s_smx_timer_t;
 
 void (*SMPI_switch_data_segment)(int) = NULL;
@@ -127,7 +131,7 @@ static void install_segvhandler(void)
   }
 
   struct sigaction action, old_action;
-  action.sa_sigaction = segvhandler;
+  action.sa_sigaction = &segvhandler;
   action.sa_flags = SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
 
@@ -189,11 +193,10 @@ void SIMIX_global_init(int *argc, char **argv)
   simgrid::mc::Client::initialize();
 #endif
 
-  s_smx_process_t proc;
-
   if (!simix_global) {
     simix_global = xbt_new0(s_smx_global_t, 1);
 
+    simgrid::simix::Process proc;
     simix_global->process_to_run = xbt_dynar_new(sizeof(smx_process_t), NULL);
     simix_global->process_that_ran = xbt_dynar_new(sizeof(smx_process_t), NULL);
     simix_global->process_list = xbt_swag_new(xbt_swag_offset(proc, process_hookup));
@@ -202,9 +205,9 @@ void SIMIX_global_init(int *argc, char **argv)
     simix_global->maestro_process = NULL;
     simix_global->registered_functions = xbt_dict_new_homogeneous(NULL);
 
-    simix_global->create_process_function = SIMIX_process_create;
-    simix_global->kill_process_function = kill_process;
-    simix_global->cleanup_process_function = SIMIX_process_cleanup;
+    simix_global->create_process_function = &SIMIX_process_create;
+    simix_global->kill_process_function = &kill_process;
+    simix_global->cleanup_process_function = &SIMIX_process_cleanup;
     simix_global->mutex = xbt_os_mutex_init();
 
     surf_init(argc, argv);      /* Initialize SURF structures */
@@ -215,8 +218,8 @@ void SIMIX_global_init(int *argc, char **argv)
     simgrid::simix::create_maestro(maestro_code);
 
     /* context exception handlers */
-    __xbt_running_ctx_fetch = SIMIX_process_get_running_context;
-    __xbt_ex_terminate = SIMIX_process_exception_terminate;
+    __xbt_running_ctx_fetch = &SIMIX_process_get_running_context;
+    __xbt_ex_terminate = &SIMIX_process_exception_terminate;
 
     /* Prepare to display some more info when dying on Ctrl-C pressing */
     signal(SIGINT, inthandler);
@@ -243,9 +246,10 @@ void SIMIX_global_init(int *argc, char **argv)
 
     SIMIX_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, SIMIX_storage_destroy);
   }
-  if (!simix_timers) {
-    simix_timers = xbt_heap_new(8, &free);
-  }
+  if (!simix_timers)
+    simix_timers = xbt_heap_new(8, [](void* p) {
+      delete static_cast<smx_timer_t>(p);
+    });
 
   if (xbt_cfg_get_boolean("clean-atexit"))
     atexit(SIMIX_clean);
@@ -294,8 +298,10 @@ void SIMIX_clean(void)
 
   /* Let's free maestro now */
   delete simix_global->maestro_process->context;
+  simix_global->maestro_process->context = nullptr;
   xbt_free(simix_global->maestro_process->running_ctx);
-  xbt_free(simix_global->maestro_process);
+  simix_global->maestro_process->running_ctx = nullptr;
+  delete simix_global->maestro_process;
   simix_global->maestro_process = NULL;
 
   /* Restore the default exception setup */
@@ -466,9 +472,15 @@ void SIMIX_run(void)
        //FIXME: make the timers being real callbacks
        // (i.e. provide dispatchers that read and expand the args)
        timer = (smx_timer_t) xbt_heap_pop(simix_timers);
-       if (timer->func)
-         timer->func(timer->args);
-       xbt_free(timer);
+       if (timer->callback) {
+         try {
+           timer->callback();
+         }
+         catch(...) {
+           xbt_die("Exception throwed ouf of timer callback");
+         }
+       }
+       delete timer;
     }
 
     /* Wake up all processes waiting for a Surf action to finish */
@@ -525,14 +537,18 @@ void SIMIX_run(void)
  */
 smx_timer_t SIMIX_timer_set(double date, void (*function)(void*), void *arg)
 {
-  smx_timer_t timer = xbt_new0(s_smx_timer_t, 1);
+  smx_timer_t timer = new s_smx_timer_t(date, std::bind(function, arg));
+  xbt_heap_push(simix_timers, timer, date);
+  return timer;
+}
 
-  timer->date = date;
-  timer->func = function;
-  timer->args = arg;
+smx_timer_t SIMIX_timer_set(double date, std::function<void()> callback)
+{
+  smx_timer_t timer = new s_smx_timer_t(date, std::move(callback));
   xbt_heap_push(simix_timers, timer, date);
   return timer;
 }
+
 /** @brief cancels a timer that was added earlier */
 void SIMIX_timer_remove(smx_timer_t timer) {
   xbt_heap_rm_elm(simix_timers, timer, timer->date);
@@ -551,8 +567,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;
 }
@@ -633,12 +648,12 @@ void SIMIX_display_process_status(void)
 */
 
       XBT_INFO("Process %lu (%s@%s): waiting for %s synchro %p (%s) in state %d to finish",
-          process->pid, process->name, sg_host_get_name(process->host),
+          process->pid, process->name.c_str(), sg_host_get_name(process->host),
           synchro_description, process->waiting_synchro,
-          process->waiting_synchro->name, (int)process->waiting_synchro->state);
+          process->waiting_synchro->name.c_str(), (int)process->waiting_synchro->state);
     }
     else {
-      XBT_INFO("Process %lu (%s@%s)", process->pid, process->name, sg_host_get_name(process->host));
+      XBT_INFO("Process %lu (%s@%s)", process->pid, process->name.c_str(), sg_host_get_name(process->host));
     }
   }
 }