Logo AND Algorithmique Numérique Distribuée

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