Logo AND Algorithmique Numérique Distribuée

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