X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/c4830be809e3969945e79f543151a676bbc12a1f..48296e83ce3ba1fa3658a3a74d10a536e33b3849:/src/sthread/sthread_impl.cpp diff --git a/src/sthread/sthread_impl.cpp b/src/sthread/sthread_impl.cpp index 1780543ad0..7721982343 100644 --- a/src/sthread/sthread_impl.cpp +++ b/src/sthread/sthread_impl.cpp @@ -12,6 +12,7 @@ #include "src/internal_config.h" #include "src/sthread/sthread.h" +#include #include #include #include @@ -23,10 +24,16 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter"); namespace sg4 = simgrid::s4u; -static sg4::Host* lilibeth = NULL; +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(); @@ -43,6 +50,7 @@ int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char** XBT_INFO("Starting the simulation."); sg4::Engine::get_instance()->run(); + sthread_disable(); XBT_INFO("All threads exited. Terminating the simulation."); return 0; @@ -63,7 +71,7 @@ static void thread_create_wrapper(void* (*user_function)(void*), void* param) sthread_disable(); } -int sthread_create(unsigned long int* thread, const /*pthread_attr_t*/ void* attr, void* (*start_routine)(void*), +int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*), void* arg) { static int TID = 0; @@ -74,16 +82,15 @@ int sthread_create(unsigned long int* thread, const /*pthread_attr_t*/ void* att if (SMPI_is_inited()) MPI_Comm_rank(MPI_COMM_WORLD, &rank); #endif - char name[100]; - sprintf(name, "%d:%d", rank, TID); - sg4::ActorPtr actor = sg4::Actor::init(name, lilibeth); + std::string name = simgrid::xbt::string_printf("%d:%d", rank, TID); + sg4::ActorPtr actor = sg4::Actor::init(name.c_str(), lilibeth); actor->start(thread_create_wrapper, start_routine, arg); intrusive_ptr_add_ref(actor.get()); *thread = reinterpret_cast(actor.get()); return 0; } -int sthread_join(sthread_t thread, void** retval) +int sthread_join(sthread_t thread, void** /*retval*/) { sg4::ActorPtr actor(reinterpret_cast(thread)); actor->join(); @@ -92,7 +99,7 @@ int sthread_join(sthread_t thread, void** retval) return 0; } -int sthread_mutex_init(sthread_mutex_t* mutex, const /*pthread_mutexattr_t*/ void* attr) +int sthread_mutex_init(sthread_mutex_t* mutex, const void* /*pthread_mutexattr_t* attr*/) { auto m = sg4::Mutex::create(); intrusive_ptr_add_ref(m.get()); @@ -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(secs); + tv->tv_usec = static_casttv_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;