Logo AND Algorithmique Numérique Distribuée

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