1 /* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved. */
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. */
6 /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */
8 #include "simgrid/s4u/Barrier.hpp"
9 #include "simgrid/s4u/ConditionVariable.hpp"
10 #include "smpi/smpi.h"
11 #include "xbt/asserts.h"
14 #include "xbt/string.hpp"
15 #include <simgrid/actor.h>
16 #include <simgrid/s4u/Actor.hpp>
17 #include <simgrid/s4u/Engine.hpp>
18 #include <simgrid/s4u/Mutex.hpp>
19 #include <simgrid/s4u/NetZone.hpp>
20 #include <simgrid/s4u/Semaphore.hpp>
22 #include <xbt/sysdep.h>
24 #include "src/internal_config.h"
25 #include "src/sthread/sthread.h"
30 #include <semaphore.h>
34 #include <string_view>
37 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
38 namespace sg4 = simgrid::s4u;
40 static sg4::Host* lilibeth = nullptr;
42 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
44 /* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
45 for (int i = 0; envp[i] != nullptr; i++)
46 if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0) {
47 printf("sthread refuses to intercept the SMPI application %s directly, as its interception is done otherwise.\n",
49 return raw_main(argc, argv, envp);
52 /* Do not intercept valgrind step 1 */
53 if (not strcmp(argv[0], "/usr/bin/valgrind.bin") || not strcmp(argv[0], "/bin/sh")|| not strcmp(argv[0], "/bin/bash")|| not strcmp(argv[0], "gdb")) {
54 printf("sthread refuses to intercept the execution of %s. Running the application unmodified.\n", argv[0]);
56 return raw_main(argc, argv, envp);
59 /* If not in SMPI, the old main becomes an actor in a newly created simulation */
60 printf("sthread is intercepting the execution of %s\n", argv[0]);
63 sg4::Engine e(&argc, argv);
64 auto* zone = sg4::create_full_zone("world");
65 lilibeth = zone->create_host("Lilibeth", 1e15);
68 /* Launch the user's main() on an actor */
70 sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
72 sg4::Engine::get_instance()->run();
74 XBT_INFO("All threads exited. Terminating the simulation.");
79 struct sthread_mutex {
83 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
88 XBT_VERB("Create thread %d", TID);
89 std::string name = std::string("thread ") + std::to_string(TID);
91 if (SMPI_is_inited()) {
93 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
94 name = simgrid::xbt::string_printf("%d:%d", rank, TID);
97 sg4::ActorPtr actor = sg4::Actor::create(
99 [](auto* user_function, auto* param) {
101 if (SMPI_is_inited())
102 SMPI_thread_create();
105 user_function(param);
110 intrusive_ptr_add_ref(actor.get());
111 *thread = reinterpret_cast<unsigned long>(actor.get());
114 int sthread_join(sthread_t thread, void** /*retval*/)
116 sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
118 intrusive_ptr_release(actor.get());
123 int sthread_mutexattr_init(sthread_mutexattr_t* attr)
125 memset(attr, 0, sizeof(*attr));
128 int sthread_mutexattr_settype(sthread_mutexattr_t* attr, int type)
131 case PTHREAD_MUTEX_NORMAL:
132 xbt_assert(not attr->recursive, "S4U does not allow to remove the recursivness of a mutex.");
135 case PTHREAD_MUTEX_RECURSIVE:
137 attr->errorcheck = 0; // reset
139 case PTHREAD_MUTEX_ERRORCHECK:
140 attr->errorcheck = 1;
148 int sthread_mutexattr_gettype(const sthread_mutexattr_t* attr, int* type)
151 *type = PTHREAD_MUTEX_RECURSIVE;
152 else if (attr->errorcheck)
153 *type = PTHREAD_MUTEX_ERRORCHECK;
155 *type = PTHREAD_MUTEX_NORMAL;
158 int sthread_mutexattr_getrobust(const sthread_mutexattr_t* attr, int* robustness)
160 *robustness = attr->robust;
163 int sthread_mutexattr_setrobust(sthread_mutexattr_t* attr, int robustness)
165 attr->robust = robustness;
171 int sthread_mutex_init(sthread_mutex_t* mutex, const sthread_mutexattr_t* attr)
173 auto m = sg4::Mutex::create(attr != nullptr && attr->recursive);
174 intrusive_ptr_add_ref(m.get());
176 mutex->mutex = m.get();
180 int sthread_mutex_lock(sthread_mutex_t* mutex)
182 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
183 if (mutex->mutex == nullptr)
184 sthread_mutex_init(mutex, nullptr);
186 XBT_DEBUG("%s(%p)", __func__, mutex);
187 static_cast<sg4::Mutex*>(mutex->mutex)->lock();
191 int sthread_mutex_trylock(sthread_mutex_t* mutex)
193 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
194 if (mutex->mutex == nullptr)
195 sthread_mutex_init(mutex, nullptr);
197 XBT_DEBUG("%s(%p)", __func__, mutex);
198 if (static_cast<sg4::Mutex*>(mutex->mutex)->try_lock())
203 int sthread_mutex_unlock(sthread_mutex_t* mutex)
205 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
206 if (mutex->mutex == nullptr)
207 sthread_mutex_init(mutex, nullptr);
209 XBT_DEBUG("%s(%p)", __func__, mutex);
210 static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
213 int sthread_mutex_destroy(sthread_mutex_t* mutex)
215 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
216 if (mutex->mutex == nullptr)
217 sthread_mutex_init(mutex, nullptr);
219 XBT_DEBUG("%s(%p)", __func__, mutex);
220 intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
224 int sthread_barrier_init(sthread_barrier_t* barrier, const sthread_barrierattr_t* attr, unsigned count){
225 auto b = sg4::Barrier::create(count);
226 intrusive_ptr_add_ref(b.get());
228 barrier->barrier = b.get();
231 int sthread_barrier_wait(sthread_barrier_t* barrier){
232 XBT_DEBUG("%s(%p)", __func__, barrier);
233 static_cast<sg4::Barrier*>(barrier->barrier)->wait();
236 int sthread_barrier_destroy(sthread_barrier_t* barrier){
237 XBT_DEBUG("%s(%p)", __func__, barrier);
238 intrusive_ptr_release(static_cast<sg4::Barrier*>(barrier->barrier));
242 int sthread_cond_init(sthread_cond_t* cond, sthread_condattr_t* attr)
244 auto cv = sg4::ConditionVariable::create();
245 intrusive_ptr_add_ref(cv.get());
247 cond->cond = cv.get();
248 cond->mutex = nullptr;
251 int sthread_cond_signal(sthread_cond_t* cond)
253 XBT_DEBUG("%s(%p)", __func__, cond);
255 if (cond->mutex == nullptr)
256 XBT_WARN("No mutex was associated so far with condition variable %p. Safety checks skipped.", cond);
258 auto* owner = static_cast<sg4::Mutex*>(cond->mutex)->get_owner();
259 if (owner == nullptr)
260 XBT_WARN("The mutex associated to condition %p is not currently owned by anyone when calling "
261 "pthread_cond_signal(). The signal could get lost.",
263 else if (owner != simgrid::s4u::Actor::self())
264 XBT_WARN("The mutex associated to condition %p is currently owned by %s, not by the thread currently calling "
265 "calling pthread_cond_signal(). The signal could get lost.",
266 cond, owner->get_cname());
269 static_cast<sg4::ConditionVariable*>(cond->cond)->notify_one();
272 int sthread_cond_broadcast(sthread_cond_t* cond)
274 XBT_DEBUG("%s(%p)", __func__, cond);
276 if (cond->mutex == nullptr)
277 XBT_WARN("No mutex was associated so far with condition variable %p. Safety checks skipped.", cond);
279 auto* owner = static_cast<sg4::Mutex*>(cond->mutex)->get_owner();
280 if (owner == nullptr)
281 XBT_WARN("The mutex associated to condition %p is not currently owned by anyone when calling "
282 "pthread_cond_broadcast(). The signal could get lost.",
284 else if (owner != simgrid::s4u::Actor::self())
285 XBT_WARN("The mutex associated to condition %p is currently owned by %s, not by the thread currently calling "
286 "calling pthread_cond_broadcast(). The signal could get lost.",
287 cond, owner->get_cname());
290 static_cast<sg4::ConditionVariable*>(cond->cond)->notify_all();
293 int sthread_cond_wait(sthread_cond_t* cond, sthread_mutex_t* mutex)
295 XBT_DEBUG("%s(%p)", __func__, cond);
297 if (cond->mutex == nullptr)
298 cond->mutex = mutex->mutex;
299 else if (cond->mutex != mutex->mutex)
300 XBT_WARN("The condition %p is now waited with mutex %p while it was previoulsy waited with mutex %p. sthread may "
301 "not work with such a dangerous code.",
302 cond, cond->mutex, mutex->mutex);
304 static_cast<sg4::ConditionVariable*>(cond->cond)->wait(static_cast<sg4::Mutex*>(mutex->mutex));
307 int sthread_cond_timedwait(sthread_cond_t* cond, sthread_mutex_t* mutex, const struct timespec* abs_timeout)
309 XBT_DEBUG("%s(%p)", __func__, cond);
311 if (cond->mutex == nullptr)
312 cond->mutex = mutex->mutex;
313 else if (cond->mutex != mutex->mutex)
314 XBT_WARN("The condition %p is now waited with mutex %p while it was previoulsy waited with mutex %p. sthread may "
315 "not work with such a dangerous code.",
316 cond, cond->mutex, mutex->mutex);
320 int sthread_cond_destroy(sthread_cond_t* cond)
322 XBT_DEBUG("%s(%p)", __func__, cond);
323 intrusive_ptr_release(static_cast<sg4::ConditionVariable*>(cond->cond));
327 int sthread_sem_init(sthread_sem_t* sem, int /*pshared*/, unsigned int value)
329 auto s = sg4::Semaphore::create(value);
330 intrusive_ptr_add_ref(s.get());
335 int sthread_sem_destroy(sthread_sem_t* sem)
337 intrusive_ptr_release(static_cast<sg4::Semaphore*>(sem->sem));
340 int sthread_sem_post(sthread_sem_t* sem)
342 static_cast<sg4::Semaphore*>(sem->sem)->release();
345 int sthread_sem_wait(sthread_sem_t* sem)
347 static_cast<sg4::Semaphore*>(sem->sem)->acquire();
350 int sthread_sem_trywait(sthread_sem_t* sem)
352 auto* s = static_cast<sg4::Semaphore*>(sem->sem);
353 if (s->would_block()) {
360 int sthread_sem_timedwait(sthread_sem_t* sem, const struct timespec* abs_timeout)
362 if (static_cast<sg4::Semaphore*>(sem->sem)->acquire_timeout(static_cast<double>(abs_timeout->tv_sec) +
363 static_cast<double>(abs_timeout->tv_nsec) / 1E9)) {
370 int sthread_gettimeofday(struct timeval* tv)
373 double now = simgrid::s4u::Engine::get_clock();
374 double secs = trunc(now);
375 double usecs = (now - secs) * 1e6;
376 tv->tv_sec = static_cast<time_t>(secs);
377 tv->tv_usec = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t
382 unsigned int sthread_sleep(double seconds)
384 XBT_DEBUG("sleep(%lf)", seconds);
385 simgrid::s4u::this_actor::sleep_for(seconds);
388 int sthread_usleep(double seconds)
390 XBT_DEBUG("sleep(%lf)", seconds);
391 simgrid::s4u::this_actor::sleep_for(seconds);