X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/28e6a5182f40acf3ea901f1c092ea680c33141d7:/src/sthread/sthread.c..cc2913b56286d901265196839351b53dbff7b0b9:/src/sthread/sthread.cpp diff --git a/src/sthread/sthread.c b/src/sthread/sthread.cpp similarity index 62% rename from src/sthread/sthread.c rename to src/sthread/sthread.cpp index ad27e3fa1d..04077479f8 100644 --- a/src/sthread/sthread.c +++ b/src/sthread/sthread.cpp @@ -5,7 +5,6 @@ /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */ -#define _GNU_SOURCE #include "src/sthread/sthread.h" #include "src/internal_config.h" #include @@ -59,6 +58,7 @@ static int (*raw_sem_timedwait)(sem_t*, const struct timespec*); static void intercepter_init() { +<<<<<<< HEAD:src/sthread/sthread.c raw_pthread_create = dlsym(RTLD_NEXT, "pthread_create"); raw_pthread_join = dlsym(RTLD_NEXT, "pthread_join"); raw_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init"); @@ -95,17 +95,39 @@ static void intercepter_init() raw_sem_destroy = dlsym(RTLD_NEXT, "sem_destroy"); raw_sem_trywait = dlsym(RTLD_NEXT, "sem_trywait"); raw_sem_timedwait = dlsym(RTLD_NEXT, "sem_timedwait"); +======= + raw_pthread_create = reinterpret_cast(dlsym(RTLD_NEXT, "pthread_create")); + raw_pthread_join = reinterpret_cast(dlsym(RTLD_NEXT, "pthread_join")); + raw_mutex_init = reinterpret_cast(dlsym(RTLD_NEXT, "pthread_mutex_init")); + raw_mutex_lock = reinterpret_cast(dlsym(RTLD_NEXT, "pthread_mutex_lock")); + raw_mutex_trylock = reinterpret_cast(dlsym(RTLD_NEXT, "pthread_mutex_trylock")); + raw_mutex_unlock = reinterpret_cast(dlsym(RTLD_NEXT, "pthread_mutex_unlock")); + raw_mutex_destroy = reinterpret_cast(dlsym(RTLD_NEXT, "pthread_mutex_destroy")); + + raw_sleep = reinterpret_cast(dlsym(RTLD_NEXT, "sleep")); + raw_usleep = reinterpret_cast(dlsym(RTLD_NEXT, "usleep")); + raw_gettimeofday = reinterpret_cast(dlsym(RTLD_NEXT, "gettimeofday")); + + raw_sem_open = reinterpret_cast(dlsym(RTLD_NEXT, "sem_open")); + raw_sem_init = reinterpret_cast(dlsym(RTLD_NEXT, "sem_init")); + raw_sem_wait = reinterpret_cast(dlsym(RTLD_NEXT, "sem_wait")); + raw_sem_post = reinterpret_cast(dlsym(RTLD_NEXT, "sem_post")); + raw_sem_destroy = reinterpret_cast(dlsym(RTLD_NEXT, "sem_destroy")); + raw_sem_trywait = reinterpret_cast(dlsym(RTLD_NEXT, "sem_trywait")); + raw_sem_timedwait = reinterpret_cast(dlsym(RTLD_NEXT, "sem_timedwait")); +>>>>>>> Convert sthread.c to C++.:src/sthread/sthread.cpp } -static int sthread_inside_simgrid = 1; +static bool sthread_inside_simgrid = true; void sthread_enable(void) { // Start intercepting all pthread calls - sthread_inside_simgrid = 0; + sthread_inside_simgrid = false; } void sthread_disable(void) { // Stop intercepting all pthread calls - sthread_inside_simgrid = 1; + sthread_inside_simgrid = true; } +<<<<<<< HEAD:src/sthread/sthread.c #define _STHREAD_CONCAT(a, b) a##b #define intercepted_pthcall(name, raw_params, call_params, sim_params) \ @@ -180,6 +202,180 @@ 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)); +======= +int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg) +{ + if (not raw_pthread_create) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_pthread_create(thread, attr, start_routine, arg); + + sthread_disable(); + int res = sthread_create(reinterpret_cast(thread), attr, start_routine, arg); + sthread_enable(); + return res; +} +int pthread_join(pthread_t thread, void** retval) +{ + if (not raw_pthread_join) + intercepter_init(); + if (sthread_inside_simgrid) + return raw_pthread_join(thread, retval); + + sthread_disable(); + int res = sthread_join(reinterpret_cast(thread), retval); + sthread_enable(); + return res; +} + +int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) +{ + if (not raw_mutex_init) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_mutex_init(mutex, attr); + + sthread_disable(); + int res = sthread_mutex_init(reinterpret_cast(mutex), attr); + sthread_enable(); + return res; +} + +int pthread_mutex_lock(pthread_mutex_t* mutex) +{ + if (not raw_mutex_lock) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_mutex_lock(mutex); + + sthread_disable(); + int res = sthread_mutex_lock(reinterpret_cast(mutex)); + sthread_enable(); + return res; +} + +int pthread_mutex_trylock(pthread_mutex_t* mutex) +{ + if (not raw_mutex_trylock) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_mutex_trylock(mutex); + + sthread_disable(); + int res = sthread_mutex_trylock(reinterpret_cast(mutex)); + sthread_enable(); + return res; +} + +int pthread_mutex_unlock(pthread_mutex_t* mutex) +{ + if (not raw_mutex_unlock) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_mutex_unlock(mutex); + + sthread_disable(); + int res = sthread_mutex_unlock(reinterpret_cast(mutex)); + sthread_enable(); + return res; +} +int pthread_mutex_destroy(pthread_mutex_t* mutex) +{ + if (not raw_mutex_destroy) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_mutex_destroy(mutex); + + sthread_disable(); + int res = sthread_mutex_destroy(reinterpret_cast(mutex)); + sthread_enable(); + return res; +} +int sem_init(sem_t* sem, int pshared, unsigned int value) +{ + if (not raw_sem_init) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_sem_init(sem, pshared, value); + + sthread_disable(); + int res = sthread_sem_init(reinterpret_cast(sem), pshared, value); + sthread_enable(); + return res; +} +int sem_destroy(sem_t* sem) +{ + if (not raw_sem_destroy) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_sem_destroy(sem); + + sthread_disable(); + int res = sthread_sem_destroy(reinterpret_cast(sem)); + sthread_enable(); + return res; +} +int sem_post(sem_t* sem) +{ + if (not raw_sem_post) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_sem_post(sem); + + sthread_disable(); + int res = sthread_sem_post(reinterpret_castsem); + sthread_enable(); + return res; +} +int sem_wait(sem_t* sem) +{ + if (not raw_sem_wait) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_sem_wait(sem); + + sthread_disable(); + int res = sthread_sem_wait(reinterpret_cast(sem)); + sthread_enable(); + return res; +} +int sem_trywait(sem_t* sem) +{ + if (not raw_sem_trywait) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_sem_trywait(sem); + + sthread_disable(); + int res = sthread_sem_trywait(reinterpret_cast(sem)); + sthread_enable(); + return res; +} +int sem_timedwait(sem_t* sem, const struct timespec* abs_timeout) +{ + if (not raw_sem_timedwait) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_sem_timedwait(sem, abs_timeout); + + sthread_disable(); + int res = sthread_sem_timedwait(reinterpret_cast(sem), abs_timeout); + sthread_enable(); + return res; +} +>>>>>>> Convert sthread.c to C++.:src/sthread/sthread.cpp /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday. Other implementations use "void *" instead. */ @@ -191,9 +387,81 @@ intercepted_call(int, sem_timedwait, (sem_t * sem, const struct timespec* abs_ti #ifndef TIMEZONE_TYPE #define TIMEZONE_TYPE void #endif +<<<<<<< HEAD:src/sthread/sthread.c 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.)); +======= + +int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz) +{ + if (not raw_gettimeofday) + 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 (not raw_sleep) + 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 (not raw_usleep) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_usleep(usec); + + sthread_disable(); + sthread_sleep(((double)usec) / 1000000.); + sthread_enable(); + return 0; +} + +#if 0 +int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) { + *cond = sg_cond_init(); + return 0; +} + +int pthread_cond_signal(pthread_cond_t *cond) { + sg_cond_notify_one(*cond); + return 0; +} + +int pthread_cond_broadcast(pthread_cond_t *cond) { + sg_cond_notify_all(*cond); + return 0; +} + +int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { + sg_cond_wait(*cond, *mutex); + return 0; +} + +int pthread_cond_destroy(pthread_cond_t *cond) { + sg_cond_destroy(*cond); + return 0; +} +#endif +>>>>>>> Convert sthread.c to C++.:src/sthread/sthread.cpp /* Trampoline for the real main() */ static int (*raw_main)(int, char**, char**); @@ -205,8 +473,9 @@ static int main_hook(int argc, char** argv, char** envp) } /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */ -int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**), - void (*fini)(void), void (*rtld_fini)(void), void* stack_end); +extern "C" int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, + int (*init)(int, char**, char**), void (*fini)(void), void (*rtld_fini)(void), + void* stack_end); int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**), void (*fini)(void), void (*rtld_fini)(void), void* stack_end) @@ -215,11 +484,11 @@ int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, i raw_main = main; /* Find the real __libc_start_main()... */ - typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main"); + auto orig = reinterpret_cast(dlsym(RTLD_NEXT, "__libc_start_main")); /* ... and call it with our custom main function */ #if HAVE_VALGRIND_H /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */ - if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND) + if (getenv("VALGRIND_LIB") && not RUNNING_ON_VALGRIND) return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end); #endif return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);