Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sure that sthread_inside_simgrid is initially 1 to protect all lib constructors
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Fri, 24 Jun 2022 23:11:09 +0000 (01:11 +0200)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Fri, 24 Jun 2022 23:14:17 +0000 (01:14 +0200)
Even the ones that we don't control, such as gdb's...

In addition, there is no need to define sthread_inside_simgrid in
libsimgrid. We can keep it to libsthread where it belongs.

Also do some other cleanups around.

examples/sthread/pthread-mutex-simple.c
src/sthread/sthread.c
src/sthread/sthread_impl.cpp
src/xbt/log.cpp
src/xbt/xbt_main.cpp

index ae928c2..f39de63 100644 (file)
@@ -27,8 +27,8 @@ int main(int argc, char* argv[])
   pthread_mutex_init(&mutex, NULL);
 
   pthread_t thread1, thread2;
-  fprintf(stderr, "prout\n");
   pthread_create(&thread1, NULL, thread1_fun, NULL);
+  fprintf(stderr, "here\n");
   pthread_create(&thread2, NULL, thread2_fun, NULL);
   // pthread_join(thread1, NULL);
   // pthread_join(thread2, NULL);
index 4c36bd7..b993434 100644 (file)
@@ -5,6 +5,9 @@
 #include <dlfcn.h>
 #include <pthread.h>
 #include <semaphore.h>
+#include <stdio.h>
+
+int sthread_inside_simgrid = 1; // whether sthread should leave pthread operations, or intercept them.
 
 /* We don't want to intercept pthread within simgrid. Instead we should provide the real implem to simgrid */
 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
@@ -187,7 +190,7 @@ int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, i
 
   /* Find the real __libc_start_main()... */
   typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
-
+  fprintf(stderr, "__libc_start_main\n");
   /* ... and call it with our custom main function */
   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
 }
\ No newline at end of file
index d2bb5d6..8dce02f 100644 (file)
@@ -1,11 +1,5 @@
 /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */
 
-#include <dlfcn.h>
-#include <pthread.h>
-#include <semaphore.h>
-#include <stdio.h>
-#include <stdlib.h>
-
 #include "smpi/smpi.h"
 #include <simgrid/actor.h>
 #include <simgrid/s4u/Actor.hpp>
 #include "src/internal_config.h"
 #include "src/sthread/sthread.h"
 
+#include <dlfcn.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <sstream>
+#include <stdio.h>
+#include <stdlib.h>
+#include <thread>
+
 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
 namespace sg4 = simgrid::s4u;
 
@@ -25,16 +27,18 @@ static sg4::Host* lilibeth = NULL;
 
 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
 {
-  XBT_INFO("sthread main() is starting");
-  sthread_inside_simgrid = 1;
+  std::ostringstream id;
+  id << std::this_thread::get_id();
+
+  XBT_INFO("sthread main() is starting in thread %s", id.str().c_str());
 
   sg4::Engine e(&argc, argv);
   auto* zone = sg4::create_full_zone("world");
   lilibeth   = zone->create_host("Lilibeth", 1e15);
   zone->seal();
-  sthread_inside_simgrid = 0;
 
   /* Launch the user's main() on an actor */
+  sthread_inside_simgrid   = 0;
   sg4::ActorPtr main_actor = sg4::Actor::create("tid 0", lilibeth, raw_main, argc, argv, envp);
 
   XBT_INFO("sthread main() is launching the simulation");
@@ -62,8 +66,6 @@ int sthread_create(unsigned long int* thread, const /*pthread_attr_t*/ void* att
 {
   static int TID = 1;
 
-  if (TID == 0) {
-  }
   TID++;
   int rank = 0;
 #if HAVE_SMPI
index bdb7d6f..11681b2 100644 (file)
@@ -247,8 +247,6 @@ int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority
   if (category->initialized)
     return priority >= category->threshold;
 
-  int old_inside_simgrid = sthread_inside_simgrid;
-  sthread_inside_simgrid = 1;
   if (log_cat_init_mutex != nullptr)
     log_cat_init_mutex->lock();
 
@@ -296,7 +294,6 @@ int _xbt_log_cat_init(xbt_log_category_t category, e_xbt_log_priority_t priority
   category->initialized = 1;
   if (log_cat_init_mutex != nullptr)
     log_cat_init_mutex->unlock();
-  sthread_inside_simgrid = old_inside_simgrid;
   return priority >= category->threshold;
 }
 
index f452565..d9e473e 100644 (file)
@@ -41,8 +41,6 @@ std::string binary_name;          /* Name of the system process containing us (m
 std::vector<std::string> cmdline; /* all we got in argv */
 } // namespace simgrid::xbt
 
-int sthread_inside_simgrid = 0; // whether sthread should leave pthread operations or intercept them.
-
 int xbt_initialized = 0;
 simgrid::config::Flag<bool> cfg_dbg_clean_atexit{
     "debug/clean-atexit",
@@ -82,7 +80,6 @@ static BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserv
 
 static void xbt_preinit()
 {
-  sthread_inside_simgrid = 1;
 #ifdef _WIN32
   SYSTEM_INFO si;
   GetSystemInfo(&si);
@@ -102,7 +99,6 @@ static void xbt_preinit()
   xbt_log_preinit();
   xbt_dict_preinit();
   atexit(xbt_postexit);
-  sthread_inside_simgrid = 0;
 }
 
 static void xbt_postexit()