X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/1e9ffea6fdfe24e3268eebcda6fa00d34d6a652b..a6d5fdeb22df5f5b1ca02a3dad8ba6094202a4f3:/src/sthread/sthread_impl.cpp diff --git a/src/sthread/sthread_impl.cpp b/src/sthread/sthread_impl.cpp index 5ec2a0435a..45ba730029 100644 --- a/src/sthread/sthread_impl.cpp +++ b/src/sthread/sthread_impl.cpp @@ -1,3 +1,8 @@ +/* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */ #include "smpi/smpi.h" @@ -7,6 +12,7 @@ #include #include #include +#include #include #include @@ -20,6 +26,7 @@ #include #include #include +#include #include XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter"); @@ -31,7 +38,7 @@ int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, 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) + if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0) return raw_main(argc, argv, envp); /* If not in SMPI, the old main becomes an actor in a newly created simulation */ @@ -61,30 +68,32 @@ struct sthread_mutex { s4u_Mutex* mutex; }; -static void thread_create_wrapper(void* (*user_function)(void*), void* param) -{ -#if HAVE_SMPI - if (SMPI_is_inited()) - SMPI_thread_create(); -#endif - sthread_enable(); - user_function(param); - sthread_disable(); -} - int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*), void* arg) { static int TID = 0; TID++; XBT_VERB("Create thread %d", TID); - int rank = 0; + std::string name = std::string("thread ") + std::to_string(TID); #if HAVE_SMPI - if (SMPI_is_inited()) + if (SMPI_is_inited()) { + int rank = 0; MPI_Comm_rank(MPI_COMM_WORLD, &rank); + name = simgrid::xbt::string_printf("%d:%d", rank, TID); + } #endif - std::string name = simgrid::xbt::string_printf("%d:%d", rank, TID); - sg4::ActorPtr actor = sg4::Actor::create(name, lilibeth, thread_create_wrapper, start_routine, arg); + sg4::ActorPtr actor = sg4::Actor::create( + name, lilibeth, + [](auto* user_function, auto* param) { +#if HAVE_SMPI + if (SMPI_is_inited()) + SMPI_thread_create(); +#endif + sthread_enable(); + user_function(param); + sthread_disable(); + }, + start_routine, arg); intrusive_ptr_add_ref(actor.get()); *thread = reinterpret_cast(actor.get()); @@ -145,6 +154,48 @@ int sthread_mutex_destroy(sthread_mutex_t* mutex) intrusive_ptr_release(static_cast(mutex->mutex)); return 0; } +int sthread_sem_init(sthread_sem_t* sem, int /*pshared*/, unsigned int value) +{ + auto s = sg4::Semaphore::create(value); + intrusive_ptr_add_ref(s.get()); + + sem->sem = s.get(); + return 0; +} +int sthread_sem_destroy(sthread_sem_t* sem) +{ + intrusive_ptr_release(static_cast(sem->sem)); + return 0; +} +int sthread_sem_post(sthread_sem_t* sem) +{ + static_cast(sem->sem)->release(); + return 0; +} +int sthread_sem_wait(sthread_sem_t* sem) +{ + static_cast(sem->sem)->acquire(); + return 0; +} +int sthread_sem_trywait(sthread_sem_t* sem) +{ + auto* s = static_cast(sem->sem); + if (s->would_block()) { + errno = EAGAIN; + return -1; + } + s->acquire(); + return 0; +} +int sthread_sem_timedwait(sthread_sem_t* sem, const struct timespec* abs_timeout) +{ + if (static_cast(sem->sem)->acquire_timeout(static_cast(abs_timeout->tv_sec) + + static_cast(abs_timeout->tv_nsec) / 1E9)) { + errno = ETIMEDOUT; + return -1; + } + return 0; +} int sthread_gettimeofday(struct timeval* tv) { @@ -153,7 +204,7 @@ int sthread_gettimeofday(struct timeval* tv) 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) + tv->tv_usec = static_casttv_usec)>(usecs); // suseconds_t } return 0; } @@ -164,24 +215,6 @@ void sthread_sleep(double seconds) } #if 0 -int sem_init(sem_t *sem, int pshared, unsigned int value) { - int res; - - res=raw_sem_init(sem,pshared,value); - return res; -} - -int sem_wait(sem_t *sem) { - int res; - - res = raw_sem_wait(sem); - return res; -} - -int sem_post(sem_t *sem) { - return raw_sem_post(sem); -} - int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) { *cond = sg_cond_init(); return 0;