Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sthread_inside_simgrid static into libsthread in the (vain) hope that it'll...
[simgrid.git] / src / sthread / sthread_impl.cpp
index d2bb5d6..19a19b8 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_enable();
   sg4::ActorPtr main_actor = sg4::Actor::create("tid 0", lilibeth, raw_main, argc, argv, envp);
 
   XBT_INFO("sthread main() is launching the simulation");
@@ -42,6 +46,7 @@ int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**
 
   return 0;
 }
+
 struct sthread_mutex {
   s4u_Mutex* mutex;
 };
@@ -52,19 +57,17 @@ static void thread_create_wrapper(void* (*user_function)(void*), void* param)
   if (SMPI_is_inited())
     SMPI_thread_create();
 #endif
-  sthread_inside_simgrid = 0;
+  sthread_enable();
   user_function(param);
-  sthread_inside_simgrid = 1;
+  sthread_disable();
 }
 
 int sthread_create(unsigned long int* thread, const /*pthread_attr_t*/ void* attr, void* (*start_routine)(void*),
                    void* arg)
 {
-  static int TID = 1;
-
-  if (TID == 0) {
-  }
+  static int TID = 0;
   TID++;
+  XBT_INFO("Create thread %d", TID);
   int rank = 0;
 #if HAVE_SMPI
   if (SMPI_is_inited())
@@ -79,6 +82,14 @@ int sthread_create(unsigned long int* thread, const /*pthread_attr_t*/ void* att
   *thread = reinterpret_cast<unsigned long>(actor.get());
   return 0;
 }
+int sthread_join(sthread_t thread, void** retval)
+{
+  sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
+  actor->join();
+  intrusive_ptr_release(actor.get());
+
+  return 0;
+}
 
 int sthread_mutex_init(sthread_mutex_t* mutex, const /*pthread_mutexattr_t*/ void* attr)
 {