Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sure that sthread_inside_simgrid is initially 1 to protect all lib constructors
[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 <dlfcn.h>
6 #include <pthread.h>
7 #include <semaphore.h>
8 #include <stdio.h>
9
10 int sthread_inside_simgrid = 1; // whether sthread should leave pthread operations, or intercept them.
11
12 /* We don't want to intercept pthread within simgrid. Instead we should provide the real implem to simgrid */
13 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
14 static int (*raw_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*) = NULL;
15 static int (*raw_mutex_lock)(pthread_mutex_t*)                             = NULL;
16 static int (*raw_mutex_trylock)(pthread_mutex_t*)                          = NULL;
17 static int (*raw_mutex_unlock)(pthread_mutex_t*)                           = NULL;
18 static int (*raw_mutex_destroy)(pthread_mutex_t*)                          = NULL;
19 static sem_t* (*raw_sem_open)(const char*, int)                            = NULL;
20 static int (*raw_sem_init)(sem_t*, int, unsigned int)                      = NULL;
21 static int (*raw_sem_wait)(sem_t*)                                         = NULL;
22 static int (*raw_sem_post)(sem_t*)                                         = NULL;
23 static void intercepter_init()
24 {
25   raw_pthread_create =
26       (int (*)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*))dlsym(RTLD_NEXT, "pthread_create");
27   raw_mutex_init    = (int (*)(pthread_mutex_t*, const pthread_mutexattr_t*))dlsym(RTLD_NEXT, "pthread_mutex_init");
28   raw_mutex_lock    = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_lock");
29   raw_mutex_trylock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_trylock");
30   raw_mutex_unlock  = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
31   raw_mutex_destroy = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_destroy");
32
33   raw_sem_open = (sem_t * (*)(const char*, int)) dlsym(RTLD_NEXT, "sem_open");
34   raw_sem_init = (int (*)(sem_t*, int, unsigned int))dlsym(RTLD_NEXT, "sem_init");
35   raw_sem_wait = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_wait");
36   raw_sem_post = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_post");
37 }
38
39 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
40 {
41   if (raw_pthread_create == NULL)
42     intercepter_init();
43
44   if (sthread_inside_simgrid)
45     return raw_pthread_create(thread, attr, start_routine, arg);
46
47   sthread_inside_simgrid = 1;
48   int res                = sthread_create(thread, attr, start_routine, arg);
49   sthread_inside_simgrid = 0;
50   return res;
51 }
52
53 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
54 {
55   if (raw_mutex_init == NULL)
56     intercepter_init();
57
58   if (sthread_inside_simgrid)
59     raw_mutex_init(mutex, attr);
60
61   sthread_inside_simgrid = 1;
62   int res                = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
63   sthread_inside_simgrid = 0;
64   return res;
65 }
66
67 int pthread_mutex_lock(pthread_mutex_t* mutex)
68 {
69   if (raw_mutex_lock == NULL)
70     intercepter_init();
71
72   if (sthread_inside_simgrid)
73     return raw_mutex_lock(mutex);
74
75   sthread_inside_simgrid = 1;
76   int res                = sthread_mutex_lock((sthread_mutex_t*)mutex);
77   sthread_inside_simgrid = 0;
78   return res;
79 }
80
81 int pthread_mutex_trylock(pthread_mutex_t* mutex)
82 {
83   if (raw_mutex_trylock == NULL)
84     intercepter_init();
85
86   if (sthread_inside_simgrid)
87     return raw_mutex_trylock(mutex);
88
89   sthread_inside_simgrid = 1;
90   int res                = sthread_mutex_trylock((sthread_mutex_t*)mutex);
91   sthread_inside_simgrid = 0;
92   return res;
93 }
94
95 int pthread_mutex_unlock(pthread_mutex_t* mutex)
96 {
97   if (raw_mutex_unlock == NULL)
98     intercepter_init();
99
100   if (sthread_inside_simgrid)
101     return raw_mutex_unlock(mutex);
102
103   sthread_inside_simgrid = 1;
104   int res                = sthread_mutex_unlock((sthread_mutex_t*)mutex);
105   sthread_inside_simgrid = 0;
106   return res;
107 }
108 int pthread_mutex_destroy(pthread_mutex_t* mutex)
109 {
110   if (raw_mutex_destroy == NULL)
111     intercepter_init();
112
113   if (sthread_inside_simgrid)
114     return raw_mutex_destroy(mutex);
115
116   sthread_inside_simgrid = 1;
117   int res                = sthread_mutex_destroy((sthread_mutex_t*)mutex);
118   sthread_inside_simgrid = 0;
119   return res;
120 }
121
122 #if 0
123 int sem_init(sem_t *sem, int pshared, unsigned int value) {
124         int res;
125
126         res=raw_sem_init(sem,pshared,value);
127         return res;
128 }
129
130 int sem_wait(sem_t *sem) {
131         int res;
132
133         res = raw_sem_wait(sem);
134         return res;
135 }
136
137 int sem_post(sem_t *sem) {
138         return raw_sem_post(sem);
139 }
140
141 int pthread_join(pthread_t thread, void **retval) {
142         sg_actor_join(thread, -1);
143     return 0;
144 }
145
146 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
147     *cond = sg_cond_init();
148     return 0;
149 }
150
151 int pthread_cond_signal(pthread_cond_t *cond) {
152         sg_cond_notify_one(*cond);
153     return 0;
154 }
155
156 int pthread_cond_broadcast(pthread_cond_t *cond) {
157         sg_cond_notify_all(*cond);
158     return 0;
159 }
160
161 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
162         sg_cond_wait(*cond, *mutex);
163     return 0;
164 }
165
166 int pthread_cond_destroy(pthread_cond_t *cond) {
167         sg_cond_destroy(*cond);
168     return 0;
169 }
170 #endif
171
172 /* Trampoline for the real main() */
173 static int (*raw_main)(int, char**, char**);
174
175 /* Our fake main() that gets called by __libc_start_main() */
176 static int main_hook(int argc, char** argv, char** envp)
177 {
178   return sthread_main(argc, argv, envp, raw_main);
179 }
180
181 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
182 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
183                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
184
185 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
186                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
187 {
188   /* Save the real main function address */
189   raw_main = main;
190
191   /* Find the real __libc_start_main()... */
192   typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
193   fprintf(stderr, "__libc_start_main\n");
194   /* ... and call it with our custom main function */
195   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
196 }