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_impl.cpp
1 /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */
2
3 #include <dlfcn.h>
4 #include <pthread.h>
5 #include <semaphore.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "smpi/smpi.h"
10 #include <simgrid/actor.h>
11 #include <simgrid/s4u/Actor.hpp>
12 #include <simgrid/s4u/Engine.hpp>
13 #include <simgrid/s4u/Mutex.hpp>
14 #include <simgrid/s4u/NetZone.hpp>
15 #include <xbt/base.h>
16 #include <xbt/sysdep.h>
17
18 #include "src/internal_config.h"
19 #include "src/sthread/sthread.h"
20
21 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
22 namespace sg4 = simgrid::s4u;
23
24 static sg4::Host* lilibeth = NULL;
25
26 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
27 {
28   XBT_INFO("sthread main() is starting");
29   sthread_inside_simgrid = 1;
30
31   sg4::Engine e(&argc, argv);
32   auto* zone = sg4::create_full_zone("world");
33   lilibeth   = zone->create_host("Lilibeth", 1e15);
34   zone->seal();
35   sthread_inside_simgrid = 0;
36
37   /* Launch the user's main() on an actor */
38   sg4::ActorPtr main_actor = sg4::Actor::create("tid 0", lilibeth, raw_main, argc, argv, envp);
39
40   XBT_INFO("sthread main() is launching the simulation");
41   sg4::Engine::get_instance()->run();
42
43   return 0;
44 }
45 struct sthread_mutex {
46   s4u_Mutex* mutex;
47 };
48
49 static void thread_create_wrapper(void* (*user_function)(void*), void* param)
50 {
51 #if HAVE_SMPI
52   if (SMPI_is_inited())
53     SMPI_thread_create();
54 #endif
55   sthread_inside_simgrid = 0;
56   user_function(param);
57   sthread_inside_simgrid = 1;
58 }
59
60 int sthread_create(unsigned long int* thread, const /*pthread_attr_t*/ void* attr, void* (*start_routine)(void*),
61                    void* arg)
62 {
63   static int TID = 1;
64
65   if (TID == 0) {
66   }
67   TID++;
68   int rank = 0;
69 #if HAVE_SMPI
70   if (SMPI_is_inited())
71     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
72 #endif
73   char name[100];
74   sprintf(name, "%d:%d", rank, TID);
75   sg4::ActorPtr actor = sg4::Actor::init(name, lilibeth);
76   actor->start(thread_create_wrapper, start_routine, arg);
77
78   intrusive_ptr_add_ref(actor.get());
79   *thread = reinterpret_cast<unsigned long>(actor.get());
80   return 0;
81 }
82
83 int sthread_mutex_init(sthread_mutex_t* mutex, const /*pthread_mutexattr_t*/ void* attr)
84 {
85   auto m = sg4::Mutex::create();
86   intrusive_ptr_add_ref(m.get());
87
88   mutex->mutex = m.get();
89   return 0;
90 }
91
92 int sthread_mutex_lock(sthread_mutex_t* mutex)
93 {
94   static_cast<sg4::Mutex*>(mutex->mutex)->lock();
95   return 0;
96 }
97
98 int sthread_mutex_trylock(sthread_mutex_t* mutex)
99 {
100   return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
101 }
102
103 int sthread_mutex_unlock(sthread_mutex_t* mutex)
104 {
105   static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
106   return 0;
107 }
108 int sthread_mutex_destroy(sthread_mutex_t* mutex)
109 {
110   intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
111   return 0;
112 }
113
114 #if 0
115 int sem_init(sem_t *sem, int pshared, unsigned int value) {
116         int res;
117
118         res=raw_sem_init(sem,pshared,value);
119         return res;
120 }
121
122 int sem_wait(sem_t *sem) {
123         int res;
124
125         res = raw_sem_wait(sem);
126         return res;
127 }
128
129 int sem_post(sem_t *sem) {
130         return raw_sem_post(sem);
131 }
132
133 int pthread_join(pthread_t thread, void **retval) {
134         sg_actor_join(thread, -1);
135     return 0;
136 }
137
138 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
139     *cond = sg_cond_init();
140     return 0;
141 }
142
143 int pthread_cond_signal(pthread_cond_t *cond) {
144         sg_cond_notify_one(*cond);
145     return 0;
146 }
147
148 int pthread_cond_broadcast(pthread_cond_t *cond) {
149         sg_cond_notify_all(*cond);
150     return 0;
151 }
152
153 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
154         sg_cond_wait(*cond, *mutex);
155     return 0;
156 }
157
158 int pthread_cond_destroy(pthread_cond_t *cond) {
159         sg_cond_destroy(*cond);
160     return 0;
161 }
162 #endif