Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[project-description] Fix extraction of the ns-3 version.
[simgrid.git] / src / sthread / sthread.c
1 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
2
3 #define _GNU_SOURCE
4 #include "src/internal_config.h"
5 #include "src/sthread/sthread.h"
6 #include <dlfcn.h>
7 #include <pthread.h>
8 #include <semaphore.h>
9 #include <stdio.h>
10
11 #if HAVE_VALGRIND_H
12 #include <stdlib.h>
13 #include <valgrind/valgrind.h>
14 #endif
15
16 /* We don't want to intercept pthread within simgrid. Instead we should provide the real implem to simgrid */
17 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
18 static int (*raw_pthread_join)(pthread_t, void**);
19 static int (*raw_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*) = NULL;
20 static int (*raw_mutex_lock)(pthread_mutex_t*)                             = NULL;
21 static int (*raw_mutex_trylock)(pthread_mutex_t*)                          = NULL;
22 static int (*raw_mutex_unlock)(pthread_mutex_t*)                           = NULL;
23 static int (*raw_mutex_destroy)(pthread_mutex_t*)                          = NULL;
24 static sem_t* (*raw_sem_open)(const char*, int)                            = NULL;
25 static int (*raw_sem_init)(sem_t*, int, unsigned int)                      = NULL;
26 static int (*raw_sem_wait)(sem_t*)                                         = NULL;
27 static int (*raw_sem_post)(sem_t*)                                         = NULL;
28 static void intercepter_init()
29 {
30   raw_pthread_create = (typeof(raw_pthread_create))dlsym(RTLD_NEXT, "pthread_create");
31   raw_pthread_join   = (typeof(raw_pthread_join))dlsym(RTLD_NEXT, "pthread_join");
32   raw_mutex_init    = (int (*)(pthread_mutex_t*, const pthread_mutexattr_t*))dlsym(RTLD_NEXT, "pthread_mutex_init");
33   raw_mutex_lock    = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_lock");
34   raw_mutex_trylock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_trylock");
35   raw_mutex_unlock  = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
36   raw_mutex_destroy = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_destroy");
37
38   raw_sem_open = (sem_t * (*)(const char*, int)) dlsym(RTLD_NEXT, "sem_open");
39   raw_sem_init = (int (*)(sem_t*, int, unsigned int))dlsym(RTLD_NEXT, "sem_init");
40   raw_sem_wait = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_wait");
41   raw_sem_post = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_post");
42 }
43
44 static int sthread_inside_simgrid = 1;
45 void sthread_enable(void)
46 { // Start intercepting all pthread calls
47   sthread_inside_simgrid = 0;
48 }
49 void sthread_disable(void)
50 { // Stop intercepting all pthread calls
51   sthread_inside_simgrid = 1;
52 }
53 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
54 {
55   if (raw_pthread_create == NULL)
56     intercepter_init();
57
58   if (sthread_inside_simgrid)
59     return raw_pthread_create(thread, attr, start_routine, arg);
60
61   sthread_inside_simgrid = 1;
62   int res                = sthread_create(thread, attr, start_routine, arg);
63   sthread_inside_simgrid = 0;
64   return res;
65 }
66 int pthread_join(pthread_t thread, void** retval)
67 {
68   if (raw_pthread_join == NULL)
69     intercepter_init();
70
71   if (sthread_inside_simgrid)
72     return raw_pthread_join(thread, retval);
73
74   sthread_inside_simgrid = 1;
75   int res                = sthread_join(thread, retval);
76   sthread_inside_simgrid = 0;
77   return res;
78 }
79
80 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
81 {
82   if (raw_mutex_init == NULL)
83     intercepter_init();
84
85   if (sthread_inside_simgrid)
86     return raw_mutex_init(mutex, attr);
87
88   sthread_inside_simgrid = 1;
89   int res                = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
90   sthread_inside_simgrid = 0;
91   return res;
92 }
93
94 int pthread_mutex_lock(pthread_mutex_t* mutex)
95 {
96   if (raw_mutex_lock == NULL)
97     intercepter_init();
98
99   if (sthread_inside_simgrid)
100     return raw_mutex_lock(mutex);
101
102   sthread_inside_simgrid = 1;
103   int res                = sthread_mutex_lock((sthread_mutex_t*)mutex);
104   sthread_inside_simgrid = 0;
105   return res;
106 }
107
108 int pthread_mutex_trylock(pthread_mutex_t* mutex)
109 {
110   if (raw_mutex_trylock == NULL)
111     intercepter_init();
112
113   if (sthread_inside_simgrid)
114     return raw_mutex_trylock(mutex);
115
116   sthread_inside_simgrid = 1;
117   int res                = sthread_mutex_trylock((sthread_mutex_t*)mutex);
118   sthread_inside_simgrid = 0;
119   return res;
120 }
121
122 int pthread_mutex_unlock(pthread_mutex_t* mutex)
123 {
124   if (raw_mutex_unlock == NULL)
125     intercepter_init();
126
127   if (sthread_inside_simgrid)
128     return raw_mutex_unlock(mutex);
129
130   sthread_inside_simgrid = 1;
131   int res                = sthread_mutex_unlock((sthread_mutex_t*)mutex);
132   sthread_inside_simgrid = 0;
133   return res;
134 }
135 int pthread_mutex_destroy(pthread_mutex_t* mutex)
136 {
137   if (raw_mutex_destroy == NULL)
138     intercepter_init();
139
140   if (sthread_inside_simgrid)
141     return raw_mutex_destroy(mutex);
142
143   sthread_inside_simgrid = 1;
144   int res                = sthread_mutex_destroy((sthread_mutex_t*)mutex);
145   sthread_inside_simgrid = 0;
146   return res;
147 }
148
149 #if 0
150 int sem_init(sem_t *sem, int pshared, unsigned int value) {
151         int res;
152
153         res=raw_sem_init(sem,pshared,value);
154         return res;
155 }
156
157 int sem_wait(sem_t *sem) {
158         int res;
159
160         res = raw_sem_wait(sem);
161         return res;
162 }
163
164 int sem_post(sem_t *sem) {
165         return raw_sem_post(sem);
166 }
167
168 int pthread_join(pthread_t thread, void **retval) {
169         sg_actor_join(thread, -1);
170     return 0;
171 }
172
173 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
174     *cond = sg_cond_init();
175     return 0;
176 }
177
178 int pthread_cond_signal(pthread_cond_t *cond) {
179         sg_cond_notify_one(*cond);
180     return 0;
181 }
182
183 int pthread_cond_broadcast(pthread_cond_t *cond) {
184         sg_cond_notify_all(*cond);
185     return 0;
186 }
187
188 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
189         sg_cond_wait(*cond, *mutex);
190     return 0;
191 }
192
193 int pthread_cond_destroy(pthread_cond_t *cond) {
194         sg_cond_destroy(*cond);
195     return 0;
196 }
197 #endif
198
199 /* Trampoline for the real main() */
200 static int (*raw_main)(int, char**, char**);
201
202 /* Our fake main() that gets called by __libc_start_main() */
203 static int main_hook(int argc, char** argv, char** envp)
204 {
205   return sthread_main(argc, argv, envp, raw_main);
206 }
207
208 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
209 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
210                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
211
212 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
213                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
214 {
215   /* Save the real main function address */
216   raw_main = main;
217
218   /* Find the real __libc_start_main()... */
219   typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
220   /* ... and call it with our custom main function */
221 #if HAVE_VALGRIND_H
222   /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
223   if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
224     return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
225 #endif
226   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
227 }