Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Port ThreadContext to C++
authorGabriel Corona <gabriel.corona@loria.fr>
Fri, 4 Dec 2015 12:51:50 +0000 (13:51 +0100)
committerGabriel Corona <gabriel.corona@loria.fr>
Mon, 7 Dec 2015 12:47:51 +0000 (13:47 +0100)
src/simix/ThreadContext.cpp [new file with mode: 0644]
src/simix/ThreadContext.hpp [new file with mode: 0644]
src/simix/smx_context.cpp
src/simix/smx_context_thread.cpp [deleted file]
tools/cmake/DefinePackages.cmake

diff --git a/src/simix/ThreadContext.cpp b/src/simix/ThreadContext.cpp
new file mode 100644 (file)
index 0000000..19be3d4
--- /dev/null
@@ -0,0 +1,172 @@
+/* Copyright (c) 2009-2015. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* 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 "xbt/function_types.h"
+#include "smx_private.h"
+#include "src/portable.h"           /* loads context system definitions */
+#include "xbt/swag.h"
+#include "xbt/xbt_os_thread.h"
+#include "src/xbt_modinter.h"       /* prototype of os thread module's init/exit in XBT */
+
+#include "src/simix/ThreadContext.hpp"
+
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
+
+static xbt_os_sem_t smx_ctx_thread_sem = nullptr;
+
+namespace simgrid {
+namespace simix {
+
+XBT_PRIVATE ContextFactory* thread_factory()
+{
+  XBT_VERB("Activating thread context factory");
+  return new ThreadContextFactory();
+}
+
+ThreadContextFactory::ThreadContextFactory()
+  : ContextFactory("ThreadContextFactory")
+{
+  if (SIMIX_context_is_parallel()) {
+    smx_ctx_thread_sem = xbt_os_sem_init(SIMIX_context_get_nthreads());
+  } else {
+    smx_ctx_thread_sem = nullptr;
+  }
+}
+
+ThreadContextFactory::~ThreadContextFactory()
+{
+  if (smx_ctx_thread_sem) {
+    xbt_os_sem_destroy(smx_ctx_thread_sem);
+    smx_ctx_thread_sem = nullptr;
+  }
+}
+
+ThreadContext* ThreadContextFactory::create_context(
+    xbt_main_func_t code, int argc, char ** argv,
+    void_pfn_smxprocess_t cleanup,
+    smx_process_t process)
+{
+  return this->new_context<ThreadContext>(code, argc, argv, cleanup, process);
+}
+
+ThreadContext::ThreadContext(xbt_main_func_t code,
+    int argc, char **argv,
+    void_pfn_smxprocess_t cleanup,
+    smx_process_t process)
+  : Context(code, argc, argv, cleanup, process)
+{
+  /* If the user provided a function for the process then use it
+     otherwise is the context for maestro */
+  if (code) {
+    this->begin_ = xbt_os_sem_init(0);
+    this->end_ = xbt_os_sem_init(0);
+    if (smx_context_stack_size_was_set)
+      xbt_os_thread_setstacksize(smx_context_stack_size);
+    if (smx_context_guard_size_was_set)
+      xbt_os_thread_setguardsize(smx_context_guard_size);
+
+    /* create and start the process */
+    /* NOTE: The first argument to xbt_os_thread_create used to be the process *
+    * name, but now the name is stored at SIMIX level, so we pass a null  */
+    this->thread_ =
+      xbt_os_thread_create(NULL, ThreadContext::wrapper, this, this);
+
+    /* wait the starting of the newly created process */
+    xbt_os_sem_acquire(this->end_);
+
+  } else {
+    xbt_os_thread_set_extra_data(this);
+  }
+}
+
+void ThreadContextFactory::run_all()
+{
+  if (smx_ctx_thread_sem == nullptr) {
+    // Serial execution
+    smx_process_t process;
+    unsigned int cursor;
+    xbt_dynar_foreach(simix_global->process_to_run, cursor, process) {
+      XBT_DEBUG("Handling %p",process);
+      ThreadContext* context = static_cast<ThreadContext*>(process->context);
+      xbt_os_sem_release(context->begin_);
+      xbt_os_sem_acquire(context->end_);
+    }
+  } else {
+    // Parallel execution
+    unsigned int index;
+    smx_process_t process;
+    xbt_dynar_foreach(simix_global->process_to_run, index, process)
+      xbt_os_sem_release(static_cast<ThreadContext*>(process->context)->begin_);
+    xbt_dynar_foreach(simix_global->process_to_run, index, process)
+       xbt_os_sem_acquire(static_cast<ThreadContext*>(process->context)->end_);
+  }
+}
+
+ThreadContext* ThreadContextFactory::self()
+{
+  return static_cast<ThreadContext*>(xbt_os_thread_get_extra_data());
+}
+
+ThreadContext::~ThreadContext()
+{
+  /* check if this is the context of maestro (it doesn't have a real thread) */
+  if (this->thread_) {
+    /* wait about the thread terminason */
+    xbt_os_thread_join(this->thread_, nullptr);
+
+    /* destroy the synchronisation objects */
+    xbt_os_sem_destroy(this->begin_);
+    xbt_os_sem_destroy(this->end_);
+  }
+}
+
+void *ThreadContext::wrapper(void *param)
+{
+  ThreadContext* context = static_cast<ThreadContext*>(param);
+
+#ifndef WIN32
+  /* Install alternate signal stack, for SIGSEGV handler. */
+  stack_t stack;
+  stack.ss_sp = sigsegv_stack;
+  stack.ss_size = sizeof sigsegv_stack;
+  stack.ss_flags = 0;
+  sigaltstack(&stack, nullptr);
+#endif
+  /* Tell the maestro we are starting, and wait for its green light */
+  xbt_os_sem_release(context->end_);
+  xbt_os_sem_acquire(context->begin_);
+  if (smx_ctx_thread_sem)       /* parallel run */
+    xbt_os_sem_acquire(smx_ctx_thread_sem);
+
+  (*context)();
+  context->stop();
+  return nullptr;
+}
+
+void ThreadContext::stop()
+{
+  Context::stop();
+  if (smx_ctx_thread_sem)
+    xbt_os_sem_release(smx_ctx_thread_sem);
+
+  // Signal to the maestro that it has finished:
+  xbt_os_sem_release(this->end_);
+
+  xbt_os_thread_exit(NULL);
+}
+
+void ThreadContext::suspend()
+{
+  if (smx_ctx_thread_sem)
+    xbt_os_sem_release(smx_ctx_thread_sem);
+  xbt_os_sem_release(this->end_);
+  xbt_os_sem_acquire(this->begin_);
+  if (smx_ctx_thread_sem)
+    xbt_os_sem_acquire(smx_ctx_thread_sem);
+}
+
+}
+}
diff --git a/src/simix/ThreadContext.hpp b/src/simix/ThreadContext.hpp
new file mode 100644 (file)
index 0000000..4ecd959
--- /dev/null
@@ -0,0 +1,57 @@
+/* Copyright (c) 2009-2015. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* 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. */
+
+/* \file ThreadContext.hpp Context switching with native threads */
+
+#ifndef SIMGRID_SIMIX_THREAD_CONTEXT_HPP
+#define SIMGRID_SIMIX_THREAD_CONTEXT_HPP
+
+#include <simgrid/simix.hpp>
+
+
+namespace simgrid {
+namespace simix {
+
+class ThreadContext;
+class ThreadContextFactory;
+
+class ThreadContext : public Context {
+public:
+  friend ThreadContextFactory;
+  ThreadContext(xbt_main_func_t code,
+          int argc, char **argv,
+          void_pfn_smxprocess_t cleanup_func,
+          smx_process_t process);
+  ~ThreadContext();
+  void stop() override;
+  void suspend() override;
+private:
+  /** A portable thread */
+  xbt_os_thread_t thread_;
+  /** Semaphore used to schedule/yield the process */
+  xbt_os_sem_t begin_;
+  /** Semaphore used to schedule/unschedule */
+  xbt_os_sem_t end_;
+private:
+  static void* wrapper(void *param);
+};
+
+class ThreadContextFactory : public ContextFactory {
+public:
+  ThreadContextFactory();
+  ~ThreadContextFactory();
+  virtual ThreadContext* create_context(
+    xbt_main_func_t, int, char **, void_pfn_smxprocess_t,
+    smx_process_t process
+    ) override;
+  void run_all() override;
+  ThreadContext* self() override;
+};
+
+}
+}
+
+#endif
\ No newline at end of file
index 3a552c8..bacb466 100644 (file)
@@ -66,7 +66,7 @@ void SIMIX_context_mod_init(void)
     if (simgrid::simix::factory_initializer)
       simix_global->context_factory = simgrid::simix::factory_initializer();
     else { /* use the factory specified by --cfg=contexts/factory:value */
-#if defined(CONTEXT_THREADS) && 0
+#if defined(CONTEXT_THREADS)
       if (!strcmp(smx_context_factory_name, "thread"))
         simix_global->context_factory = simgrid::simix::thread_factory();
 #else
diff --git a/src/simix/smx_context_thread.cpp b/src/simix/smx_context_thread.cpp
deleted file mode 100644 (file)
index a151abf..0000000
+++ /dev/null
@@ -1,213 +0,0 @@
-/* context_thread - implementation of context switching with native threads */
-
-/* Copyright (c) 2009-2015. The SimGrid Team.
- * All rights reserved.                                                     */
-
-/* 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 "xbt/function_types.h"
-#include "smx_private.h"
-#include "src/portable.h"           /* loads context system definitions */
-#include "xbt/swag.h"
-#include "xbt/xbt_os_thread.h"
-#include "src/xbt_modinter.h"       /* prototype of os thread module's init/exit in XBT */
-
-XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
-
-typedef struct s_smx_ctx_thread {
-  s_smx_ctx_base_t super;       /* Fields of super implementation */
-  xbt_os_thread_t thread;       /* a plain dumb thread (portable to posix or windows) */
-  xbt_os_sem_t begin;           /* this semaphore is used to schedule/yield the process  */
-  xbt_os_sem_t end;             /* this semaphore is used to schedule/unschedule the process   */
-} s_smx_ctx_thread_t, *smx_ctx_thread_t;
-
-static xbt_os_sem_t smx_ctx_thread_sem;
-
-static smx_context_t
-smx_ctx_thread_factory_create_context(xbt_main_func_t code,
-                                      int argc, char **argv,
-                                      void_pfn_smxprocess_t cleanup_func,
-                                      smx_process_t process);
-
-static void smx_ctx_thread_free(smx_context_t context);
-static void smx_ctx_thread_stop(smx_context_t context);
-static void smx_ctx_thread_suspend(smx_context_t context);
-static void smx_ctx_thread_runall_serial(void);
-static void smx_ctx_thread_runall_parallel(void);
-static smx_context_t smx_ctx_thread_self(void);
-
-static int smx_ctx_thread_factory_finalize(smx_context_factory_t *factory);
-static void *smx_ctx_thread_wrapper(void *param);
-
-void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
-{
-  smx_ctx_base_factory_init(factory);
-  XBT_VERB("Activating thread context factory");
-
-  (*factory)->finalize  = smx_ctx_thread_factory_finalize;
-  (*factory)->create_context = smx_ctx_thread_factory_create_context;
-  /* Do not overload that method (*factory)->finalize */
-  (*factory)->free = smx_ctx_thread_free;
-  (*factory)->stop = smx_ctx_thread_stop;
-  (*factory)->suspend = smx_ctx_thread_suspend;
-
-  if (SIMIX_context_is_parallel())
-    (*factory)->runall = smx_ctx_thread_runall_parallel;
-  else
-    (*factory)->runall = smx_ctx_thread_runall_serial;
-
-  (*factory)->self = smx_ctx_thread_self;
-  (*factory)->name = "ctx_thread_factory";
-
-  if (SIMIX_context_is_parallel()) {
-    smx_ctx_thread_sem = xbt_os_sem_init(SIMIX_context_get_nthreads());
-  } else {
-    smx_ctx_thread_sem = NULL;
-  }
-}
-
-static int smx_ctx_thread_factory_finalize(smx_context_factory_t *factory)
-{
-  if (smx_ctx_thread_sem) {
-    xbt_os_sem_destroy(smx_ctx_thread_sem);
-    smx_ctx_thread_sem = NULL;
-  }
-  return smx_ctx_base_factory_finalize(factory);
-}
-
-static smx_context_t
-smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
-                                      char **argv,
-                                      void_pfn_smxprocess_t cleanup_func,
-                                      smx_process_t process)
-{
-  smx_ctx_thread_t context = (smx_ctx_thread_t)
-      smx_ctx_base_factory_create_context_sized(sizeof(s_smx_ctx_thread_t),
-                                                code, argc, argv,
-                                                cleanup_func, process);
-
-  /* If the user provided a function for the process then use it
-     otherwise is the context for maestro */
-  if (code) {
-    context->begin = xbt_os_sem_init(0);
-    context->end = xbt_os_sem_init(0);
-    if (smx_context_stack_size_was_set)
-      xbt_os_thread_setstacksize(smx_context_stack_size);
-    if (smx_context_guard_size_was_set)
-      xbt_os_thread_setguardsize(smx_context_guard_size);
-
-    /* create and start the process */
-    /* NOTE: The first argument to xbt_os_thread_create used to be the process *
-    * name, but now the name is stored at SIMIX level, so we pass a null  */
-    context->thread =
-      xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, context, context);
-
-
-    /* wait the starting of the newly created process */
-    xbt_os_sem_acquire(context->end);
-
-  } else {
-    xbt_os_thread_set_extra_data(context);
-  }
-
-  return (smx_context_t) context;
-}
-
-static void smx_ctx_thread_free(smx_context_t pcontext)
-{
-  smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
-
-  /* check if this is the context of maestro (it doesn't have a real thread) */
-  if (context->thread) {
-    /* wait about the thread terminason */
-    xbt_os_thread_join(context->thread, NULL);
-
-    /* destroy the synchronisation objects */
-    xbt_os_sem_destroy(context->begin);
-    xbt_os_sem_destroy(context->end);
-  }
-
-  smx_ctx_base_free(pcontext);
-}
-
-static void smx_ctx_thread_stop(smx_context_t pcontext)
-{
-  smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
-
-  /* please no debug here: our procdata was already free'd */
-  smx_ctx_base_stop(pcontext);
-
-  if (smx_ctx_thread_sem)       /* parallel run */
-    xbt_os_sem_release(smx_ctx_thread_sem);
-
-  /* signal to the maestro that it has finished */
-  xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
-
-  /* exit */
-  /* We should provide return value in case other wants it */
-  xbt_os_thread_exit(NULL);
-}
-
-static void *smx_ctx_thread_wrapper(void *param)
-{
-  smx_ctx_thread_t context = (smx_ctx_thread_t) param;
-#ifndef WIN32
-  /* Install alternate signal stack, for SIGSEGV handler. */
-  stack_t stack;
-  stack.ss_sp = sigsegv_stack;
-  stack.ss_size = sizeof sigsegv_stack;
-  stack.ss_flags = 0;
-  sigaltstack(&stack, NULL);
-#endif
-  /* Tell the maestro we are starting, and wait for its green light */
-  xbt_os_sem_release(context->end);
-  xbt_os_sem_acquire(context->begin);
-  if (smx_ctx_thread_sem)       /* parallel run */
-    xbt_os_sem_acquire(smx_ctx_thread_sem);
-
-  (context->super.code) (context->super.argc, context->super.argv);
-
-  smx_ctx_thread_stop((smx_context_t) context);
-  return NULL;
-}
-
-static void smx_ctx_thread_suspend(smx_context_t context)
-{
-  if (smx_ctx_thread_sem)       /* parallel run */
-    xbt_os_sem_release(smx_ctx_thread_sem);
-  xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
-  xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
-  if (smx_ctx_thread_sem)       /* parallel run */
-    xbt_os_sem_acquire(smx_ctx_thread_sem);
-}
-
-static void smx_ctx_thread_runall_serial(void)
-{
-  smx_process_t process;
-  unsigned int cursor;
-
-  xbt_dynar_foreach(simix_global->process_to_run, cursor, process) {
-    XBT_DEBUG("Handling %p",process);
-    xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
-    xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
-  }
-}
-
-static void smx_ctx_thread_runall_parallel(void)
-{
-  unsigned int index;
-  smx_process_t process;
-
-  xbt_dynar_foreach(simix_global->process_to_run, index, process)
-    xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
-
-  xbt_dynar_foreach(simix_global->process_to_run, index, process) {
-     xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
-  }
-}
-
-static smx_context_t smx_ctx_thread_self(void)
-{
-  return (smx_context_t) xbt_os_thread_get_extra_data();
-}
index a3cb141..cb6c6d0 100644 (file)
@@ -778,15 +778,17 @@ set(source_of_generated_headers
 
 ### depend of some variables setted upper
 # -->CONTEXT_THREADS CONTEXT_UCONTEXT
-if(0) #pthread
+if(${CONTEXT_THREADS}) #pthread
   set(SURF_SRC
     ${SURF_SRC}
-    src/simix/smx_context_thread.cpp
+    src/simix/ThreadContext.cpp
+    src/simix/ThreadContext.hpp
     )
 else() # NOT pthread
   set(EXTRA_DIST
     ${EXTRA_DIST}
-    src/simix/smx_context_thread.cpp
+    src/simix/ThreadContext.cpp
+    src/simix/ThreadContext.hpp
     )
 endif()
 
@@ -873,7 +875,8 @@ endif()
 if(WIN32)
   set(simgrid_sources
     ${simgrid_sources}
-    src/simix/smx_context_thread.cpp
+    src/simix/src/simix/ThreadContext.cpp
+    src/simix/src/simix/ThreadContext.hpp
     src/xbt/win32_ucontext.c
     src/xbt/xbt_os_thread.c
     )