Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement pthread_join in MC mode for sthread
[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/sthread/sthread.h"
5 #include "src/internal_config.h"
6 #include <dlfcn.h>
7 #include <pthread.h>
8 #include <semaphore.h>
9 #include <stdio.h>
10 #include <unistd.h>
11
12 #if HAVE_VALGRIND_H
13 #include <stdlib.h>
14 #include <valgrind/valgrind.h>
15 #endif
16
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
26 static unsigned int (*raw_sleep)(unsigned int)         = NULL;
27 static int (*raw_usleep)(useconds_t)                   = NULL;
28 static int (*raw_gettimeofday)(struct timeval*, void*) = NULL;
29
30 static sem_t* (*raw_sem_open)(const char*, int)                            = NULL;
31 static int (*raw_sem_init)(sem_t*, int, unsigned int)                      = NULL;
32 static int (*raw_sem_wait)(sem_t*)                                         = NULL;
33 static int (*raw_sem_post)(sem_t*)                                         = NULL;
34
35 static void intercepter_init()
36 {
37   raw_pthread_create = (typeof(raw_pthread_create))dlsym(RTLD_NEXT, "pthread_create");
38   raw_pthread_join   = (typeof(raw_pthread_join))dlsym(RTLD_NEXT, "pthread_join");
39   raw_mutex_init    = (int (*)(pthread_mutex_t*, const pthread_mutexattr_t*))dlsym(RTLD_NEXT, "pthread_mutex_init");
40   raw_mutex_lock    = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_lock");
41   raw_mutex_trylock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_trylock");
42   raw_mutex_unlock  = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
43   raw_mutex_destroy = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_destroy");
44
45   raw_sleep        = (unsigned int (*)(unsigned int))dlsym(RTLD_NEXT, "sleep");
46   raw_usleep       = (int (*)(useconds_t usec))dlsym(RTLD_NEXT, "usleep");
47   raw_gettimeofday = (int (*)(struct timeval*, void*))dlsym(RTLD_NEXT, "gettimeofday");
48
49   raw_sem_open = (sem_t * (*)(const char*, int)) dlsym(RTLD_NEXT, "sem_open");
50   raw_sem_init = (int (*)(sem_t*, int, unsigned int))dlsym(RTLD_NEXT, "sem_init");
51   raw_sem_wait = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_wait");
52   raw_sem_post = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_post");
53 }
54
55 static int sthread_inside_simgrid = 1;
56 void sthread_enable(void)
57 { // Start intercepting all pthread calls
58   sthread_inside_simgrid = 0;
59 }
60 void sthread_disable(void)
61 { // Stop intercepting all pthread calls
62   sthread_inside_simgrid = 1;
63 }
64 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
65 {
66   if (raw_pthread_create == NULL)
67     intercepter_init();
68
69   if (sthread_inside_simgrid)
70     return raw_pthread_create(thread, attr, start_routine, arg);
71
72   sthread_inside_simgrid = 1;
73   int res                = sthread_create((sthread_t*)thread, attr, start_routine, arg);
74   sthread_inside_simgrid = 0;
75   return res;
76 }
77 int pthread_join(pthread_t thread, void** retval)
78 {
79   if (raw_pthread_join == NULL)
80     intercepter_init();
81   if (sthread_inside_simgrid)
82     return raw_pthread_join(thread, retval);
83
84   sthread_inside_simgrid = 1;
85   int res                = sthread_join((sthread_t)thread, retval);
86   sthread_inside_simgrid = 0;
87   return res;
88 }
89
90 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
91 {
92   if (raw_mutex_init == NULL)
93     intercepter_init();
94
95   if (sthread_inside_simgrid)
96     return raw_mutex_init(mutex, attr);
97
98   sthread_inside_simgrid = 1;
99   int res                = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
100   sthread_inside_simgrid = 0;
101   return res;
102 }
103
104 int pthread_mutex_lock(pthread_mutex_t* mutex)
105 {
106   if (raw_mutex_lock == NULL)
107     intercepter_init();
108
109   if (sthread_inside_simgrid)
110     return raw_mutex_lock(mutex);
111
112   sthread_inside_simgrid = 1;
113   int res                = sthread_mutex_lock((sthread_mutex_t*)mutex);
114   sthread_inside_simgrid = 0;
115   return res;
116 }
117
118 int pthread_mutex_trylock(pthread_mutex_t* mutex)
119 {
120   if (raw_mutex_trylock == NULL)
121     intercepter_init();
122
123   if (sthread_inside_simgrid)
124     return raw_mutex_trylock(mutex);
125
126   sthread_inside_simgrid = 1;
127   int res                = sthread_mutex_trylock((sthread_mutex_t*)mutex);
128   sthread_inside_simgrid = 0;
129   return res;
130 }
131
132 int pthread_mutex_unlock(pthread_mutex_t* mutex)
133 {
134   if (raw_mutex_unlock == NULL)
135     intercepter_init();
136
137   if (sthread_inside_simgrid)
138     return raw_mutex_unlock(mutex);
139
140   sthread_inside_simgrid = 1;
141   int res                = sthread_mutex_unlock((sthread_mutex_t*)mutex);
142   sthread_inside_simgrid = 0;
143   return res;
144 }
145 int pthread_mutex_destroy(pthread_mutex_t* mutex)
146 {
147   if (raw_mutex_destroy == NULL)
148     intercepter_init();
149
150   if (sthread_inside_simgrid)
151     return raw_mutex_destroy(mutex);
152
153   sthread_inside_simgrid = 1;
154   int res                = sthread_mutex_destroy((sthread_mutex_t*)mutex);
155   sthread_inside_simgrid = 0;
156   return res;
157 }
158
159 /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday.
160    Other implementations use "void *" instead. */
161 #ifdef __GLIBC__
162 #if !__GLIBC_PREREQ(2, 31)
163 #define TIMEZONE_TYPE struct timezone
164 #endif
165 #endif
166 #ifndef TIMEZONE_TYPE
167 #define TIMEZONE_TYPE void
168 #endif
169
170 int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz)
171 {
172   if (raw_gettimeofday == NULL)
173     intercepter_init();
174
175   if (sthread_inside_simgrid)
176     return raw_gettimeofday(tv, tz);
177
178   sthread_inside_simgrid = 1;
179   int res                = sthread_gettimeofday(tv);
180   sthread_inside_simgrid = 0;
181   return res;
182 }
183
184 unsigned int sleep(unsigned int seconds)
185 {
186   if (raw_sleep == NULL)
187     intercepter_init();
188
189   if (sthread_inside_simgrid)
190     return raw_sleep(seconds);
191
192   sthread_inside_simgrid = 1;
193   sthread_sleep(seconds);
194   sthread_inside_simgrid = 0;
195   return 0;
196 }
197
198 int usleep(useconds_t usec)
199 {
200   if (raw_usleep == NULL)
201     intercepter_init();
202
203   if (sthread_inside_simgrid)
204     return raw_usleep(usec);
205
206   sthread_inside_simgrid = 1;
207   sthread_sleep(((double)usec) / 1000000.);
208   sthread_inside_simgrid = 0;
209   return 0;
210 }
211
212 #if 0
213 int sem_init(sem_t *sem, int pshared, unsigned int value) {
214         int res;
215
216         res=raw_sem_init(sem,pshared,value);
217         return res;
218 }
219
220 int sem_wait(sem_t *sem) {
221         int res;
222
223         res = raw_sem_wait(sem);
224         return res;
225 }
226
227 int sem_post(sem_t *sem) {
228         return raw_sem_post(sem);
229 }
230
231 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
232     *cond = sg_cond_init();
233     return 0;
234 }
235
236 int pthread_cond_signal(pthread_cond_t *cond) {
237         sg_cond_notify_one(*cond);
238     return 0;
239 }
240
241 int pthread_cond_broadcast(pthread_cond_t *cond) {
242         sg_cond_notify_all(*cond);
243     return 0;
244 }
245
246 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
247         sg_cond_wait(*cond, *mutex);
248     return 0;
249 }
250
251 int pthread_cond_destroy(pthread_cond_t *cond) {
252         sg_cond_destroy(*cond);
253     return 0;
254 }
255 #endif
256
257 /* Trampoline for the real main() */
258 static int (*raw_main)(int, char**, char**);
259
260 /* Our fake main() that gets called by __libc_start_main() */
261 static int main_hook(int argc, char** argv, char** envp)
262 {
263   return sthread_main(argc, argv, envp, raw_main);
264 }
265
266 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
267 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
268                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
269
270 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
271                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
272 {
273   /* Save the real main function address */
274   raw_main = main;
275
276   /* Find the real __libc_start_main()... */
277   typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
278   /* ... and call it with our custom main function */
279 #if HAVE_VALGRIND_H
280   /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
281   if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
282     return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
283 #endif
284   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
285 }