Logo AND Algorithmique Numérique Distribuée

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