Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : forget include ucontext.h for struct smx_ctx_sysv
[simgrid.git] / src / simix / smx_context_sysv.c
index 9cedc55..b292db7 100644 (file)
@@ -9,31 +9,16 @@
 #include <stdarg.h>
 
 #include "xbt/parmap.h"
-#include "simix/private.h"
+#include "smx_private.h"
 #include "gras_config.h"
 #include "context_sysv_config.h"        /* loads context system definitions */
 
-#ifdef _XBT_WIN32
-#  include <win32_ucontext.h>     /* context relative declarations */
-#else
-#  include <ucontext.h>           /* context relative declarations */
-#endif
-
 #ifdef HAVE_VALGRIND_VALGRIND_H
 #  include <valgrind/valgrind.h>
 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
 
-typedef struct s_smx_ctx_sysv {
-  s_smx_ctx_base_t super;       /* Fields of super implementation */
-  ucontext_t uc;                /* the ucontext that executes the code */
-#ifdef HAVE_VALGRIND_VALGRIND_H
-  unsigned int valgrind_stack_id;       /* the valgrind stack id */
-#endif
-  char stack[0];                /* the thread stack (must remain the last element of the structure) */
-} s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
-
 #ifdef CONTEXT_THREADS
 static xbt_parmap_t sysv_parmap;
 static ucontext_t* sysv_workers_stacks;        /* space to save the worker's stack in each thread */
@@ -68,20 +53,11 @@ static void smx_ctx_sysv_suspend_parallel(smx_context_t context);
 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process);
 static void smx_ctx_sysv_runall_parallel(void);
 
-/* This is a bit paranoid about SIZEOF_VOIDP not being a multiple of SIZEOF_INT,
- * but it doesn't harm. */
-#define CTX_ADDR_LEN (SIZEOF_VOIDP / SIZEOF_INT + !!(SIZEOF_VOIDP % SIZEOF_INT))
-union u_ctx_addr {
-  void *addr;
-  int intv[CTX_ADDR_LEN];
-};
-#if (CTX_ADDR_LEN == 1)
-#  define CTX_ADDR_SPLIT(u) (u).intv[0]
-#elif (CTX_ADDR_LEN == 2)
-#  define CTX_ADDR_SPLIT(u) (u).intv[0], (u).intv[1]
-#else
-#  error Your architecture is not supported yet
-#endif
+/* This is a bit paranoid about sizeof(smx_ctx_sysv_t) not being a multiple of
+ * sizeof(int), but it doesn't harm. */
+#define CTX_ADDR_LEN                            \
+  (sizeof(smx_ctx_sysv_t) / sizeof(int) +       \
+   !!(sizeof(smx_ctx_sysv_t) % sizeof(int)))
 
 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
 {
@@ -95,7 +71,7 @@ void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
   (*factory)->name = "smx_sysv_context_factory";
 
   if (SIMIX_context_is_parallel()) {
-#ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
+#ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
     int nthreads = SIMIX_context_get_nthreads();
     sysv_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
     sysv_workers_stacks = xbt_new(ucontext_t, nthreads);
@@ -129,7 +105,7 @@ smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
                                   void_pfn_smxprocess_t cleanup_func,
                                   void *data)
 {
-  union u_ctx_addr ctx_addr;
+  int ctx_addr[CTX_ADDR_LEN];
   smx_ctx_sysv_t context =
       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
                                                                  code,
@@ -158,9 +134,20 @@ smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
                                 ((char *) context->uc.uc_stack.ss_sp) +
                                 context->uc.uc_stack.ss_size);
 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
-    ctx_addr.addr = context;
-    makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
-                CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
+    memcpy(ctx_addr, &context, sizeof(smx_ctx_sysv_t));
+    switch (CTX_ADDR_LEN) {
+    case 1:
+      makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
+                  1, ctx_addr[0]);
+      break;
+    case 2:
+      makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
+                  2, ctx_addr[0], ctx_addr[1]);
+      break;
+    default:
+      xbt_die("Ucontexts are not supported on this arch yet (addr len = %zu/%zu = %zu)",
+              sizeof(smx_ctx_sysv_t), sizeof(int), CTX_ADDR_LEN);
+    }
   } else {
     sysv_maestro_context = context;
   }
@@ -196,19 +183,19 @@ static void smx_ctx_sysv_free(smx_context_t context)
 
 static void smx_ctx_sysv_wrapper(int first, ...)
 { 
-  union u_ctx_addr ctx_addr;
+  int ctx_addr[CTX_ADDR_LEN];
   smx_ctx_sysv_t context;
 
-  ctx_addr.intv[0] = first;
+  ctx_addr[0] = first;
   if (CTX_ADDR_LEN > 1) {
     va_list ap;
     int i;
     va_start(ap, first);
     for (i = 1; i < CTX_ADDR_LEN; i++)
-      ctx_addr.intv[i] = va_arg(ap, int);
+      ctx_addr[i] = va_arg(ap, int);
     va_end(ap);
   }
-  context = ctx_addr.addr;
+  memcpy(&context, ctx_addr, sizeof(smx_ctx_sysv_t));
   (context->super.code) (context->super.argc, context->super.argv);
 
   simix_global->context_factory->stop((smx_context_t) context);