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 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
40 if (raw_pthread_create == NULL)
43 if (sthread_inside_simgrid)
44 return raw_pthread_create(thread, attr, start_routine, arg);
46 sthread_inside_simgrid = 1;
47 int res = sthread_create(thread, attr, start_routine, arg);
48 sthread_inside_simgrid = 0;
51 int pthread_join(pthread_t thread, void** retval)
53 if (raw_pthread_join == NULL)
56 if (sthread_inside_simgrid)
57 return raw_pthread_join(thread, retval);
59 sthread_inside_simgrid = 1;
60 int res = sthread_join(thread, retval);
61 sthread_inside_simgrid = 0;
65 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
67 if (raw_mutex_init == NULL)
70 if (sthread_inside_simgrid)
71 raw_mutex_init(mutex, attr);
73 sthread_inside_simgrid = 1;
74 int res = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
75 sthread_inside_simgrid = 0;
79 int pthread_mutex_lock(pthread_mutex_t* mutex)
81 if (raw_mutex_lock == NULL)
84 if (sthread_inside_simgrid)
85 return raw_mutex_lock(mutex);
87 sthread_inside_simgrid = 1;
88 int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
89 sthread_inside_simgrid = 0;
93 int pthread_mutex_trylock(pthread_mutex_t* mutex)
95 if (raw_mutex_trylock == NULL)
98 if (sthread_inside_simgrid)
99 return raw_mutex_trylock(mutex);
101 sthread_inside_simgrid = 1;
102 int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
103 sthread_inside_simgrid = 0;
107 int pthread_mutex_unlock(pthread_mutex_t* mutex)
109 if (raw_mutex_unlock == NULL)
112 if (sthread_inside_simgrid)
113 return raw_mutex_unlock(mutex);
115 sthread_inside_simgrid = 1;
116 int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
117 sthread_inside_simgrid = 0;
120 int pthread_mutex_destroy(pthread_mutex_t* mutex)
122 if (raw_mutex_destroy == NULL)
125 if (sthread_inside_simgrid)
126 return raw_mutex_destroy(mutex);
128 sthread_inside_simgrid = 1;
129 int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
130 sthread_inside_simgrid = 0;
135 int sem_init(sem_t *sem, int pshared, unsigned int value) {
138 res=raw_sem_init(sem,pshared,value);
142 int sem_wait(sem_t *sem) {
145 res = raw_sem_wait(sem);
149 int sem_post(sem_t *sem) {
150 return raw_sem_post(sem);
153 int pthread_join(pthread_t thread, void **retval) {
154 sg_actor_join(thread, -1);
158 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
159 *cond = sg_cond_init();
163 int pthread_cond_signal(pthread_cond_t *cond) {
164 sg_cond_notify_one(*cond);
168 int pthread_cond_broadcast(pthread_cond_t *cond) {
169 sg_cond_notify_all(*cond);
173 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
174 sg_cond_wait(*cond, *mutex);
178 int pthread_cond_destroy(pthread_cond_t *cond) {
179 sg_cond_destroy(*cond);
184 /* Trampoline for the real main() */
185 static int (*raw_main)(int, char**, char**);
187 /* Our fake main() that gets called by __libc_start_main() */
188 static int main_hook(int argc, char** argv, char** envp)
190 return sthread_main(argc, argv, envp, raw_main);
193 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
194 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
195 void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
197 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
198 void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
200 /* Save the real main function address */
203 /* Find the real __libc_start_main()... */
204 typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
205 fprintf(stderr, "__libc_start_main\n");
206 /* ... and call it with our custom main function */
207 return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);