Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Handle different types for second parameter of 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 #if !defined(__GLIBC__) || __GLIBC_PREREQ(2, 31)
151 int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED void* tz)
152 #else
153 int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED struct timezone* tz)
154 #endif
155 {
156   return sthread_gettimeofday(tv);
157 }
158
159 unsigned int sleep(unsigned int seconds)
160 {
161   sthread_sleep(seconds);
162   return 0;
163 }
164
165 int usleep(useconds_t usec)
166 {
167   sthread_sleep(((double)usec) / 1000000.);
168   return 0;
169 }
170
171 #if 0
172 int sem_init(sem_t *sem, int pshared, unsigned int value) {
173         int res;
174
175         res=raw_sem_init(sem,pshared,value);
176         return res;
177 }
178
179 int sem_wait(sem_t *sem) {
180         int res;
181
182         res = raw_sem_wait(sem);
183         return res;
184 }
185
186 int sem_post(sem_t *sem) {
187         return raw_sem_post(sem);
188 }
189
190 int pthread_join(pthread_t thread, void **retval) {
191         sg_actor_join(thread, -1);
192     return 0;
193 }
194
195 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
196     *cond = sg_cond_init();
197     return 0;
198 }
199
200 int pthread_cond_signal(pthread_cond_t *cond) {
201         sg_cond_notify_one(*cond);
202     return 0;
203 }
204
205 int pthread_cond_broadcast(pthread_cond_t *cond) {
206         sg_cond_notify_all(*cond);
207     return 0;
208 }
209
210 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
211         sg_cond_wait(*cond, *mutex);
212     return 0;
213 }
214
215 int pthread_cond_destroy(pthread_cond_t *cond) {
216         sg_cond_destroy(*cond);
217     return 0;
218 }
219 #endif
220
221 /* Trampoline for the real main() */
222 static int (*raw_main)(int, char**, char**);
223
224 /* Our fake main() that gets called by __libc_start_main() */
225 static int main_hook(int argc, char** argv, char** envp)
226 {
227   return sthread_main(argc, argv, envp, raw_main);
228 }
229
230 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
231 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
232                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
233
234 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
235                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
236 {
237   /* Save the real main function address */
238   raw_main = main;
239
240   /* Find the real __libc_start_main()... */
241   typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
242   /* ... and call it with our custom main function */
243 #if HAVE_VALGRIND_H
244   /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
245   if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
246     return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
247 #endif
248   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
249 }