From 8344f32ee2339f2b53a6e79ac0e9aa3a93384f58 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sun, 12 Nov 2023 19:04:44 +0100 Subject: [PATCH] Refactorize the sthread interception of functions --- src/sthread/sthread.c | 290 +++++++---------------------------- src/sthread/sthread.h | 3 +- src/sthread/sthread_impl.cpp | 9 +- 3 files changed, 66 insertions(+), 236 deletions(-) diff --git a/src/sthread/sthread.c b/src/sthread/sthread.c index 2005f297ad..7ba6a23181 100644 --- a/src/sthread/sthread.c +++ b/src/sthread/sthread.c @@ -22,11 +22,11 @@ /* We don't want to intercept pthread within SimGrid. Instead we should provide the real implem to SimGrid */ static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*); static int (*raw_pthread_join)(pthread_t, void**); -static int (*raw_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*); -static int (*raw_mutex_lock)(pthread_mutex_t*); -static int (*raw_mutex_trylock)(pthread_mutex_t*); -static int (*raw_mutex_unlock)(pthread_mutex_t*); -static int (*raw_mutex_destroy)(pthread_mutex_t*); +static int (*raw_pthread_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*); +static int (*raw_pthread_mutex_lock)(pthread_mutex_t*); +static int (*raw_pthread_mutex_trylock)(pthread_mutex_t*); +static int (*raw_pthread_mutex_unlock)(pthread_mutex_t*); +static int (*raw_pthread_mutex_destroy)(pthread_mutex_t*); static int (*raw_pthread_mutexattr_init)(pthread_mutexattr_t*); static int (*raw_pthread_mutexattr_settype)(pthread_mutexattr_t*, int); @@ -50,11 +50,11 @@ static void intercepter_init() { raw_pthread_create = dlsym(RTLD_NEXT, "pthread_create"); raw_pthread_join = dlsym(RTLD_NEXT, "pthread_join"); - raw_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init"); - raw_mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock"); - raw_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock"); - raw_mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock"); - raw_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy"); + raw_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init"); + raw_pthread_mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock"); + raw_pthread_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock"); + raw_pthread_mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock"); + raw_pthread_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy"); raw_pthread_mutexattr_init = dlsym(RTLD_NEXT, "pthread_mutexattr_init"); raw_pthread_mutexattr_settype = dlsym(RTLD_NEXT, "pthread_mutexattr_settype"); @@ -84,22 +84,9 @@ void sthread_disable(void) { // Stop intercepting all pthread calls sthread_inside_simgrid = 1; } -int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg) -{ - if (raw_pthread_create == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_pthread_create(thread, attr, start_routine, arg); - - sthread_disable(); - int res = sthread_create((sthread_t*)thread, attr, start_routine, arg); - sthread_enable(); - return res; -} #define _STHREAD_CONCAT(a, b) a##b -#define intercepted_call(name, raw_params, call_params, sim_params) \ +#define intercepted_pthcall(name, raw_params, call_params, sim_params) \ int _STHREAD_CONCAT(pthread_, name) raw_params \ { \ if (_STHREAD_CONCAT(raw_pthread_, name) == NULL) \ @@ -113,175 +100,49 @@ int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_ return res; \ } -intercepted_call(mutexattr_init, (pthread_mutexattr_t * attr), (attr), ((sthread_mutexattr_t*)attr)); -intercepted_call(mutexattr_settype, (pthread_mutexattr_t * attr, int type), (attr, type), - ((sthread_mutexattr_t*)attr, type)); -intercepted_call(mutexattr_gettype, (const pthread_mutexattr_t* restrict attr, int* type), (attr, type), - ((sthread_mutexattr_t*)attr, type)); -intercepted_call(mutexattr_setrobust, (pthread_mutexattr_t* restrict attr, int robustness), (attr, robustness), - ((sthread_mutexattr_t*)attr, robustness)); -intercepted_call(mutexattr_getrobust, (const pthread_mutexattr_t* restrict attr, int* restrict robustness), - (attr, robustness), ((sthread_mutexattr_t*)attr, robustness)); - -int pthread_join(pthread_t thread, void** retval) -{ - if (raw_pthread_join == NULL) - intercepter_init(); - if (sthread_inside_simgrid) - return raw_pthread_join(thread, retval); - - sthread_disable(); - int res = sthread_join((sthread_t)thread, retval); - sthread_enable(); - return res; -} - -int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) -{ - if (raw_mutex_init == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_mutex_init(mutex, attr); - - sthread_disable(); - int res = sthread_mutex_init((sthread_mutex_t*)mutex, (sthread_mutexattr_t*)attr); - sthread_enable(); - return res; -} - -int pthread_mutex_lock(pthread_mutex_t* mutex) -{ - if (raw_mutex_lock == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_mutex_lock(mutex); - - sthread_disable(); - int res = sthread_mutex_lock((sthread_mutex_t*)mutex); - sthread_enable(); - return res; -} - -int pthread_mutex_trylock(pthread_mutex_t* mutex) -{ - if (raw_mutex_trylock == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_mutex_trylock(mutex); - - sthread_disable(); - int res = sthread_mutex_trylock((sthread_mutex_t*)mutex); - sthread_enable(); - return res; -} - -int pthread_mutex_unlock(pthread_mutex_t* mutex) -{ - if (raw_mutex_unlock == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_mutex_unlock(mutex); - - sthread_disable(); - int res = sthread_mutex_unlock((sthread_mutex_t*)mutex); - sthread_enable(); - return res; -} -int pthread_mutex_destroy(pthread_mutex_t* mutex) -{ - if (raw_mutex_destroy == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_mutex_destroy(mutex); - - sthread_disable(); - int res = sthread_mutex_destroy((sthread_mutex_t*)mutex); - sthread_enable(); - return res; -} -int sem_init(sem_t* sem, int pshared, unsigned int value) -{ - if (raw_sem_init == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_sem_init(sem, pshared, value); - - sthread_disable(); - int res = sthread_sem_init((sthread_sem_t*)sem, pshared, value); - sthread_enable(); - return res; -} -int sem_destroy(sem_t* sem) -{ - if (raw_sem_destroy == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_sem_destroy(sem); - - sthread_disable(); - int res = sthread_sem_destroy((sthread_sem_t*)sem); - sthread_enable(); - return res; -} -int sem_post(sem_t* sem) -{ - if (raw_sem_post == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_sem_post(sem); - - sthread_disable(); - int res = sthread_sem_post((sthread_sem_t*)sem); - sthread_enable(); - return res; -} -int sem_wait(sem_t* sem) -{ - if (raw_sem_wait == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_sem_wait(sem); - - sthread_disable(); - int res = sthread_sem_wait((sthread_sem_t*)sem); - sthread_enable(); - return res; -} -int sem_trywait(sem_t* sem) -{ - if (raw_sem_trywait == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_sem_trywait(sem); - - sthread_disable(); - int res = sthread_sem_trywait((sthread_sem_t*)sem); - sthread_enable(); - return res; -} -int sem_timedwait(sem_t* sem, const struct timespec* abs_timeout) -{ - if (raw_sem_timedwait == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_sem_timedwait(sem, abs_timeout); +intercepted_pthcall(mutexattr_init, (pthread_mutexattr_t * attr), (attr), ((sthread_mutexattr_t*)attr)); +intercepted_pthcall(mutexattr_settype, (pthread_mutexattr_t * attr, int type), (attr, type), + ((sthread_mutexattr_t*)attr, type)); +intercepted_pthcall(mutexattr_gettype, (const pthread_mutexattr_t* restrict attr, int* type), (attr, type), + ((sthread_mutexattr_t*)attr, type)); +intercepted_pthcall(mutexattr_setrobust, (pthread_mutexattr_t* restrict attr, int robustness), (attr, robustness), + ((sthread_mutexattr_t*)attr, robustness)); +intercepted_pthcall(mutexattr_getrobust, (const pthread_mutexattr_t* restrict attr, int* restrict robustness), + (attr, robustness), ((sthread_mutexattr_t*)attr, robustness)); + +intercepted_pthcall(create, (pthread_t * thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg), + (thread, attr, start_routine, arg), ((sthread_t*)thread, attr, start_routine, arg)); +intercepted_pthcall(join, (pthread_t thread, void** retval), (thread, retval), ((sthread_t)thread, retval)); + +intercepted_pthcall(mutex_init, (pthread_mutex_t * mutex, const pthread_mutexattr_t* attr), (mutex, attr), + ((sthread_mutex_t*)mutex, (sthread_mutexattr_t*)attr)); +intercepted_pthcall(mutex_lock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex)); +intercepted_pthcall(mutex_trylock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex)); +intercepted_pthcall(mutex_unlock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex)); +intercepted_pthcall(mutex_destroy, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex)); + +#define intercepted_call(rettype, name, raw_params, call_params, sim_params) \ + rettype name raw_params \ + { \ + if (_STHREAD_CONCAT(raw_, name) == NULL) \ + intercepter_init(); \ + if (sthread_inside_simgrid) \ + return _STHREAD_CONCAT(raw_, name) call_params; \ + \ + sthread_disable(); \ + rettype res = _STHREAD_CONCAT(sthread_, name) sim_params; \ + sthread_enable(); \ + return res; \ + } - sthread_disable(); - int res = sthread_sem_timedwait((sthread_sem_t*)sem, abs_timeout); - sthread_enable(); - return res; -} +intercepted_call(int, sem_init, (sem_t * sem, int pshared, unsigned int value), (sem, pshared, value), + ((sthread_sem_t*)sem, pshared, value)); +intercepted_call(int, sem_destroy, (sem_t * sem), (sem), ((sthread_sem_t*)sem)); +intercepted_call(int, sem_post, (sem_t * sem), (sem), ((sthread_sem_t*)sem)); +intercepted_call(int, sem_wait, (sem_t * sem), (sem), ((sthread_sem_t*)sem)); +intercepted_call(int, sem_trywait, (sem_t * sem), (sem), ((sthread_sem_t*)sem)); +intercepted_call(int, sem_timedwait, (sem_t * sem, const struct timespec* abs_timeout), (sem, abs_timeout), + ((sthread_sem_t*)sem, abs_timeout)); /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday. Other implementations use "void *" instead. */ @@ -293,48 +154,9 @@ int sem_timedwait(sem_t* sem, const struct timespec* abs_timeout) #ifndef TIMEZONE_TYPE #define TIMEZONE_TYPE void #endif - -int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz) -{ - if (raw_gettimeofday == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_gettimeofday(tv, tz); - - sthread_disable(); - int res = sthread_gettimeofday(tv); - sthread_enable(); - return res; -} - -unsigned int sleep(unsigned int seconds) -{ - if (raw_sleep == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_sleep(seconds); - - sthread_disable(); - sthread_sleep(seconds); - sthread_enable(); - return 0; -} - -int usleep(useconds_t usec) -{ - if (raw_usleep == NULL) - intercepter_init(); - - if (sthread_inside_simgrid) - return raw_usleep(usec); - - sthread_disable(); - sthread_sleep(((double)usec) / 1000000.); - sthread_enable(); - return 0; -} +intercepted_call(int, gettimeofday, (struct timeval * tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz), (tv, tz), (tv)); +intercepted_call(unsigned int, sleep, (unsigned int seconds), (seconds), (seconds)); +intercepted_call(int, usleep, (useconds_t usec), (usec), (((double)usec) / 1000000.)); #if 0 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) { diff --git a/src/sthread/sthread.h b/src/sthread/sthread.h index f0a9117db1..4a518667f8 100644 --- a/src/sthread/sthread.h +++ b/src/sthread/sthread.h @@ -63,7 +63,8 @@ int sthread_sem_trywait(sthread_sem_t* sem); int sthread_sem_timedwait(sthread_sem_t* sem, const struct timespec* abs_timeout); int sthread_gettimeofday(struct timeval* tv); -void sthread_sleep(double seconds); +unsigned int sthread_sleep(double seconds); +int sthread_usleep(double seconds); int sthread_access_begin(void* objaddr, const char* objname, const char* file, int line, const char* function); void sthread_access_end(void* objaddr, const char* objname, const char* file, int line, const char* function); diff --git a/src/sthread/sthread_impl.cpp b/src/sthread/sthread_impl.cpp index c1b58b2699..e54a9c3014 100644 --- a/src/sthread/sthread_impl.cpp +++ b/src/sthread/sthread_impl.cpp @@ -273,10 +273,17 @@ int sthread_gettimeofday(struct timeval* tv) return 0; } -void sthread_sleep(double seconds) +unsigned int sthread_sleep(double seconds) { XBT_DEBUG("sleep(%lf)", seconds); simgrid::s4u::this_actor::sleep_for(seconds); + return 0; +} +int sthread_usleep(double seconds) +{ + XBT_DEBUG("sleep(%lf)", seconds); + simgrid::s4u::this_actor::sleep_for(seconds); + return 0; } #if 0 -- 2.20.1