1 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
4 #include "src/sthread/sthread.h"
10 int sthread_inside_simgrid = 1; // whether sthread should leave pthread operations, or intercept them.
12 /* We don't want to intercept pthread within simgrid. Instead we should provide the real implem to simgrid */
13 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
14 static int (*raw_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*) = NULL;
15 static int (*raw_mutex_lock)(pthread_mutex_t*) = NULL;
16 static int (*raw_mutex_trylock)(pthread_mutex_t*) = NULL;
17 static int (*raw_mutex_unlock)(pthread_mutex_t*) = NULL;
18 static int (*raw_mutex_destroy)(pthread_mutex_t*) = NULL;
19 static sem_t* (*raw_sem_open)(const char*, int) = NULL;
20 static int (*raw_sem_init)(sem_t*, int, unsigned int) = NULL;
21 static int (*raw_sem_wait)(sem_t*) = NULL;
22 static int (*raw_sem_post)(sem_t*) = NULL;
23 static void intercepter_init()
26 (int (*)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*))dlsym(RTLD_NEXT, "pthread_create");
27 raw_mutex_init = (int (*)(pthread_mutex_t*, const pthread_mutexattr_t*))dlsym(RTLD_NEXT, "pthread_mutex_init");
28 raw_mutex_lock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_lock");
29 raw_mutex_trylock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_trylock");
30 raw_mutex_unlock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
31 raw_mutex_destroy = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_destroy");
33 raw_sem_open = (sem_t * (*)(const char*, int)) dlsym(RTLD_NEXT, "sem_open");
34 raw_sem_init = (int (*)(sem_t*, int, unsigned int))dlsym(RTLD_NEXT, "sem_init");
35 raw_sem_wait = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_wait");
36 raw_sem_post = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_post");
39 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
41 if (raw_pthread_create == NULL)
44 if (sthread_inside_simgrid)
45 return raw_pthread_create(thread, attr, start_routine, arg);
47 sthread_inside_simgrid = 1;
48 int res = sthread_create(thread, attr, start_routine, arg);
49 sthread_inside_simgrid = 0;
53 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
55 if (raw_mutex_init == NULL)
58 if (sthread_inside_simgrid)
59 raw_mutex_init(mutex, attr);
61 sthread_inside_simgrid = 1;
62 int res = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
63 sthread_inside_simgrid = 0;
67 int pthread_mutex_lock(pthread_mutex_t* mutex)
69 if (raw_mutex_lock == NULL)
72 if (sthread_inside_simgrid)
73 return raw_mutex_lock(mutex);
75 sthread_inside_simgrid = 1;
76 int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
77 sthread_inside_simgrid = 0;
81 int pthread_mutex_trylock(pthread_mutex_t* mutex)
83 if (raw_mutex_trylock == NULL)
86 if (sthread_inside_simgrid)
87 return raw_mutex_trylock(mutex);
89 sthread_inside_simgrid = 1;
90 int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
91 sthread_inside_simgrid = 0;
95 int pthread_mutex_unlock(pthread_mutex_t* mutex)
97 if (raw_mutex_unlock == NULL)
100 if (sthread_inside_simgrid)
101 return raw_mutex_unlock(mutex);
103 sthread_inside_simgrid = 1;
104 int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
105 sthread_inside_simgrid = 0;
108 int pthread_mutex_destroy(pthread_mutex_t* mutex)
110 if (raw_mutex_destroy == NULL)
113 if (sthread_inside_simgrid)
114 return raw_mutex_destroy(mutex);
116 sthread_inside_simgrid = 1;
117 int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
118 sthread_inside_simgrid = 0;
123 int sem_init(sem_t *sem, int pshared, unsigned int value) {
126 res=raw_sem_init(sem,pshared,value);
130 int sem_wait(sem_t *sem) {
133 res = raw_sem_wait(sem);
137 int sem_post(sem_t *sem) {
138 return raw_sem_post(sem);
141 int pthread_join(pthread_t thread, void **retval) {
142 sg_actor_join(thread, -1);
146 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
147 *cond = sg_cond_init();
151 int pthread_cond_signal(pthread_cond_t *cond) {
152 sg_cond_notify_one(*cond);
156 int pthread_cond_broadcast(pthread_cond_t *cond) {
157 sg_cond_notify_all(*cond);
161 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
162 sg_cond_wait(*cond, *mutex);
166 int pthread_cond_destroy(pthread_cond_t *cond) {
167 sg_cond_destroy(*cond);
172 /* Trampoline for the real main() */
173 static int (*raw_main)(int, char**, char**);
175 /* Our fake main() that gets called by __libc_start_main() */
176 static int main_hook(int argc, char** argv, char** envp)
178 return sthread_main(argc, argv, envp, raw_main);
181 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
182 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
183 void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
185 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
186 void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
188 /* Save the real main function address */
191 /* Find the real __libc_start_main()... */
192 typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
193 fprintf(stderr, "__libc_start_main\n");
194 /* ... and call it with our custom main function */
195 return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);