Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'wifi_clean' into 'master'
[simgrid.git] / src / sthread / sthread_impl.cpp
index df1854109ba41460876d0d07ecd41a7d11fa142e..77219823430fb795f28e5d9831d2356747b75836 100644 (file)
@@ -12,6 +12,7 @@
 #include "src/internal_config.h"
 #include "src/sthread/sthread.h"
 
+#include <cmath>
 #include <dlfcn.h>
 #include <pthread.h>
 #include <semaphore.h>
@@ -27,6 +28,12 @@ static sg4::Host* lilibeth = nullptr;
 
 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
 {
+  /* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
+  for (int i = 0; envp[i] != nullptr; i++)
+    if (strncmp(envp[i], "SMPI_GLOBAL_SIZE", strlen("SMPI_GLOBAL_SIZE")) == 0)
+      return raw_main(argc, argv, envp);
+
+  /* If not in SMPI, the old main becomes an actor in a newly created simulation */
   std::ostringstream id;
   id << std::this_thread::get_id();
 
@@ -123,6 +130,23 @@ int sthread_mutex_destroy(sthread_mutex_t* mutex)
   return 0;
 }
 
+int sthread_gettimeofday(struct timeval* tv)
+{
+  if (tv) {
+    double now   = simgrid::s4u::Engine::get_clock();
+    double secs  = trunc(now);
+    double usecs = (now - secs) * 1e6;
+    tv->tv_sec   = static_cast<time_t>(secs);
+    tv->tv_usec  = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t (or useconds_t on WIN32)
+  }
+  return 0;
+}
+
+void sthread_sleep(double seconds)
+{
+  simgrid::s4u::this_actor::sleep_for(seconds);
+}
+
 #if 0
 int sem_init(sem_t *sem, int pshared, unsigned int value) {
        int res;
@@ -142,11 +166,6 @@ int sem_post(sem_t *sem) {
        return raw_sem_post(sem);
 }
 
-int pthread_join(pthread_t thread, void **retval) {
-       sg_actor_join(thread, -1);
-    return 0;
-}
-
 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
     *cond = sg_cond_init();
     return 0;