Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Deal with PTHREAD_STATIC_INITIALIZER if it behave as in glibc
[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 "smpi/smpi.h"
4 #include <simgrid/actor.h>
5 #include <simgrid/s4u/Actor.hpp>
6 #include <simgrid/s4u/Engine.hpp>
7 #include <simgrid/s4u/Mutex.hpp>
8 #include <simgrid/s4u/NetZone.hpp>
9 #include <xbt/base.h>
10 #include <xbt/sysdep.h>
11
12 #include "src/internal_config.h"
13 #include "src/sthread/sthread.h"
14
15 #include <cmath>
16 #include <dlfcn.h>
17 #include <pthread.h>
18 #include <semaphore.h>
19 #include <sstream>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <thread>
23
24 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
25 namespace sg4 = simgrid::s4u;
26
27 static sg4::Host* lilibeth = nullptr;
28
29 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
30 {
31   /* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
32   for (int i = 0; envp[i] != nullptr; i++)
33     if (strncmp(envp[i], "SMPI_GLOBAL_SIZE", strlen("SMPI_GLOBAL_SIZE")) == 0)
34       return raw_main(argc, argv, envp);
35
36   /* If not in SMPI, the old main becomes an actor in a newly created simulation */
37   std::ostringstream id;
38   id << std::this_thread::get_id();
39
40   XBT_DEBUG("sthread main() is starting in thread %s", id.str().c_str());
41
42   sg4::Engine e(&argc, argv);
43   auto* zone = sg4::create_full_zone("world");
44   lilibeth   = zone->create_host("Lilibeth", 1e15);
45   zone->seal();
46
47   /* Launch the user's main() on an actor */
48   sthread_enable();
49   sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
50
51   XBT_INFO("Starting the simulation.");
52   sg4::Engine::get_instance()->run();
53   sthread_disable();
54   XBT_INFO("All threads exited. Terminating the simulation.");
55
56   return 0;
57 }
58
59 struct sthread_mutex {
60   s4u_Mutex* mutex;
61 };
62
63 static void thread_create_wrapper(void* (*user_function)(void*), void* param)
64 {
65 #if HAVE_SMPI
66   if (SMPI_is_inited())
67     SMPI_thread_create();
68 #endif
69   sthread_enable();
70   user_function(param);
71   sthread_disable();
72 }
73
74 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
75                    void* arg)
76 {
77   static int TID = 0;
78   TID++;
79   XBT_VERB("Create thread %d", TID);
80   int rank = 0;
81 #if HAVE_SMPI
82   if (SMPI_is_inited())
83     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
84 #endif
85   std::string name    = simgrid::xbt::string_printf("%d:%d", rank, TID);
86   sg4::ActorPtr actor = sg4::Actor::create(name, lilibeth, thread_create_wrapper, start_routine, arg);
87
88   intrusive_ptr_add_ref(actor.get());
89   *thread = reinterpret_cast<unsigned long>(actor.get());
90   return 0;
91 }
92 int sthread_join(sthread_t thread, void** /*retval*/)
93 {
94   sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
95   actor->join();
96   intrusive_ptr_release(actor.get());
97
98   return 0;
99 }
100
101 int sthread_mutex_init(sthread_mutex_t* mutex, const void* /*pthread_mutexattr_t* attr*/)
102 {
103   auto m = sg4::Mutex::create();
104   intrusive_ptr_add_ref(m.get());
105
106   mutex->mutex = m.get();
107   return 0;
108 }
109
110 int sthread_mutex_lock(sthread_mutex_t* mutex)
111 {
112   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
113   if (mutex->mutex == nullptr)
114     sthread_mutex_init(mutex, nullptr);
115
116   static_cast<sg4::Mutex*>(mutex->mutex)->lock();
117   return 0;
118 }
119
120 int sthread_mutex_trylock(sthread_mutex_t* mutex)
121 {
122   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
123   if (mutex->mutex == nullptr)
124     sthread_mutex_init(mutex, nullptr);
125
126   return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
127 }
128
129 int sthread_mutex_unlock(sthread_mutex_t* mutex)
130 {
131   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
132   if (mutex->mutex == nullptr)
133     sthread_mutex_init(mutex, nullptr);
134
135   static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
136   return 0;
137 }
138 int sthread_mutex_destroy(sthread_mutex_t* mutex)
139 {
140   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
141   if (mutex->mutex == nullptr)
142     sthread_mutex_init(mutex, nullptr);
143
144   intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
145   return 0;
146 }
147
148 int sthread_gettimeofday(struct timeval* tv)
149 {
150   if (tv) {
151     double now   = simgrid::s4u::Engine::get_clock();
152     double secs  = trunc(now);
153     double usecs = (now - secs) * 1e6;
154     tv->tv_sec   = static_cast<time_t>(secs);
155     tv->tv_usec  = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t (or useconds_t on WIN32)
156   }
157   return 0;
158 }
159
160 void sthread_sleep(double seconds)
161 {
162   simgrid::s4u::this_actor::sleep_for(seconds);
163 }
164
165 #if 0
166 int sem_init(sem_t *sem, int pshared, unsigned int value) {
167         int res;
168
169         res=raw_sem_init(sem,pshared,value);
170         return res;
171 }
172
173 int sem_wait(sem_t *sem) {
174         int res;
175
176         res = raw_sem_wait(sem);
177         return res;
178 }
179
180 int sem_post(sem_t *sem) {
181         return raw_sem_post(sem);
182 }
183
184 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
185     *cond = sg_cond_init();
186     return 0;
187 }
188
189 int pthread_cond_signal(pthread_cond_t *cond) {
190         sg_cond_notify_one(*cond);
191     return 0;
192 }
193
194 int pthread_cond_broadcast(pthread_cond_t *cond) {
195         sg_cond_notify_all(*cond);
196     return 0;
197 }
198
199 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
200         sg_cond_wait(*cond, *mutex);
201     return 0;
202 }
203
204 int pthread_cond_destroy(pthread_cond_t *cond) {
205         sg_cond_destroy(*cond);
206     return 0;
207 }
208 #endif