1 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
4 #include "src/sthread/sthread.h"
10 /* We don't want to intercept pthread within simgrid. Instead we should provide the real implem to simgrid */
11 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
12 static int (*raw_pthread_join)(pthread_t, void**);
13 static int (*raw_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*) = NULL;
14 static int (*raw_mutex_lock)(pthread_mutex_t*) = NULL;
15 static int (*raw_mutex_trylock)(pthread_mutex_t*) = NULL;
16 static int (*raw_mutex_unlock)(pthread_mutex_t*) = NULL;
17 static int (*raw_mutex_destroy)(pthread_mutex_t*) = NULL;
18 static sem_t* (*raw_sem_open)(const char*, int) = NULL;
19 static int (*raw_sem_init)(sem_t*, int, unsigned int) = NULL;
20 static int (*raw_sem_wait)(sem_t*) = NULL;
21 static int (*raw_sem_post)(sem_t*) = NULL;
22 static void intercepter_init()
24 raw_pthread_create = (typeof(raw_pthread_create))dlsym(RTLD_NEXT, "pthread_create");
25 raw_pthread_join = (typeof(raw_pthread_join))dlsym(RTLD_NEXT, "pthread_join");
26 raw_mutex_init = (int (*)(pthread_mutex_t*, const pthread_mutexattr_t*))dlsym(RTLD_NEXT, "pthread_mutex_init");
27 raw_mutex_lock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_lock");
28 raw_mutex_trylock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_trylock");
29 raw_mutex_unlock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
30 raw_mutex_destroy = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_destroy");
32 raw_sem_open = (sem_t * (*)(const char*, int)) dlsym(RTLD_NEXT, "sem_open");
33 raw_sem_init = (int (*)(sem_t*, int, unsigned int))dlsym(RTLD_NEXT, "sem_init");
34 raw_sem_wait = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_wait");
35 raw_sem_post = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_post");
38 static int sthread_inside_simgrid = 1;
39 void sthread_enable(void)
40 { // Start intercepting all pthread calls
41 sthread_inside_simgrid = 0;
43 void sthread_disable(void)
44 { // Stop intercepting all pthread calls
45 sthread_inside_simgrid = 1;
47 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
49 if (raw_pthread_create == NULL)
52 if (sthread_inside_simgrid)
53 return raw_pthread_create(thread, attr, start_routine, arg);
55 sthread_inside_simgrid = 1;
56 int res = sthread_create(thread, attr, start_routine, arg);
57 sthread_inside_simgrid = 0;
60 int pthread_join(pthread_t thread, void** retval)
62 if (raw_pthread_join == NULL)
65 if (sthread_inside_simgrid)
66 return raw_pthread_join(thread, retval);
68 sthread_inside_simgrid = 1;
69 int res = sthread_join(thread, retval);
70 sthread_inside_simgrid = 0;
74 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
76 if (raw_mutex_init == NULL)
79 if (sthread_inside_simgrid)
80 return raw_mutex_init(mutex, attr);
82 sthread_inside_simgrid = 1;
83 int res = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
84 sthread_inside_simgrid = 0;
88 int pthread_mutex_lock(pthread_mutex_t* mutex)
90 if (raw_mutex_lock == NULL)
93 if (sthread_inside_simgrid)
94 return raw_mutex_lock(mutex);
96 sthread_inside_simgrid = 1;
97 int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
98 sthread_inside_simgrid = 0;
102 int pthread_mutex_trylock(pthread_mutex_t* mutex)
104 if (raw_mutex_trylock == NULL)
107 if (sthread_inside_simgrid)
108 return raw_mutex_trylock(mutex);
110 sthread_inside_simgrid = 1;
111 int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
112 sthread_inside_simgrid = 0;
116 int pthread_mutex_unlock(pthread_mutex_t* mutex)
118 if (raw_mutex_unlock == NULL)
121 if (sthread_inside_simgrid)
122 return raw_mutex_unlock(mutex);
124 sthread_inside_simgrid = 1;
125 int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
126 sthread_inside_simgrid = 0;
129 int pthread_mutex_destroy(pthread_mutex_t* mutex)
131 if (raw_mutex_destroy == NULL)
134 if (sthread_inside_simgrid)
135 return raw_mutex_destroy(mutex);
137 sthread_inside_simgrid = 1;
138 int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
139 sthread_inside_simgrid = 0;
144 int sem_init(sem_t *sem, int pshared, unsigned int value) {
147 res=raw_sem_init(sem,pshared,value);
151 int sem_wait(sem_t *sem) {
154 res = raw_sem_wait(sem);
158 int sem_post(sem_t *sem) {
159 return raw_sem_post(sem);
162 int pthread_join(pthread_t thread, void **retval) {
163 sg_actor_join(thread, -1);
167 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
168 *cond = sg_cond_init();
172 int pthread_cond_signal(pthread_cond_t *cond) {
173 sg_cond_notify_one(*cond);
177 int pthread_cond_broadcast(pthread_cond_t *cond) {
178 sg_cond_notify_all(*cond);
182 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
183 sg_cond_wait(*cond, *mutex);
187 int pthread_cond_destroy(pthread_cond_t *cond) {
188 sg_cond_destroy(*cond);
193 /* Trampoline for the real main() */
194 static int (*raw_main)(int, char**, char**);
196 /* Our fake main() that gets called by __libc_start_main() */
197 static int main_hook(int argc, char** argv, char** envp)
199 return sthread_main(argc, argv, envp, raw_main);
202 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
203 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
204 void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
206 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
207 void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
209 /* Save the real main function address */
212 /* Find the real __libc_start_main()... */
213 typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
214 /* ... and call it with our custom main function */
215 return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);