1 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
4 #include "src/sthread/sthread.h"
5 #include "src/internal_config.h"
14 #include <valgrind/valgrind.h>
17 /* We don't want to intercept pthread within simgrid. Instead we should provide the real implem to simgrid */
18 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
19 static int (*raw_pthread_join)(pthread_t, void**);
20 static int (*raw_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*) = NULL;
21 static int (*raw_mutex_lock)(pthread_mutex_t*) = NULL;
22 static int (*raw_mutex_trylock)(pthread_mutex_t*) = NULL;
23 static int (*raw_mutex_unlock)(pthread_mutex_t*) = NULL;
24 static int (*raw_mutex_destroy)(pthread_mutex_t*) = NULL;
25 static sem_t* (*raw_sem_open)(const char*, int) = NULL;
26 static int (*raw_sem_init)(sem_t*, int, unsigned int) = NULL;
27 static int (*raw_sem_wait)(sem_t*) = NULL;
28 static int (*raw_sem_post)(sem_t*) = NULL;
29 static void intercepter_init()
31 raw_pthread_create = (typeof(raw_pthread_create))dlsym(RTLD_NEXT, "pthread_create");
32 raw_pthread_join = (typeof(raw_pthread_join))dlsym(RTLD_NEXT, "pthread_join");
33 raw_mutex_init = (int (*)(pthread_mutex_t*, const pthread_mutexattr_t*))dlsym(RTLD_NEXT, "pthread_mutex_init");
34 raw_mutex_lock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_lock");
35 raw_mutex_trylock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_trylock");
36 raw_mutex_unlock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
37 raw_mutex_destroy = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_destroy");
39 raw_sem_open = (sem_t * (*)(const char*, int)) dlsym(RTLD_NEXT, "sem_open");
40 raw_sem_init = (int (*)(sem_t*, int, unsigned int))dlsym(RTLD_NEXT, "sem_init");
41 raw_sem_wait = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_wait");
42 raw_sem_post = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_post");
45 static int sthread_inside_simgrid = 1;
46 void sthread_enable(void)
47 { // Start intercepting all pthread calls
48 sthread_inside_simgrid = 0;
50 void sthread_disable(void)
51 { // Stop intercepting all pthread calls
52 sthread_inside_simgrid = 1;
54 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
56 if (raw_pthread_create == NULL)
59 if (sthread_inside_simgrid)
60 return raw_pthread_create(thread, attr, start_routine, arg);
62 sthread_inside_simgrid = 1;
63 int res = sthread_create(thread, attr, start_routine, arg);
64 sthread_inside_simgrid = 0;
67 int pthread_join(pthread_t thread, void** retval)
69 if (raw_pthread_join == NULL)
72 if (sthread_inside_simgrid)
73 return raw_pthread_join(thread, retval);
75 sthread_inside_simgrid = 1;
76 int res = sthread_join(thread, retval);
77 sthread_inside_simgrid = 0;
81 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
83 if (raw_mutex_init == NULL)
86 if (sthread_inside_simgrid)
87 return raw_mutex_init(mutex, attr);
89 sthread_inside_simgrid = 1;
90 int res = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
91 sthread_inside_simgrid = 0;
95 int pthread_mutex_lock(pthread_mutex_t* mutex)
97 if (raw_mutex_lock == NULL)
100 if (sthread_inside_simgrid)
101 return raw_mutex_lock(mutex);
103 sthread_inside_simgrid = 1;
104 int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
105 sthread_inside_simgrid = 0;
109 int pthread_mutex_trylock(pthread_mutex_t* mutex)
111 if (raw_mutex_trylock == NULL)
114 if (sthread_inside_simgrid)
115 return raw_mutex_trylock(mutex);
117 sthread_inside_simgrid = 1;
118 int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
119 sthread_inside_simgrid = 0;
123 int pthread_mutex_unlock(pthread_mutex_t* mutex)
125 if (raw_mutex_unlock == NULL)
128 if (sthread_inside_simgrid)
129 return raw_mutex_unlock(mutex);
131 sthread_inside_simgrid = 1;
132 int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
133 sthread_inside_simgrid = 0;
136 int pthread_mutex_destroy(pthread_mutex_t* mutex)
138 if (raw_mutex_destroy == NULL)
141 if (sthread_inside_simgrid)
142 return raw_mutex_destroy(mutex);
144 sthread_inside_simgrid = 1;
145 int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
146 sthread_inside_simgrid = 0;
150 #if !defined(__GLIBC__) || __GLIBC_PREREQ(2, 31)
151 int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED void* tz)
153 int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED struct timezone* tz)
156 return sthread_gettimeofday(tv);
159 unsigned int sleep(unsigned int seconds)
161 sthread_sleep(seconds);
165 int usleep(useconds_t usec)
167 sthread_sleep(((double)usec) / 1000000.);
172 int sem_init(sem_t *sem, int pshared, unsigned int value) {
175 res=raw_sem_init(sem,pshared,value);
179 int sem_wait(sem_t *sem) {
182 res = raw_sem_wait(sem);
186 int sem_post(sem_t *sem) {
187 return raw_sem_post(sem);
190 int pthread_join(pthread_t thread, void **retval) {
191 sg_actor_join(thread, -1);
195 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
196 *cond = sg_cond_init();
200 int pthread_cond_signal(pthread_cond_t *cond) {
201 sg_cond_notify_one(*cond);
205 int pthread_cond_broadcast(pthread_cond_t *cond) {
206 sg_cond_notify_all(*cond);
210 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
211 sg_cond_wait(*cond, *mutex);
215 int pthread_cond_destroy(pthread_cond_t *cond) {
216 sg_cond_destroy(*cond);
221 /* Trampoline for the real main() */
222 static int (*raw_main)(int, char**, char**);
224 /* Our fake main() that gets called by __libc_start_main() */
225 static int main_hook(int argc, char** argv, char** envp)
227 return sthread_main(argc, argv, envp, raw_main);
230 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
231 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
232 void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
234 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
235 void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
237 /* Save the real main function address */
240 /* Find the real __libc_start_main()... */
241 typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
242 /* ... and call it with our custom main function */
244 /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
245 if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
246 return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
248 return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);