Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A few spelling mistakes and many replacements: [Ss]imgrid -> SimGrid.
[simgrid.git] / src / sthread / sthread.c
1 /* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
7
8 #define _GNU_SOURCE
9 #include "src/sthread/sthread.h"
10 #include "src/internal_config.h"
11 #include <dlfcn.h>
12 #include <pthread.h>
13 #include <semaphore.h>
14 #include <stdio.h>
15 #include <unistd.h>
16
17 #if HAVE_VALGRIND_H
18 #include <stdlib.h>
19 #include <valgrind/valgrind.h>
20 #endif
21
22 /* We don't want to intercept pthread within SimGrid. Instead we should provide the real implem to SimGrid */
23 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
24 static int (*raw_pthread_join)(pthread_t, void**);
25 static int (*raw_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*);
26 static int (*raw_mutex_lock)(pthread_mutex_t*);
27 static int (*raw_mutex_trylock)(pthread_mutex_t*);
28 static int (*raw_mutex_unlock)(pthread_mutex_t*);
29 static int (*raw_mutex_destroy)(pthread_mutex_t*);
30
31 static unsigned int (*raw_sleep)(unsigned int);
32 static int (*raw_usleep)(useconds_t);
33 static int (*raw_gettimeofday)(struct timeval*, void*);
34
35 static sem_t* (*raw_sem_open)(const char*, int);
36 static int (*raw_sem_init)(sem_t*, int, unsigned int);
37 static int (*raw_sem_wait)(sem_t*);
38 static int (*raw_sem_post)(sem_t*);
39 static int (*raw_sem_destroy)(sem_t*);
40 static int (*raw_sem_trywait)(sem_t*);
41 static int (*raw_sem_timedwait)(sem_t*, const struct timespec*);
42
43 static void intercepter_init()
44 {
45   raw_pthread_create = dlsym(RTLD_NEXT, "pthread_create");
46   raw_pthread_join   = dlsym(RTLD_NEXT, "pthread_join");
47   raw_mutex_init     = dlsym(RTLD_NEXT, "pthread_mutex_init");
48   raw_mutex_lock     = dlsym(RTLD_NEXT, "pthread_mutex_lock");
49   raw_mutex_trylock  = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
50   raw_mutex_unlock   = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
51   raw_mutex_destroy  = dlsym(RTLD_NEXT, "pthread_mutex_destroy");
52
53   raw_sleep        = dlsym(RTLD_NEXT, "sleep");
54   raw_usleep       = dlsym(RTLD_NEXT, "usleep");
55   raw_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
56
57   raw_sem_open = dlsym(RTLD_NEXT, "sem_open");
58   raw_sem_init = dlsym(RTLD_NEXT, "sem_init");
59   raw_sem_wait = dlsym(RTLD_NEXT, "sem_wait");
60   raw_sem_post = dlsym(RTLD_NEXT, "sem_post");
61   raw_sem_destroy   = dlsym(RTLD_NEXT, "sem_destroy");
62   raw_sem_trywait   = dlsym(RTLD_NEXT, "sem_trywait");
63   raw_sem_timedwait = dlsym(RTLD_NEXT, "sem_timedwait");
64 }
65
66 static int sthread_inside_simgrid = 1;
67 void sthread_enable(void)
68 { // Start intercepting all pthread calls
69   sthread_inside_simgrid = 0;
70 }
71 void sthread_disable(void)
72 { // Stop intercepting all pthread calls
73   sthread_inside_simgrid = 1;
74 }
75 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
76 {
77   if (raw_pthread_create == NULL)
78     intercepter_init();
79
80   if (sthread_inside_simgrid)
81     return raw_pthread_create(thread, attr, start_routine, arg);
82
83   sthread_disable();
84   int res = sthread_create((sthread_t*)thread, attr, start_routine, arg);
85   sthread_enable();
86   return res;
87 }
88 int pthread_join(pthread_t thread, void** retval)
89 {
90   if (raw_pthread_join == NULL)
91     intercepter_init();
92   if (sthread_inside_simgrid)
93     return raw_pthread_join(thread, retval);
94
95   sthread_disable();
96   int res = sthread_join((sthread_t)thread, retval);
97   sthread_enable();
98   return res;
99 }
100
101 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
102 {
103   if (raw_mutex_init == NULL)
104     intercepter_init();
105
106   if (sthread_inside_simgrid)
107     return raw_mutex_init(mutex, attr);
108
109   sthread_disable();
110   int res = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
111   sthread_enable();
112   return res;
113 }
114
115 int pthread_mutex_lock(pthread_mutex_t* mutex)
116 {
117   if (raw_mutex_lock == NULL)
118     intercepter_init();
119
120   if (sthread_inside_simgrid)
121     return raw_mutex_lock(mutex);
122
123   sthread_disable();
124   int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
125   sthread_enable();
126   return res;
127 }
128
129 int pthread_mutex_trylock(pthread_mutex_t* mutex)
130 {
131   if (raw_mutex_trylock == NULL)
132     intercepter_init();
133
134   if (sthread_inside_simgrid)
135     return raw_mutex_trylock(mutex);
136
137   sthread_disable();
138   int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
139   sthread_enable();
140   return res;
141 }
142
143 int pthread_mutex_unlock(pthread_mutex_t* mutex)
144 {
145   if (raw_mutex_unlock == NULL)
146     intercepter_init();
147
148   if (sthread_inside_simgrid)
149     return raw_mutex_unlock(mutex);
150
151   sthread_disable();
152   int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
153   sthread_enable();
154   return res;
155 }
156 int pthread_mutex_destroy(pthread_mutex_t* mutex)
157 {
158   if (raw_mutex_destroy == NULL)
159     intercepter_init();
160
161   if (sthread_inside_simgrid)
162     return raw_mutex_destroy(mutex);
163
164   sthread_disable();
165   int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
166   sthread_enable();
167   return res;
168 }
169 int sem_init(sem_t* sem, int pshared, unsigned int value)
170 {
171   if (raw_sem_init == NULL)
172     intercepter_init();
173
174   if (sthread_inside_simgrid)
175     return raw_sem_init(sem, pshared, value);
176
177   sthread_disable();
178   int res = sthread_sem_init((sthread_sem_t*)sem, pshared, value);
179   sthread_enable();
180   return res;
181 }
182 int sem_destroy(sem_t* sem)
183 {
184   if (raw_sem_destroy == NULL)
185     intercepter_init();
186
187   if (sthread_inside_simgrid)
188     return raw_sem_destroy(sem);
189
190   sthread_disable();
191   int res = sthread_sem_destroy((sthread_sem_t*)sem);
192   sthread_enable();
193   return res;
194 }
195 int sem_post(sem_t* sem)
196 {
197   if (raw_sem_post == NULL)
198     intercepter_init();
199
200   if (sthread_inside_simgrid)
201     return raw_sem_post(sem);
202
203   sthread_disable();
204   int res = sthread_sem_post((sthread_sem_t*)sem);
205   sthread_enable();
206   return res;
207 }
208 int sem_wait(sem_t* sem)
209 {
210   if (raw_sem_wait == NULL)
211     intercepter_init();
212
213   if (sthread_inside_simgrid)
214     return raw_sem_wait(sem);
215
216   sthread_disable();
217   int res = sthread_sem_wait((sthread_sem_t*)sem);
218   sthread_enable();
219   return res;
220 }
221 int sem_trywait(sem_t* sem)
222 {
223   if (raw_sem_trywait == NULL)
224     intercepter_init();
225
226   if (sthread_inside_simgrid)
227     return raw_sem_trywait(sem);
228
229   sthread_disable();
230   int res = sthread_sem_trywait((sthread_sem_t*)sem);
231   sthread_enable();
232   return res;
233 }
234 int sem_timedwait(sem_t* sem, const struct timespec* abs_timeout)
235 {
236   if (raw_sem_timedwait == NULL)
237     intercepter_init();
238
239   if (sthread_inside_simgrid)
240     return raw_sem_timedwait(sem, abs_timeout);
241
242   sthread_disable();
243   int res = sthread_sem_timedwait((sthread_sem_t*)sem, abs_timeout);
244   sthread_enable();
245   return res;
246 }
247
248 /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday.
249    Other implementations use "void *" instead. */
250 #ifdef __GLIBC__
251 #if !__GLIBC_PREREQ(2, 31)
252 #define TIMEZONE_TYPE struct timezone
253 #endif
254 #endif
255 #ifndef TIMEZONE_TYPE
256 #define TIMEZONE_TYPE void
257 #endif
258
259 int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz)
260 {
261   if (raw_gettimeofday == NULL)
262     intercepter_init();
263
264   if (sthread_inside_simgrid)
265     return raw_gettimeofday(tv, tz);
266
267   sthread_disable();
268   int res = sthread_gettimeofday(tv);
269   sthread_enable();
270   return res;
271 }
272
273 unsigned int sleep(unsigned int seconds)
274 {
275   if (raw_sleep == NULL)
276     intercepter_init();
277
278   if (sthread_inside_simgrid)
279     return raw_sleep(seconds);
280
281   sthread_disable();
282   sthread_sleep(seconds);
283   sthread_enable();
284   return 0;
285 }
286
287 int usleep(useconds_t usec)
288 {
289   if (raw_usleep == NULL)
290     intercepter_init();
291
292   if (sthread_inside_simgrid)
293     return raw_usleep(usec);
294
295   sthread_disable();
296   sthread_sleep(((double)usec) / 1000000.);
297   sthread_enable();
298   return 0;
299 }
300
301 #if 0
302 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
303     *cond = sg_cond_init();
304     return 0;
305 }
306
307 int pthread_cond_signal(pthread_cond_t *cond) {
308         sg_cond_notify_one(*cond);
309     return 0;
310 }
311
312 int pthread_cond_broadcast(pthread_cond_t *cond) {
313         sg_cond_notify_all(*cond);
314     return 0;
315 }
316
317 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
318         sg_cond_wait(*cond, *mutex);
319     return 0;
320 }
321
322 int pthread_cond_destroy(pthread_cond_t *cond) {
323         sg_cond_destroy(*cond);
324     return 0;
325 }
326 #endif
327
328 /* Trampoline for the real main() */
329 static int (*raw_main)(int, char**, char**);
330
331 /* Our fake main() that gets called by __libc_start_main() */
332 static int main_hook(int argc, char** argv, char** envp)
333 {
334   return sthread_main(argc, argv, envp, raw_main);
335 }
336
337 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
338 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
339                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
340
341 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
342                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
343 {
344   /* Save the real main function address */
345   raw_main = main;
346
347   /* Find the real __libc_start_main()... */
348   typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
349   /* ... and call it with our custom main function */
350 #if HAVE_VALGRIND_H
351   /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
352   if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
353     return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
354 #endif
355   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
356 }