Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sthread: Intercept gettimeofday + sleep + usleep
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Sun, 10 Jul 2022 18:00:43 +0000 (20:00 +0200)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Sun, 10 Jul 2022 22:22:11 +0000 (00:22 +0200)
src/sthread/sthread.c
src/sthread/sthread.h
src/sthread/sthread_impl.cpp

index 24213e0..0de4be3 100644 (file)
@@ -1,12 +1,13 @@
 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
 
 #define _GNU_SOURCE
-#include "src/internal_config.h"
 #include "src/sthread/sthread.h"
+#include "src/internal_config.h"
 #include <dlfcn.h>
 #include <pthread.h>
 #include <semaphore.h>
 #include <stdio.h>
+#include <unistd.h>
 
 #if HAVE_VALGRIND_H
 #include <stdlib.h>
@@ -146,6 +147,23 @@ int pthread_mutex_destroy(pthread_mutex_t* mutex)
   return res;
 }
 
+int gettimeofday(struct timeval* tv, void* tz)
+{
+  return sthread_gettimeofday(tv, tz);
+}
+
+unsigned int sleep(unsigned int seconds)
+{
+  sthread_sleep(seconds);
+  return 0;
+}
+
+int usleep(useconds_t usec)
+{
+  sthread_sleep(((double)usec) / 1000000.);
+  return 0;
+}
+
 #if 0
 int sem_init(sem_t *sem, int pshared, unsigned int value) {
        int res;
index 9bc6c50..1af8618 100644 (file)
@@ -10,6 +10,8 @@
 #ifndef SIMGRID_STHREAD_H
 #define SIMGRID_STHREAD_H
 
+#include <sys/time.h>
+
 #if defined(__ELF__)
 #define XBT_PUBLIC __attribute__((visibility("default")))
 #else
@@ -38,6 +40,9 @@ int sthread_mutex_trylock(sthread_mutex_t* mutex);
 int sthread_mutex_unlock(sthread_mutex_t* mutex);
 int sthread_mutex_destroy(sthread_mutex_t* mutex);
 
+int sthread_gettimeofday(struct timeval* tv, struct timezone* tz);
+void sthread_sleep(double seconds);
+
 #if defined(__cplusplus)
 }
 #endif
index d1a88a7..243aa7b 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>
@@ -129,6 +130,23 @@ int sthread_mutex_destroy(sthread_mutex_t* mutex)
   return 0;
 }
 
+int sthread_gettimeofday(struct timeval* tv, struct timezone* tz)
+{
+  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;