Logo AND Algorithmique Numérique Distribuée

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