Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
885c8b6bd24d2fb796c0a9bc1768954fcbbabb63
[simgrid.git] / src / sthread / sthread_impl.cpp
1 /* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */
7
8 #include "smpi/smpi.h"
9 #include "xbt/ex.h"
10 #include "xbt/string.hpp"
11 #include <simgrid/actor.h>
12 #include <simgrid/s4u/Actor.hpp>
13 #include <simgrid/s4u/Engine.hpp>
14 #include <simgrid/s4u/Mutex.hpp>
15 #include <simgrid/s4u/NetZone.hpp>
16 #include <simgrid/s4u/Semaphore.hpp>
17 #include <xbt/base.h>
18 #include <xbt/sysdep.h>
19
20 #include "src/internal_config.h"
21 #include "src/sthread/sthread.h"
22
23 #include <cmath>
24 #include <dlfcn.h>
25 #include <pthread.h>
26 #include <semaphore.h>
27 #include <sstream>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string_view>
31 #include <thread>
32
33 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
34 namespace sg4 = simgrid::s4u;
35
36 static sg4::Host* lilibeth = nullptr;
37
38 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
39 {
40   /* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
41   for (int i = 0; envp[i] != nullptr; i++)
42     if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0) {
43       printf("sthread refuses to intercept the SMPI application %s directly, as its interception is done otherwise.\n",
44              argv[0]);
45       return raw_main(argc, argv, envp);
46     }
47
48   /* Do not intercept valgrind step 1 */
49   if (not strcmp(argv[0], "/usr/bin/valgrind.bin") || not strcmp(argv[0], "/bin/sh")) {
50     printf("sthread refuses to intercept the execution of %s. Running the application unmodified.\n", argv[0]);
51     fflush(stdout);
52     return raw_main(argc, argv, envp);
53   }
54
55   /* If not in SMPI, the old main becomes an actor in a newly created simulation */
56   printf("sthread is intercepting the execution of %s\n", argv[0]);
57   fflush(stdout);
58
59   sg4::Engine e(&argc, argv);
60   auto* zone = sg4::create_full_zone("world");
61   lilibeth   = zone->create_host("Lilibeth", 1e15);
62   zone->seal();
63
64   /* Launch the user's main() on an actor */
65   sthread_enable();
66   sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
67
68   sg4::Engine::get_instance()->run();
69   sthread_disable();
70   XBT_INFO("All threads exited. Terminating the simulation.");
71
72   return 0;
73 }
74
75 struct sthread_mutex {
76   s4u_Mutex* mutex;
77 };
78
79 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
80                    void* arg)
81 {
82   static int TID = 0;
83   TID++;
84   XBT_VERB("Create thread %d", TID);
85   std::string name = std::string("thread ") + std::to_string(TID);
86 #if HAVE_SMPI
87   if (SMPI_is_inited()) {
88     int rank = 0;
89     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
90     name = simgrid::xbt::string_printf("%d:%d", rank, TID);
91   }
92 #endif
93   sg4::ActorPtr actor = sg4::Actor::create(
94       name, lilibeth,
95       [](auto* user_function, auto* param) {
96 #if HAVE_SMPI
97         if (SMPI_is_inited())
98           SMPI_thread_create();
99 #endif
100         sthread_enable();
101         user_function(param);
102         sthread_disable();
103       },
104       start_routine, arg);
105
106   intrusive_ptr_add_ref(actor.get());
107   *thread = reinterpret_cast<unsigned long>(actor.get());
108   return 0;
109 }
110 int sthread_join(sthread_t thread, void** /*retval*/)
111 {
112   sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
113   actor->join();
114   intrusive_ptr_release(actor.get());
115
116   return 0;
117 }
118
119 int sthread_mutexattr_init(sthread_mutexattr_t* attr)
120 {
121   memset(attr, 0, sizeof(*attr));
122   return 0;
123 }
124 int sthread_mutexattr_settype(sthread_mutexattr_t* attr, int type)
125 {
126   // reset
127   attr->recursive  = 0;
128   attr->errorcheck = 0;
129
130   switch (type) {
131     case PTHREAD_MUTEX_NORMAL:
132       attr->recursive = 0;
133       break;
134     case PTHREAD_MUTEX_RECURSIVE:
135       attr->recursive = 1;
136       break;
137     case PTHREAD_MUTEX_ERRORCHECK:
138       attr->errorcheck = 1;
139       THROW_UNIMPLEMENTED;
140       break;
141     default:
142       THROW_IMPOSSIBLE;
143   }
144   return 0;
145 }
146 int sthread_mutexattr_gettype(const sthread_mutexattr_t* attr, int* type)
147 {
148   if (attr->recursive)
149     *type = PTHREAD_MUTEX_RECURSIVE;
150   else if (attr->errorcheck)
151     *type = PTHREAD_MUTEX_ERRORCHECK;
152   else
153     *type = PTHREAD_MUTEX_NORMAL;
154   return 0;
155 }
156 int sthread_mutexattr_getrobust(const sthread_mutexattr_t* attr, int* robustness)
157 {
158   *robustness = attr->robust;
159   return 0;
160 }
161 int sthread_mutexattr_setrobust(sthread_mutexattr_t* attr, int robustness)
162 {
163   attr->robust = robustness;
164   if (robustness)
165     THROW_UNIMPLEMENTED;
166   return 0;
167 }
168
169 int sthread_mutex_init(sthread_mutex_t* mutex, const sthread_mutexattr_t* attr)
170 {
171   auto m = sg4::Mutex::create();
172   intrusive_ptr_add_ref(m.get());
173
174   mutex->mutex = m.get();
175   return 0;
176 }
177
178 int sthread_mutex_lock(sthread_mutex_t* mutex)
179 {
180   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
181   if (mutex->mutex == nullptr)
182     sthread_mutex_init(mutex, nullptr);
183
184   XBT_DEBUG("%s(%p)", __FUNCTION__, mutex);
185   static_cast<sg4::Mutex*>(mutex->mutex)->lock();
186   return 0;
187 }
188
189 int sthread_mutex_trylock(sthread_mutex_t* mutex)
190 {
191   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
192   if (mutex->mutex == nullptr)
193     sthread_mutex_init(mutex, nullptr);
194
195   XBT_DEBUG("%s(%p)", __FUNCTION__, mutex);
196   if (static_cast<sg4::Mutex*>(mutex->mutex)->try_lock())
197     return 0;
198   return EBUSY;
199 }
200
201 int sthread_mutex_unlock(sthread_mutex_t* mutex)
202 {
203   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
204   if (mutex->mutex == nullptr)
205     sthread_mutex_init(mutex, nullptr);
206
207   XBT_DEBUG("%s(%p)", __FUNCTION__, mutex);
208   static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
209   return 0;
210 }
211 int sthread_mutex_destroy(sthread_mutex_t* mutex)
212 {
213   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
214   if (mutex->mutex == nullptr)
215     sthread_mutex_init(mutex, nullptr);
216
217   XBT_DEBUG("%s(%p)", __FUNCTION__, mutex);
218   intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
219   return 0;
220 }
221 int sthread_sem_init(sthread_sem_t* sem, int /*pshared*/, unsigned int value)
222 {
223   auto s = sg4::Semaphore::create(value);
224   intrusive_ptr_add_ref(s.get());
225
226   sem->sem = s.get();
227   return 0;
228 }
229 int sthread_sem_destroy(sthread_sem_t* sem)
230 {
231   intrusive_ptr_release(static_cast<sg4::Semaphore*>(sem->sem));
232   return 0;
233 }
234 int sthread_sem_post(sthread_sem_t* sem)
235 {
236   static_cast<sg4::Semaphore*>(sem->sem)->release();
237   return 0;
238 }
239 int sthread_sem_wait(sthread_sem_t* sem)
240 {
241   static_cast<sg4::Semaphore*>(sem->sem)->acquire();
242   return 0;
243 }
244 int sthread_sem_trywait(sthread_sem_t* sem)
245 {
246   auto* s = static_cast<sg4::Semaphore*>(sem->sem);
247   if (s->would_block()) {
248     errno = EAGAIN;
249     return -1;
250   }
251   s->acquire();
252   return 0;
253 }
254 int sthread_sem_timedwait(sthread_sem_t* sem, const struct timespec* abs_timeout)
255 {
256   if (static_cast<sg4::Semaphore*>(sem->sem)->acquire_timeout(static_cast<double>(abs_timeout->tv_sec) +
257                                                               static_cast<double>(abs_timeout->tv_nsec) / 1E9)) {
258     errno = ETIMEDOUT;
259     return -1;
260   }
261   return 0;
262 }
263
264 int sthread_gettimeofday(struct timeval* tv)
265 {
266   if (tv) {
267     double now   = simgrid::s4u::Engine::get_clock();
268     double secs  = trunc(now);
269     double usecs = (now - secs) * 1e6;
270     tv->tv_sec   = static_cast<time_t>(secs);
271     tv->tv_usec  = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t
272   }
273   return 0;
274 }
275
276 void sthread_sleep(double seconds)
277 {
278   simgrid::s4u::this_actor::sleep_for(seconds);
279 }
280
281 #if 0
282 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
283     *cond = sg_cond_init();
284     return 0;
285 }
286
287 int pthread_cond_signal(pthread_cond_t *cond) {
288         sg_cond_notify_one(*cond);
289     return 0;
290 }
291
292 int pthread_cond_broadcast(pthread_cond_t *cond) {
293         sg_cond_notify_all(*cond);
294     return 0;
295 }
296
297 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
298         sg_cond_wait(*cond, *mutex);
299     return 0;
300 }
301
302 int pthread_cond_destroy(pthread_cond_t *cond) {
303         sg_cond_destroy(*cond);
304     return 0;
305 }
306 #endif