Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Convert sthread.c to C++.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 18 Jul 2022 09:05:05 +0000 (11:05 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 27 Nov 2023 08:45:06 +0000 (09:45 +0100)
MANIFEST.in
src/sthread/sthread.cpp [moved from src/sthread/sthread.c with 62% similarity]
src/sthread/sthread.h
tools/cmake/DefinePackages.cmake

index 938585b..92d874b 100644 (file)
@@ -2551,7 +2551,7 @@ include src/smpi/smpiff.in
 include src/smpi/smpirun.in
 include src/smpi/smpitools.sh
 include src/sthread/ObjectAccess.cpp
-include src/sthread/sthread.c
+include src/sthread/sthread.cpp
 include src/sthread/sthread.h
 include src/sthread/sthread_impl.cpp
 include src/xbt/OsSemaphore.hpp
similarity index 62%
rename from src/sthread/sthread.c
rename to src/sthread/sthread.cpp
index ad27e3f..0407747 100644 (file)
@@ -5,7 +5,6 @@
 
 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
 
-#define _GNU_SOURCE
 #include "src/sthread/sthread.h"
 #include "src/internal_config.h"
 #include <dlfcn.h>
@@ -59,6 +58,7 @@ static int (*raw_sem_timedwait)(sem_t*, const struct timespec*);
 
 static void intercepter_init()
 {
+<<<<<<< HEAD:src/sthread/sthread.c
   raw_pthread_create = dlsym(RTLD_NEXT, "pthread_create");
   raw_pthread_join   = dlsym(RTLD_NEXT, "pthread_join");
   raw_pthread_mutex_init    = dlsym(RTLD_NEXT, "pthread_mutex_init");
@@ -95,17 +95,39 @@ static void intercepter_init()
   raw_sem_destroy   = dlsym(RTLD_NEXT, "sem_destroy");
   raw_sem_trywait   = dlsym(RTLD_NEXT, "sem_trywait");
   raw_sem_timedwait = dlsym(RTLD_NEXT, "sem_timedwait");
+=======
+  raw_pthread_create = reinterpret_cast<typeof(raw_pthread_create)>(dlsym(RTLD_NEXT, "pthread_create"));
+  raw_pthread_join   = reinterpret_cast<typeof(raw_pthread_join)>(dlsym(RTLD_NEXT, "pthread_join"));
+  raw_mutex_init     = reinterpret_cast<typeof(raw_mutex_init)>(dlsym(RTLD_NEXT, "pthread_mutex_init"));
+  raw_mutex_lock     = reinterpret_cast<typeof(raw_mutex_lock)>(dlsym(RTLD_NEXT, "pthread_mutex_lock"));
+  raw_mutex_trylock  = reinterpret_cast<typeof(raw_mutex_trylock)>(dlsym(RTLD_NEXT, "pthread_mutex_trylock"));
+  raw_mutex_unlock   = reinterpret_cast<typeof(raw_mutex_unlock)>(dlsym(RTLD_NEXT, "pthread_mutex_unlock"));
+  raw_mutex_destroy  = reinterpret_cast<typeof(raw_mutex_destroy)>(dlsym(RTLD_NEXT, "pthread_mutex_destroy"));
+
+  raw_sleep        = reinterpret_cast<typeof(raw_sleep)>(dlsym(RTLD_NEXT, "sleep"));
+  raw_usleep       = reinterpret_cast<typeof(raw_usleep)>(dlsym(RTLD_NEXT, "usleep"));
+  raw_gettimeofday = reinterpret_cast<typeof(raw_gettimeofday)>(dlsym(RTLD_NEXT, "gettimeofday"));
+
+  raw_sem_open      = reinterpret_cast<typeof(raw_sem_open)>(dlsym(RTLD_NEXT, "sem_open"));
+  raw_sem_init      = reinterpret_cast<typeof(raw_sem_init)>(dlsym(RTLD_NEXT, "sem_init"));
+  raw_sem_wait      = reinterpret_cast<typeof(raw_sem_wait)>(dlsym(RTLD_NEXT, "sem_wait"));
+  raw_sem_post      = reinterpret_cast<typeof(raw_sem_post)>(dlsym(RTLD_NEXT, "sem_post"));
+  raw_sem_destroy   = reinterpret_cast<typeof(raw_sem_destroy)>(dlsym(RTLD_NEXT, "sem_destroy"));
+  raw_sem_trywait   = reinterpret_cast<typeof(raw_sem_trywait)>(dlsym(RTLD_NEXT, "sem_trywait"));
+  raw_sem_timedwait = reinterpret_cast<typeof(raw_sem_timedwait)>(dlsym(RTLD_NEXT, "sem_timedwait"));
+>>>>>>> Convert sthread.c to C++.:src/sthread/sthread.cpp
 }
 
-static int sthread_inside_simgrid = 1;
+static bool sthread_inside_simgrid = true;
 void sthread_enable(void)
 { // Start intercepting all pthread calls
-  sthread_inside_simgrid = 0;
+  sthread_inside_simgrid = false;
 }
 void sthread_disable(void)
 { // Stop intercepting all pthread calls
-  sthread_inside_simgrid = 1;
+  sthread_inside_simgrid = true;
 }
+<<<<<<< HEAD:src/sthread/sthread.c
 
 #define _STHREAD_CONCAT(a, b) a##b
 #define intercepted_pthcall(name, raw_params, call_params, sim_params)                                                 \
@@ -180,6 +202,180 @@ intercepted_call(int, sem_wait, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
 intercepted_call(int, sem_trywait, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
 intercepted_call(int, sem_timedwait, (sem_t * sem, const struct timespec* abs_timeout), (sem, abs_timeout),
                  ((sthread_sem_t*)sem, abs_timeout));
+=======
+int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
+{
+  if (not raw_pthread_create)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_pthread_create(thread, attr, start_routine, arg);
+
+  sthread_disable();
+  int res = sthread_create(reinterpret_cast<sthread_t*>(thread), attr, start_routine, arg);
+  sthread_enable();
+  return res;
+}
+int pthread_join(pthread_t thread, void** retval)
+{
+  if (not raw_pthread_join)
+    intercepter_init();
+  if (sthread_inside_simgrid)
+    return raw_pthread_join(thread, retval);
+
+  sthread_disable();
+  int res = sthread_join(reinterpret_cast<sthread_t>(thread), retval);
+  sthread_enable();
+  return res;
+}
+
+int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
+{
+  if (not raw_mutex_init)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_mutex_init(mutex, attr);
+
+  sthread_disable();
+  int res = sthread_mutex_init(reinterpret_cast<sthread_mutex_t*>(mutex), attr);
+  sthread_enable();
+  return res;
+}
+
+int pthread_mutex_lock(pthread_mutex_t* mutex)
+{
+  if (not raw_mutex_lock)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_mutex_lock(mutex);
+
+  sthread_disable();
+  int res = sthread_mutex_lock(reinterpret_cast<sthread_mutex_t*>(mutex));
+  sthread_enable();
+  return res;
+}
+
+int pthread_mutex_trylock(pthread_mutex_t* mutex)
+{
+  if (not raw_mutex_trylock)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_mutex_trylock(mutex);
+
+  sthread_disable();
+  int res = sthread_mutex_trylock(reinterpret_cast<sthread_mutex_t*>(mutex));
+  sthread_enable();
+  return res;
+}
+
+int pthread_mutex_unlock(pthread_mutex_t* mutex)
+{
+  if (not raw_mutex_unlock)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_mutex_unlock(mutex);
+
+  sthread_disable();
+  int res = sthread_mutex_unlock(reinterpret_cast<sthread_mutex_t*>(mutex));
+  sthread_enable();
+  return res;
+}
+int pthread_mutex_destroy(pthread_mutex_t* mutex)
+{
+  if (not raw_mutex_destroy)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_mutex_destroy(mutex);
+
+  sthread_disable();
+  int res = sthread_mutex_destroy(reinterpret_cast<sthread_mutex_t*>(mutex));
+  sthread_enable();
+  return res;
+}
+int sem_init(sem_t* sem, int pshared, unsigned int value)
+{
+  if (not raw_sem_init)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_sem_init(sem, pshared, value);
+
+  sthread_disable();
+  int res = sthread_sem_init(reinterpret_cast<sthread_sem_t*>(sem), pshared, value);
+  sthread_enable();
+  return res;
+}
+int sem_destroy(sem_t* sem)
+{
+  if (not raw_sem_destroy)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_sem_destroy(sem);
+
+  sthread_disable();
+  int res = sthread_sem_destroy(reinterpret_cast<sthread_sem_t*>(sem));
+  sthread_enable();
+  return res;
+}
+int sem_post(sem_t* sem)
+{
+  if (not raw_sem_post)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_sem_post(sem);
+
+  sthread_disable();
+  int res = sthread_sem_post(reinterpret_cast<sthread_sem_t*>sem);
+  sthread_enable();
+  return res;
+}
+int sem_wait(sem_t* sem)
+{
+  if (not raw_sem_wait)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_sem_wait(sem);
+
+  sthread_disable();
+  int res = sthread_sem_wait(reinterpret_cast<sthread_sem_t*>(sem));
+  sthread_enable();
+  return res;
+}
+int sem_trywait(sem_t* sem)
+{
+  if (not raw_sem_trywait)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_sem_trywait(sem);
+
+  sthread_disable();
+  int res = sthread_sem_trywait(reinterpret_cast<sthread_sem_t*>(sem));
+  sthread_enable();
+  return res;
+}
+int sem_timedwait(sem_t* sem, const struct timespec* abs_timeout)
+{
+  if (not raw_sem_timedwait)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_sem_timedwait(sem, abs_timeout);
+
+  sthread_disable();
+  int res = sthread_sem_timedwait(reinterpret_cast<sthread_sem_t*>(sem), abs_timeout);
+  sthread_enable();
+  return res;
+}
+>>>>>>> Convert sthread.c to C++.:src/sthread/sthread.cpp
 
 /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday.
    Other implementations use "void *" instead. */
@@ -191,9 +387,81 @@ intercepted_call(int, sem_timedwait, (sem_t * sem, const struct timespec* abs_ti
 #ifndef TIMEZONE_TYPE
 #define TIMEZONE_TYPE void
 #endif
+<<<<<<< HEAD:src/sthread/sthread.c
 intercepted_call(int, gettimeofday, (struct timeval * tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz), (tv, tz), (tv));
 intercepted_call(unsigned int, sleep, (unsigned int seconds), (seconds), (seconds));
 intercepted_call(int, usleep, (useconds_t usec), (usec), (((double)usec) / 1000000.));
+=======
+
+int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz)
+{
+  if (not raw_gettimeofday)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_gettimeofday(tv, tz);
+
+  sthread_disable();
+  int res = sthread_gettimeofday(tv);
+  sthread_enable();
+  return res;
+}
+
+unsigned int sleep(unsigned int seconds)
+{
+  if (not raw_sleep)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_sleep(seconds);
+
+  sthread_disable();
+  sthread_sleep(seconds);
+  sthread_enable();
+  return 0;
+}
+
+int usleep(useconds_t usec)
+{
+  if (not raw_usleep)
+    intercepter_init();
+
+  if (sthread_inside_simgrid)
+    return raw_usleep(usec);
+
+  sthread_disable();
+  sthread_sleep(((double)usec) / 1000000.);
+  sthread_enable();
+  return 0;
+}
+
+#if 0
+int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
+    *cond = sg_cond_init();
+    return 0;
+}
+
+int pthread_cond_signal(pthread_cond_t *cond) {
+       sg_cond_notify_one(*cond);
+    return 0;
+}
+
+int pthread_cond_broadcast(pthread_cond_t *cond) {
+       sg_cond_notify_all(*cond);
+    return 0;
+}
+
+int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
+       sg_cond_wait(*cond, *mutex);
+    return 0;
+}
+
+int pthread_cond_destroy(pthread_cond_t *cond) {
+       sg_cond_destroy(*cond);
+    return 0;
+}
+#endif
+>>>>>>> Convert sthread.c to C++.:src/sthread/sthread.cpp
 
 /* Trampoline for the real main() */
 static int (*raw_main)(int, char**, char**);
@@ -205,8 +473,9 @@ static int main_hook(int argc, char** argv, char** envp)
 }
 
 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
-int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
-                      void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
+extern "C" int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv,
+                                 int (*init)(int, char**, char**), void (*fini)(void), void (*rtld_fini)(void),
+                                 void* stack_end);
 
 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
@@ -215,11 +484,11 @@ int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, i
   raw_main = main;
 
   /* Find the real __libc_start_main()... */
-  typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
+  auto orig = reinterpret_cast<typeof(&__libc_start_main)>(dlsym(RTLD_NEXT, "__libc_start_main"));
   /* ... and call it with our custom main function */
 #if HAVE_VALGRIND_H
   /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
-  if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
+  if (getenv("VALGRIND_LIB") && not RUNNING_ON_VALGRIND)
     return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
 #endif
   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
index cf2b477..8fdc33f 100644 (file)
@@ -6,7 +6,7 @@
 /* SimGrid's pthread interposer. Intercepts most of the pthread and semaphore calls to model-check them.
  *
  * Intercepting on pthread is somewhat complicated by the fact that pthread is used everywhere in the system headers.
- * To reduce definition conflicts, our redefinitions of the pthread symbols (in sthread.c) load as little headers as
+ * To reduce definition conflicts, our redefinitions of the pthread symbols (in sthread.cpp) load as little headers as
  * possible. Thus, the actual implementations are separated in another file (sthread_impl.cpp) and are used as black
  * boxes by our redefinitions.
  *
index f641c2a..d4900ad 100644 (file)
@@ -230,7 +230,7 @@ set(SMPI_SRC
   )
 set(STHREAD_SRC
   src/sthread/sthread_impl.cpp
-  src/sthread/sthread.c
+  src/sthread/sthread.cpp
   src/sthread/sthread.h
   src/sthread/ObjectAccess.cpp
 )