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 system binaries such as valgrind step 1 */
53 std::vector<std::string> binaries = {"/usr/bin/valgrind.bin", "/bin/sh", "/bin/bash", "gdb", "addr2line"};
54 for (int i = 0; envp[i] != nullptr; i++) {
55 auto view = std::string_view(envp[i]);
56 /* If you want to ignore more than one binary, export STHREAD_IGNORE_BINARY1=toto STHREAD_IGNORE_BINARY2=tutu */
57 /* Note that this cannot be configured with --cfg because we are before the main() */
58 if (view.rfind("STHREAD_IGNORE_BINARY", 0) == 0) {
59 view.remove_prefix(std::min(view.rfind("=") + 1, view.size()));
60 binaries.push_back(std::string(view));
63 auto binary_view = std::string_view(argv[0]);
64 for (auto binary : binaries) {
65 if (binary_view.rfind(binary) != std::string_view::npos) {
66 printf("sthread refuses to intercept the execution of %s. Running the application unmodified.\n", argv[0]);
68 return raw_main(argc, argv, envp);
72 /* If not in SMPI, the old main becomes an actor in a newly created simulation */
73 printf("sthread is intercepting the execution of %s. If it's not what you want, export STHREAD_IGNORE_BINARY=%s\n",
77 sg4::Engine e(&argc, argv);
78 auto* zone = sg4::create_full_zone("world");
79 lilibeth = zone->create_host("Lilibeth", 1e15);
82 /* Launch the user's main() on an actor */
84 sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
86 sg4::Engine::get_instance()->run();
88 XBT_INFO("All threads exited. Terminating the simulation.");
93 struct sthread_mutex {
97 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
102 XBT_VERB("Create thread %d", TID);
103 std::string name = std::string("thread ") + std::to_string(TID);
105 if (SMPI_is_inited()) {
107 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
108 name = simgrid::xbt::string_printf("%d:%d", rank, TID);
111 sg4::ActorPtr actor = sg4::Actor::create(
113 [](auto* user_function, auto* param) {
115 if (SMPI_is_inited())
116 SMPI_thread_create();
119 user_function(param);
124 intrusive_ptr_add_ref(actor.get());
125 *thread = reinterpret_cast<unsigned long>(actor.get());
128 int sthread_join(sthread_t thread, void** /*retval*/)
130 sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
132 intrusive_ptr_release(actor.get());
137 int sthread_mutexattr_init(sthread_mutexattr_t* attr)
139 memset(attr, 0, sizeof(*attr));
142 int sthread_mutexattr_settype(sthread_mutexattr_t* attr, int type)
145 case PTHREAD_MUTEX_NORMAL:
146 xbt_assert(not attr->recursive, "S4U does not allow to remove the recursivness of a mutex.");
149 case PTHREAD_MUTEX_RECURSIVE:
151 attr->errorcheck = 0; // reset
153 case PTHREAD_MUTEX_ERRORCHECK:
154 attr->errorcheck = 1;
162 int sthread_mutexattr_gettype(const sthread_mutexattr_t* attr, int* type)
165 *type = PTHREAD_MUTEX_RECURSIVE;
166 else if (attr->errorcheck)
167 *type = PTHREAD_MUTEX_ERRORCHECK;
169 *type = PTHREAD_MUTEX_NORMAL;
172 int sthread_mutexattr_getrobust(const sthread_mutexattr_t* attr, int* robustness)
174 *robustness = attr->robust;
177 int sthread_mutexattr_setrobust(sthread_mutexattr_t* attr, int robustness)
179 attr->robust = robustness;
185 int sthread_mutex_init(sthread_mutex_t* mutex, const sthread_mutexattr_t* attr)
187 auto m = sg4::Mutex::create(attr != nullptr && attr->recursive);
188 intrusive_ptr_add_ref(m.get());
190 mutex->mutex = m.get();
194 int sthread_mutex_lock(sthread_mutex_t* mutex)
196 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
197 if (mutex->mutex == nullptr)
198 sthread_mutex_init(mutex, nullptr);
200 XBT_DEBUG("%s(%p)", __func__, mutex);
201 static_cast<sg4::Mutex*>(mutex->mutex)->lock();
205 int sthread_mutex_trylock(sthread_mutex_t* mutex)
207 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
208 if (mutex->mutex == nullptr)
209 sthread_mutex_init(mutex, nullptr);
211 XBT_DEBUG("%s(%p)", __func__, mutex);
212 if (static_cast<sg4::Mutex*>(mutex->mutex)->try_lock())
217 int sthread_mutex_unlock(sthread_mutex_t* mutex)
219 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
220 if (mutex->mutex == nullptr)
221 sthread_mutex_init(mutex, nullptr);
223 XBT_DEBUG("%s(%p)", __func__, mutex);
224 static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
227 int sthread_mutex_destroy(sthread_mutex_t* mutex)
229 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
230 if (mutex->mutex == nullptr)
231 sthread_mutex_init(mutex, nullptr);
233 XBT_DEBUG("%s(%p)", __func__, mutex);
234 intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
238 int sthread_barrier_init(sthread_barrier_t* barrier, const sthread_barrierattr_t* attr, unsigned count){
239 auto b = sg4::Barrier::create(count);
240 intrusive_ptr_add_ref(b.get());
242 barrier->barrier = b.get();
245 int sthread_barrier_wait(sthread_barrier_t* barrier){
246 XBT_DEBUG("%s(%p)", __func__, barrier);
247 static_cast<sg4::Barrier*>(barrier->barrier)->wait();
250 int sthread_barrier_destroy(sthread_barrier_t* barrier){
251 XBT_DEBUG("%s(%p)", __func__, barrier);
252 intrusive_ptr_release(static_cast<sg4::Barrier*>(barrier->barrier));
256 int sthread_cond_init(sthread_cond_t* cond, sthread_condattr_t* attr)
258 auto cv = sg4::ConditionVariable::create();
259 intrusive_ptr_add_ref(cv.get());
261 cond->cond = cv.get();
262 cond->mutex = nullptr;
265 int sthread_cond_signal(sthread_cond_t* cond)
267 XBT_DEBUG("%s(%p)", __func__, cond);
269 if (cond->mutex == nullptr)
270 XBT_WARN("No mutex was associated so far with condition variable %p. Safety checks skipped.", cond);
272 auto* owner = static_cast<sg4::Mutex*>(cond->mutex)->get_owner();
273 if (owner == nullptr)
274 XBT_WARN("The mutex associated to condition %p is not currently owned by anyone when calling "
275 "pthread_cond_signal(). The signal could get lost.",
277 else if (owner != simgrid::s4u::Actor::self())
278 XBT_WARN("The mutex associated to condition %p is currently owned by %s, not by the thread currently calling "
279 "calling pthread_cond_signal(). The signal could get lost.",
280 cond, owner->get_cname());
283 static_cast<sg4::ConditionVariable*>(cond->cond)->notify_one();
286 int sthread_cond_broadcast(sthread_cond_t* cond)
288 XBT_DEBUG("%s(%p)", __func__, cond);
290 if (cond->mutex == nullptr)
291 XBT_WARN("No mutex was associated so far with condition variable %p. Safety checks skipped.", cond);
293 auto* owner = static_cast<sg4::Mutex*>(cond->mutex)->get_owner();
294 if (owner == nullptr)
295 XBT_WARN("The mutex associated to condition %p is not currently owned by anyone when calling "
296 "pthread_cond_broadcast(). The signal could get lost.",
298 else if (owner != simgrid::s4u::Actor::self())
299 XBT_WARN("The mutex associated to condition %p is currently owned by %s, not by the thread currently calling "
300 "calling pthread_cond_broadcast(). The signal could get lost.",
301 cond, owner->get_cname());
304 static_cast<sg4::ConditionVariable*>(cond->cond)->notify_all();
307 int sthread_cond_wait(sthread_cond_t* cond, sthread_mutex_t* mutex)
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);
318 static_cast<sg4::ConditionVariable*>(cond->cond)->wait(static_cast<sg4::Mutex*>(mutex->mutex));
321 int sthread_cond_timedwait(sthread_cond_t* cond, sthread_mutex_t* mutex, const struct timespec* abs_timeout)
323 XBT_DEBUG("%s(%p)", __func__, cond);
325 if (cond->mutex == nullptr)
326 cond->mutex = mutex->mutex;
327 else if (cond->mutex != mutex->mutex)
328 XBT_WARN("The condition %p is now waited with mutex %p while it was previoulsy waited with mutex %p. sthread may "
329 "not work with such a dangerous code.",
330 cond, cond->mutex, mutex->mutex);
334 int sthread_cond_destroy(sthread_cond_t* cond)
336 XBT_DEBUG("%s(%p)", __func__, cond);
337 intrusive_ptr_release(static_cast<sg4::ConditionVariable*>(cond->cond));
341 int sthread_sem_init(sthread_sem_t* sem, int /*pshared*/, unsigned int value)
343 auto s = sg4::Semaphore::create(value);
344 intrusive_ptr_add_ref(s.get());
349 int sthread_sem_destroy(sthread_sem_t* sem)
351 intrusive_ptr_release(static_cast<sg4::Semaphore*>(sem->sem));
354 int sthread_sem_post(sthread_sem_t* sem)
356 static_cast<sg4::Semaphore*>(sem->sem)->release();
359 int sthread_sem_wait(sthread_sem_t* sem)
361 static_cast<sg4::Semaphore*>(sem->sem)->acquire();
364 int sthread_sem_trywait(sthread_sem_t* sem)
366 auto* s = static_cast<sg4::Semaphore*>(sem->sem);
367 if (s->would_block()) {
374 int sthread_sem_timedwait(sthread_sem_t* sem, const struct timespec* abs_timeout)
376 if (static_cast<sg4::Semaphore*>(sem->sem)->acquire_timeout(static_cast<double>(abs_timeout->tv_sec) +
377 static_cast<double>(abs_timeout->tv_nsec) / 1E9)) {
384 int sthread_gettimeofday(struct timeval* tv)
387 double now = simgrid::s4u::Engine::get_clock();
388 double secs = trunc(now);
389 double usecs = (now - secs) * 1e6;
390 tv->tv_sec = static_cast<time_t>(secs);
391 tv->tv_usec = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t
396 unsigned int sthread_sleep(double seconds)
398 XBT_DEBUG("sleep(%lf)", seconds);
399 simgrid::s4u::this_actor::sleep_for(seconds);
402 int sthread_usleep(double seconds)
404 XBT_DEBUG("sleep(%lf)", seconds);
405 simgrid::s4u::this_actor::sleep_for(seconds);