Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Attempt to inline sthread_enable() / sthread_disable().
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 19 Jul 2022 08:03:06 +0000 (10:03 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 27 Nov 2023 08:45:06 +0000 (09:45 +0100)
src/sthread/sthread.cpp
src/sthread/sthread.h
src/xbt/xbt_misc.cpp

index 0407747..757c0ff 100644 (file)
@@ -118,15 +118,6 @@ static void intercepter_init()
 >>>>>>> Convert sthread.c to C++.:src/sthread/sthread.cpp
 }
 
-static bool sthread_inside_simgrid = true;
-void sthread_enable(void)
-{ // Start intercepting all pthread calls
-  sthread_inside_simgrid = false;
-}
-void sthread_disable(void)
-{ // Stop intercepting all pthread calls
-  sthread_inside_simgrid = true;
-}
 <<<<<<< HEAD:src/sthread/sthread.c
 
 #define _STHREAD_CONCAT(a, b) a##b
@@ -208,7 +199,7 @@ int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_
   if (not raw_pthread_create)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_pthread_create(thread, attr, start_routine, arg);
 
   sthread_disable();
@@ -220,7 +211,8 @@ int pthread_join(pthread_t thread, void** retval)
 {
   if (not raw_pthread_join)
     intercepter_init();
-  if (sthread_inside_simgrid)
+
+  if (sthread_inside_simgrid())
     return raw_pthread_join(thread, retval);
 
   sthread_disable();
@@ -234,7 +226,7 @@ int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
   if (not raw_mutex_init)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_mutex_init(mutex, attr);
 
   sthread_disable();
@@ -248,7 +240,7 @@ int pthread_mutex_lock(pthread_mutex_t* mutex)
   if (not raw_mutex_lock)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_mutex_lock(mutex);
 
   sthread_disable();
@@ -262,7 +254,7 @@ int pthread_mutex_trylock(pthread_mutex_t* mutex)
   if (not raw_mutex_trylock)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_mutex_trylock(mutex);
 
   sthread_disable();
@@ -276,7 +268,7 @@ int pthread_mutex_unlock(pthread_mutex_t* mutex)
   if (not raw_mutex_unlock)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_mutex_unlock(mutex);
 
   sthread_disable();
@@ -289,7 +281,7 @@ int pthread_mutex_destroy(pthread_mutex_t* mutex)
   if (not raw_mutex_destroy)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_mutex_destroy(mutex);
 
   sthread_disable();
@@ -302,7 +294,7 @@ int sem_init(sem_t* sem, int pshared, unsigned int value)
   if (not raw_sem_init)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_sem_init(sem, pshared, value);
 
   sthread_disable();
@@ -315,7 +307,7 @@ int sem_destroy(sem_t* sem)
   if (not raw_sem_destroy)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_sem_destroy(sem);
 
   sthread_disable();
@@ -328,7 +320,7 @@ int sem_post(sem_t* sem)
   if (not raw_sem_post)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_sem_post(sem);
 
   sthread_disable();
@@ -341,7 +333,7 @@ int sem_wait(sem_t* sem)
   if (not raw_sem_wait)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_sem_wait(sem);
 
   sthread_disable();
@@ -354,7 +346,7 @@ int sem_trywait(sem_t* sem)
   if (not raw_sem_trywait)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_sem_trywait(sem);
 
   sthread_disable();
@@ -367,7 +359,7 @@ int sem_timedwait(sem_t* sem, const struct timespec* abs_timeout)
   if (not raw_sem_timedwait)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_sem_timedwait(sem, abs_timeout);
 
   sthread_disable();
@@ -398,7 +390,7 @@ int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz)
   if (not raw_gettimeofday)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_gettimeofday(tv, tz);
 
   sthread_disable();
@@ -412,7 +404,7 @@ unsigned int sleep(unsigned int seconds)
   if (not raw_sleep)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_sleep(seconds);
 
   sthread_disable();
@@ -426,7 +418,7 @@ int usleep(useconds_t usec)
   if (not raw_usleep)
     intercepter_init();
 
-  if (sthread_inside_simgrid)
+  if (sthread_inside_simgrid())
     return raw_usleep(usec);
 
   sthread_disable();
index 8fdc33f..6e05674 100644 (file)
 #include <sys/time.h>
 
 #if defined(__cplusplus)
+
+inline bool& sthread_inside_simgrid()
+{
+  static bool value = true;
+  return value;
+}
+
+inline void sthread_enable(void)
+{ // Start intercepting all pthread calls
+  sthread_inside_simgrid() = false;
+}
+
+inline void sthread_disable(void)
+{ // Stop intercepting all pthread calls
+  sthread_inside_simgrid() = true;
+}
+
 extern "C" {
 #endif
 
 // Launch the simulation. The old main function (passed as a parameter) is launched as an actor
 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**));
-XBT_PUBLIC void sthread_enable(void);  // Start intercepting all pthread calls
-XBT_PUBLIC void sthread_disable(void); // Stop intercepting all pthread calls
 
 typedef unsigned long int sthread_t;
 int sthread_create(sthread_t* thread, const /*pthread_attr_t*/ void* attr, void* (*start_routine)(void*), void* arg);
index d5522f8..d0960ec 100644 (file)
@@ -26,15 +26,6 @@ XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories"); /* lives here even if that's
 const int xbt_pagesize = static_cast<int>(sysconf(_SC_PAGESIZE));
 const int xbt_pagebits = static_cast<int>(log2(xbt_pagesize));
 
-XBT_ATTRIB_NOINLINE void sthread_enable()
-{ // These symbols are used from ContextSwapped in any case, but they are only useful
-  asm("");
-}
-XBT_ATTRIB_NOINLINE void sthread_disable()
-{ //  when libsthread is LD_PRELOADED. In this case, sthread's implem gets used instead.
-  asm("");
-}
-
 /* these two functions belong to xbt/sysdep.h, which have no corresponding .c file */
 /** @brief like xbt_free, but you can be sure that it is a function  */
 void xbt_free_f(void* p) noexcept(noexcept(::free))